Source to include/stdio.h


0001  #ifndef _STDIO_H
0002  #define _STDIO_H
0003  /*
0004   * stdio.h
0005   *	My poor little stdio emulation
0006   */
0007  #include <sys/types.h>
0008  #include <stdarg.h>
0009  
0010  /*
0011   * A open, buffered file
0012   */
0013  typedef struct __file {
0014  	ushort f_flags;			/* See below */
0015  	ushort f_fd;			/* File descriptor we use */
0016  	uchar *f_buf, *f_pos;		/* Buffer */
0017  	int f_bufsz, f_cnt;		/*  ...size, bytes queued */
0018  	struct __file			/* List of all open files */
0019  		*f_next, *f_prev;
0020  } FILE;
0021  
0022  /*
0023   * Bits in f_flags
0024   */
0025  #define _F_WRITE (0x0001)	/* Can write */
0026  #define _F_READ (0x0002)	/*  ...read */
0027  #define _F_DIRTY (0x0004)	/* Buffer holds new data */
0028  #define _F_EOF (0x0008)		/* Sticky EOF */
0029  #define _F_ERR (0x0010)		/*  ...error */
0030  #define _F_UNBUF (0x0020)	/* Flush after each put */
0031  #define _F_LINE (0x0040)	/*  ...after each newline */
0032  #define _F_SETUP (0x0080)	/* Buffers, etc. set up now */
0033  #define _F_UBUF (0x0100)	/* User-provided buffer */
0034  #define _F_TTY (0x0200)		/* isatty(f_fd) == TRUE */
0035  
0036  /*
0037   * These need to be seen before getc()/putc() are defined
0038   */
0039  extern int fgetc(FILE *), fputc(int, FILE *);
0040  
0041  /*
0042   * The following could be inline functions, but GNU C has no reliable
0043   * way to express inline functions which is operable across all levels
0044   * of optimization.  So we're back to classic UNIX ?: constructs.
0045   */
0046  
0047  /*
0048   * getc()
0049   *	Get a char from a stream
0050   */
0051  #define getc(fp) (\
0052  	(((fp->f_flags & (_F_READ|_F_DIRTY)) != _F_READ) || \
0053  			(fp->f_cnt == 0)) ? fgetc(fp) : \
0054  		(fp->f_cnt -= 1, fp->f_pos += 1, fp->f_pos[-1]))
0055  
0056  /*
0057   * putc()
0058   *	Put a char to a stream
0059   */
0060  #define putc(c, fp) (\
0061  	(((fp->f_flags & (_F_UNBUF|_F_WRITE|_F_SETUP|_F_DIRTY)) != \
0062  			(_F_WRITE|_F_SETUP)) || \
0063  			(fp->f_cnt >= fp->f_bufsz) || \
0064  			((fp->f_flags & _F_LINE) && (c == '\n'))) ? \
0065  		fputc(c, fp) : \
0066  	(*(fp->f_pos) = c, fp->f_pos += 1, fp->f_cnt += 1, \
0067  	fp->f_flags |= _F_DIRTY, \
0068  	0))
0069  
0070  /*
0071   * Smoke and mirrors
0072   */
0073  #define getchar() getc(stdin)
0074  #define putchar(c) putc(c, stdout)
0075  
0076  /*
0077   * Pre-allocated stdio structs
0078   */
0079  extern FILE *__iob, *__get_iob(void);
0080  #define stdin (__iob)
0081  #define stdout (__iob+1)
0082  #define stderr (__iob+2)
0083  
0084  /*
0085   * stdio routines
0086   */
0087  extern FILE *fopen(const char *fname, const char *mode),
0088  	*freopen(const char *, const char *, FILE *),
0089  	*fdopen(int, const char *);
0090  extern int fclose(FILE *),
0091  	fread(void *buf, int size, int nelem, FILE *fp),
0092  	fwrite(const void *buf, int size, int nelem, FILE *fp),
0093  	feof(FILE *), ferror(FILE *),
0094  	fileno(FILE *), ungetc(int, FILE *);
0095  extern off_t fseek(FILE *, off_t, int), ftell(FILE *);
0096  extern char *gets(char *), *fgets(char *, int, FILE *);
0097  extern int puts(const char *), fputs(const char *, FILE *);
0098  extern int getw(FILE *), putw(int, FILE *);
0099  extern void clearerr(FILE *);
0100  extern int setvbuf(FILE *, char *, int, size_t);
0101  extern void setbuf(FILE *, char *),
0102  	setbuffer(FILE *, char *, size_t),
0103  	setlinebuf(FILE *);
0104  extern void rewind(FILE *);
0105  extern int fflush(FILE *);
0106  extern int printf(const char *, ...),
0107  	fprintf(FILE *, const char *, ...),
0108  	sprintf(char *, const char *, ...);
0109  extern int vprintf(const char *, va_list),
0110  	vfprintf(FILE *, const char *, va_list),
0111  	vsprintf(char *, const char *, va_list);
0112  extern int scanf(const char *, ...),
0113  	fscanf(FILE *, const char *, ...),
0114  	sscanf(const char *, const char *, ...);
0115  extern int vscanf(const char *, va_list),
0116  	vfscanf(FILE *, const char *, va_list),
0117  	vsscanf(const char *, const char *, va_list);
0118  extern FILE *tmpfile(void);
0119  extern FILE *popen(const char *, const char *);
0120  extern int pclose(FILE *);
0121  extern int rename(const char *, const char *);
0122  
0123  /*
0124   * Pseudo-FDL at the FILE * layer
0125   */
0126  extern FILE *funopen(void *, intfun, intfun, void *, intfun);
0127  #define fropen(cookie, readfn) funopen(cookie, readfn, 0, 0, 0)
0128  #define fwopen(cookie, writefn) funopen(cookie, 0, writefn, 0, 0)
0129  
0130  /*
0131   * Buffer strategy constants
0132   */
0133  #define _IOFBF 0		/* Fully buffered */
0134  #define _IOLBF 1		/* Line buffered */
0135  #define _IONBF 2		/* Not buffered */
0136  
0137  /*
0138   * Miscellany
0139   */
0140  #if !defined(TRUE) && !defined(FALSE)
0141  #define TRUE (1)
0142  #define FALSE (0)
0143  #endif
0144  #define EOF (-1)
0145  #if !defined(MIN) && !defined(MAX)
0146  #define MIN(x, y) (((x) < (y)) ? (x) : (y))
0147  #define MAX(x, y) (((x) > (y)) ? (x) : (y))
0148  #endif
0149  #define BUFSIZ (4096)
0150  #ifndef NULL
0151  #define NULL (0)
0152  #endif
0153  #ifndef MAX
0154  #define MAX(a, b) (((a) > (b)) ? (a) : (b))
0155  #endif
0156  #ifndef MIN
0157  #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
0158  #endif
0159  
0160  #endif /* _STDIO_H */