• 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 	/* Compute the flags to pass to open() */
74 	mode_flags = __fmodeflags(mode, &file_flags);
75 	if (mode_flags < 0) {
76 		return NULL;
77 	}
78 
79 	if (mode_flags & O_CLOEXEC) {
80 		__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
81 	}
82 
83 	if (mode_flags & O_APPEND) {
84 		int flags = __syscall(SYS_fcntl, fd, F_GETFL);
85 		if (!(flags & O_APPEND))
86 			__syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
87 	}
88 
89 	f = __fdopenx(fd, file_flags);
90 	if (f) {
91 		return f;
92 	}
93 
94 	return NULL;
95 }
96 weak_alias(__fdopen, fdopen);
97 
__fdopenx(int fd,int flags)98 FILE *__fdopenx(int fd, int flags)
99 {
100 	FILE *f = 0;
101 	struct winsize wsz;
102 
103 	/* Allocate FILE or fail */
104 	if (!(f = __ofl_alloc())) {
105 		return NULL;
106 	}
107 
108 	/* Zero-fill only the struct, not the buffer */
109 	memset(f, 0, sizeof *f);
110 
111 	f->flags = flags;
112 	f->fd = fd;
113 
114 	/* Activate line buffered mode for terminals */
115 	f->lbf = EOF;
116 	if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) {
117 		f->lbf = '\n';
118 	}
119 
120 	/* Initialize op ptrs. No problem if some are unneeded. */
121 	f->read = __stdio_read;
122 	f->write = __stdio_write;
123 	f->seek = __stdio_seek;
124 	f->close = __stdio_close;
125 	f->readx = __stdio_readx;
126 
127 	if (!libc.threaded) {
128 		f->lock = -1;
129 	}
130 
131 	/* Add new FILE to open file list */
132 	return __ofl_add(f);
133 }
134 
135