xref: /plan9-contrib/sys/include/ape/stdio.h (revision 22df390c30710ddd2119f3e7bb6c92dc399cabb9)
1 #ifndef	_STDIO_H_
2 #define	_STDIO_H_
3 #pragma lib "/$M/lib/ape/libap.a"
4 
5 /*
6  * pANS stdio.h
7  */
8 #include <stdarg.h>
9 #include <stddef.h>
10 #include <sys/types.h>
11 /*
12  * According to X3J11, there is only one i/o buffer
13  * and it must not be occupied by both input and output data.
14  *
15  * If rp<wp, we must have state==RD and
16  * if wp<rp, we must have state==WR, so that getc and putc work correctly.
17  * On open, rp, wp and buf are set to 0, so first getc or putc will call
18  * _IO_getc or _IO_putc, which will allocate the buffer.
19  * If setvbuf(., ., _IONBF, .) is called, bufl is set to 0 and
20  * buf, rp and wp are pointed at unbuf.
21  * If setvbuf(., ., _IOLBF, .) is called, _IO_putc leaves wp and rp pointed at
22  * the end of the buffer so that it can be called on each putc to check whether
23  * it's got a newline.  This nonsense is in order to avoid impacting performance
24  * of the other buffering modes more than necessary -- putting the test in putc
25  * adds many instructions that are wasted in non-_IOLBF mode:
26  * #define putc(c, f) (_IO_ctmp=(c),\
27  * 		(f)->wp>=(f)->rp || (f)->flags&LINEBUF && _IO_ctmp=='\n'?\
28  * 		_IO_putc(_IO_ctmp, f): *(f)->wp++=_IO_ctmp)
29  */
30 typedef struct{
31 	int fd;		/* UNIX file pointer */
32 	char flags;	/* bits for must free buffer on close, line-buffered */
33 	char state;	/* last operation was read, write, position, error, eof */
34 	unsigned char *buf;	/* pointer to i/o buffer */
35 	unsigned char *rp;	/* read pointer (or write end-of-buffer) */
36 	unsigned char *wp;	/* write pointer (or read end-of-buffer) */
37 	unsigned char *lp;	/* actual write pointer used when line-buffering */
38 	size_t bufl;	/* actual length of buffer */
39 	unsigned char unbuf[1];	/* tiny buffer for unbuffered io (used for ungetc?) */
40 	int	junk;
41 }FILE;
42 
43 typedef off_t fpos_t;
44 
45 #ifndef NULL
46 #ifdef __cplusplus
47 #define NULL 0
48 #else
49 #define NULL ((void*)0)
50 #endif
51 #endif
52 
53 /*
54  * Third arg of setvbuf
55  */
56 #define	_IOFBF	1			/* block-buffered */
57 #define	_IOLBF	2			/* line-buffered */
58 #define	_IONBF	3			/* unbuffered */
59 
60 #define	BUFSIZ	8192			/* size of setbuf buffer */
61 #define	EOF	(-1)			/* returned on end of file */
62 #define	FOPEN_MAX	128		/* max files open */
63 #define	FILENAME_MAX	BUFSIZ		/* silly filename length */
64 #define	L_tmpnam	20		/* sizeof "/tmp/abcdefghij9999 */
65 #define	L_cuserid	32		/* maximum size user name */
66 #define	L_ctermid	32		/* size of name of controlling tty */
67 
68 #ifndef SEEK_SET			/* also defined in unistd.h */
69 #define	SEEK_CUR	1
70 #define	SEEK_END	2
71 #define	SEEK_SET	0
72 #endif
73 #define	TMP_MAX		64		/* very hard to set correctly */
74 
75 #define	stderr	(&_IO_stream[2])
76 #define	stdin	(&_IO_stream[0])
77 #define	stdout	(&_IO_stream[1])
78 
79 extern FILE _IO_stream[FOPEN_MAX];
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 
85 int	_IO_getc(FILE *f);
86 int	_IO_putc(int, FILE *);
87 void	clearerr(FILE *);
88 int	fclose(FILE *);
89 FILE	*fdopen(const int, const char *);
90 int	feof(FILE *);
91 int	ferror(FILE *);
92 int	fflush(FILE *);
93 int	fgetc(FILE *);
94 int	fgetpos(FILE *, fpos_t *);
95 char	*fgets(char *, int, FILE *);
96 int	fileno(FILE *);
97 FILE	*fopen(const char *, const char *);
98 int	fprintf(FILE *, const char *, ...);
99 int	fputc(int, FILE *);
100 int	fputs(const char *, FILE *);
101 size_t	fread(void *, size_t, size_t, FILE *);
102 FILE	*freopen(const char *, const char *, FILE *);
103 int	fscanf(FILE *, const char *, ...);
104 int	fseek(FILE *, long, int);
105 int	fseeko(FILE *, off_t, int);
106 int	fsetpos(FILE *, const fpos_t *);
107 long	ftell(FILE *);
108 off_t	ftello(FILE *);
109 size_t	fwrite(const void *, size_t, size_t, FILE *);
110 int	getc(FILE *);
111 #define	getc(f)	((f)->rp>=(f)->wp? _IO_getc(f): *(f)->rp++)
112 int	getchar(void);
113 #define	getchar()	getc(stdin)
114 char	*gets(char *);
115 void	perror(const char *);
116 int	printf(const char *, ...);
117 int	putc(int, FILE *);
118 /* assignment to f->junk eliminates warnings about unused result of operation */
119 #define	putc(c, f) ((f)->junk = ((f)->wp>=(f)->rp? \
120 	_IO_putc(c, f): (*(f)->wp++ = (c))))
121 int	putchar(int);
122 #define	putchar(c)	putc(c, stdout)
123 int	puts(const char *);
124 int	remove(const char *);
125 int	rename(const char *, const char *);
126 void	rewind(FILE *);
127 int	scanf(const char *, ...);
128 void	setbuf(FILE *, char *);
129 int	setvbuf(FILE *, char *, int, size_t);
130 /*
131  * NB: C99 now requires *snprintf to return the number of characters
132  * that would have been written, had there been room.
133  */
134 int	snprintf(char *, size_t, const char *, ...);
135 int	sprintf(char *, const char *, ...);
136 int	sscanf(const char *, const char *, ...);
137 FILE	*tmpfile(void);
138 char	*tmpnam(char *);
139 int	ungetc(int, FILE *);
140 int	vfprintf(FILE *, const char *, va_list);
141 int	vfscanf(FILE *, const char *, va_list);
142 int	vprintf(const char *, va_list);
143 int	vsnprintf(char *, size_t, const char *, va_list);
144 int	vsprintf(char *, const char *, va_list);
145 
146 #ifdef _POSIX_SOURCE
147 int	fileno(FILE *);
148 FILE*	fdopen(int, const char*);
149 char	*ctermid(char *);
150 #endif
151 
152 #ifdef _REENTRANT_SOURCE
153 char	*tmpnam_r(char *);
154 char	*ctermid_r(char *);
155 #endif
156 
157 #ifdef _BSD_EXTENSION
158 #pragma lib "/$M/lib/ape/libbsd.a"
159 FILE	*popen(char *, char *);
160 int	pclose(FILE	*);
161 #endif
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #endif
168