stdio.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef __STDIO_H
  2. #define __STDIO_H
  3. #include <features.h>
  4. #include <sys/types.h>
  5. #ifndef SEEK_SET
  6. #define SEEK_SET 0
  7. #define SEEK_CUR 1
  8. #define SEEK_END 2
  9. #endif
  10. #define _IOFBF 0x00 /* full buffering */
  11. #define _IOLBF 0x01 /* line buffering */
  12. #define _IONBF 0x02 /* no buffering */
  13. #define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */
  14. #define __MODE_FREEBUF 0x04 /* Buffer allocated with malloc, can free */
  15. #define __MODE_FREEFIL 0x08 /* FILE allocated with malloc, can free */
  16. #define __MODE_READ 0x10 /* Opened in read only */
  17. #define __MODE_WRITE 0x20 /* Opened in write only */
  18. #define __MODE_RDWR 0x30 /* Opened in read/write */
  19. #define __MODE_READING 0x40 /* Buffer has pending read data */
  20. #define __MODE_WRITING 0x80 /* Buffer has pending write data */
  21. #define __MODE_EOF 0x100 /* EOF status */
  22. #define __MODE_ERR 0x200 /* Error status */
  23. #define __MODE_UNGOT 0x400 /* Buffer has been polluted by ungetc */
  24. #define __MODE_IOTRAN 0
  25. /* when you add or change fields here, be sure to change the initialization
  26. * in stdio_init and fopen */
  27. struct __stdio_file {
  28. unsigned char *bufpos; /* the next byte to write to or read from */
  29. unsigned char *bufread; /* the end of data returned by last read() */
  30. unsigned char *bufwrite; /* highest address writable by macro */
  31. unsigned char *bufstart; /* the start of the buffer */
  32. unsigned char *bufend; /* the end of the buffer; ie the byte after the last
  33. malloc()ed byte */
  34. int fd; /* the file descriptor associated with the stream */
  35. int mode;
  36. char unbuf[8]; /* The buffer for 'unbuffered' streams */
  37. struct __stdio_file * next;
  38. };
  39. #define EOF (-1)
  40. #ifndef NULL
  41. #define NULL (0)
  42. #endif
  43. typedef struct __stdio_file FILE;
  44. #define BUFSIZ (500) /*(508) should get us a fully used kmalloc bucket */
  45. extern FILE stdin[1];
  46. extern FILE stdout[1];
  47. extern FILE stderr[1];
  48. #define putc(c, stream) \
  49. (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
  50. : (unsigned char) (*(stream)->bufpos++ = (c)) )
  51. #define getc(stream) \
  52. (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
  53. (*(stream)->bufpos++))
  54. #define putchar(c) putc((c), stdout)
  55. #define getchar() getc(stdin)
  56. #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
  57. #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
  58. #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
  59. #define fileno(fp) ((fp)->fd)
  60. /* These two call malloc */
  61. #define setlinebuf(__fp) setvbuf((__fp), (char*)0, _IOLBF, 0)
  62. extern int setvbuf __P((FILE*, char*, int, size_t));
  63. /* These don't */
  64. #define setbuf(__fp, __buf) setbuffer((__fp), (__buf), BUFSIZ)
  65. extern void setbuffer __P((FILE*, char*, int));
  66. extern int fgetc __P((FILE*));
  67. extern int fputc __P((int, FILE*));
  68. extern int fclose __P((FILE*));
  69. extern int fflush __P((FILE*));
  70. extern char *fgets __P((char*, size_t, FILE*));
  71. extern FILE *__fopen __P((char*, int, FILE*, char*));
  72. #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
  73. #define freopen(__file, __mode, __fp) __fopen((__file), -1, (__fp), (__mode))
  74. #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
  75. extern int fseek __P((FILE*, long, int));
  76. extern long ftell __P((FILE*));
  77. extern void rewind __P((FILE*));
  78. extern int fputs __P((char*, FILE*));
  79. extern int puts __P((char*));
  80. extern int printf __P ((__const char*, ...));
  81. extern int fprintf __P ((FILE*, __const char*, ...));
  82. extern int sprintf __P ((char*, __const char*, ...));
  83. extern int ungetc __P ((int c, FILE * stream));
  84. #define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)
  85. #endif /* __STDIO_H */