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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 joel
 
2 39 lampret
/*
3
 * Copyright (c) 1990 The Regents of the University of California.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms are permitted
7
 * provided that the above copyright notice and this paragraph are
8
 * duplicated in all such forms and that any documentation,
9
 * advertising materials, and other materials related to such
10
 * distribution and use acknowledge that the software was developed
11
 * by the University of California, Berkeley.  The name of the
12
 * University may not be used to endorse or promote products derived
13
 * from this software without specific prior written permission.
14
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17
 */
18
 
19
/*
20
FUNCTION
21
<<setvbuf>>---specify file or stream buffering
22
 
23
INDEX
24
        setvbuf
25
 
26
ANSI_SYNOPSIS
27
        #include <stdio.h>
28
        int setvbuf(FILE *<[fp]>, char *<[buf]>,
29
                    int <[mode]>, size_t <[size]>);
30
 
31
TRAD_SYNOPSIS
32
        #include <stdio.h>
33
        int setvbuf(<[fp]>, <[buf]>, <[mode]>, <[size]>)
34
        FILE *<[fp]>;
35
        char *<[buf]>;
36
        int <[mode]>;
37
        size_t <[size]>;
38
 
39
DESCRIPTION
40
Use <<setvbuf>> to specify what kind of buffering you want for the
41
file or stream identified by <[fp]>, by using one of the following
42
values (from <<stdio.h>>) as the <[mode]> argument:
43
 
44
o+
45
o _IONBF
46
Do not use a buffer: send output directly to the host system for the
47
file or stream identified by <[fp]>.
48
 
49
o _IOFBF
50
Use full output buffering: output will be passed on to the host system
51
only when the buffer is full, or when an input operation intervenes.
52
 
53
o _IOLBF
54
Use line buffering: pass on output to the host system at every
55
newline, as well as when the buffer is full, or when an input
56
operation intervenes.
57
o-
58
 
59
Use the <[size]> argument to specify how large a buffer you wish.  You
60
can supply the buffer itself, if you wish, by passing a pointer to a
61
suitable area of memory as <[buf]>.  Otherwise, you may pass <<NULL>>
62
as the <[buf]> argument, and <<setvbuf>> will allocate the buffer.
63
 
64
WARNINGS
65
You may only use <<setvbuf>> before performing any file operation other
66
than opening the file.
67
 
68
If you supply a non-null <[buf]>, you must ensure that the associated
69
storage continues to be available until you close the stream
70
identified by <[fp]>.
71
 
72
RETURNS
73
A <<0>> result indicates success, <<EOF>> failure (invalid <[mode]> or
74
<[size]> can cause failure).
75
 
76
PORTABILITY
77
Both ANSI C and the System V Interface Definition (Issue 2) require
78
<<setvbuf>>. However, they differ on the meaning of a <<NULL>> buffer
79
pointer: the SVID issue 2 specification says that a <<NULL>> buffer
80
pointer requests unbuffered output.  For maximum portability, avoid
81
<<NULL>> buffer pointers.
82
 
83
Both specifications describe the result on failure only as a
84
nonzero value.
85
 
86
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
87
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
88
*/
89
 
90
#include <_ansi.h>
91
#include <stdio.h>
92
#include <stdlib.h>
93
#include "local.h"
94
 
95
/*
96
 * Set one of the three kinds of buffering, optionally including a buffer.
97
 */
98
 
99
int
100
_DEFUN (setvbuf, (fp, buf, mode, size),
101
        register FILE * fp _AND
102
        char *buf _AND
103
        register int mode _AND
104
        register size_t size)
105
{
106
  int ret = 0;
107
  CHECK_INIT (fp);
108
 
109
  /*
110
   * Verify arguments.  The `int' limit on `size' is due to this
111
   * particular implementation.
112
   */
113
 
114
  if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int)(_POINTER_INT) size < 0)
115
    return (EOF);
116
 
117
  /*
118
   * Write current buffer, if any; drop read count, if any.
119
   * Make sure putc() will not think fp is line buffered.
120
   * Free old buffer if it was from malloc().  Clear line and
121
   * non buffer flags, and clear malloc flag.
122
   */
123
 
124
  (void) fflush (fp);
125
  fp->_r = 0;
126
  fp->_lbfsize = 0;
127
  if (fp->_flags & __SMBF)
128
    _free_r (fp->_data, (void *) fp->_bf._base);
129
  fp->_flags &= ~(__SLBF | __SNBF | __SMBF);
130
 
131 56 joel
  if (mode == _IONBF)
132
    goto nbf;
133
 
134 39 lampret
  /*
135
   * Allocate buffer if needed. */
136
  if (buf == NULL)
137
    {
138
      if ((buf = malloc (size)) == NULL)
139
        {
140
          ret = EOF;
141
          /* Try another size... */
142
          buf = malloc (BUFSIZ);
143 56 joel
          size = BUFSIZ;
144 39 lampret
        }
145
      if (buf == NULL)
146
        {
147
          /* Can't allocate it, let's try another approach */
148
nbf:
149
          fp->_flags |= __SNBF;
150
          fp->_w = 0;
151
          fp->_bf._base = fp->_p = fp->_nbuf;
152
          fp->_bf._size = 1;
153
          return (ret);
154
        }
155
      fp->_flags |= __SMBF;
156
    }
157
  /*
158
   * Now put back whichever flag is needed, and fix _lbfsize
159
   * if line buffered.  Ensure output flush on exit if the
160
   * stream will be buffered at all.
161
   * If buf is NULL then make _lbfsize 0 to force the buffer
162
   * to be flushed and hence malloced on first use
163
   */
164
 
165
  switch (mode)
166
    {
167
    case _IOLBF:
168
      fp->_flags |= __SLBF;
169
      fp->_lbfsize = buf ? -size : 0;
170
      /* FALLTHROUGH */
171
 
172
    case _IOFBF:
173
      /* no flag */
174
      fp->_data->__cleanup = _cleanup_r;
175
      fp->_bf._base = fp->_p = (unsigned char *) buf;
176
      fp->_bf._size = size;
177
      break;
178
    }
179
 
180
  /*
181
   * Patch up write count if necessary.
182
   */
183
 
184
  if (fp->_flags & __SWR)
185
    fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size;
186
 
187
  return 0;
188
}

powered by: WebSVN 2.1.0

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