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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [fvwrite.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
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
    {
65
      fp->_flags |= __SERR;
66
      ptr->_errno = EBADF;
67
      return EOF;
68
    }
69
 
70
  iov = uio->uio_iov;
71
  len = 0;
72
 
73
#ifdef __SCLE
74
  if (fp->_flags & __SCLE) /* text mode */
75
    {
76
      do
77
        {
78
          GETIOV (;);
79
          while (len > 0)
80
            {
81
              if (putc (*p, fp) == EOF)
82
                return EOF;
83
              p++;
84
              len--;
85
              uio->uio_resid--;
86
            }
87
        }
88
      while (uio->uio_resid > 0);
89
      return 0;
90
    }
91
#endif
92
 
93
  if (fp->_flags & __SNBF)
94
    {
95
      /*
96
       * Unbuffered: write up to BUFSIZ bytes at a time.
97
       */
98
      do
99
        {
100
          GETIOV (;);
101
          w = fp->_write (ptr, fp->_cookie, p, MIN (len, BUFSIZ));
102
          if (w <= 0)
103
            goto err;
104
          p += w;
105
          len -= w;
106
        }
107
      while ((uio->uio_resid -= w) != 0);
108
    }
109
  else if ((fp->_flags & __SLBF) == 0)
110
    {
111
      /*
112
       * Fully buffered: fill partially full buffer, if any,
113
       * and then flush.  If there is no partial buffer, write
114
       * one _bf._size byte chunk directly (without copying).
115
       *
116
       * String output is a special case: write as many bytes
117
       * as fit, but pretend we wrote everything.  This makes
118
       * snprintf() return the number of bytes needed, rather
119
       * than the number used, and avoids its write function
120
       * (so that the write function can be invalid).  If
121
       * we are dealing with the asprintf routines, we will
122
       * dynamically increase the buffer size as needed.
123
       */
124
      do
125
        {
126
          GETIOV (;);
127
          w = fp->_w;
128
          if (fp->_flags & __SSTR)
129
            {
130
              if (len >= w && fp->_flags & (__SMBF | __SOPT))
131
                { /* must be asprintf family */
132
                  unsigned char *str;
133
                  int curpos = (fp->_p - fp->_bf._base);
134
                  /* Choose a geometric growth factor to avoid
135
                     quadratic realloc behavior, but use a rate less
136
                     than (1+sqrt(5))/2 to accomodate malloc
137
                     overhead. asprintf EXPECTS us to overallocate, so
138
                     that it can add a trailing \0 without
139
                     reallocating.  The new allocation should thus be
140
                     max(prev_size*1.5, curpos+len+1). */
141
                  int newsize = fp->_bf._size * 3 / 2;
142
                  if (newsize < curpos + len + 1)
143
                    newsize = curpos + len + 1;
144
                  if (fp->_flags & __SOPT)
145
                    {
146
                      /* asnprintf leaves original buffer alone.  */
147
                      str = (unsigned char *)_malloc_r (ptr, newsize);
148
                      if (!str)
149
                        {
150
                          ptr->_errno = ENOMEM;
151
                          goto err;
152
                        }
153
                      memcpy (str, fp->_bf._base, curpos);
154
                      fp->_flags = (fp->_flags & ~__SOPT) | __SMBF;
155
                    }
156
                  else
157
                    {
158
                      str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
159
                                                         newsize);
160
                      if (!str)
161
                        {
162
                          /* Free buffer which is no longer used.  */
163
                          _free_r (ptr, fp->_bf._base);
164
                          /* Ensure correct errno, even if free changed it.  */
165
                          ptr->_errno = ENOMEM;
166
                          goto err;
167
                        }
168
                    }
169
                  fp->_bf._base = str;
170
                  fp->_p = str + curpos;
171
                  fp->_bf._size = newsize;
172
                  w = len;
173
                  fp->_w = newsize - curpos;
174
                }
175
              if (len < w)
176
                w = len;
177
              COPY (w);         /* copy MIN(fp->_w,len), */
178
              fp->_w -= w;
179
              fp->_p += w;
180
              w = len;          /* but pretend copied all */
181
            }
182
          else if (fp->_p > fp->_bf._base && len > w)
183
            {
184
              /* fill and flush */
185
              COPY (w);
186
              /* fp->_w -= w; *//* unneeded */
187
              fp->_p += w;
188
              if (_fflush_r (ptr, fp))
189
                goto err;
190
            }
191
          else if (len >= (w = fp->_bf._size))
192
            {
193
              /* write directly */
194
              w = fp->_write (ptr, fp->_cookie, p, w);
195
              if (w <= 0)
196
                goto err;
197
            }
198
          else
199
            {
200
              /* fill and done */
201
              w = len;
202
              COPY (w);
203
              fp->_w -= w;
204
              fp->_p += w;
205
            }
206
          p += w;
207
          len -= w;
208
        }
209
      while ((uio->uio_resid -= w) != 0);
210
    }
211
  else
212
    {
213
      /*
214
       * Line buffered: like fully buffered, but we
215
       * must check for newlines.  Compute the distance
216
       * to the first newline (including the newline),
217
       * or `infinity' if there is none, then pretend
218
       * that the amount to write is MIN(len,nldist).
219
       */
220
      nlknown = 0;
221
      nldist = 0;
222
      do
223
        {
224
          GETIOV (nlknown = 0);
225
          if (!nlknown)
226
            {
227
              nl = memchr ((_PTR) p, '\n', len);
228
              nldist = nl ? nl + 1 - p : len + 1;
229
              nlknown = 1;
230
            }
231
          s = MIN (len, nldist);
232
          w = fp->_w + fp->_bf._size;
233
          if (fp->_p > fp->_bf._base && s > w)
234
            {
235
              COPY (w);
236
              /* fp->_w -= w; */
237
              fp->_p += w;
238
              if (_fflush_r (ptr, fp))
239
                goto err;
240
            }
241
          else if (s >= (w = fp->_bf._size))
242
            {
243
              w = fp->_write (ptr, fp->_cookie, p, w);
244
              if (w <= 0)
245
                goto err;
246
            }
247
          else
248
            {
249
              w = s;
250
              COPY (w);
251
              fp->_w -= w;
252
              fp->_p += w;
253
            }
254
          if ((nldist -= w) == 0)
255
            {
256
              /* copied the newline: flush and forget */
257
              if (_fflush_r (ptr, fp))
258
                goto err;
259
              nlknown = 0;
260
            }
261
          p += w;
262
          len -= w;
263
        }
264
      while ((uio->uio_resid -= w) != 0);
265
    }
266
  return 0;
267
 
268
err:
269
  fp->_flags |= __SERR;
270
  return EOF;
271
}

powered by: WebSVN 2.1.0

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