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