Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #include "stdio_impl.h" |
Rich Felker | 835f9f9 | 2012-11-08 16:39:41 -0500 | [diff] [blame] | 2 | #include <stdlib.h> |
Rich Felker | 835f9f9 | 2012-11-08 16:39:41 -0500 | [diff] [blame] | 3 | #include <sys/ioctl.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 7 | |
| 8 | FILE *__fdopen(int fd, const char *mode) |
| 9 | { |
| 10 | FILE *f; |
Rich Felker | 2de85a9 | 2015-02-23 18:53:01 -0500 | [diff] [blame] | 11 | struct winsize wsz; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 12 | |
| 13 | /* Check for valid initial mode character */ |
Rich Felker | 3b43d10 | 2012-06-17 20:34:04 -0400 | [diff] [blame] | 14 | if (!strchr("rwa", *mode)) { |
| 15 | errno = EINVAL; |
| 16 | return 0; |
| 17 | } |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 18 | |
| 19 | /* Allocate FILE+buffer or fail */ |
| 20 | if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0; |
| 21 | |
| 22 | /* Zero-fill only the struct, not the buffer */ |
| 23 | memset(f, 0, sizeof *f); |
| 24 | |
| 25 | /* Impose mode restrictions */ |
Rich Felker | 8582a6e | 2012-09-29 18:09:34 -0400 | [diff] [blame] | 26 | if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD; |
| 27 | |
| 28 | /* Apply close-on-exec flag */ |
| 29 | if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 30 | |
| 31 | /* Set append mode on fd if opened for append */ |
| 32 | if (*mode == 'a') { |
Rich Felker | eb0e8fa | 2011-04-17 16:32:15 -0400 | [diff] [blame] | 33 | int flags = __syscall(SYS_fcntl, fd, F_GETFL); |
Rich Felker | 758ab35 | 2014-02-07 01:16:53 -0500 | [diff] [blame] | 34 | if (!(flags & O_APPEND)) |
| 35 | __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND); |
Rich Felker | 3af2ede | 2014-02-07 00:57:50 -0500 | [diff] [blame] | 36 | f->flags |= F_APP; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | f->fd = fd; |
| 40 | f->buf = (unsigned char *)f + sizeof *f + UNGET; |
| 41 | f->buf_size = BUFSIZ; |
| 42 | |
| 43 | /* Activate line buffered mode for terminals */ |
| 44 | f->lbf = EOF; |
Rich Felker | 2de85a9 | 2015-02-23 18:53:01 -0500 | [diff] [blame] | 45 | if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 46 | f->lbf = '\n'; |
| 47 | |
| 48 | /* Initialize op ptrs. No problem if some are unneeded. */ |
| 49 | f->read = __stdio_read; |
| 50 | f->write = __stdio_write; |
| 51 | f->seek = __stdio_seek; |
| 52 | f->close = __stdio_close; |
| 53 | |
Rich Felker | dba68bf | 2011-07-30 08:02:14 -0400 | [diff] [blame] | 54 | if (!libc.threaded) f->lock = -1; |
| 55 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 56 | /* Add new FILE to open file list */ |
Rich Felker | 1b0cdc8 | 2015-06-16 07:11:19 +0000 | [diff] [blame] | 57 | return __ofl_add(f); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | weak_alias(__fdopen, fdopen); |