1 #include "stdio_impl.h"
3 FILE *__fdopen(int fd, const char *mode)
7 int plus = !!strchr(mode, '+');
9 /* Check for valid initial mode character */
10 if (!strchr("rwa", *mode)) return 0;
12 /* Allocate FILE+buffer or fail */
13 if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0;
15 /* Zero-fill only the struct, not the buffer */
16 memset(f, 0, sizeof *f);
18 /* Impose mode restrictions */
19 if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
21 /* Set append mode on fd if opened for append */
23 int flags = __syscall_fcntl(fd, F_GETFL, 0);
24 __syscall_fcntl(fd, F_SETFL, flags | O_APPEND);
28 f->buf = (unsigned char *)f + sizeof *f + UNGET;
31 /* Activate line buffered mode for terminals */
33 if (!(f->flags & F_NOWR) && !__syscall_ioctl(fd, TCGETS, &tio))
36 /* Initialize op ptrs. No problem if some are unneeded. */
37 f->read = __stdio_read;
38 f->write = __stdio_write;
39 f->seek = __stdio_seek;
40 f->close = __stdio_close;
42 /* Add new FILE to open file list */
45 if (ofl_head) ofl_head->prev = f;
52 weak_alias(__fdopen, fdopen);