a01026741bb93d2b864a081210b30a50fe784580
[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)) return 0;
11
12         /* Allocate FILE+buffer or fail */
13         if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0;
14
15         /* Zero-fill only the struct, not the buffer */
16         memset(f, 0, sizeof *f);
17
18         /* Impose mode restrictions */
19         if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
20
21         /* Set append mode on fd if opened for append */
22         if (*mode == 'a') {
23                 int flags = syscall(SYS_fcntl, fd, F_GETFL, 0);
24                 syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
25         }
26
27         f->fd = fd;
28         f->buf = (unsigned char *)f + sizeof *f + UNGET;
29         f->buf_size = BUFSIZ;
30
31         /* Activate line buffered mode for terminals */
32         f->lbf = EOF;
33         if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TCGETS, &tio))
34                 f->lbf = '\n';
35
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;
41
42         /* Add new FILE to open file list */
43         OFLLOCK();
44         f->next = ofl_head;
45         if (ofl_head) ofl_head->prev = f;
46         ofl_head = f;
47         OFLUNLOCK();
48
49         return f;
50 }
51
52 weak_alias(__fdopen, fdopen);