Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

stdio.h

00001 /* stdio.h stdc header
00002    Adapted by Stephane Carrez (stcarrez@nerim.fr) from FreeBSD stdio.h
00003    (Cleaned up and removed non-relevant operations)
00004 */
00005 /*-
00006  * Copyright (c) 1990, 1993
00007  *      The Regents of the University of California.  All rights reserved.
00008  *
00009  * This code is derived from software contributed to Berkeley by
00010  * Chris Torek.
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions
00014  * are met:
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above copyright
00018  *    notice, this list of conditions and the following disclaimer in the
00019  *    documentation and/or other materials provided with the distribution.
00020  * 3. All advertising materials mentioning features or use of this software
00021  *    must display the following acknowledgement:
00022  *      This product includes software developed by the University of
00023  *      California, Berkeley and its contributors.
00024  * 4. Neither the name of the University nor the names of its contributors
00025  *    may be used to endorse or promote products derived from this software
00026  *    without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00029  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00032  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00033  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00034  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00035  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00036  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00037  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00038  * SUCH DAMAGE.
00039  *
00040  *      @(#)stdio.h     8.5 (Berkeley) 4/29/95
00041  * $FreeBSD: src/include/stdio.h,v 1.51 2003/01/13 08:41:47 tjr Exp $
00042  */
00043 
00044 #ifndef _STDIO_H_
00045 #define _STDIO_H_
00046 
00047 #include <sys/cdefs.h>
00048 #include <sys/_types.h>
00049 
00050 typedef __off_t         fpos_t;
00051 
00052 #ifndef _SIZE_T_DECLARED
00053 typedef __size_t        size_t;
00054 #define _SIZE_T_DECLARED
00055 #endif
00056 
00057 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
00058 #include <stdarg.h>
00059 typedef va_list __va_list;
00060 #endif
00061 
00062 #ifndef NULL
00063 #define NULL    0
00064 #endif
00065 
00066 #define _FSTDIO                 /* Define for new stdio with functions. */
00067 
00068 /*
00069  * NB: to fit things in six character monocase externals, the stdio
00070  * code uses the prefix `__s' for stdio objects, typically followed
00071  * by a three-character attempt at a mnemonic.
00072  */
00073 
00074 /* stdio buffers */
00075 struct __sbuf {
00076         unsigned char *_base;
00077         int     _size;
00078 };
00079 
00080 /* hold a buncha junk that would grow the ABI */
00081 struct __sFILEX;
00082 struct __File_ops;
00083 
00084 /*
00085  * stdio state variables.
00086  *
00087  * The following always hold:
00088  *
00089  *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
00090  *              _lbfsize is -_bf._size, else _lbfsize is 0
00091  *      if _flags&__SRD, _w is 0
00092  *      if _flags&__SWR, _r is 0
00093  *
00094  * This ensures that the getc and putc macros (or inline functions) never
00095  * try to write or read from a file that is in `read' or `write' mode.
00096  * (Moreover, they can, and do, automatically switch from read mode to
00097  * write mode, and back, on "r+" and "w+" files.)
00098  *
00099  * _lbfsize is used only to make the inline line-buffered output stream
00100  * code as compact as possible.
00101  *
00102  * _ub, _up, and _ur are used when ungetc() pushes back more characters
00103  * than fit in the current _bf, or when ungetc() pushes back a character
00104  * that does not match the previous one in _bf.  When this happens,
00105  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
00106  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
00107  *
00108  * NB: see WARNING above before changing the layout of this structure!
00109  */
00110 typedef struct __sFILE {
00111         unsigned char *_p;      /* current position in (some) buffer */
00112         int     _r;             /* read space left for getc() */
00113         int     _w;             /* write space left for putc() */
00114         short   _flags;         /* flags, below; this FILE is free if 0 */
00115         short   _file;          /* fileno, if Unix descriptor, else -1 */
00116         struct  __sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
00117         int     _lbfsize;       /* 0 or -_bf._size, for inline putc */
00118         const struct __File_ops* _ops;
00119         /* separate buffer for long sequences of ungetc() */
00120         struct  __sbuf _ub;     /* ungetc buffer */
00121         int     _ur;            /* saved _r when _r is counting ungetc data */
00122         char* _up;
00123 } FILE;
00124 
00125 __BEGIN_DECLS
00126 extern FILE *const __stdinp;
00127 extern FILE *const __stdoutp;
00128 extern FILE *const __stderrp;
00129 __END_DECLS
00130 
00131 #define __SLBF  0x0001          /* line buffered */
00132 #define __SNBF  0x0002          /* unbuffered */
00133 #define __SRD   0x0004          /* OK to read */
00134 #define __SWR   0x0008          /* OK to write */
00135         /* RD and WR are never simultaneously asserted */
00136 #define __SRW   0x0010          /* open for reading & writing */
00137 #define __SEOF  0x0020          /* found EOF */
00138 #define __SERR  0x0040          /* found error */
00139 #define __SMBF  0x0080          /* _buf is from malloc */
00140 #define __SAPP  0x0100          /* fdopen()ed in append mode */
00141 #define __SSTR  0x0200          /* this is an sprintf/snprintf string */
00142 #define __SNPT  0x0800          /* do not do fseek() optimization */
00143 #define __SALC  0x4000          /* allocate string space dynamically */
00144 #define __SIGN  0x8000          /* ignore this file in _fwalk */
00145 
00146 /*
00147  * The following three definitions are for ANSI C, which took them
00148  * from System V, which brilliantly took internal interface macros and
00149  * made them official arguments to setvbuf(), without renaming them.
00150  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
00151  *
00152  * Although numbered as their counterparts above, the implementation
00153  * does not rely on this.
00154  */
00155 #define _IOFBF  0               /* setvbuf should set fully buffered */
00156 #define _IOLBF  1               /* setvbuf should set line buffered */
00157 #define _IONBF  2               /* setvbuf should set unbuffered */
00158 
00159 #define BUFSIZ  1024            /* size of buffer used by setbuf */
00160 #define EOF     (-1)
00161 
00162 /*
00163  * FOPEN_MAX is a minimum maximum, and is the number of streams that
00164  * stdio can provide without attempting to allocate further resources
00165  * (which could fail).  Do not use this for anything.
00166  */
00167                                 /* must be == _POSIX_STREAM_MAX <limits.h> */
00168 #define FOPEN_MAX       20      /* must be <= OPEN_MAX <sys/syslimits.h> */
00169 #define FILENAME_MAX    1024    /* must be <= PATH_MAX <sys/syslimits.h> */
00170 
00171 #ifndef SEEK_SET
00172 #define SEEK_SET        0       /* set file offset to offset */
00173 #endif
00174 #ifndef SEEK_CUR
00175 #define SEEK_CUR        1       /* set file offset to current plus offset */
00176 #endif
00177 #ifndef SEEK_END
00178 #define SEEK_END        2       /* set file offset to EOF plus offset */
00179 #endif
00180 
00181 #define stdin   __stdinp
00182 #define stdout  __stdoutp
00183 #define stderr  __stderrp
00184 
00185 __BEGIN_DECLS
00186 /*
00187  * Functions defined in ANSI C standard.
00188  */
00189 void     clearerr(FILE *);
00190 int      fclose(FILE *);
00191 int      feof(FILE *);
00192 int      ferror(FILE *);
00193 int      fflush(FILE *);
00194 int      fgetc(FILE *);
00195 char    *fgets(char * __restrict, int, FILE * __restrict);
00196 int      fprintf(FILE * __restrict, const char * __restrict, ...);
00197 int      fputc(int, FILE *);
00198 int      fputs(const char * __restrict, FILE * __restrict);
00199 size_t   fread(void * __restrict, size_t, size_t, FILE * __restrict);
00200 int      fscanf(FILE * __restrict, const char * __restrict, ...);
00201 size_t   fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
00202 int      getc(FILE *);
00203 int      getchar(void);
00204 char    *gets(char *);
00205 int      printf(const char * __restrict, ...);
00206 int      putc(int, FILE *);
00207 int      putchar(int);
00208 int      puts(const char *);
00209 int      scanf(const char * __restrict, ...);
00210 void     setbuf(FILE * __restrict, char * __restrict);
00211 int      setvbuf(FILE * __restrict, char * __restrict, int, size_t);
00212 int      sprintf(char * __restrict, const char * __restrict, ...);
00213 int      sscanf(const char * __restrict, const char * __restrict, ...);
00214 int      ungetc(int, FILE *);
00215 int      vfprintf(FILE * __restrict, const char * __restrict, __va_list);
00216 int      vprintf(const char * __restrict, __va_list);
00217 int      vsprintf(char * __restrict, const char * __restrict, __va_list);
00218 
00219 #if __ISO_C_VISIBLE >= 1999
00220 int      snprintf(char * __restrict, size_t, const char * __restrict,
00221             ...) __printflike(3, 4);
00222 int      vfscanf(FILE * __restrict, const char * __restrict, __va_list)
00223             __scanflike(2, 0);
00224 int      vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
00225 int      vsnprintf(char * __restrict, size_t, const char * __restrict,
00226             __va_list) __printflike(3, 0);
00227 int      vsscanf(const char * __restrict, const char * __restrict, __va_list)
00228             __scanflike(2, 0);
00229 #endif
00230 
00231 
00232 #if __POSIX_VISIBLE
00233 int      fileno(FILE *);
00234 #endif /* __POSIX_VISIBLE */
00235 
00236 /*
00237  * Routines that are purely local.
00238  */
00239 #if __BSD_VISIBLE
00240 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
00241 #define __ATTR_FORMAT_ARG       __attribute__((__format_arg__(2)))
00242 #else
00243 #define __ATTR_FORMAT_ARG
00244 #endif
00245 void     setbuffer(FILE *, char *, int);
00246 int      setlinebuf(FILE *);
00247 
00248 /*
00249  * Stdio function-access interface.
00250  */
00251 FILE    *funopen(const void *,
00252             int (*)(void *, char *, int),
00253             int (*)(void *, const char *, int),
00254             fpos_t (*)(void *, fpos_t, int),
00255             int (*)(void *));
00256 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
00257 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
00258 
00259 #endif /* __BSD_VISIBLE */
00260 
00261 /*
00262  * Functions internal to the implementation.
00263  */
00264 int     __srget(FILE *);
00265 int     __swbufc(char, FILE *);
00266 
00267 /*
00268  * The __sfoo macros are here so that we can
00269  * define function versions in the C library.
00270  */
00271 #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
00272 #if defined(__GNUC__) && defined(__STDC__)
00273 extern int __sputc(int _c, FILE *_p);
00274 
00275 extern __inline int __sputc(int _c, FILE *_p) {
00276         if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
00277                 return (*_p->_p++ = _c);
00278         else
00279                 return (__swbufc(_c, _p));
00280 }
00281 #else
00282 /*
00283  * This has been tuned to generate reasonable code on the vax using pcc.
00284  */
00285 #define __sputc(c, p) \
00286         (--(p)->_w < 0 ? \
00287                 (p)->_w >= (p)->_lbfsize ? \
00288                         (*(p)->_p = (c)), *(p)->_p != '\n' ? \
00289                                 (int)*(p)->_p++ : \
00290                                 __swbuf('\n', p) : \
00291                         __swbuf((int)(c), p) : \
00292                 (*(p)->_p = (c), (int)*(p)->_p++))
00293 #endif
00294 
00295 #define __sfeof(p)      (((p)->_flags & __SEOF) != 0)
00296 #define __sferror(p)    (((p)->_flags & __SERR) != 0)
00297 #define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
00298 #define __sfileno(p)    ((p)->_file)
00299 
00300 __END_DECLS
00301 #endif /* !_STDIO_H_ */