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