• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
__falloc_buf(FILE * f)40 int __falloc_buf(FILE *f)
41 {
42 	/* return if already allocated, or F_NOBUF set */
43 	if (f->buf != NULL || f->buf_size != 0 || f->flags & F_NOBUF) {
44 		return 0;
45 	}
46 
47 	/* Default,  base and buf are NULL,and buf_size = 0 */
48 	size_t buf_size = 0;
49 
50 	/* get buffer size via file stat */
51 	buf_size = __get_bufsize(f->fd);
52 
53 	/* alloc R/W buffer */
54 	f->base = (unsigned char *)malloc(UNGET + buf_size * sizeof(unsigned char));
55 	if (!f->base) {
56 		errno = -ENOMEM;
57 		return errno;
58 	}
59 
60 	/* reserve UNGET buffer */
61 	f->buf = f->base + UNGET;
62 	f->buf_size = buf_size;
63 
64 	return 0;
65 }
66 
__fdopen(int fd,const char * mode)67 FILE *__fdopen(int fd, const char *mode)
68 {
69 	FILE *f = NULL;
70 	int file_flags = 0;
71 	int mode_flags = 0;
72 
73 	int flags = syscall(SYS_fcntl, fd, F_GETFL, 0);
74 	if (flags == -1) {
75 		return NULL;
76 	}
77 
78 	/* Compute the flags to pass to open() */
79 	mode_flags = __fmodeflags(mode, &file_flags);
80 	if (mode_flags < 0) {
81 		return NULL;
82 	}
83 
84 	if (mode_flags & O_CLOEXEC) {
85 		__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
86 	}
87 
88 	if (mode_flags & O_APPEND) {
89 		flags = __syscall(SYS_fcntl, fd, F_GETFL);
90 		if (!(flags & O_APPEND))
91 			__syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
92 	}
93 
94 	f = __fdopenx(fd, file_flags);
95 	if (f) {
96 		return f;
97 	}
98 
99 	return NULL;
100 }
101 weak_alias(__fdopen, fdopen);
102 
__fdopenx(int fd,int flags)103 FILE *__fdopenx(int fd, int flags)
104 {
105 	FILE *f = 0;
106 	struct winsize wsz;
107 
108 	/* Allocate FILE or fail */
109 	if (!(f = __ofl_alloc())) {
110 		return NULL;
111 	}
112 
113 	/* Zero-fill only the struct, not the buffer */
114 	memset(f, 0, sizeof *f);
115 
116 	f->flags = flags;
117 	f->fd = fd;
118 
119 	/* Activate line buffered mode for terminals */
120 	f->lbf = EOF;
121 	if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) {
122 		f->lbf = '\n';
123 	}
124 
125 	/* set file tag in fdtable, FILE type */
126 	fdsan_exchange_owner_tag(fd, 0, __get_file_tag(f));
127 
128 	/* Initialize op ptrs. No problem if some are unneeded. */
129 	f->read = __stdio_read;
130 	f->write = __stdio_write;
131 	f->seek = __stdio_seek;
132 	f->close = __stdio_close;
133 	f->readx = __stdio_readx;
134 
135 	if (!libc.threaded) {
136 		f->lock = -1;
137 	}
138 
139 	/* Add new FILE to open file list */
140 	return __ofl_add(f);
141 }
142 
143