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