OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [fvwrite.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/* No user fns here.  Pesch 15apr92. */
2
 
3
/*
4
 * Copyright (c) 1990 The Regents of the University of California.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms are permitted
8
 * provided that the above copyright notice and this paragraph are
9
 * duplicated in all such forms and that any documentation,
10
 * advertising materials, and other materials related to such
11
 * distribution and use acknowledge that the software was developed
12
 * by the University of California, Berkeley.  The name of the
13
 * University may not be used to endorse or promote products derived
14
 * from this software without specific prior written permission.
15
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
 */
19
 
20
#include <stdio.h>
21
#include <string.h>
22
#include "local.h"
23
#include "fvwrite.h"
24
 
25
#define MIN(a, b) ((a) < (b) ? (a) : (b))
26
#define COPY(n)   (void) memmove((void *) fp->_p, (void *) p, (size_t) (n))
27
 
28
#define GETIOV(extra_work) \
29
  while (len == 0) \
30
    { \
31
      extra_work; \
32
      p = iov->iov_base; \
33
      len = iov->iov_len; \
34
      iov++; \
35
    }
36
 
37
/*
38
 * Write some memory regions.  Return zero on success, EOF on error.
39
 *
40
 * This routine is large and unsightly, but most of the ugliness due
41
 * to the three different kinds of output buffering is handled here.
42
 */
43
 
44
int
45
__sfvwrite (fp, uio)
46
     register FILE *fp;
47
     register struct __suio *uio;
48
{
49
  register size_t len;
50
  register _CONST char *p;
51
  register struct __siov *iov;
52
  register int w, s;
53
  char *nl;
54
  int nlknown, nldist;
55
 
56
  if ((len = uio->uio_resid) == 0)
57
    return 0;
58
 
59
  /* make sure we can write */
60
  if (cantwrite (fp))
61
    return EOF;
62
 
63
  iov = uio->uio_iov;
64
  len = 0;
65
  if (fp->_flags & __SNBF)
66
    {
67
      /*
68
       * Unbuffered: write up to BUFSIZ bytes at a time.
69
       */
70
      do
71
        {
72
          GETIOV (;);
73
          w = (*fp->_write) (fp->_cookie, p, MIN (len, BUFSIZ));
74
          if (w <= 0)
75
            goto err;
76
          p += w;
77
          len -= w;
78
        }
79
      while ((uio->uio_resid -= w) != 0);
80
    }
81
  else if ((fp->_flags & __SLBF) == 0)
82
    {
83
      /*
84
       * Fully buffered: fill partially full buffer, if any,
85
       * and then flush.  If there is no partial buffer, write
86
       * one _bf._size byte chunk directly (without copying).
87
       *
88
       * String output is a special case: write as many bytes
89
       * as fit, but pretend we wrote everything.  This makes
90
       * snprintf() return the number of bytes needed, rather
91
       * than the number used, and avoids its write function
92
       * (so that the write function can be invalid).
93
       */
94
      do
95
        {
96
          GETIOV (;);
97
          w = fp->_w;
98
          if (fp->_flags & __SSTR)
99
            {
100
              if (len < w)
101
                w = len;
102
              COPY (w);         /* copy MIN(fp->_w,len), */
103
              fp->_w -= w;
104
              fp->_p += w;
105
              w = len;          /* but pretend copied all */
106
            }
107
          else if (fp->_p > fp->_bf._base && len > w)
108
            {
109
              /* fill and flush */
110
              COPY (w);
111
              /* fp->_w -= w; *//* unneeded */
112
              fp->_p += w;
113
              if (fflush (fp))
114
                goto err;
115
            }
116
          else if (len >= (w = fp->_bf._size))
117
            {
118
              /* write directly */
119
              w = (*fp->_write) (fp->_cookie, p, w);
120
              if (w <= 0)
121
                goto err;
122
            }
123
          else
124
            {
125
              /* fill and done */
126
              w = len;
127
              COPY (w);
128
              fp->_w -= w;
129
              fp->_p += w;
130
            }
131
          p += w;
132
          len -= w;
133
        }
134
      while ((uio->uio_resid -= w) != 0);
135
    }
136
  else
137
    {
138
      /*
139
       * Line buffered: like fully buffered, but we
140
       * must check for newlines.  Compute the distance
141
       * to the first newline (including the newline),
142
       * or `infinity' if there is none, then pretend
143
       * that the amount to write is MIN(len,nldist).
144
       */
145
      nlknown = 0;
146
      do
147
        {
148
          GETIOV (nlknown = 0);
149
          if (!nlknown)
150
            {
151
              nl = memchr ((void *) p, '\n', len);
152
              nldist = nl ? nl + 1 - p : len + 1;
153
              nlknown = 1;
154
            }
155
          s = MIN (len, nldist);
156
          w = fp->_w + fp->_bf._size;
157
          if (fp->_p > fp->_bf._base && s > w)
158
            {
159
              COPY (w);
160
              /* fp->_w -= w; */
161
              fp->_p += w;
162
              if (fflush (fp))
163
                goto err;
164
            }
165
          else if (s >= (w = fp->_bf._size))
166
            {
167
              w = (*fp->_write) (fp->_cookie, p, w);
168
              if (w <= 0)
169
                goto err;
170
            }
171
          else
172
            {
173
              w = s;
174
              COPY (w);
175
              fp->_w -= w;
176
              fp->_p += w;
177
            }
178
          if ((nldist -= w) == 0)
179
            {
180
              /* copied the newline: flush and forget */
181
              if (fflush (fp))
182
                goto err;
183
              nlknown = 0;
184
            }
185
          p += w;
186
          len -= w;
187
        }
188
      while ((uio->uio_resid -= w) != 0);
189
    }
190
  return 0;
191
 
192
err:
193
  fp->_flags |= __SERR;
194
  return EOF;
195
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.