home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdio_2 / rcs / fvwrite.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  5.7 KB  |  218 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    92.06.08.15.10.10;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @initial checkin
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*-
  26.  * Copyright (c) 1990 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * This code is derived from software contributed to Berkeley by
  30.  * Chris Torek.
  31.  *
  32.  * Redistribution and use in source and binary forms, with or without
  33.  * modification, are permitted provided that the following conditions
  34.  * are met:
  35.  * 1. Redistributions of source code must retain the above copyright
  36.  *    notice, this list of conditions and the following disclaimer.
  37.  * 2. Redistributions in binary form must reproduce the above copyright
  38.  *    notice, this list of conditions and the following disclaimer in the
  39.  *    documentation and/or other materials provided with the distribution.
  40.  * 3. All advertising materials mentioning features or use of this software
  41.  *    must display the following acknowledgement:
  42.  *    This product includes software developed by the University of
  43.  *    California, Berkeley and its contributors.
  44.  * 4. Neither the name of the University nor the names of its contributors
  45.  *    may be used to endorse or promote products derived from this software
  46.  *    without specific prior written permission.
  47.  *
  48.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  49.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58.  * SUCH DAMAGE.
  59.  */
  60.  
  61. #if defined(LIBC_SCCS) && !defined(lint)
  62. static char sccsid[] = "@@(#)fvwrite.c    5.3 (Berkeley) 5/4/91";
  63. #endif /* LIBC_SCCS and not lint */
  64.  
  65. #define KERNEL
  66. #include "ixemul.h"
  67.  
  68. #include <stdio.h>
  69. #include <string.h>
  70. #include "local.h"
  71. #include "fvwrite.h"
  72.  
  73. /*
  74.  * Write some memory regions.  Return zero on success, EOF on error.
  75.  *
  76.  * This routine is large and unsightly, but most of the ugliness due
  77.  * to the three different kinds of output buffering is handled here.
  78.  */
  79. __sfvwrite(fp, uio)
  80.     register FILE *fp;
  81.     register struct __suio *uio;
  82. {
  83.     register size_t len;
  84.     register char *p;
  85.     register struct __siov *iov;
  86.     register int w, s;
  87.     char *nl;
  88.     int nlknown, nldist;
  89.  
  90.     if ((len = uio->uio_resid) == 0)
  91.         return (0);
  92.     /* make sure we can write */
  93.     if (!fp || cantwrite(fp))
  94.         return (EOF);
  95.  
  96. #define    MIN(a, b) ((a) < (b) ? (a) : (b))
  97. #define    COPY(n)      (void) bcopy((void *)p, (void *)fp->_p, (size_t)(n));
  98.  
  99.     iov = uio->uio_iov;
  100.     p = iov->iov_base;
  101.     len = iov->iov_len;
  102.     iov++;
  103. #define GETIOV(extra_work) \
  104.     while (len == 0) { \
  105.         extra_work; \
  106.         p = iov->iov_base; \
  107.         len = iov->iov_len; \
  108.         iov++; \
  109.     }
  110.     if (fp->_flags & __SNBF) {
  111.         /*
  112.          * Unbuffered: write up to BUFSIZ bytes at a time.
  113.          */
  114.         do {
  115.             GETIOV(;);
  116.             w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
  117.             if (w <= 0)
  118.                 goto err;
  119.             p += w;
  120.             len -= w;
  121.         } while ((uio->uio_resid -= w) != 0);
  122.     } else if ((fp->_flags & __SLBF) == 0) {
  123.         /*
  124.          * Fully buffered: fill partially full buffer, if any,
  125.          * and then flush.  If there is no partial buffer, write
  126.          * one _bf._size byte chunk directly (without copying).
  127.          *
  128.          * String output is a special case: write as many bytes
  129.          * as fit, but pretend we wrote everything.  This makes
  130.          * snprintf() return the number of bytes needed, rather
  131.          * than the number used, and avoids its write function
  132.          * (so that the write function can be invalid).
  133.          */
  134.         do {
  135.             GETIOV(;);
  136.             w = fp->_w;
  137.             if (fp->_flags & __SSTR) {
  138.                 if (len < w)
  139.                     w = len;
  140.                 COPY(w);    /* copy MIN(fp->_w,len), */
  141.                 fp->_w -= w;
  142.                 fp->_p += w;
  143.                 w = len;    /* but pretend copied all */
  144.             } else if (fp->_p > fp->_bf._base && len > w) {
  145.                 /* fill and flush */
  146.                 COPY(w);
  147.                 /* fp->_w -= w; */ /* unneeded */
  148.                 fp->_p += w;
  149.                 if (fflush(fp))
  150.                     goto err;
  151.             } else if (len >= (w = fp->_bf._size)) {
  152.                 /* write directly */
  153.                 w = (*fp->_write)(fp->_cookie, p, w);
  154.                 if (w <= 0)
  155.                     goto err;
  156.             } else {
  157.                 /* fill and done */
  158.                 w = len;
  159.                 COPY(w);
  160.                 fp->_w -= w;
  161.                 fp->_p += w;
  162.             }
  163.             p += w;
  164.             len -= w;
  165.         } while ((uio->uio_resid -= w) != 0);
  166.     } else {
  167.         /*
  168.          * Line buffered: like fully buffered, but we
  169.          * must check for newlines.  Compute the distance
  170.          * to the first newline (including the newline),
  171.          * or `infinity' if there is none, then pretend
  172.          * that the amount to write is MIN(len,nldist).
  173.          */
  174.         nlknown = 0;
  175.         nldist = 0;    /* XXX just to keep gcc happy */
  176.         do {
  177.             GETIOV(nlknown = 0);
  178.             if (!nlknown) {
  179.                 nl = memchr((void *)p, '\n', len);
  180.                 nldist = nl ? nl + 1 - p : len + 1;
  181.                 nlknown = 1;
  182.             }
  183.             s = MIN(len, nldist);
  184.             w = fp->_w + fp->_bf._size;
  185.             if (fp->_p > fp->_bf._base && s > w) {
  186.                 COPY(w);
  187.                 /* fp->_w -= w; */
  188.                 fp->_p += w;
  189.                 if (fflush(fp))
  190.                     goto err;
  191.             } else if (s >= (w = fp->_bf._size)) {
  192.                 w = (*fp->_write)(fp->_cookie, p, w);
  193.                 if (w <= 0)
  194.                      goto err;
  195.             } else {
  196.                 w = s;
  197.                 COPY(w);
  198.                 fp->_w -= w;
  199.                 fp->_p += w;
  200.             }
  201.             if ((nldist -= w) == 0) {
  202.                 /* copied the newline: flush and forget */
  203.                 if (fflush(fp))
  204.                     goto err;
  205.                 nlknown = 0;
  206.             }
  207.             p += w;
  208.             len -= w;
  209.         } while ((uio->uio_resid -= w) != 0);
  210.     }
  211.     return (0);
  212.  
  213. err:
  214.     fp->_flags |= __SERR;
  215.     return (EOF);
  216. }
  217. @
  218.