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