xref: /freebsd/stand/liblua/lstd.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 #include "lstd.h"
30 #include "math.h"
31 
32 #ifdef LOADER_VERIEXEC
33 #include <verify_file.h>
34 #endif
35 
36 FILE *
37 fopen(const char *filename, const char *mode)
38 {
39 	struct stat	st;
40 	int		fd, m, o;
41 	FILE		*f;
42 
43 	if (mode == NULL)
44 		return NULL;
45 
46 	switch (*mode++) {
47 	case 'r':	/* open for reading */
48 		m = O_RDONLY;
49 		o = 0;
50 		break;
51 
52 	case 'w':	/* open for writing */
53 		m = O_WRONLY;
54 		/* These are not actually implemented yet */
55 		o = O_CREAT | O_TRUNC;
56 		break;
57 
58 	default:	/* illegal mode */
59 		return (NULL);
60 	}
61 
62 	if (*mode == '+')
63 		m = O_RDWR;
64 
65 	fd = open(filename, m | o);
66 	if (fd < 0)
67 		return NULL;
68 
69 	f = malloc(sizeof(FILE));
70 	if (f == NULL) {
71 		close(fd);
72 		return NULL;
73 	}
74 
75 	if (fstat(fd, &st) != 0) {
76 		free(f);
77 		close(fd);
78 		return (NULL);
79 	}
80 
81 #ifdef LOADER_VERIEXEC
82 	/* only regular files and only reading makes sense */
83 	if (S_ISREG(st.st_mode) && !(m & O_WRONLY)) {
84 		if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
85 			free(f);
86 			close(fd);
87 			return (NULL);
88 		}
89 	}
90 #endif
91 
92 	f->fd = fd;
93 	f->offset = 0;
94 	f->size = st.st_size;
95 
96 	return (f);
97 }
98 
99 
100 FILE *
101 freopen(const char *filename, const char *mode, FILE *stream)
102 {
103 	fclose(stream);
104 	return (fopen(filename, mode));
105 }
106 
107 size_t
108 fread(void *ptr, size_t size, size_t count, FILE *stream)
109 {
110 	size_t r;
111 
112 	if (stream == NULL)
113 		return 0;
114 	r = (size_t)read(stream->fd, ptr, size * count);
115 	stream->offset += r;
116 
117 	return (r);
118 }
119 
120 size_t
121 fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
122 {
123 	ssize_t w;
124 
125 	if (stream == NULL || ptr == NULL)
126 		return (0);
127 	w = write(stream->fd, ptr, size * count);
128 	if (w == -1)
129 		return (0);
130 
131 	stream->offset += w;
132 	return ((size_t)w);
133 }
134 
135 int
136 fclose(FILE *stream)
137 {
138 	if (stream == NULL)
139 		return EOF;
140 	close(stream->fd);
141 	free(stream);
142 
143 	return (0);
144 }
145 
146 int
147 ferror(FILE *stream)
148 {
149 
150 	return (stream == NULL || stream->fd < 0);
151 }
152 
153 int
154 feof(FILE *stream)
155 {
156 
157 	if (stream == NULL)
158 		return 1;
159 
160 	return (stream->offset >= stream->size);
161 }
162 
163 int
164 getc(FILE *stream)
165 {
166 	char	ch;
167 	size_t	r;
168 
169 	if (stream == NULL)
170 		return EOF;
171 	r = read(stream->fd, &ch, 1);
172 	if (r == 1)
173 		return ch;
174 	return EOF;
175 }
176 
177 DIR *
178 opendir(const char *name)
179 {
180 	DIR *dp;
181 	int fd;
182 
183 	fd = open(name, O_RDONLY);
184 	if (fd < 0)
185 		return NULL;
186 	dp = fdopendir(fd);
187 	if (dp == NULL)
188 		close(fd);
189 	return dp;
190 }
191 
192 DIR *
193 fdopendir(int fd)
194 {
195 	DIR *dp;
196 
197 	dp = malloc(sizeof(*dp));
198 	if (dp == NULL)
199 		return NULL;
200 	dp->fd = fd;
201 	return dp;
202 }
203 
204 int
205 closedir(DIR *dp)
206 {
207 	close(dp->fd);
208 	dp->fd = -1;
209 	free(dp);
210 	return 0;
211 }
212 
213 void
214 luai_writestring(const char *s, int i)
215 {
216 
217 	while (i-- > 0)
218 		putchar(*s++);
219 }
220 
221 /*
222  * These routines from here on down are to implement the lua math
223  * library, but that's not presently included by default. They are
224  * little more than placeholders to allow compilation due to linkage
225  * issues with upstream Lua.
226  */
227 
228 int64_t
229 lstd_pow(int64_t x, int64_t y)
230 {
231 	int64_t rv = 1;
232 
233 	if (y < 0)
234 		return 0;
235 	rv = x;
236 	while (--y)
237 		rv *= x;
238 
239 	return rv;
240 }
241 
242 int64_t
243 lstd_floor(int64_t x)
244 {
245 
246 	return (x);
247 }
248 
249 int64_t
250 lstd_fmod(int64_t a, int64_t b)
251 {
252 
253 	return (a % b);
254 }
255 
256 /*
257  * This can't be implemented, so maybe it should just abort.
258  */
259 int64_t
260 lstd_frexp(int64_t a, int *y)
261 {
262 	*y = 0;
263 
264 	return 0;
265 }
266