xref: /plan9/sys/src/libstdio/Stdio.h (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 /*
2  * pANS stdio.h
3  */
4 /*
5  * According to X3J11, there is only one i/o buffer
6  * and it must not be occupied by both input and output data.
7  *	If rp<wp, we must have state==RD and
8  *	if wp<rp, we must have state==WR, so that getc and putc work correctly.
9  *	On open, rp, wp and buf are set to 0, so first getc or putc will call _IO_getc
10  *	or _IO_putc, which will allocate the buffer.
11  *	If setvbuf(., ., _IONBF, .) is called, bufl is set to 0 and
12  *	buf, rp and wp are pointed at unbuf.
13  *	If setvbuf(., ., _IOLBF, .) is called, _IO_putc leaves wp and rp pointed at the
14  *	end of the buffer so that it can be called on each putc to check whether it's got
15  *	a newline.  This nonsense is in order to avoid impacting performance of the other
16  *	buffering modes more than necessary -- putting the test in putc adds many
17  *	instructions that are wasted in non-_IOLBF mode:
18  *	#define putc(c, f)	(_IO_ctmp=(c),\
19  *				(f)->wp>=(f)->rp || (f)->flags&LINEBUF && _IO_ctmp=='\n'\
20  *					?_IO_putc(_IO_ctmp, f)\
21  *					:*(f)->wp++=_IO_ctmp)
22  *
23  */
24 typedef struct{
25 	int fd;		/* UNIX file pointer */
26 	char flags;	/* bits for must free buffer on close, line-buffered */
27 	char state;	/* last operation was read, write, position, error, eof */
28 	char *buf;	/* pointer to i/o buffer */
29 	char *rp;	/* read pointer (or write end-of-buffer) */
30 	char *wp;	/* write pointer (or read end-of-buffer) */
31 	char *lp;	/* actual write pointer used when line-buffering */
32 	long bufl;	/* actual length of buffer */
33 	char unbuf[1];	/* tiny buffer for unbuffered io (used for ungetc?) */
34 }FILE;
35 typedef long fpos_t;
36 #ifndef NULL
37 #define	NULL	0
38 #endif
39 /*
40  * Third arg of setvbuf
41  */
42 #define	_IOFBF	1			/* block-buffered */
43 #define	_IOLBF	2			/* line-buffered */
44 #define	_IONBF	3			/* unbuffered */
45 #define	BUFSIZ	4096			/* size of setbuf buffer */
46 #define	EOF	(-1)			/* returned on end of file */
47 #define	FOPEN_MAX	128		/* max files open */
48 #define	FILENAME_MAX	BUFSIZ		/* silly filename length */
49 #define	L_tmpnam	20		/* sizeof "/tmp/abcdefghij9999 */
50 #define	SEEK_CUR	1
51 #define	SEEK_END	2
52 #define	SEEK_SET	0
53 #define	TMP_MAX		64		/* very hard to set correctly */
54 #define	stderr	(&_IO_stream[2])
55 #define	stdin	(&_IO_stream[0])
56 #define	stdout	(&_IO_stream[1])
57 #define	_IO_CHMASK	0377		/* mask for 8 bit characters */
58 FILE *tmpfile(void);
59 char *tmpnam(char *);
60 int fclose(FILE *);
61 int fflush(FILE *);
62 FILE *fopen(const char *, const char *);
63 FILE *freopen(const char *, const char *, FILE *);
64 void setbuf(FILE *, char *);
65 int setvbuf(FILE *, char *, int, long);
66 int fprintf(FILE *, const char *, ...);
67 int fscanf(FILE *, const char *, ...);
68 int printf(const char *, ...);
69 int scanf(const char *, ...);
70 int sprintf(char *, const char *, ...);
71 int sscanf(const char *, const char *, ...);
72 int vfprintf(FILE *, const char *, va_list);
73 int vprintf(const char *, va_list);
74 int vsprintf(char *, const char *, va_list);
75 int vfscanf(FILE *, const char *, va_list);
76 int fgetc(FILE *);
77 char *fgets(char *, int, FILE *);
78 int fputc(int, FILE *);
79 int fputs(const char *, FILE *);
80 int getc(FILE *);
81 #define	getc(f)	((f)->rp>=(f)->wp?_IO_getc(f):*(f)->rp++&_IO_CHMASK)
82 int _IO_getc(FILE *f);
83 int getchar(void);
84 #define	getchar()	getc(stdin)
85 char *gets(char *);
86 int putc(int, FILE *);
87 #define	putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=c)&_IO_CHMASK)
88 int _IO_putc(int, FILE *);
89 int putchar(int);
90 #define	putchar(c)	putc(c, stdout)
91 int puts(const char *);
92 int ungetc(int, FILE *);
93 long fread(void *, long, long, FILE *);
94 long fwrite(const void *, long, long, FILE *);
95 int fgetpos(FILE *, fpos_t *);
96 int fseek(FILE *, long int, int);
97 int fsetpos(FILE *, const fpos_t *);
98 long int ftell(FILE *);
99 void rewind(FILE *);
100 void clearerr(FILE *);
101 int feof(FILE *);
102 int ferror(FILE *);
103 void perror(const char *);
104 extern FILE _IO_stream[FOPEN_MAX];
105 FILE *sopenr(const char *);
106 FILE *sopenw(void);
107 char *sclose(FILE *);
108 char	*dtoa(double, int, int, int*, int*, char**);
109 void	freedtoa(char*);
110