• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _STDIO_IMPL_H
2 #define _STDIO_IMPL_H
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include "syscall.h"
8 
9 #define UNGET 8
10 
11 #define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0)
12 #define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0)
13 #define FUNLOCK(f) do { if (__need_unlock) __unlockfile((f)); } while (0)
14 
15 #define F_PERM 1
16 #define F_NORD 4
17 #define F_NOWR 8
18 #define F_EOF 16
19 #define F_ERR 32
20 #define F_SVB 64
21 #define F_APP 128
22 #define F_NOBUF 256
23 #define F_PBUF 512
24 
25 
26 #define FILE_LIST_NEXT(fl) ((fl)->next)
27 #define FILE_LIST_HEAD(head) (head)
28 #define FILE_LIST_EMPTY(head) ((head) == NULL)
29 #define	FILE_SAVELINK(name, link)	void **(name) = (void *)&(link)
30 #define	INVALID_LINK(x)	do {(x) = (void *)-1;} while (0)
31 
32 #define FILE_LIST_CHECK_NEXT(fl) do { \
33 	if (FILE_LIST_NEXT(fl) != NULL && FILE_LIST_NEXT(fl)->prev != &(fl)->next) { \
34 		abort(); \
35 	} \
36 } while (0) \
37 
38 #define FILE_LIST_CHECK_PREV(fl) do { \
39 	if (*(fl)->prev != (fl)) { \
40 		abort(); \
41 	} \
42 } while (0) \
43 
44 #define FILE_LIST_CHECK_HEAD(head) do { \
45 	if (FILE_LIST_HEAD(head) != NULL && \
46 		FILE_LIST_HEAD(head)->prev != &FILE_LIST_HEAD(head)) { \
47 		abort(); \
48 	} \
49 } while (0) \
50 
51 
52 #define FILE_LIST_INSERT_HEAD(head, fl) do { \
53 	FILE_LIST_CHECK_HEAD((head)); \
54 	if ((FILE_LIST_NEXT((fl)) = FILE_LIST_HEAD((head))) != NULL) { \
55 		FILE_LIST_HEAD((head))->prev = &FILE_LIST_NEXT((fl)); \
56 	} \
57 	FILE_LIST_HEAD(head) = (fl); \
58 	(fl)->prev = &FILE_LIST_HEAD((head)); \
59 } while (0) \
60 
61 #define FILE_LIST_REMOVE(fl) do { \
62 	FILE_SAVELINK(oldnext, (fl)->next); \
63 	FILE_SAVELINK(oldprev, (fl)->prev); \
64 	FILE_LIST_CHECK_NEXT(fl); \
65 	FILE_LIST_CHECK_PREV(fl); \
66 	if (FILE_LIST_NEXT(fl) != NULL) { \
67 		FILE_LIST_NEXT(fl)->prev = (fl)->prev; \
68 	} \
69 	*(fl)->prev = FILE_LIST_NEXT(fl); \
70 	INVALID_LINK(*oldnext); \
71 	INVALID_LINK(*oldprev); \
72 } while (0) \
73 
74 struct _IO_FILE {
75 	unsigned flags;
76 	unsigned char *rpos, *rend;
77 	int (*close)(FILE *);
78 	unsigned char *wend, *wpos;
79 	unsigned char *mustbezero_1;
80 	unsigned char *wbase;
81 	size_t (*read)(FILE *, unsigned char *, size_t);
82 	size_t (*readx)(FILE *, unsigned char *, size_t);
83 	size_t (*write)(FILE *, const unsigned char *, size_t);
84 	off_t (*seek)(FILE *, off_t, int);
85 	unsigned char *buf;
86 	size_t buf_size;
87 	/* when allocating buffer dynamically, base == buf - UNGET,
88 	 * free base when calling fclose.
89 	 * otherwise, base == NULL, cases:
90 	 * 1. in stdout, stdin, stdout, base is static array.
91 	 * 2. call setvbuf to set buffer or non-buffer.
92 	 * 3. call fmemopen, base == NULL && buf_size != 0.
93 	 */
94 	unsigned char *base;
95 	FILE **prev, *next;
96 	int fd;
97 	int pipe_pid;
98 	long lockcount;
99 	int mode;
100 	volatile int lock;
101 	int lbf;
102 	void *cookie;
103 	off_t off;
104 	char *getln_buf;
105 	void *mustbezero_2;
106 	unsigned char *shend;
107 	off_t shlim, shcnt;
108 	FILE *prev_locked, *next_locked;
109 	struct __locale_struct *locale;
110 };
111 
112 extern hidden FILE *volatile __stdin_used;
113 extern hidden FILE *volatile __stdout_used;
114 extern hidden FILE *volatile __stderr_used;
115 
116 hidden int __lockfile(FILE *);
117 hidden void __unlockfile(FILE *);
118 
119 hidden size_t __stdio_read(FILE *, unsigned char *, size_t);
120 hidden size_t __stdio_readx(FILE *, unsigned char *, size_t);
121 hidden size_t __stdio_write(FILE *, const unsigned char *, size_t);
122 hidden size_t __stdout_write(FILE *, const unsigned char *, size_t);
123 hidden off_t __stdio_seek(FILE *, off_t, int);
124 hidden int __stdio_close(FILE *);
125 
126 hidden int __fill_buffer(FILE *f);
127 hidden ssize_t __flush_buffer(FILE *f);
128 
129 hidden int __toread(FILE *);
130 hidden int __towrite(FILE *);
131 hidden int __falloc_buf(FILE *);
132 
133 hidden void __stdio_exit(void);
134 hidden void __stdio_exit_needed(void);
135 
136 #if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303)
137 __attribute__((visibility("protected")))
138 #endif
139 int __overflow(FILE *, int), __uflow(FILE *);
140 
141 hidden int __fseeko(FILE *, off_t, int);
142 hidden int __fseeko_unlocked(FILE *, off_t, int);
143 hidden off_t __ftello(FILE *);
144 hidden off_t __ftello_unlocked(FILE *);
145 hidden size_t __fwritex(const unsigned char *, size_t, FILE *);
146 hidden int __putc_unlocked(int, FILE *);
147 
148 hidden FILE *__fdopen(int, const char *);
149 hidden FILE *__fdopenx(int, int);
150 hidden int __fmodeflags(const char *, int *);
151 
152 hidden FILE *__ofl_add(FILE *f);
153 hidden FILE **__ofl_lock(void);
154 hidden void __ofl_unlock(void);
155 hidden void __ofl_free(FILE *f);
156 hidden FILE *__ofl_alloc();
157 
158 struct __pthread;
159 hidden void __register_locked_file(FILE *, struct __pthread *);
160 hidden void __unlist_locked_file(FILE *);
161 hidden void __do_orphaned_stdio_locks(void);
162 
163 #define MAYBE_WAITERS 0x40000000
164 
165 hidden void __getopt_msg(const char *, const char *, const char *, size_t);
166 
167 #define feof(f) ((f)->flags & F_EOF)
168 #define ferror(f) ((f)->flags & F_ERR)
169 
170 #define getc_unlocked(f) \
171 	( ((f)->rpos != (f)->rend) ? *(f)->rpos++ : __uflow((f)) )
172 
173 #define putc_unlocked(c, f) \
174 	( (((unsigned char)(c)!=(f)->lbf && (f)->wpos!=(f)->wend)) \
175 	  ? *(f)->wpos++ = (unsigned char)(c) \
176 	  : __overflow((f),(unsigned char)(c)) )
177 
178 /* Caller-allocated FILE * operations */
179 hidden FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
180 hidden int __fclose_ca(FILE *);
181 
__get_file_tag(FILE * fp)182 static uint64_t __get_file_tag(FILE* fp)
183 {
184 	if (fp == stdin || fp == stderr || fp == stdout) {
185 		return 0;
186 	}
187 	return fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, (uint64_t)fp);
188 }
189 
190 #endif
191