stdio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef __STDIO_H
  2. #define __STDIO_H
  3. #include <features.h>
  4. #include <stdarg.h>
  5. #include <sys/types.h>
  6. #ifndef SEEK_SET
  7. #define SEEK_SET 0
  8. #define SEEK_CUR 1
  9. #define SEEK_END 2
  10. #endif
  11. #define _IOFBF 0x00 /* full buffering */
  12. #define _IOLBF 0x01 /* line buffering */
  13. #define _IONBF 0x02 /* no buffering */
  14. #define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */
  15. #define __MODE_FREEBUF 0x04 /* Buffer allocated with malloc, can free */
  16. #define __MODE_FREEFIL 0x08 /* FILE allocated with malloc, can free */
  17. #define __MODE_READ 0x10 /* Opened in read only */
  18. #define __MODE_WRITE 0x20 /* Opened in write only */
  19. #define __MODE_RDWR 0x30 /* Opened in read/write */
  20. #define __MODE_READING 0x40 /* Buffer has pending read data */
  21. #define __MODE_WRITING 0x80 /* Buffer has pending write data */
  22. #define __MODE_EOF 0x100 /* EOF status */
  23. #define __MODE_ERR 0x200 /* Error status */
  24. #define __MODE_UNGOT 0x400 /* Buffer has been polluted by ungetc */
  25. #define __MODE_IOTRAN 0
  26. /* when you add or change fields here, be sure to change the initialization
  27. * in stdio_init and fopen */
  28. struct __stdio_file {
  29. unsigned char *bufpos; /* the next byte to write to or read from */
  30. unsigned char *bufread; /* the end of data returned by last read() */
  31. unsigned char *bufwrite; /* highest address writable by macro */
  32. unsigned char *bufstart; /* the start of the buffer */
  33. unsigned char *bufend; /* the end of the buffer; ie the byte after the last
  34. malloc()ed byte */
  35. int fd; /* the file descriptor associated with the stream */
  36. int mode;
  37. char unbuf[8]; /* The buffer for 'unbuffered' streams */
  38. struct __stdio_file * next;
  39. };
  40. #define EOF (-1)
  41. #ifndef NULL
  42. #define NULL (0)
  43. #endif
  44. typedef struct __stdio_file FILE;
  45. #define BUFSIZ (500) /*(508) should get us a fully used kmalloc bucket */
  46. extern FILE stdin[1];
  47. extern FILE stdout[1];
  48. extern FILE stderr[1];
  49. #define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)
  50. /* Read chunks of generic data from STREAM. */
  51. extern size_t fread __P ((void *__restrict __ptr, size_t __size,
  52. size_t __n, FILE *__restrict __stream));
  53. /* Write chunks of generic data to STREAM. */
  54. extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
  55. size_t __n, FILE *__restrict __s));
  56. #define putc(c, stream) \
  57. (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
  58. : (unsigned char) (*(stream)->bufpos++ = (c)) )
  59. #define getc(stream) \
  60. (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
  61. (*(stream)->bufpos++))
  62. #define putchar(c) putc((c), stdout)
  63. #define getchar() getc(stdin)
  64. #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
  65. #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
  66. #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
  67. #define fileno(fp) ((fp)->fd)
  68. /* These two call malloc */
  69. #define setlinebuf(__fp) setvbuf((__fp), (char*)0, _IOLBF, 0)
  70. extern int setvbuf __P((FILE*, char*, int, size_t));
  71. /* These don't */
  72. #define setbuf(__fp, __buf) setbuffer((__fp), (__buf), BUFSIZ)
  73. extern void setbuffer __P((FILE*, char*, int));
  74. /* Read a character from STREAM. */
  75. extern int fgetc __P ((FILE *__stream));
  76. extern int getc __P ((FILE *__stream));
  77. /* Push a character back onto the input buffer of STREAM. */
  78. extern int ungetc __P ((int __c, FILE *__stream));
  79. /* Read a character from stdin. */
  80. extern int getchar __P ((void));
  81. /* Write a character to STREAM. */
  82. extern int fputc __P ((int __c, FILE *__stream));
  83. extern int putc __P ((int __c, FILE *__stream));
  84. /* Write a character to stdout. */
  85. extern int putchar __P ((int __c));
  86. /* Close STREAM. */
  87. extern int fclose __P ((FILE *__stream));
  88. /* Flush STREAM, or all streams if STREAM is NULL. */
  89. extern int fflush __P ((FILE *__stream));
  90. /* Get a newline-terminated string from stdin, removing the newline.
  91. DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
  92. extern char *gets __P ((char *__s));
  93. /* Get a newline-terminated string of finite length from STREAM. */
  94. extern char *fgets __P ((char *__restrict __s, int __n,
  95. FILE *__restrict __stream));
  96. extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
  97. FILE *__restrict __stream, __const char *__restrict __modes));
  98. /* Open a file and create a new stream for it. */
  99. #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
  100. /* Open a file, replacing an existing stream with it. */
  101. #define freopen(__file, __mode, __fp) __fopen((__file), -1, (__fp), (__mode))
  102. /* Create a new stream that refers to an existing system file descriptor. */
  103. #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
  104. /* Seek to a certain position on STREAM. */
  105. extern int fseek __P ((FILE *__stream, long int __off, int __whence));
  106. /* Return the current position of STREAM. */
  107. extern long int ftell __P ((FILE *__stream));
  108. /* Rewind to the beginning of STREAM. */
  109. extern void rewind __P ((FILE *__stream));
  110. /* Write a string, followed by a newline, to stdout. */
  111. extern int puts __P ((__const char *__s));
  112. /* Write a string to STREAM. */
  113. extern int fputs __P ((__const char *__restrict __s,
  114. FILE *__restrict __stream));
  115. /* Write formatted output to stdout. */
  116. extern int printf __P ((__const char *__restrict __format, ...));
  117. /* Write formatted output to STREAM. */
  118. extern int fprintf __P ((FILE *__restrict __stream,
  119. __const char *__restrict __format, ...));
  120. /* Write formatted output to S. */
  121. extern int sprintf __P ((char *__restrict __s,
  122. __const char *__restrict __format, ...));
  123. /* Write formatted output to stdout from argument list ARG. */
  124. extern int vprintf __P ((__const char *__restrict __format,
  125. va_list __arg));
  126. /* Write formatted output to S from argument list ARG. */
  127. extern int vfprintf __P ((FILE *__restrict __s,
  128. __const char *__restrict __format,
  129. va_list __arg));
  130. /* Write formatted output to S from argument list ARG. */
  131. extern int vsprintf __P ((char *__restrict __s,
  132. __const char *__restrict __format,
  133. va_list __arg));
  134. /* Read formatted input from stdin. */
  135. extern int scanf __P ((__const char *__restrict __format, ...));
  136. /* Read formatted input from S. */
  137. extern int sscanf __P ((__const char *__restrict __s,
  138. __const char *__restrict __format, ...));
  139. /* Read formatted input from STREAM. */
  140. extern int fscanf __P ((FILE *__restrict __stream,
  141. __const char *__restrict __format, ...));
  142. /* Read formatted input from stdin into argument list ARG. */
  143. extern int vscanf __P ((__const char *__restrict __format, va_list __arg))
  144. __attribute__ ((__format__ (__scanf__, 1, 0)));
  145. /* Read formatted input from S into argument list ARG. */
  146. extern int vsscanf __P ((__const char *__restrict __s,
  147. __const char *__restrict __format,
  148. va_list __arg))
  149. __attribute__ ((__format__ (__scanf__, 2, 0)));
  150. /* Read formatted input from S into argument list ARG. */
  151. extern int vfscanf __P ((FILE *__restrict __s,
  152. __const char *__restrict __format,
  153. va_list __arg))
  154. __attribute__ ((__format__ (__scanf__, 2, 0)));
  155. /* Print a message describing the meaning of the value of errno. */
  156. extern void perror __P ((__const char *__s));
  157. #endif /* __STDIO_H */