xref: /freebsd/stand/liblua/lstd.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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 __FBSDID("$FreeBSD$");
30 
31 #include "lstd.h"
32 #include "math.h"
33 
34 #ifdef LOADER_VERIEXEC
35 #include <verify_file.h>
36 #endif
37 
38 FILE *
39 fopen(const char *filename, const char *mode)
40 {
41 	struct stat	st;
42 	int		fd, m, o;
43 	FILE		*f;
44 
45 	if (mode == NULL)
46 		return NULL;
47 
48 	switch (*mode++) {
49 	case 'r':	/* open for reading */
50 		m = O_RDONLY;
51 		o = 0;
52 		break;
53 
54 	case 'w':	/* open for writing */
55 		m = O_WRONLY;
56 		/* These are not actually implemented yet */
57 		o = O_CREAT | O_TRUNC;
58 		break;
59 
60 	default:	/* illegal mode */
61 		return (NULL);
62 	}
63 
64 	if (*mode == '+')
65 		m = O_RDWR;
66 
67 	fd = open(filename, m | o);
68 	if (fd < 0)
69 		return NULL;
70 
71 	f = malloc(sizeof(FILE));
72 	if (f == NULL) {
73 		close(fd);
74 		return NULL;
75 	}
76 
77 	if (fstat(fd, &st) != 0) {
78 		free(f);
79 		close(fd);
80 		return (NULL);
81 	}
82 
83 #ifdef LOADER_VERIEXEC
84 	/* only regular files and only reading makes sense */
85 	if (S_ISREG(st.st_mode) && !(m & O_WRONLY)) {
86 		if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
87 			free(f);
88 			close(fd);
89 			return (NULL);
90 		}
91 	}
92 #endif
93 
94 	f->fd = fd;
95 	f->offset = 0;
96 	f->size = st.st_size;
97 
98 	return (f);
99 }
100 
101 
102 FILE *
103 freopen(const char *filename, const char *mode, FILE *stream)
104 {
105 	fclose(stream);
106 	return (fopen(filename, mode));
107 }
108 
109 size_t
110 fread(void *ptr, size_t size, size_t count, FILE *stream)
111 {
112 	size_t r;
113 
114 	if (stream == NULL)
115 		return 0;
116 	r = (size_t)read(stream->fd, ptr, size * count);
117 	stream->offset += r;
118 
119 	return (r);
120 }
121 
122 size_t
123 fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
124 {
125 	ssize_t w;
126 
127 	if (stream == NULL || ptr == NULL)
128 		return (0);
129 	w = write(stream->fd, ptr, size * count);
130 	if (w == -1)
131 		return (0);
132 
133 	stream->offset += w;
134 	return ((size_t)w);
135 }
136 
137 int
138 fclose(FILE *stream)
139 {
140 	if (stream == NULL)
141 		return EOF;
142 	close(stream->fd);
143 	free(stream);
144 
145 	return (0);
146 }
147 
148 int
149 ferror(FILE *stream)
150 {
151 
152 	return (stream == NULL || stream->fd < 0);
153 }
154 
155 int
156 feof(FILE *stream)
157 {
158 
159 	if (stream == NULL)
160 		return 1;
161 
162 	return (stream->offset >= stream->size);
163 }
164 
165 int
166 getc(FILE *stream)
167 {
168 	char	ch;
169 	size_t	r;
170 
171 	if (stream == NULL)
172 		return EOF;
173 	r = read(stream->fd, &ch, 1);
174 	if (r == 1)
175 		return ch;
176 	return EOF;
177 }
178 
179 DIR *
180 opendir(const char *name)
181 {
182 	DIR *dp;
183 	int fd;
184 
185 	fd = open(name, O_RDONLY);
186 	if (fd < 0)
187 		return NULL;
188 	dp = fdopendir(fd);
189 	if (dp == NULL)
190 		close(fd);
191 	return dp;
192 }
193 
194 DIR *
195 fdopendir(int fd)
196 {
197 	DIR *dp;
198 
199 	dp = malloc(sizeof(*dp));
200 	if (dp == NULL)
201 		return NULL;
202 	dp->fd = fd;
203 	return dp;
204 }
205 
206 int
207 closedir(DIR *dp)
208 {
209 	close(dp->fd);
210 	dp->fd = -1;
211 	free(dp);
212 	return 0;
213 }
214 
215 void
216 luai_writestring(const char *s, int i)
217 {
218 
219 	while (i-- > 0)
220 		putchar(*s++);
221 }
222 
223 /*
224  * These routines from here on down are to implement the lua math
225  * library, but that's not presently included by default. They are
226  * little more than placeholders to allow compilation due to linkage
227  * issues with upstream Lua.
228  */
229 
230 int64_t
231 lstd_pow(int64_t x, int64_t y)
232 {
233 	int64_t rv = 1;
234 
235 	if (y < 0)
236 		return 0;
237 	rv = x;
238 	while (--y)
239 		rv *= x;
240 
241 	return rv;
242 }
243 
244 int64_t
245 lstd_floor(int64_t x)
246 {
247 
248 	return (x);
249 }
250 
251 int64_t
252 lstd_fmod(int64_t a, int64_t b)
253 {
254 
255 	return (a % b);
256 }
257 
258 /*
259  * This can't be implemented, so maybe it should just abort.
260  */
261 int64_t
262 lstd_frexp(int64_t a, int *y)
263 {
264 	*y = 0;
265 
266 	return 0;
267 }
268