1 #ifndef _STDIO_IMPL_H
2 #define _STDIO_IMPL_H
3 
4 #include <assert.h>
5 #include <stdio.h>
6 
7 #define UNGET 8
8 
9 #if defined(TRUSTY_USERSPACE)
10 #define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0)
11 #define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0)
12 #define FUNLOCK(f) do { if (__need_unlock) __unlockfile((f)); } while (0)
13 #else
14 /* Trusty only supports the std streams which all have `lock = -1`, so these
15  * macros are unnecessary. Defining them as no-ops lets us avoid compiling
16  * __lockfile.c which makes use of __pthread_self which is not defined in the
17  * kernel. */
18 #define FFINALLOCK(f) do { DEBUG_ASSERT((f)->lock == -1); } while (0)
19 #define FLOCK(f) do { DEBUG_ASSERT((f)->lock == -1); } while (0)
20 #define FUNLOCK(f) do { DEBUG_ASSERT((f)->lock == -1); } while (0)
21 #endif
22 
23 #define F_PERM 1
24 #define F_NORD 4
25 #define F_NOWR 8
26 #define F_EOF 16
27 #define F_ERR 32
28 #define F_SVB 64
29 #define F_APP 128
30 
31 struct _IO_FILE {
32 	unsigned flags;
33 	unsigned char *rpos, *rend;
34 	int (*close)(FILE *);
35 	unsigned char *wend, *wpos;
36 	unsigned char *mustbezero_1;
37 	unsigned char *wbase;
38 	size_t (*read)(FILE *, unsigned char *, size_t);
39 	size_t (*write)(FILE *, const unsigned char *, size_t);
40 	off_t (*seek)(FILE *, off_t, int);
41 	unsigned char *buf;
42 	size_t buf_size;
43 	FILE *prev, *next;
44 	int fd;
45 	int pipe_pid;
46 	long lockcount;
47 	int mode;
48 	volatile int lock;
49 	int lbf;
50 	void *cookie;
51 	off_t off;
52 	char *getln_buf;
53 	void *mustbezero_2;
54 	unsigned char *shend;
55 	off_t shlim, shcnt;
56 	FILE *prev_locked, *next_locked;
57 	struct __locale_struct *locale;
58 };
59 
60 extern hidden FILE *volatile __stdin_used;
61 extern hidden FILE *volatile __stdout_used;
62 extern hidden FILE *volatile __stderr_used;
63 
64 hidden int __lockfile(FILE *);
65 hidden void __unlockfile(FILE *);
66 
67 hidden size_t __stdio_read(FILE *, unsigned char *, size_t);
68 hidden size_t __stdio_write(FILE *, const unsigned char *, size_t);
69 hidden size_t __stdout_write(FILE *, const unsigned char *, size_t);
70 hidden off_t __stdio_seek(FILE *, off_t, int);
71 hidden int __stdio_close(FILE *);
72 
73 hidden size_t __string_read(FILE *, unsigned char *, size_t);
74 
75 hidden int __toread(FILE *);
76 hidden int __towrite(FILE *);
77 
78 hidden void __stdio_exit(void);
79 hidden void __stdio_exit_needed(void);
80 
81 #if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303)
82 __attribute__((visibility("protected")))
83 #endif
84 int __overflow(FILE *, int), __uflow(FILE *);
85 
86 hidden int __fseeko(FILE *, off_t, int);
87 hidden int __fseeko_unlocked(FILE *, off_t, int);
88 hidden off_t __ftello(FILE *);
89 hidden off_t __ftello_unlocked(FILE *);
90 hidden size_t __fwritex(const unsigned char *, size_t, FILE *);
91 hidden int __putc_unlocked(int, FILE *);
92 
93 hidden FILE *__fdopen(int, const char *);
94 hidden int __fmodeflags(const char *);
95 
96 hidden FILE *__ofl_add(FILE *f);
97 hidden FILE **__ofl_lock(void);
98 hidden void __ofl_unlock(void);
99 
100 struct __pthread;
101 hidden void __register_locked_file(FILE *, struct __pthread *);
102 hidden void __unlist_locked_file(FILE *);
103 hidden void __do_orphaned_stdio_locks(void);
104 
105 #define MAYBE_WAITERS 0x40000000
106 
107 hidden void __getopt_msg(const char *, const char *, const char *, size_t);
108 
109 #define feof(f) ((f)->flags & F_EOF)
110 #define ferror(f) ((f)->flags & F_ERR)
111 
112 #define getc_unlocked(f) \
113 	( ((f)->rpos != (f)->rend) ? *(f)->rpos++ : __uflow((f)) )
114 
115 #define putc_unlocked(c, f) \
116 	( (((unsigned char)(c)!=(f)->lbf && (f)->wpos!=(f)->wend)) \
117 	? *(f)->wpos++ = (unsigned char)(c) \
118 	: __overflow((f),(unsigned char)(c)) )
119 
120 /* Caller-allocated FILE * operations */
121 hidden FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
122 hidden int __fclose_ca(FILE *);
123 
124 #endif
125