8bd51c66e7efbfa61b2d8875061eced89c46f4ae
[musl] / src / stdio / __fdopen.c
1 #include "stdio_impl.h"
2
3 FILE *__fdopen(int fd, const char *mode)
4 {
5         FILE *f;
6         struct termios tio;
7         int plus = !!strchr(mode, '+');
8
9         /* Check for valid initial mode character */
10         if (!strchr("rwa", *mode)) {
11                 errno = EINVAL;
12                 return 0;
13         }
14
15         /* Allocate FILE+buffer or fail */
16         if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0;
17
18         /* Zero-fill only the struct, not the buffer */
19         memset(f, 0, sizeof *f);
20
21         /* Impose mode restrictions */
22         if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
23
24         /* Set append mode on fd if opened for append */
25         if (*mode == 'a') {
26                 int flags = __syscall(SYS_fcntl, fd, F_GETFL);
27                 __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
28         }
29
30         f->fd = fd;
31         f->buf = (unsigned char *)f + sizeof *f + UNGET;
32         f->buf_size = BUFSIZ;
33
34         /* Activate line buffered mode for terminals */
35         f->lbf = EOF;
36         if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TCGETS, &tio))
37                 f->lbf = '\n';
38
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;
44
45         if (!libc.threaded) f->lock = -1;
46
47         /* Add new FILE to open file list */
48         OFLLOCK();
49         f->next = libc.ofl_head;
50         if (libc.ofl_head) libc.ofl_head->prev = f;
51         libc.ofl_head = f;
52         OFLUNLOCK();
53
54         return f;
55 }
56
57 weak_alias(__fdopen, fdopen);