OLD | NEW |
1 #include "stdio_impl.h" | 1 #include "stdio_impl.h" |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <sys/ioctl.h> | |
4 #include <fcntl.h> | 3 #include <fcntl.h> |
5 #include <errno.h> | 4 #include <errno.h> |
6 #include <string.h> | 5 #include <string.h> |
| 6 #include <unistd.h> |
7 | 7 |
8 FILE* __fdopen(int fd, const char* mode) { | 8 FILE* __fdopen(int fd, const char* mode) { |
9 FILE* f; | 9 FILE* f; |
10 struct winsize wsz; | |
11 | 10 |
12 /* Check for valid initial mode character */ | 11 /* Check for valid initial mode character */ |
13 if (!strchr("rwa", *mode)) { | 12 if (!strchr("rwa", *mode)) { |
14 errno = EINVAL; | 13 errno = EINVAL; |
15 return 0; | 14 return 0; |
16 } | 15 } |
17 | 16 |
18 /* Allocate FILE+buffer or fail */ | 17 /* Allocate FILE+buffer or fail */ |
19 if (!(f = malloc(sizeof *f + UNGET + BUFSIZ))) | 18 if (!(f = malloc(sizeof *f + UNGET + BUFSIZ))) |
20 return 0; | 19 return 0; |
(...skipping 16 matching lines...) Expand all Loading... |
37 __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND); | 36 __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND); |
38 f->flags |= F_APP; | 37 f->flags |= F_APP; |
39 } | 38 } |
40 | 39 |
41 f->fd = fd; | 40 f->fd = fd; |
42 f->buf = (unsigned char*)f + sizeof *f + UNGET; | 41 f->buf = (unsigned char*)f + sizeof *f + UNGET; |
43 f->buf_size = BUFSIZ; | 42 f->buf_size = BUFSIZ; |
44 | 43 |
45 /* Activate line buffered mode for terminals */ | 44 /* Activate line buffered mode for terminals */ |
46 f->lbf = EOF; | 45 f->lbf = EOF; |
47 if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) | 46 if (!(f->flags & F_NOWR) && isatty(fd)) |
48 f->lbf = '\n'; | 47 f->lbf = '\n'; |
49 | 48 |
50 /* Initialize op ptrs. No problem if some are unneeded. */ | 49 /* Initialize op ptrs. No problem if some are unneeded. */ |
51 f->read = __stdio_read; | 50 f->read = __stdio_read; |
52 f->write = __stdio_write; | 51 f->write = __stdio_write; |
53 f->seek = __stdio_seek; | 52 f->seek = __stdio_seek; |
54 f->close = __stdio_close; | 53 f->close = __stdio_close; |
55 | 54 |
56 if (!libc.threaded) | 55 if (!libc.threaded) |
57 f->lock = -1; | 56 f->lock = -1; |
58 | 57 |
59 /* Add new FILE to open file list */ | 58 /* Add new FILE to open file list */ |
60 return __ofl_add(f); | 59 return __ofl_add(f); |
61 } | 60 } |
62 | 61 |
63 weak_alias(__fdopen, fdopen); | 62 weak_alias(__fdopen, fdopen); |
OLD | NEW |