]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/stdio/__fdopen.c
8445f39608169b34ec36257f90b2037591431d8d
1 #include "stdio_impl.h"
8 #ifdef __wasilibc_unmodified_upstream
10 #include <__function___isatty.h>
13 FILE *__fdopen(int fd
, const char *mode
)
16 #ifdef __wasilibc_unmodified_upstream
20 /* Check for valid initial mode character */
21 if (!strchr("rwa", *mode
)) {
26 /* Allocate FILE+buffer or fail */
27 if (!(f
=malloc(sizeof *f
+ UNGET
+ BUFSIZ
))) return 0;
29 /* Zero-fill only the struct, not the buffer */
30 memset(f
, 0, sizeof *f
);
32 /* Impose mode restrictions */
33 if (!strchr(mode
, '+')) f
->flags
= (*mode
== 'r') ? F_NOWR
: F_NORD
;
35 /* Apply close-on-exec flag */
36 #ifdef __wasilibc_unmodified_upstream
37 if (strchr(mode
, 'e')) __syscall(SYS_fcntl
, fd
, F_SETFD
, FD_CLOEXEC
);
39 if (strchr(mode
, 'e')) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
42 /* Set append mode on fd if opened for append */
44 #ifdef __wasilibc_unmodified_upstream
45 int flags
= __syscall(SYS_fcntl
, fd
, F_GETFL
);
47 int flags
= fcntl(fd
, F_GETFL
);
49 if (!(flags
& O_APPEND
))
50 #ifdef __wasilibc_unmodified_upstream
51 __syscall(SYS_fcntl
, fd
, F_SETFL
, flags
| O_APPEND
);
53 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
59 f
->buf
= (unsigned char *)f
+ sizeof *f
+ UNGET
;
62 /* Activate line buffered mode for terminals */
64 #ifdef __wasilibc_unmodified_upstream
65 if (!(f
->flags
& F_NOWR
) && !__syscall(SYS_ioctl
, fd
, TIOCGWINSZ
, &wsz
))
67 if (!(f
->flags
& F_NOWR
) && __isatty(fd
))
71 /* Initialize op ptrs. No problem if some are unneeded. */
72 f
->read
= __stdio_read
;
73 f
->write
= __stdio_write
;
74 f
->seek
= __stdio_seek
;
75 f
->close
= __stdio_close
;
77 if (!libc
.threaded
) f
->lock
= -1;
79 /* Add new FILE to open file list */
83 weak_alias(__fdopen
, fdopen
);