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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [fvwrite.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/* 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
 
66
#ifdef __SCLE
67
  if (fp->_flags & __SCLE) /* text mode */
68
    {
69
      do
70
      {
71
        GETIOV (;);
72
        while (len > 0)
73
          {
74
            if (putc(*p, fp) == EOF)
75
              return EOF;
76
            p++;
77
            len--;
78
            uio->uio_resid--;
79
          }
80
      }
81
      while (uio->uio_resid > 0);
82
      return 0;
83
    }
84
#endif
85
 
86
  if (fp->_flags & __SNBF)
87
    {
88
      /*
89
       * Unbuffered: write up to BUFSIZ bytes at a time.
90
       */
91
      do
92
        {
93
          GETIOV (;);
94
          w = (*fp->_write) (fp->_cookie, p, MIN (len, BUFSIZ));
95
          if (w <= 0)
96
            goto err;
97
          p += w;
98
          len -= w;
99
        }
100
      while ((uio->uio_resid -= w) != 0);
101
    }
102
  else if ((fp->_flags & __SLBF) == 0)
103
    {
104
      /*
105
       * Fully buffered: fill partially full buffer, if any,
106
       * and then flush.  If there is no partial buffer, write
107
       * one _bf._size byte chunk directly (without copying).
108
       *
109
       * String output is a special case: write as many bytes
110
       * as fit, but pretend we wrote everything.  This makes
111
       * snprintf() return the number of bytes needed, rather
112
       * than the number used, and avoids its write function
113
       * (so that the write function can be invalid).
114
       */
115
      do
116
        {
117
          GETIOV (;);
118
          w = fp->_w;
119
          if (fp->_flags & __SSTR)
120
            {
121
              if (len < w)
122
                w = len;
123
              COPY (w);         /* copy MIN(fp->_w,len), */
124
              fp->_w -= w;
125
              fp->_p += w;
126
              w = len;          /* but pretend copied all */
127
            }
128
          else if (fp->_p > fp->_bf._base && len > w)
129
            {
130
              /* fill and flush */
131
              COPY (w);
132
              /* fp->_w -= w; *//* unneeded */
133
              fp->_p += w;
134
              if (fflush (fp))
135
                goto err;
136
            }
137
          else if (len >= (w = fp->_bf._size))
138
            {
139
              /* write directly */
140
              w = (*fp->_write) (fp->_cookie, p, w);
141
              if (w <= 0)
142
                goto err;
143
            }
144
          else
145
            {
146
              /* fill and done */
147
              w = len;
148
              COPY (w);
149
              fp->_w -= w;
150
              fp->_p += w;
151
            }
152
          p += w;
153
          len -= w;
154
        }
155
      while ((uio->uio_resid -= w) != 0);
156
    }
157
  else
158
    {
159
      /*
160
       * Line buffered: like fully buffered, but we
161
       * must check for newlines.  Compute the distance
162
       * to the first newline (including the newline),
163
       * or `infinity' if there is none, then pretend
164
       * that the amount to write is MIN(len,nldist).
165
       */
166
      nlknown = 0;
167
      do
168
        {
169
          GETIOV (nlknown = 0);
170
          if (!nlknown)
171
            {
172
              nl = memchr ((void *) p, '\n', len);
173
              nldist = nl ? nl + 1 - p : len + 1;
174
              nlknown = 1;
175
            }
176
          s = MIN (len, nldist);
177
          w = fp->_w + fp->_bf._size;
178
          if (fp->_p > fp->_bf._base && s > w)
179
            {
180
              COPY (w);
181
              /* fp->_w -= w; */
182
              fp->_p += w;
183
              if (fflush (fp))
184
                goto err;
185
            }
186
          else if (s >= (w = fp->_bf._size))
187
            {
188
              w = (*fp->_write) (fp->_cookie, p, w);
189
              if (w <= 0)
190
                goto err;
191
            }
192
          else
193
            {
194
              w = s;
195
              COPY (w);
196
              fp->_w -= w;
197
              fp->_p += w;
198
            }
199
          if ((nldist -= w) == 0)
200
            {
201
              /* copied the newline: flush and forget */
202
              if (fflush (fp))
203
                goto err;
204
              nlknown = 0;
205
            }
206
          p += w;
207
          len -= w;
208
        }
209
      while ((uio->uio_resid -= w) != 0);
210
    }
211
  return 0;
212
 
213
err:
214
  fp->_flags |= __SERR;
215
  return EOF;
216
}

powered by: WebSVN 2.1.0

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