stdio.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* Define ISO C stdio on top of C++ iostreams.
  2. Copyright (C) 1991, 1994-1999, 2000 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. /*
  16. * ISO C Standard: 4.9 INPUT/OUTPUT <stdio.h>
  17. */
  18. #ifndef _STDIO_H
  19. #define _STDIO_H
  20. #include <features.h>
  21. #include <stdarg.h>
  22. #include <sys/types.h>
  23. __BEGIN_DECLS
  24. /* when you add or change fields here, be sure to change the initialization
  25. * in stdio_init and fopen */
  26. struct __stdio_file {
  27. unsigned char *bufpos; /* the next byte to write to or read from */
  28. unsigned char *bufread; /* the end of data returned by last read() */
  29. unsigned char *bufwrite; /* 1 + highest address writable by macro */
  30. unsigned char *bufstart; /* the start of the buffer */
  31. unsigned char *bufend; /* the end of the buffer; ie the byte after the last
  32. malloc()ed byte */
  33. struct __stdio_file * next;
  34. int fd; /* the file descriptor associated with the stream */
  35. unsigned char mode;
  36. unsigned char ungot;
  37. char unbuf[2]; /* The buffer for 'unbuffered' streams */
  38. };
  39. typedef struct __stdio_file FILE;
  40. /* Default buffer size. */
  41. #define BUFSIZ (512)
  42. /* Define EOF and NULL */
  43. #define EOF (-1)
  44. #ifndef NULL
  45. #define NULL (0)
  46. #endif
  47. /* The possibilities for the third argument to `setvbuf'. */
  48. #define _IOFBF 0 /* Fully buffered. */
  49. #define _IOLBF 1 /* Line buffered. */
  50. #define _IONBF 2 /* No buffering. */
  51. /* Possible states for a file stream -- internal use only */
  52. #define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */
  53. #define __MODE_FREEBUF 0x04 /* Buffer allocated by stdio code, can free */
  54. #define __MODE_FREEFIL 0x08 /* FILE allocated by stdio code, can free */
  55. #define __MODE_UNGOT 0x10 /* Buffer has been polluted by ungetc */
  56. #define __MODE_TIED 0x20 /* FILE is tied with stdin/stdout */
  57. #define __MODE_EOF 0x40 /* EOF status */
  58. #define __MODE_ERR 0x80 /* Error status */
  59. /* The possibilities for the third argument to `fseek'.
  60. These values should not be changed. */
  61. #define SEEK_SET 0 /* Seek from beginning of file. */
  62. #define SEEK_CUR 1 /* Seek from current position. */
  63. #define SEEK_END 2 /* Seek from end of file. */
  64. /* Default path prefix for `tempnam' and `tmpnam'. */
  65. #define P_tmpdir "/tmp"
  66. /* Get the values:
  67. L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
  68. TMP_MAX The minimum number of unique filenames generated by tmpnam
  69. (and tempnam when it uses tmpnam's name space),
  70. or tempnam (the two are separate).
  71. L_ctermid How long an array to pass to `ctermid'.
  72. L_cuserid How long an array to pass to `cuserid'.
  73. FOPEN_MAX Minimum number of files that can be open at once.
  74. FILENAME_MAX Maximum length of a filename. */
  75. #define __need_FOPEN_MAX
  76. #include <bits/stdio_lim.h>
  77. #undef __need_FOPEN_MAX
  78. /* Standard streams (internal). */
  79. extern FILE *_stdin;
  80. extern FILE *_stdout;
  81. extern FILE *_stderr;
  82. /* C89/C99 say they're macros. Make them happy. */
  83. #define stdin _stdin
  84. #define stdout _stdout
  85. #define stderr _stderr
  86. /* Remove file FILENAME. */
  87. extern int remove __P ((__const char *__filename));
  88. /* Rename file OLD to NEW. */
  89. extern int rename __P ((__const char *__old, __const char *__new));
  90. /* Create a temporary file and open it read/write. */
  91. extern FILE *tmpfile __P ((void));
  92. #ifdef __USE_LARGEFILE64
  93. extern FILE *tmpfile64 __P ((void));
  94. #endif
  95. /* Generate a temporary filename. */
  96. extern char *tmpnam __P ((char *__s));
  97. #ifdef __USE_MISC
  98. /* This is the reentrant variant of `tmpnam'. The only difference is
  99. that it does not allow S to be NULL. */
  100. extern char *tmpnam_r __P ((char *__s));
  101. #endif
  102. #if defined __USE_SVID || defined __USE_XOPEN
  103. /* Generate a unique temporary filename using up to five characters of PFX
  104. if it is not NULL. The directory to put this file in is searched for
  105. as follows: First the environment variable "TMPDIR" is checked.
  106. If it contains the name of a writable directory, that directory is used.
  107. If not and if DIR is not NULL, that value is checked. If that fails,
  108. P_tmpdir is tried and finally "/tmp". The storage for the filename
  109. is allocated by `malloc'. */
  110. extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
  111. #endif
  112. /* Close STREAM. */
  113. extern int fclose __P ((FILE *__stream));
  114. /* Flush STREAM, or all streams if STREAM is NULL. */
  115. extern int fflush __P ((FILE *__stream));
  116. /* Used internally to actuall open files */
  117. extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
  118. FILE *__restrict __stream, __const char *__restrict __mode));
  119. /* Open a file and create a new stream for it. */
  120. extern FILE *fopen __P ((__const char *__restrict __filename,
  121. __const char *__restrict __mode));
  122. #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
  123. /* Open a file, replacing an existing stream with it. */
  124. extern FILE *freopen __P ((__const char *__restrict __filename,
  125. __const char *__restrict __mode,
  126. FILE *__restrict __stream));
  127. #ifdef __USE_MISC
  128. /*
  129. * Open a file using an automatically (stack) or statically allocated FILE.
  130. * The FILE * returned behaves just as any other FILE * with respect to the
  131. * stdio functions, but be aware of the following:
  132. * NOTE: The buffer used for the file is FILE's builtin 2-byte buffer, so
  133. * setting a new buffer is probably advisable.
  134. * NOTE: This function is primarily intended to be used for stack-allocated
  135. * FILEs when uClibc stdio has been built with no dynamic memory support.
  136. * For the statically allocated case, it is probably better to increase
  137. * the value of FIXED_STREAMS in stdio.c.
  138. * WARNING: If allocated on the stack, make sure you call fclose before the
  139. * stack memory is reclaimed!
  140. */
  141. extern FILE *fsfopen __P ((__const char *__restrict __filename,
  142. __const char *__restrict __mode,
  143. FILE *__restrict __stream));
  144. #endif
  145. #ifdef __USE_LARGEFILE64
  146. extern FILE *fopen64 __P ((__const char *__restrict __filename,
  147. __const char *__restrict __mode));
  148. extern FILE *freopen64 __P ((__const char *__restrict __filename,
  149. __const char *__restrict __mode,
  150. FILE *__restrict __stream));
  151. #endif
  152. #ifdef __USE_POSIX
  153. /* Create a new stream that refers to an existing system file descriptor. */
  154. extern FILE *fdopen __P ((int __fd, __const char *__mode));
  155. #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
  156. #endif
  157. /* Make STREAM use buffering mode MODE.
  158. If BUF is not NULL, use N bytes of it for buffering;
  159. else allocate an internal buffer N bytes long. */
  160. extern int setvbuf __P ((FILE *__restrict __stream, char *__restrict __buf,
  161. int __mode, size_t __n));
  162. /* If BUF is NULL, make STREAM unbuffered.
  163. Else make it use buffer BUF, of size BUFSIZ. */
  164. extern void setbuf __P ((FILE *__restrict __stream, char *__restrict __buf));
  165. #ifdef __USE_BSD
  166. /* If BUF is NULL, make STREAM unbuffered.
  167. Else make it use SIZE bytes of BUF for buffering. */
  168. extern void setbuffer __P ((FILE *__restrict __stream, char *__restrict __buf,
  169. size_t __size));
  170. /* Make STREAM line-buffered. */
  171. extern void setlinebuf __P ((FILE *__stream));
  172. #endif
  173. /* Write formatted output to STREAM. */
  174. extern int fprintf __P ((FILE *__restrict __stream,
  175. __const char *__restrict __format, ...));
  176. /* Write formatted output to stdout. */
  177. extern int printf __P ((__const char *__restrict __format, ...));
  178. /* Write formatted output to S. */
  179. extern int sprintf __P ((char *__restrict __s,
  180. __const char *__restrict __format, ...));
  181. /* Write formatted output to a file descriptor */
  182. extern int vdprintf __P((int fd, __const char *__restrict __format,
  183. va_list __arg));
  184. /* Write formatted output to a buffer S dynamically allocated by asprintf. */
  185. extern int asprintf __P ((char **__restrict __s,
  186. __const char *__restrict __format, ...));
  187. /* Write formatted output to S from argument list ARG. */
  188. extern int vfprintf __P ((FILE *__restrict __s,
  189. __const char *__restrict __format,
  190. va_list __arg));
  191. /* Write formatted output to stdout from argument list ARG. */
  192. extern int vprintf __P ((__const char *__restrict __format,
  193. va_list __arg));
  194. /* Write formatted output to S from argument list ARG. */
  195. extern int vsprintf __P ((char *__restrict __s,
  196. __const char *__restrict __format,
  197. va_list __arg));
  198. /* Maximum chars of output to write in MAXLEN. */
  199. extern int snprintf __P ((char *__restrict __s, size_t __maxlen,
  200. __const char *__restrict __format, ...))
  201. __attribute__ ((__format__ (__printf__, 3, 4)));
  202. extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
  203. __const char *__restrict __format,
  204. va_list __arg))
  205. __attribute__ ((__format__ (__printf__, 3, 0)));
  206. extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
  207. __const char *__restrict __format,
  208. va_list __arg))
  209. __attribute__ ((__format__ (__printf__, 3, 0)));
  210. /* Read formatted input from STREAM. */
  211. extern int fscanf __P ((FILE *__restrict __stream,
  212. __const char *__restrict __format, ...));
  213. /* Read formatted input from stdin. */
  214. extern int scanf __P ((__const char *__restrict __format, ...));
  215. /* Read formatted input from S. */
  216. extern int sscanf __P ((__const char *__restrict __s,
  217. __const char *__restrict __format, ...));
  218. /* Read formatted input from S into argument list ARG. */
  219. extern int vfscanf __P ((FILE *__restrict __s,
  220. __const char *__restrict __format,
  221. va_list __arg))
  222. __attribute__ ((__format__ (__scanf__, 2, 0)));
  223. /* Read formatted input from stdin into argument list ARG. */
  224. extern int vscanf __P ((__const char *__restrict __format, va_list __arg))
  225. __attribute__ ((__format__ (__scanf__, 1, 0)));
  226. /* Read formatted input from S into argument list ARG. */
  227. extern int vsscanf __P ((__const char *__restrict __s,
  228. __const char *__restrict __format,
  229. va_list __arg))
  230. __attribute__ ((__format__ (__scanf__, 2, 0)));
  231. /* Read a character from STREAM. */
  232. extern int fgetc __P ((FILE *__stream));
  233. extern int getc __P ((FILE *__stream));
  234. /* Read a character from stdin. */
  235. extern int getchar __P ((void));
  236. #define getchar() getc(_stdin)
  237. /* The C standard explicitly says this is a macro, so be that way */
  238. #define getc(stream) \
  239. (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
  240. (*(stream)->bufpos++))
  241. /* Write a character to STREAM. */
  242. extern int fputc __P ((int __c, FILE *__stream));
  243. extern int putc __P ((int __c, FILE *__stream));
  244. /* Write a character to stdout. */
  245. extern int putchar __P ((int __c));
  246. /* Beware! stdout can be redefined! */
  247. #define putchar(c) putc((c), _stdout)
  248. /* The C standard explicitly says this can be a macro, so be that way */
  249. #define putc(c, stream) \
  250. (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
  251. : (unsigned char) (*(stream)->bufpos++ = (c)) )
  252. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  253. /* Get a word (int) from STREAM. */
  254. extern int getw __P ((FILE *__stream));
  255. /* Write a word (int) to STREAM. */
  256. extern int putw __P ((int __w, FILE *__stream));
  257. #endif
  258. /* Get a newline-terminated string of finite length from STREAM. */
  259. extern char *fgets __P ((char *__restrict __s, int __n,
  260. FILE *__restrict __stream));
  261. /* Get a newline-terminated string from stdin, removing the newline.
  262. DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
  263. extern char *gets __P ((char *__s));
  264. /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
  265. (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
  266. NULL), pointing to *N characters of space. It is realloc'd as
  267. necessary. Returns the number of characters read (not including the
  268. null terminator), or -1 on error or EOF. */
  269. extern ssize_t __getdelim __P ((char **__restrict __lineptr,
  270. size_t *__restrict __n, int __delimiter,
  271. FILE *__restrict __stream));
  272. extern ssize_t getdelim __P ((char **__restrict __lineptr,
  273. size_t *__restrict __n, int __delimiter,
  274. FILE *__restrict __stream));
  275. /* Like `getdelim', but reads up to a newline. */
  276. extern ssize_t getline __P ((char **__restrict __lineptr,
  277. size_t *__restrict __n,
  278. FILE *__restrict __stream));
  279. /* Write a string to STREAM. */
  280. extern int fputs __P ((__const char *__restrict __s,
  281. FILE *__restrict __stream));
  282. /* Write a string, followed by a newline, to stdout. */
  283. extern int puts __P ((__const char *__s));
  284. /* Push a character back onto the input buffer of STREAM. */
  285. extern int ungetc __P ((int __c, FILE *__stream));
  286. /* Read chunks of generic data from STREAM. */
  287. extern size_t fread __P ((void *__restrict __ptr, size_t __size,
  288. size_t __n, FILE *__restrict __stream));
  289. /* Write chunks of generic data to STREAM. */
  290. extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
  291. size_t __n, FILE *__restrict __s));
  292. /* Rewind to the beginning of STREAM. */
  293. extern void rewind __P ((FILE *__stream));
  294. /* Seek to a certain position on STREAM. */
  295. extern int fseek __P ((FILE *__stream, long int __off, int __whence));
  296. /* Return the current position of STREAM. */
  297. extern long int ftell __P ((FILE *__stream));
  298. /* The Single Unix Specification, Version 2, specifies an alternative,
  299. more adequate interface for the two functions above which deal with
  300. file offset. `long int' is not the right type. These definitions
  301. are originally defined in the Large File Support API. */
  302. /* Types needed in these functions. */
  303. #ifndef off_t
  304. typedef __off_t off_t;
  305. # define off_t off_t
  306. #endif
  307. #if defined __USE_LARGEFILE64 && !defined off64_t
  308. typedef __off64_t off64_t;
  309. # define off64_t off64_t
  310. #endif
  311. #ifndef fpos_t
  312. typedef off_t fpos_t;
  313. #define fpos_t fpos_t
  314. #endif
  315. /* Seek to a certain position on STREAM. */
  316. extern int fsetpos __P((FILE *__stream, __const fpos_t *__pos));
  317. /* Return the current position of STREAM. */
  318. extern int fgetpos __P((FILE *__stream, fpos_t *__pos));
  319. /* Clear the error and EOF indicators for STREAM. */
  320. extern void clearerr __P ((FILE *__stream));
  321. /* Return the EOF indicator for STREAM. */
  322. extern int feof __P ((FILE *__stream));
  323. /* Return the error indicator for STREAM. */
  324. extern int ferror __P ((FILE *__stream));
  325. /* Macro versions of the 3 previous functions */
  326. /* If fp is NULL... */
  327. #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR), (void)0)
  328. #define feof(fp) ((fp)->mode&__MODE_EOF)
  329. #define ferror(fp) ((fp)->mode&__MODE_ERR)
  330. /* Print a message describing the meaning of the value of errno. */
  331. extern void perror __P ((__const char *__s));
  332. /* These variables normally should not be used directly. The `strerror'
  333. function provides all the needed functionality. */
  334. extern int sys_nerr;
  335. extern __const char *__const sys_errlist[];
  336. #ifdef __USE_POSIX
  337. /* Return the system file descriptor for STREAM. */
  338. extern int fileno __P ((FILE *__stream));
  339. /* Only use the macro below if you know fp is a valid FILE for a valid fd. */
  340. #define __fileno(fp) ((fp)->fd)
  341. #endif /* Use POSIX. */
  342. #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
  343. defined __USE_MISC)
  344. /* Create a new stream connected to a pipe running the given command. */
  345. extern FILE *popen __P ((__const char *__command, __const char *__mode));
  346. /* Close a stream opened by popen and return the status of its child. */
  347. extern int pclose __P ((FILE *__stream));
  348. #endif
  349. #ifdef __USE_POSIX
  350. /* Return the name of the controlling terminal. */
  351. extern char *ctermid __P ((char *__s));
  352. #endif /* Use POSIX. */
  353. #ifdef __USE_XOPEN
  354. /* Return the name of the current user. */
  355. extern char *cuserid __P ((char *__s));
  356. #endif /* Use X/Open. */
  357. #if defined __USE_POSIX || defined __USE_MISC
  358. /* These are defined in POSIX.1:1996. */
  359. /* Acquire ownership of STREAM. */
  360. extern void flockfile __P ((FILE *__stream));
  361. /* Try to acquire ownership of STREAM but do not block if it is not
  362. possible. */
  363. extern int ftrylockfile __P ((FILE *__stream));
  364. /* Relinquish the ownership granted for STREAM. */
  365. extern void funlockfile __P ((FILE *__stream));
  366. #endif /* POSIX || misc */
  367. #if defined __USE_XOPEN && !defined __USE_GNU
  368. /* The X/Open standard requires some functions and variables to be
  369. declared here which do not belong into this header. But we have to
  370. follow. In GNU mode we don't do this nonsense. */
  371. # define __need_getopt
  372. # include <getopt.h>
  373. #endif
  374. /* If we are compiling with optimizing read this file. It contains
  375. several optizing inline functions and macros. */
  376. #ifdef __USE_EXTERN_INLINES
  377. # include <bits/stdio.h>
  378. #endif
  379. __END_DECLS
  380. #endif /* !_STDIO_H */