stdio.h 16 KB

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