stdio.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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; /* 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. int fd; /* the file descriptor associated with the stream */
  34. int mode;
  35. char unbuf[8]; /* The buffer for 'unbuffered' streams */
  36. struct __stdio_file * next;
  37. };
  38. typedef struct __stdio_file FILE;
  39. /* Default buffer size. */
  40. #define BUFSIZ (500) /* should get us a fully used kmalloc bucket */
  41. /* Define EOF and NULL */
  42. #define EOF (-1)
  43. #ifndef NULL
  44. #define NULL (0)
  45. #endif
  46. /* The possibilities for the third argument to `setvbuf'. */
  47. #define _IOFBF 0 /* Fully buffered. */
  48. #define _IOLBF 1 /* Line buffered. */
  49. #define _IONBF 2 /* No buffering. */
  50. /* Possible states for a file stream -- internal use only */
  51. #define __MODE_IOTRAN 0
  52. #define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */
  53. #define __MODE_FREEBUF 0x04 /* Buffer allocated with malloc, can free */
  54. #define __MODE_FREEFIL 0x08 /* FILE allocated with malloc, can free */
  55. #define __MODE_READ 0x10 /* Opened in read only */
  56. #define __MODE_WRITE 0x20 /* Opened in write only */
  57. #define __MODE_RDWR 0x30 /* Opened in read/write */
  58. #define __MODE_READING 0x40 /* Buffer has pending read data */
  59. #define __MODE_WRITING 0x80 /* Buffer has pending write data */
  60. #define __MODE_EOF 0x100 /* EOF status */
  61. #define __MODE_ERR 0x200 /* Error status */
  62. #define __MODE_UNGOT 0x400 /* Buffer has been polluted by ungetc */
  63. /* The possibilities for the third argument to `fseek'.
  64. These values should not be changed. */
  65. #define SEEK_SET 0 /* Seek from beginning of file. */
  66. #define SEEK_CUR 1 /* Seek from current position. */
  67. #define SEEK_END 2 /* Seek from end of file. */
  68. #define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)
  69. /* Default path prefix for `tempnam' and `tmpnam'. */
  70. #define P_tmpdir "/tmp"
  71. /* Get the values:
  72. L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
  73. TMP_MAX The minimum number of unique filenames generated by tmpnam
  74. (and tempnam when it uses tmpnam's name space),
  75. or tempnam (the two are separate).
  76. L_ctermid How long an array to pass to `ctermid'.
  77. L_cuserid How long an array to pass to `cuserid'.
  78. FOPEN_MAX Minimum number of files that can be open at once.
  79. FILENAME_MAX Maximum length of a filename. */
  80. #define __need_FOPEN_MAX
  81. #include <bits/stdio_lim.h>
  82. #undef __need_FOPEN_MAX
  83. /* Standard streams. */
  84. extern FILE stdin[1]; /* Standard input stream. */
  85. extern FILE stdout[1]; /* Standard output stream. */
  86. extern FILE stderr[1]; /* Standard error output stream. */
  87. /* C89/C99 say they're macros. Make them happy. */
  88. #define stdin stdin
  89. #define stdout stdout
  90. #define stderr stderr
  91. /* Remove file FILENAME. */
  92. extern int remove __P ((__const char *__filename));
  93. /* Rename file OLD to NEW. */
  94. extern int rename __P ((__const char *__old, __const char *__new));
  95. /* Create a temporary file and open it read/write. */
  96. extern FILE *tmpfile __P ((void));
  97. #ifdef __USE_LARGEFILE64
  98. extern FILE *tmpfile64 __P ((void));
  99. #endif
  100. /* Generate a temporary filename. */
  101. extern char *tmpnam __P ((char *__s));
  102. #ifdef __USE_MISC
  103. /* This is the reentrant variant of `tmpnam'. The only difference is
  104. that it does not allow S to be NULL. */
  105. extern char *tmpnam_r __P ((char *__s));
  106. #endif
  107. #if defined __USE_SVID || defined __USE_XOPEN
  108. /* Generate a unique temporary filename using up to five characters of PFX
  109. if it is not NULL. The directory to put this file in is searched for
  110. as follows: First the environment variable "TMPDIR" is checked.
  111. If it contains the name of a writable directory, that directory is used.
  112. If not and if DIR is not NULL, that value is checked. If that fails,
  113. P_tmpdir is tried and finally "/tmp". The storage for the filename
  114. is allocated by `malloc'. */
  115. extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
  116. #endif
  117. /* Close STREAM. */
  118. extern int fclose __P ((FILE *__stream));
  119. /* Flush STREAM, or all streams if STREAM is NULL. */
  120. extern int fflush __P ((FILE *__stream));
  121. /* Open a file and create a new stream for it. */
  122. extern FILE *fopen __P ((__const char *__restrict __filename,
  123. __const char *__restrict __modes));
  124. /* Used internally to actuall open files */
  125. extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
  126. FILE *__restrict __stream, __const char *__restrict __modes));
  127. #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
  128. /* Open a file, replacing an existing stream with it. */
  129. extern FILE *freopen __P ((__const char *__restrict __filename,
  130. __const char *__restrict __modes,
  131. FILE *__restrict __stream));
  132. #define freopen(__file, __mode, __fp) __fopen((__file), -1, (__fp), (__mode))
  133. #ifdef __USE_LARGEFILE64
  134. extern FILE *fopen64 __P ((__const char *__restrict __filename,
  135. __const char *__restrict __modes));
  136. extern FILE *freopen64 __P ((__const char *__restrict __filename,
  137. __const char *__restrict __modes,
  138. FILE *__restrict __stream));
  139. #endif
  140. #ifdef __USE_POSIX
  141. /* Create a new stream that refers to an existing system file descriptor. */
  142. extern FILE *fdopen __P ((int __fd, __const char *__modes));
  143. #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
  144. #endif
  145. /* If BUF is NULL, make STREAM unbuffered.
  146. Else make it use buffer BUF, of size BUFSIZ. */
  147. extern void setbuf __P ((FILE *__restrict __stream, char *__restrict __buf));
  148. #define setbuf(__fp, __buf) setbuffer((__fp), (__buf), BUFSIZ)
  149. /* Make STREAM use buffering mode MODE.
  150. If BUF is not NULL, use N bytes of it for buffering;
  151. else allocate an internal buffer N bytes long. */
  152. extern int setvbuf __P ((FILE *__restrict __stream, char *__restrict __buf,
  153. int __modes, size_t __n));
  154. #ifdef __USE_BSD
  155. /* If BUF is NULL, make STREAM unbuffered.
  156. Else make it use SIZE bytes of BUF for buffering. */
  157. extern void setbuffer __P ((FILE *__restrict __stream, char *__restrict __buf,
  158. size_t __size));
  159. /* Make STREAM line-buffered. */
  160. extern void setlinebuf __P ((FILE *__stream));
  161. #define setlinebuf(__fp) setvbuf((__fp), (char*)0, _IOLBF, 0)
  162. #endif
  163. /* Write formatted output to STREAM. */
  164. extern int fprintf __P ((FILE *__restrict __stream,
  165. __const char *__restrict __format, ...));
  166. /* Write formatted output to stdout. */
  167. extern int printf __P ((__const char *__restrict __format, ...));
  168. /* Write formatted output to S. */
  169. extern int sprintf __P ((char *__restrict __s,
  170. __const char *__restrict __format, ...));
  171. /* Write formatted output to S from argument list ARG. */
  172. extern int vfprintf __P ((FILE *__restrict __s,
  173. __const char *__restrict __format,
  174. va_list __arg));
  175. /* Write formatted output to stdout from argument list ARG. */
  176. extern int vprintf __P ((__const char *__restrict __format,
  177. va_list __arg));
  178. /* Write formatted output to S from argument list ARG. */
  179. extern int vsprintf __P ((char *__restrict __s,
  180. __const char *__restrict __format,
  181. va_list __arg));
  182. /* Maximum chars of output to write in MAXLEN. */
  183. extern int snprintf __P ((char *__restrict __s, size_t __maxlen,
  184. __const char *__restrict __format, ...))
  185. __attribute__ ((__format__ (__printf__, 3, 4)));
  186. extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
  187. __const char *__restrict __format,
  188. va_list __arg))
  189. __attribute__ ((__format__ (__printf__, 3, 0)));
  190. extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
  191. __const char *__restrict __format,
  192. va_list __arg))
  193. __attribute__ ((__format__ (__printf__, 3, 0)));
  194. /* Read formatted input from STREAM. */
  195. extern int fscanf __P ((FILE *__restrict __stream,
  196. __const char *__restrict __format, ...));
  197. /* Read formatted input from stdin. */
  198. extern int scanf __P ((__const char *__restrict __format, ...));
  199. /* Read formatted input from S. */
  200. extern int sscanf __P ((__const char *__restrict __s,
  201. __const char *__restrict __format, ...));
  202. /* Read formatted input from S into argument list ARG. */
  203. extern int vfscanf __P ((FILE *__restrict __s,
  204. __const char *__restrict __format,
  205. va_list __arg))
  206. __attribute__ ((__format__ (__scanf__, 2, 0)));
  207. /* Read formatted input from stdin into argument list ARG. */
  208. extern int vscanf __P ((__const char *__restrict __format, va_list __arg))
  209. __attribute__ ((__format__ (__scanf__, 1, 0)));
  210. /* Read formatted input from S into argument list ARG. */
  211. extern int vsscanf __P ((__const char *__restrict __s,
  212. __const char *__restrict __format,
  213. va_list __arg))
  214. __attribute__ ((__format__ (__scanf__, 2, 0)));
  215. /* Read a character from STREAM. */
  216. extern int fgetc __P ((FILE *__stream));
  217. extern int getc __P ((FILE *__stream));
  218. /* Read a character from stdin. */
  219. extern int getchar __P ((void));
  220. #define getchar() getc(stdin)
  221. /* The C standard explicitly says this is a macro, so be that way */
  222. #define getc(stream) \
  223. (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
  224. (*(stream)->bufpos++))
  225. /* Write a character to STREAM. */
  226. extern int fputc __P ((int __c, FILE *__stream));
  227. extern int putc __P ((int __c, FILE *__stream));
  228. /* Write a character to stdout. */
  229. extern int putchar __P ((int __c));
  230. #define putchar(c) putc((c), stdout)
  231. /* The C standard explicitly says this can be a macro, so be that way */
  232. #define putc(c, stream) \
  233. (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
  234. : (unsigned char) (*(stream)->bufpos++ = (c)) )
  235. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  236. /* Get a word (int) from STREAM. */
  237. extern int getw __P ((FILE *__stream));
  238. /* Write a word (int) to STREAM. */
  239. extern int putw __P ((int __w, FILE *__stream));
  240. #endif
  241. /* Get a newline-terminated string of finite length from STREAM. */
  242. extern char *fgets __P ((char *__restrict __s, int __n,
  243. FILE *__restrict __stream));
  244. /* Get a newline-terminated string from stdin, removing the newline.
  245. DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
  246. extern char *gets __P ((char *__s));
  247. /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
  248. (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
  249. NULL), pointing to *N characters of space. It is realloc'd as
  250. necessary. Returns the number of characters read (not including the
  251. null terminator), or -1 on error or EOF. */
  252. extern ssize_t __getdelim __P ((char **__restrict __lineptr,
  253. size_t *__restrict __n, int __delimiter,
  254. FILE *__restrict __stream));
  255. extern ssize_t getdelim __P ((char **__restrict __lineptr,
  256. size_t *__restrict __n, int __delimiter,
  257. FILE *__restrict __stream));
  258. /* Like `getdelim', but reads up to a newline. */
  259. extern ssize_t getline __P ((char **__restrict __lineptr,
  260. size_t *__restrict __n,
  261. FILE *__restrict __stream));
  262. /* Write a string to STREAM. */
  263. extern int fputs __P ((__const char *__restrict __s,
  264. FILE *__restrict __stream));
  265. /* Write a string, followed by a newline, to stdout. */
  266. extern int puts __P ((__const char *__s));
  267. /* Push a character back onto the input buffer of STREAM. */
  268. extern int ungetc __P ((int __c, FILE *__stream));
  269. /* Read chunks of generic data from STREAM. */
  270. extern size_t fread __P ((void *__restrict __ptr, size_t __size,
  271. size_t __n, FILE *__restrict __stream));
  272. /* Write chunks of generic data to STREAM. */
  273. extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
  274. size_t __n, FILE *__restrict __s));
  275. /* Seek to a certain position on STREAM. */
  276. extern int fseek __P ((FILE *__stream, long int __off, int __whence));
  277. /* Return the current position of STREAM. */
  278. extern long int ftell __P ((FILE *__stream));
  279. /* Rewind to the beginning of STREAM. */
  280. extern void rewind __P ((FILE *__stream));
  281. /* The Single Unix Specification, Version 2, specifies an alternative,
  282. more adequate interface for the two functions above which deal with
  283. file offset. `long int' is not the right type. These definitions
  284. are originally defined in the Large File Support API. */
  285. /* Types needed in these functions. */
  286. #ifndef off_t
  287. typedef __off_t off_t;
  288. # define off_t off_t
  289. #endif
  290. #if defined __USE_LARGEFILE64 && !defined off64_t
  291. typedef __off64_t off64_t;
  292. # define off64_t off64_t
  293. #endif
  294. /* Clear the error and EOF indicators for STREAM. */
  295. extern void clearerr __P ((FILE *__stream));
  296. #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
  297. /* Return the EOF indicator for STREAM. */
  298. extern int feof __P ((FILE *__stream));
  299. #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
  300. /* Return the error indicator for STREAM. */
  301. extern int ferror __P ((FILE *__stream));
  302. #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
  303. /* Print a message describing the meaning of the value of errno. */
  304. extern void perror __P ((__const char *__s));
  305. /* These variables normally should not be used directly. The `strerror'
  306. function provides all the needed functionality. */
  307. extern int sys_nerr;
  308. extern __const char *__const sys_errlist[];
  309. #ifdef __USE_POSIX
  310. /* Return the system file descriptor for STREAM. */
  311. extern int fileno __P ((FILE *__stream));
  312. #define fileno(fp) ((fp)->fd)
  313. #endif /* Use POSIX. */
  314. #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
  315. defined __USE_MISC)
  316. /* Create a new stream connected to a pipe running the given command. */
  317. extern FILE *popen __P ((__const char *__command, __const char *__modes));
  318. /* Close a stream opened by popen and return the status of its child. */
  319. extern int pclose __P ((FILE *__stream));
  320. #endif
  321. #ifdef __USE_POSIX
  322. /* Return the name of the controlling terminal. */
  323. extern char *ctermid __P ((char *__s));
  324. #endif /* Use POSIX. */
  325. #ifdef __USE_XOPEN
  326. /* Return the name of the current user. */
  327. extern char *cuserid __P ((char *__s));
  328. #endif /* Use X/Open. */
  329. #if defined __USE_POSIX || defined __USE_MISC
  330. /* These are defined in POSIX.1:1996. */
  331. /* Acquire ownership of STREAM. */
  332. extern void flockfile __P ((FILE *__stream));
  333. /* Try to acquire ownership of STREAM but do not block if it is not
  334. possible. */
  335. extern int ftrylockfile __P ((FILE *__stream));
  336. /* Relinquish the ownership granted for STREAM. */
  337. extern void funlockfile __P ((FILE *__stream));
  338. #endif /* POSIX || misc */
  339. #if defined __USE_XOPEN && !defined __USE_GNU
  340. /* The X/Open standard requires some functions and variables to be
  341. declared here which do not belong into this header. But we have to
  342. follow. In GNU mode we don't do this nonsense. */
  343. # define __need_getopt
  344. # include <getopt.h>
  345. #endif
  346. /* If we are compiling with optimizing read this file. It contains
  347. several optizing inline functions and macros. */
  348. #ifdef __USE_EXTERN_INLINES
  349. # include <bits/stdio.h>
  350. #endif
  351. __END_DECLS
  352. #endif /* !_STDIO_H */