Subversion Repositories Kolibri OS

Rev

Rev 4872 | Rev 4921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17. /* No user fns here.  Pesch 15apr92. */
  18.  
  19. #include <_ansi.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include "local.h"
  25. #include "fvwrite.h"
  26.  
  27. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  28. #define COPY(n)   _CAST_VOID memmove ((_PTR) fp->_p, (_PTR) p, (size_t) (n))
  29.  
  30. #define GETIOV(extra_work) \
  31.   while (len == 0) \
  32.     { \
  33.       extra_work; \
  34.       p = iov->iov_base; \
  35.       len = iov->iov_len; \
  36.       iov++; \
  37.     }
  38.  
  39. /*
  40.  * Write some memory regions.  Return zero on success, EOF on error.
  41.  *
  42.  * This routine is large and unsightly, but most of the ugliness due
  43.  * to the three different kinds of output buffering is handled here.
  44.  */
  45.  
  46. int
  47. _DEFUN(__sfvwrite_r, (ptr, fp, uio),
  48.        struct _reent *ptr _AND
  49.        register FILE *fp _AND
  50.        register struct __suio *uio)
  51. {
  52.   register size_t len;
  53.   register _CONST char *p = NULL;
  54.   register struct __siov *iov;
  55.   register int w, s;
  56.   char *nl;
  57.   int nlknown, nldist;
  58.  
  59.   if ((len = uio->uio_resid) == 0)
  60.     return 0;
  61.  
  62.   /* make sure we can write */
  63.   if (cantwrite (ptr, fp))
  64.       return EOF;
  65.  
  66.   iov = uio->uio_iov;
  67.   len = 0;
  68.  
  69. #ifdef __SCLE
  70.   if (fp->_flags & __SCLE) /* text mode */
  71.     {
  72.       do
  73.         {
  74.           GETIOV (;);
  75.           while (len > 0)
  76.             {
  77.               if (putc (*p, fp) == EOF)
  78.                 return EOF;
  79.               p++;
  80.               len--;
  81.               uio->uio_resid--;
  82.             }
  83.         }
  84.       while (uio->uio_resid > 0);
  85.       return 0;
  86.     }
  87. #endif
  88.  
  89.   if (fp->_flags & __SNBF)
  90.     {
  91.       /*
  92.        * Unbuffered: write up to BUFSIZ bytes at a time.
  93.        */
  94.       do
  95.         {
  96.           GETIOV (;);
  97.           w = fp->_write (ptr, fp->_cookie, p, MIN (len, BUFSIZ));
  98.           if (w <= 0)
  99.             goto err;
  100.           p += w;
  101.           len -= w;
  102.         }
  103.       while ((uio->uio_resid -= w) != 0);
  104.     }
  105.   else if ((fp->_flags & __SLBF) == 0)
  106.     {
  107.       /*
  108.        * Fully buffered: fill partially full buffer, if any,
  109.        * and then flush.  If there is no partial buffer, write
  110.        * one _bf._size byte chunk directly (without copying).
  111.        *
  112.        * String output is a special case: write as many bytes
  113.        * as fit, but pretend we wrote everything.  This makes
  114.        * snprintf() return the number of bytes needed, rather
  115.        * than the number used, and avoids its write function
  116.        * (so that the write function can be invalid).  If
  117.        * we are dealing with the asprintf routines, we will
  118.        * dynamically increase the buffer size as needed.
  119.        */
  120.       do
  121.         {
  122.           GETIOV (;);
  123.           w = fp->_w;
  124.           if (fp->_flags & __SSTR)
  125.             {
  126.               if (len >= w && fp->_flags & (__SMBF | __SOPT))
  127.                 { /* must be asprintf family */
  128.                   unsigned char *str;
  129.                   int curpos = (fp->_p - fp->_bf._base);
  130.                   /* Choose a geometric growth factor to avoid
  131.                      quadratic realloc behavior, but use a rate less
  132.                      than (1+sqrt(5))/2 to accomodate malloc
  133.                      overhead. asprintf EXPECTS us to overallocate, so
  134.                      that it can add a trailing \0 without
  135.                      reallocating.  The new allocation should thus be
  136.                      max(prev_size*1.5, curpos+len+1). */
  137.                   int newsize = fp->_bf._size * 3 / 2;
  138.                   if (newsize < curpos + len + 1)
  139.                     newsize = curpos + len + 1;
  140.                   if (fp->_flags & __SOPT)
  141.                     {
  142.                       /* asnprintf leaves original buffer alone.  */
  143.                       str = (unsigned char *)_malloc_r (ptr, newsize);
  144.                       if (!str)
  145.                         {
  146.                           ptr->_errno = ENOMEM;
  147.                           goto err;
  148.                         }
  149.                       memcpy (str, fp->_bf._base, curpos);
  150.                       fp->_flags = (fp->_flags & ~__SOPT) | __SMBF;
  151.                     }
  152.                   else
  153.                     {
  154.                       str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
  155.                                                          newsize);
  156.                       if (!str)
  157.                         {
  158.                           /* Free buffer which is no longer used.  */
  159.                           _free_r (ptr, fp->_bf._base);
  160.                           /* Ensure correct errno, even if free changed it.  */
  161.                           ptr->_errno = ENOMEM;
  162.                           goto err;
  163.                         }
  164.                     }
  165.                   fp->_bf._base = str;
  166.                   fp->_p = str + curpos;
  167.                   fp->_bf._size = newsize;
  168.                   w = len;
  169.                   fp->_w = newsize - curpos;
  170.                 }
  171.               if (len < w)
  172.                 w = len;
  173.               COPY (w);         /* copy MIN(fp->_w,len), */
  174.               fp->_w -= w;
  175.               fp->_p += w;
  176.               w = len;          /* but pretend copied all */
  177.             }
  178.           else if (fp->_p > fp->_bf._base && len > w)
  179.             {
  180.               /* fill and flush */
  181.               COPY (w);
  182.               /* fp->_w -= w; *//* unneeded */
  183.               fp->_p += w;
  184.               if (_fflush_r (ptr, fp))
  185.                 goto err;
  186.             }
  187.           else if (len >= (w = fp->_bf._size))
  188.             {
  189.               /* write directly */
  190.               w = fp->_write (ptr, fp->_cookie, p, w);
  191.               if (w <= 0)
  192.                 goto err;
  193.             }
  194.           else
  195.             {
  196.               /* fill and done */
  197.               w = len;
  198.               COPY (w);
  199.               fp->_w -= w;
  200.               fp->_p += w;
  201.             }
  202.           p += w;
  203.           len -= w;
  204.         }
  205.       while ((uio->uio_resid -= w) != 0);
  206.     }
  207.   else
  208.     {
  209.       /*
  210.        * Line buffered: like fully buffered, but we
  211.        * must check for newlines.  Compute the distance
  212.        * to the first newline (including the newline),
  213.        * or `infinity' if there is none, then pretend
  214.        * that the amount to write is MIN(len,nldist).
  215.        */
  216.       nlknown = 0;
  217.       nldist = 0;
  218.       do
  219.         {
  220.           GETIOV (nlknown = 0);
  221.           if (!nlknown)
  222.             {
  223.               nl = memchr ((_PTR) p, '\n', len);
  224.               nldist = nl ? nl + 1 - p : len + 1;
  225.               nlknown = 1;
  226.             }
  227.           s = MIN (len, nldist);
  228.           w = fp->_w + fp->_bf._size;
  229.           if (fp->_p > fp->_bf._base && s > w)
  230.             {
  231.               COPY (w);
  232.               /* fp->_w -= w; */
  233.               fp->_p += w;
  234.               if (_fflush_r (ptr, fp))
  235.                 goto err;
  236.             }
  237.           else if (s >= (w = fp->_bf._size))
  238.             {
  239.               w = fp->_write (ptr, fp->_cookie, p, w);
  240.               if (w <= 0)
  241.                 goto err;
  242.             }
  243.           else
  244.             {
  245.               w = s;
  246.               COPY (w);
  247.               fp->_w -= w;
  248.               fp->_p += w;
  249.             }
  250.           if ((nldist -= w) == 0)
  251.             {
  252.               /* copied the newline: flush and forget */
  253.               if (_fflush_r (ptr, fp))
  254.                 goto err;
  255.               nlknown = 0;
  256.             }
  257.           p += w;
  258.           len -= w;
  259.         }
  260.       while ((uio->uio_resid -= w) != 0);
  261.     }
  262.   return 0;
  263.  
  264. err:
  265.   fp->_flags |= __SERR;
  266.   return EOF;
  267. }
  268.