Merge from vendor branch OPENPAM:
[dragonfly.git] / include / stdio.h
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)stdio.h     8.5 (Berkeley) 4/29/95
37  * $FreeBSD: src/include/stdio.h,v 1.24.2.5 2002/11/09 08:07:20 imp Exp $
38  * $DragonFly: src/include/stdio.h,v 1.8 2005/07/23 20:23:06 joerg Exp $
39  */
40
41 #ifndef _STDIO_H_
42 #define _STDIO_H_
43
44 #include <sys/cdefs.h>
45 #ifndef _SYS_STDINT_H_
46 #include <sys/stdint.h>
47 #endif
48 #ifndef _MACHINE_STDARG_H_
49 #include <machine/stdarg.h>
50 #endif
51
52 #ifndef _SIZE_T_DECLARED
53 #define _SIZE_T_DECLARED
54 typedef __size_t        size_t;
55 #endif
56
57 #ifndef NULL
58 #define NULL    0
59 #endif
60
61 typedef __off_t fpos_t;
62
63 /*
64  * stdio state variables.
65  *
66  * The following always hold:
67  *
68  *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
69  *              _lbfsize is -_bf._size, else _lbfsize is 0
70  *      if _flags&__SRD, _w is 0
71  *      if _flags&__SWR, _r is 0
72  *
73  * This ensures that the getc and putc macros (or inline functions) never
74  * try to write or read from a file that is in `read' or `write' mode.
75  * (Moreover, they can, and do, automatically switch from read mode to
76  * write mode, and back, on "r+" and "w+" files.)
77  *
78  * _lbfsize is used only to make the inline line-buffered output stream
79  * code as compact as possible.
80  *
81  * WARNING: Do not change the order of the fields in __FILE_public!
82  */
83 typedef struct __FILE FILE;
84
85 struct __FILE_public {
86         unsigned char   *_p;    /* current position in (some) buffer */
87         int             _flags;         /* flags, below; this FILE is free if 0 */
88         int             _fileno;        /* fileno, if Unix descriptor, else -1 */
89         __ssize_t       _r;             /* read space left for getc() */
90         __ssize_t       _w;             /* write space left for putc() */
91         __ssize_t       _lbfsize;       /* 0 or -_bf._size, for inline putc */
92 };
93
94 __BEGIN_DECLS
95 extern FILE *__stdinp, *__stdoutp, *__stderrp;
96 __END_DECLS
97
98 #define __SLBF  0x0001          /* line buffered */
99 #define __SNBF  0x0002          /* unbuffered */
100 #define __SRD   0x0004          /* OK to read */
101 #define __SWR   0x0008          /* OK to write */
102         /* RD and WR are never simultaneously asserted */
103 #define __SRW   0x0010          /* open for reading & writing */
104 #define __SEOF  0x0020          /* found EOF */
105 #define __SERR  0x0040          /* found error */
106 #define __SMBF  0x0080          /* _buf is from malloc */
107 #define __SAPP  0x0100          /* fdopen()ed in append mode */
108 #define __SSTR  0x0200          /* this is an sprintf/snprintf string */
109 #define __SOPT  0x0400          /* do fseek() optimization */
110 #define __SNPT  0x0800          /* do not do fseek() optimization */
111 #define __SOFF  0x1000          /* set iff _offset is in fact correct */
112 #define __SMOD  0x2000          /* true => fgetln modified _p text */
113 #define __SALC  0x4000          /* allocate string space dynamically */
114
115 /*
116  * The following three definitions are for ANSI C, which took them
117  * from System V, which brilliantly took internal interface macros and
118  * made them official arguments to setvbuf(), without renaming them.
119  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
120  *
121  * Although numbered as their counterparts above, the implementation
122  * does not rely on this.
123  */
124 #define _IOFBF  0               /* setvbuf should set fully buffered */
125 #define _IOLBF  1               /* setvbuf should set line buffered */
126 #define _IONBF  2               /* setvbuf should set unbuffered */
127
128 #define BUFSIZ  1024            /* size of buffer used by setbuf */
129 #define EOF     (-1)
130
131 /*
132  * FOPEN_MAX is a minimum maximum, and is the number of streams that
133  * stdio can provide without attempting to allocate further resources
134  * (which could fail).  Do not use this for anything.
135  */
136                                 /* must be == _POSIX_STREAM_MAX <limits.h> */
137 #define FOPEN_MAX       20      /* must be <= OPEN_MAX <sys/syslimits.h> */
138 #define FILENAME_MAX    1024    /* must be <= PATH_MAX <sys/syslimits.h> */
139
140 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
141 #ifndef _ANSI_SOURCE
142 #define P_tmpdir        "/var/tmp/"
143 #endif
144 #define L_tmpnam        1024    /* XXX must be == PATH_MAX */
145 #define TMP_MAX         308915776
146
147 #ifndef SEEK_SET
148 #define SEEK_SET        0       /* set file offset to offset */
149 #endif
150 #ifndef SEEK_CUR
151 #define SEEK_CUR        1       /* set file offset to current plus offset */
152 #endif
153 #ifndef SEEK_END
154 #define SEEK_END        2       /* set file offset to EOF plus offset */
155 #endif
156
157 #define stdin   (__stdinp)
158 #define stdout  (__stdoutp)
159 #define stderr  (__stderrp)
160
161 /*
162  * Functions defined in ANSI C standard.
163  */
164 __BEGIN_DECLS
165 void     clearerr (FILE *);
166 int      fclose (FILE *);
167 int      feof (FILE *);
168 int      ferror (FILE *);
169 int      fflush (FILE *);
170 int      fgetc (FILE *);
171 int      fgetpos (FILE *, fpos_t *);
172 char    *fgets (char *, int, FILE *);
173 FILE    *fopen (const char *, const char *);
174 int      fprintf (FILE *, const char *, ...);
175 int      fputc (int, FILE *);
176 int      fputs (const char *, FILE *);
177 size_t fread (void *, size_t, size_t, FILE *);
178 FILE    *freopen (const char *, const char *, FILE *);
179 int      fscanf (FILE *, const char *, ...);
180 int      fseek (FILE *, long, int);
181 int      fsetpos (FILE *, const fpos_t *);
182 long     ftell (FILE *);
183 size_t fwrite (const void *, size_t, size_t, FILE *);
184 int      getc (FILE *);
185 int      getchar (void);
186 char    *gets (char *);
187 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
188 extern __const int sys_nerr;            /* perror(3) external variables */
189 extern __const char *__const sys_errlist[];
190 #endif
191 void     perror (const char *);
192 int      printf (const char *, ...);
193 int      putc (int, FILE *);
194 int      putchar (int);
195 int      puts (const char *);
196 int      remove (const char *);
197 int      rename  (const char *, const char *);
198 void     rewind (FILE *);
199 int      scanf (const char *, ...);
200 void     setbuf (FILE *, char *);
201 int      setvbuf (FILE *, char *, int, size_t);
202 int      sprintf (char *, const char *, ...);
203 int      sscanf (const char *, const char *, ...);
204 FILE    *tmpfile (void);
205 char    *tmpnam (char *);
206 int      ungetc (int, FILE *);
207 int      vfprintf (FILE *, const char *, __va_list);
208 int      vprintf (const char *, __va_list);
209 int      vsprintf (char *, const char *, __va_list);
210 __END_DECLS
211
212 /*
213  * Functions defined in POSIX 1003.1.
214  */
215 #ifndef _ANSI_SOURCE
216 /* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
217 #define L_cuserid       17
218
219 #define L_ctermid       1024    /* size for ctermid(3); PATH_MAX */
220
221 __BEGIN_DECLS
222 char    *ctermid (char *);
223 FILE    *fdopen (int, const char *);
224 int      fileno (FILE *);
225 int      ftrylockfile (FILE *);
226 void     flockfile (FILE *);
227 void     funlockfile (FILE *);
228 __END_DECLS
229 #endif /* not ANSI */
230
231 /*
232  * Portability hacks.  See <sys/types.h>.
233  */
234 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
235 __BEGIN_DECLS
236 #ifndef _FTRUNCATE_DECLARED
237 #define _FTRUNCATE_DECLARED
238 int      ftruncate (int, __off_t);
239 #endif
240 #ifndef _LSEEK_DECLARED
241 #define _LSEEK_DECLARED
242 __off_t lseek (int, __off_t, int);
243 #endif
244 #ifndef _MMAP_DECLARED
245 #define _MMAP_DECLARED
246 void    *mmap (void *, size_t, int, int, int, __off_t);
247 #endif
248 #ifndef _TRUNCATE_DECLARED
249 #define _TRUNCATE_DECLARED
250 int      truncate (const char *, __off_t);
251 #endif
252 __END_DECLS
253 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
254
255 /*
256  * Routines that are purely local.
257  */
258 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
259 __BEGIN_DECLS
260 int      asprintf (char **, const char *, ...) __printflike(2, 3);
261 char    *ctermid_r (char *);
262 void    *fcookie(FILE *);
263 char    *fgetln (FILE *, size_t *);
264 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
265 #define __ATTR_FORMAT_ARG       __attribute__((__format_arg__(2)))
266 #else
267 #define __ATTR_FORMAT_ARG
268 #endif
269 __const char *fmtcheck (const char *, const char *) __ATTR_FORMAT_ARG;
270 __ssize_t __fpending(FILE *);
271 int      fpurge (FILE *);
272 int      fseeko (FILE *, __off_t, int);
273 __off_t ftello (FILE *);
274 int      getw (FILE *);
275 int      pclose (FILE *);
276 FILE    *popen (const char *, const char *);
277 int      putw (int, FILE *);
278 void     setbuffer (FILE *, char *, int);
279 int      setlinebuf (FILE *);
280 char    *tempnam (const char *, const char *);
281 int      snprintf (char *, size_t, const char *, ...) __printflike(3, 4);
282 int      vasprintf (char **, const char *, __va_list)
283             __printflike(2, 0);
284 int      vsnprintf (char *, size_t, const char *, __va_list)
285             __printflike(3, 0);
286 int      vscanf (const char *, __va_list) __scanflike(1, 0);
287 int      vsscanf (const char *, const char *, __va_list)
288             __scanflike(2, 0);
289 __END_DECLS
290
291 /*
292  * This is a #define because the function is used internally and
293  * (unlike vfscanf) the name __vfscanf is guaranteed not to collide
294  * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
295  */
296 #define  vfscanf        __vfscanf
297
298 /*
299  * Stdio function-access interface.
300  */
301 __BEGIN_DECLS
302 FILE    *funopen (const void *,
303                 int (*)(void *, char *, int),
304                 int (*)(void *, const char *, int),
305                 fpos_t (*)(void *, fpos_t, int),
306                 int (*)(void *));
307 __END_DECLS
308 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
309 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
310 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
311
312 /*
313  * Functions internal to the implementation.
314  */
315 __BEGIN_DECLS
316 int     __srget (FILE *);
317 int     __vfscanf (FILE *, const char *, __va_list);
318 int     __svfscanf (FILE *, const char *, __va_list);
319 int     __swbuf (int, FILE *);
320 __END_DECLS
321
322 /*
323  * The __sfoo functions are here so that we can
324  * define real function versions in the C library.
325  */
326 static __inline int
327 __sgetc(FILE *_fp)
328 {
329         struct __FILE_public *_p = (struct __FILE_public *)_fp;
330
331         if (--_p->_r < 0)
332                 return (__srget(_fp));
333         else
334                 return (*_p->_p++);
335 }
336
337 static __inline int
338 __sputc(int _c, FILE *_fp)
339 {
340         struct __FILE_public *_p = (struct __FILE_public *)_fp;
341
342         if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && _c != '\n'))
343                 return (*_p->_p++ = _c);
344         else
345                 return (__swbuf(_c, _fp));
346 }
347
348 static __inline int
349 __sfeof(FILE *_fp)
350 {
351         struct __FILE_public *_p = (struct __FILE_public *)_fp;
352
353         return ((_p->_flags & __SEOF) != 0);
354 }
355
356 static __inline int
357 __sferror(FILE *_fp)
358 {
359         struct __FILE_public *_p = (struct __FILE_public *)_fp;
360
361         return ((_p->_flags & __SERR) != 0);
362 }
363
364 static __inline void
365 __sclearerr(FILE *_fp)
366 {
367         struct __FILE_public *_p = (struct __FILE_public *)_fp;
368
369         _p->_flags &= ~(__SERR|__SEOF);
370 }
371
372 static __inline int
373 __sfileno(FILE *_fp)
374 {
375         struct __FILE_public *_p = (struct __FILE_public *)_fp;
376
377         return (_p->_fileno);
378 }
379
380 /*
381  * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
382  * B.8.2.7 for the rationale behind the *_unlocked() macros.
383  */
384 #define feof_unlocked(p)        __sfeof(p)
385 #define ferror_unlocked(p)      __sferror(p)
386 #define clearerr_unlocked(p)    __sclearerr(p)
387
388 #ifndef _ANSI_SOURCE
389 #define fileno_unlocked(p)      __sfileno(p)
390 #endif
391
392 #define getc_unlocked(fp)       __sgetc(fp)
393 #define putc_unlocked(x, fp)    __sputc(x, fp)
394
395 #define getchar_unlocked()      getc_unlocked(stdin)
396 #define putchar_unlocked(x)     putc_unlocked(x, stdout)
397
398 #endif /* !_STDIO_H_ */