1 #pragma once
2 
3 #include "libc.h"
4 #include <stdatomic.h>
5 #include <stdio.h>
6 
7 #define UNGET 8
8 
9 #define FFINALLOCK(f) ((f)->lock >= 0 ? __lockfile((f)) : 0)
10 #define FLOCK(f) int __need_unlock = ((f)->lock >= 0 ? __lockfile((f)) : 0)
11 #define FUNLOCK(f)     \
12     if (__need_unlock) \
13     __unlockfile((f))
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 
23 struct _IO_FILE {
24     unsigned flags;
25     unsigned char *rpos, *rend;
26     int (*close)(FILE*);
27     unsigned char *wend, *wpos;
28     unsigned char* mustbezero_1;
29     unsigned char* wbase;
30     size_t (*read)(FILE*, unsigned char*, size_t);
31     size_t (*write)(FILE*, const unsigned char*, size_t);
32     off_t (*seek)(FILE*, off_t, int);
33     unsigned char* buf;
34     size_t buf_size;
35     FILE *prev, *next;
36     int fd;
37     int pipe_pid;
38     long lockcount;
39     short dummy3;
40     signed char mode;
41     signed char lbf;
42     atomic_int lock;
43     atomic_int waiters;
44     void* cookie;
45     off_t off;
46     char* getln_buf;
47     void* mustbezero_2;
48     unsigned char* shend;
49     off_t shlim, shcnt;
50     struct __locale_struct* locale;
51 };
52 
53 size_t __stdio_read(FILE*, unsigned char*, size_t) ATTR_LIBC_VISIBILITY;
54 size_t __stdio_write(FILE*, const unsigned char*, size_t) ATTR_LIBC_VISIBILITY;
55 size_t __stdout_write(FILE*, const unsigned char*, size_t) ATTR_LIBC_VISIBILITY;
56 off_t __stdio_seek(FILE*, off_t, int) ATTR_LIBC_VISIBILITY;
57 int __stdio_close(FILE*) ATTR_LIBC_VISIBILITY;
58 
59 size_t __string_read(FILE*, unsigned char*, size_t) ATTR_LIBC_VISIBILITY;
60 
61 int __toread(FILE*) ATTR_LIBC_VISIBILITY;
62 int __towrite(FILE*) ATTR_LIBC_VISIBILITY;
63 
64 zx_status_t _mmap_file(size_t offset, size_t len, uint32_t zx_flags, int flags, int fd,
65                        off_t fd_off, uintptr_t* out);
66 
67 #if defined(__PIC__) && (100 * __GNUC__ + __GNUC_MINOR__ >= 303)
68 __attribute__((visibility("protected")))
69 #endif
70 int
71 __overflow(FILE *, int),
72     __uflow(FILE *);
73 
74 int __fseeko(FILE*, off_t, int) ATTR_LIBC_VISIBILITY;
75 int __fseeko_unlocked(FILE*, off_t, int) ATTR_LIBC_VISIBILITY;
76 off_t __ftello(FILE*) ATTR_LIBC_VISIBILITY;
77 off_t __ftello_unlocked(FILE*) ATTR_LIBC_VISIBILITY;
78 size_t __fwritex(const unsigned char*, size_t, FILE*) ATTR_LIBC_VISIBILITY;
79 int __putc_unlocked(int, FILE*) ATTR_LIBC_VISIBILITY;
80 
81 FILE* __fdopen(int, const char*) ATTR_LIBC_VISIBILITY;
82 int __fmodeflags(const char*) ATTR_LIBC_VISIBILITY;
83 
84 FILE* __ofl_add(FILE* f) ATTR_LIBC_VISIBILITY;
85 FILE** __ofl_lock(void) ATTR_LIBC_VISIBILITY;
86 void __ofl_unlock(void) ATTR_LIBC_VISIBILITY;
87 
88 void __stdio_exit(void) ATTR_LIBC_VISIBILITY;
89 
90 #define feof(f) ((f)->flags & F_EOF)
91 #define ferror(f) ((f)->flags & F_ERR)
92 
93 #define getc_unlocked(f) (((f)->rpos < (f)->rend) ? *(f)->rpos++ : __uflow((f)))
94 
95 #define putc_unlocked(c, f)                                                         \
96     (((unsigned char)(c) != (f)->lbf && (f)->wpos < (f)->wend) ? *(f)->wpos++ = (c) \
97                                                                : __overflow((f), (c)))
98 
99 /* Caller-allocated FILE * operations */
100 FILE* __fopen_rb_ca(const char*, FILE*, unsigned char*, size_t)
101     ATTR_LIBC_VISIBILITY;
102 int __fclose_ca(FILE*) ATTR_LIBC_VISIBILITY;
103