1 /* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 #include "stdio_impl.h"
16 #include <stdlib.h>
17 #include <sys/ioctl.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <string.h>
22 #include "libc.h"
23
get_bufsize(int fd)24 static size_t get_bufsize(int fd)
25 {
26 struct stat st;
27 size_t buf_size = 0;
28
29 if (fstat(fd, &st) < 0) {
30 buf_size = BUFSIZ;
31 } else if (st.st_blksize == 0) {
32 buf_size = BUFSIZ;
33 } else {
34 buf_size = st.st_blksize;
35 }
36
37 return buf_size;
38 }
39
__fdopen(int fd,const char * mode)40 FILE *__fdopen(int fd, const char *mode)
41 {
42 FILE *f;
43 struct winsize wsz;
44 size_t buf_size = 0;
45
46 /* Check for valid initial mode character */
47 if (!strchr("rwa", *mode)) {
48 errno = EINVAL;
49 return 0;
50 }
51
52 /* get buffer size via file stat */
53 buf_size = get_bufsize(fd);
54
55 /* Allocate FILE+buffer or fail */
56 if (!(f = malloc(sizeof *f + UNGET + buf_size))) {
57 return 0;
58 }
59
60 /* Zero-fill only the struct, not the buffer */
61 memset(f, 0, sizeof *f);
62
63 /* Impose mode restrictions */
64 if (!strchr(mode, '+')) {
65 f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
66 }
67
68 /* Apply close-on-exec flag */
69 if (strchr(mode, 'e')) {
70 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
71 }
72
73 /* Set append mode on fd if opened for append */
74 if (*mode == 'a') {
75 int flags = __syscall(SYS_fcntl, fd, F_GETFL);
76 if (!(flags & O_APPEND))
77 __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
78 f->flags |= F_APP;
79 }
80
81 f->fd = fd;
82 f->buf = (unsigned char *)f + sizeof *f + UNGET;
83 f->buf_size = buf_size;
84
85 /* Activate line buffered mode for terminals */
86 f->lbf = EOF;
87 if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) {
88 f->lbf = '\n';
89 }
90
91 /* Initialize op ptrs. No problem if some are unneeded. */
92 f->read = __stdio_read;
93 f->write = __stdio_write;
94 f->seek = __stdio_seek;
95 f->close = __stdio_close;
96
97 if (!libc.threaded) {
98 f->lock = -1;
99 }
100
101 /* Add new FILE to open file list */
102 return __ofl_add(f);
103 }
104
105 weak_alias(__fdopen, fdopen);
106