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)) {
15 /* Allocate FILE+buffer or fail */
16 if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0;
18 /* Zero-fill only the struct, not the buffer */
19 memset(f, 0, sizeof *f);
21 /* Impose mode restrictions */
22 if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
24 /* Set append mode on fd if opened for append */
26 int flags = __syscall(SYS_fcntl, fd, F_GETFL);
27 __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
31 f->buf = (unsigned char *)f + sizeof *f + UNGET;
34 /* Activate line buffered mode for terminals */
36 if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TCGETS, &tio))
39 /* Initialize op ptrs. No problem if some are unneeded. */
40 f->read = __stdio_read;
41 f->write = __stdio_write;
42 f->seek = __stdio_seek;
43 f->close = __stdio_close;
45 if (!libc.threaded) f->lock = -1;
47 /* Add new FILE to open file list */
49 f->next = libc.ofl_head;
50 if (libc.ofl_head) libc.ofl_head->prev = f;
57 weak_alias(__fdopen, fdopen);