xref: /freebsd/stand/liblua/lstd.c (revision 82c85a42b49a243a51ecc03d3afa6dbd294e0b25)
17cafeaa1SWarner Losh /*-
27cafeaa1SWarner Losh  * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
37cafeaa1SWarner Losh  * All rights reserved.
47cafeaa1SWarner Losh  *
57cafeaa1SWarner Losh  * Redistribution and use in source and binary forms, with or without
67cafeaa1SWarner Losh  * modification, are permitted provided that the following conditions
77cafeaa1SWarner Losh  * are met:
87cafeaa1SWarner Losh  * 1. Redistributions of source code must retain the above copyright
97cafeaa1SWarner Losh  *    notice, this list of conditions and the following disclaimer.
107cafeaa1SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
117cafeaa1SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
127cafeaa1SWarner Losh  *    documentation and/or other materials provided with the distribution.
137cafeaa1SWarner Losh  *
147cafeaa1SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157cafeaa1SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167cafeaa1SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177cafeaa1SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187cafeaa1SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197cafeaa1SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207cafeaa1SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217cafeaa1SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227cafeaa1SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237cafeaa1SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247cafeaa1SWarner Losh  * SUCH DAMAGE.
257cafeaa1SWarner Losh  *
267cafeaa1SWarner Losh  */
277cafeaa1SWarner Losh 
287cafeaa1SWarner Losh #include <sys/cdefs.h>
297cafeaa1SWarner Losh __FBSDID("$FreeBSD$");
307cafeaa1SWarner Losh 
317cafeaa1SWarner Losh #include "lstd.h"
327cafeaa1SWarner Losh #include "math.h"
337cafeaa1SWarner Losh 
347cafeaa1SWarner Losh FILE *
357cafeaa1SWarner Losh fopen(const char *filename, const char *mode)
367cafeaa1SWarner Losh {
377cafeaa1SWarner Losh 	struct stat	st;
38*82c85a42SKyle Evans 	int		fd, m, o;
397cafeaa1SWarner Losh 	FILE		*f;
407cafeaa1SWarner Losh 
41*82c85a42SKyle Evans 	if (mode == NULL)
427cafeaa1SWarner Losh 		return NULL;
437cafeaa1SWarner Losh 
44*82c85a42SKyle Evans 	switch (*mode++) {
45*82c85a42SKyle Evans 	case 'r':	/* open for reading */
46*82c85a42SKyle Evans 		m = O_RDONLY;
47*82c85a42SKyle Evans 		o = 0;
48*82c85a42SKyle Evans 		break;
49*82c85a42SKyle Evans 
50*82c85a42SKyle Evans 	case 'w':	/* open for writing */
51*82c85a42SKyle Evans 		m = O_WRONLY;
52*82c85a42SKyle Evans 		/* These are not actually implemented yet */
53*82c85a42SKyle Evans 		o = O_CREAT | O_TRUNC;
54*82c85a42SKyle Evans 		break;
55*82c85a42SKyle Evans 
56*82c85a42SKyle Evans 	default:	/* illegal mode */
57*82c85a42SKyle Evans 		return (NULL);
58*82c85a42SKyle Evans 	}
59*82c85a42SKyle Evans 
60*82c85a42SKyle Evans 	if (*mode == '+')
61*82c85a42SKyle Evans 		m = O_RDWR;
62*82c85a42SKyle Evans 
63*82c85a42SKyle Evans 	fd = open(filename, m | o);
647cafeaa1SWarner Losh 	if (fd < 0)
657cafeaa1SWarner Losh 		return NULL;
667cafeaa1SWarner Losh 
677cafeaa1SWarner Losh 	f = malloc(sizeof(FILE));
687cafeaa1SWarner Losh 	if (f == NULL) {
697cafeaa1SWarner Losh 		close(fd);
707cafeaa1SWarner Losh 		return NULL;
717cafeaa1SWarner Losh 	}
727cafeaa1SWarner Losh 
737cafeaa1SWarner Losh 	if (fstat(fd, &st) != 0) {
747cafeaa1SWarner Losh 		free(f);
757cafeaa1SWarner Losh 		close(fd);
767cafeaa1SWarner Losh 		return (NULL);
777cafeaa1SWarner Losh 	}
787cafeaa1SWarner Losh 
797cafeaa1SWarner Losh 	f->fd = fd;
807cafeaa1SWarner Losh 	f->offset = 0;
817cafeaa1SWarner Losh 	f->size = st.st_size;
827cafeaa1SWarner Losh 
837cafeaa1SWarner Losh 	return (f);
847cafeaa1SWarner Losh }
857cafeaa1SWarner Losh 
867cafeaa1SWarner Losh 
877cafeaa1SWarner Losh FILE *
887cafeaa1SWarner Losh freopen(const char *filename, const char *mode, FILE *stream)
897cafeaa1SWarner Losh {
907cafeaa1SWarner Losh 	fclose(stream);
917cafeaa1SWarner Losh 	return (fopen(filename, mode));
927cafeaa1SWarner Losh }
937cafeaa1SWarner Losh 
947cafeaa1SWarner Losh size_t
957cafeaa1SWarner Losh fread(void *ptr, size_t size, size_t count, FILE *stream)
967cafeaa1SWarner Losh {
977cafeaa1SWarner Losh 	size_t r;
987cafeaa1SWarner Losh 
997cafeaa1SWarner Losh 	if (stream == NULL)
1007cafeaa1SWarner Losh 		return 0;
1017cafeaa1SWarner Losh 	r = (size_t)read(stream->fd, ptr, size * count);
1027cafeaa1SWarner Losh 	stream->offset += r;
1037cafeaa1SWarner Losh 
1047cafeaa1SWarner Losh 	return (r);
1057cafeaa1SWarner Losh }
1067cafeaa1SWarner Losh 
107*82c85a42SKyle Evans size_t
108*82c85a42SKyle Evans fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
109*82c85a42SKyle Evans {
110*82c85a42SKyle Evans 	ssize_t w;
111*82c85a42SKyle Evans 
112*82c85a42SKyle Evans 	if (stream == NULL || ptr == NULL)
113*82c85a42SKyle Evans 		return (0);
114*82c85a42SKyle Evans 	w = write(stream->fd, ptr, size * count);
115*82c85a42SKyle Evans 	if (w == -1)
116*82c85a42SKyle Evans 		return (0);
117*82c85a42SKyle Evans 
118*82c85a42SKyle Evans 	stream->offset += w;
119*82c85a42SKyle Evans 	return ((size_t)w);
120*82c85a42SKyle Evans }
121*82c85a42SKyle Evans 
1227cafeaa1SWarner Losh int
1237cafeaa1SWarner Losh fclose(FILE *stream)
1247cafeaa1SWarner Losh {
1257cafeaa1SWarner Losh 	if (stream == NULL)
1267cafeaa1SWarner Losh 		return EOF;
1277cafeaa1SWarner Losh 	close(stream->fd);
1287cafeaa1SWarner Losh 	free(stream);
1297cafeaa1SWarner Losh 
1307cafeaa1SWarner Losh 	return (0);
1317cafeaa1SWarner Losh }
1327cafeaa1SWarner Losh 
1337cafeaa1SWarner Losh int
1347cafeaa1SWarner Losh ferror(FILE *stream)
1357cafeaa1SWarner Losh {
1367cafeaa1SWarner Losh 
1377cafeaa1SWarner Losh 	return (stream == NULL || stream->fd < 0);
1387cafeaa1SWarner Losh }
1397cafeaa1SWarner Losh 
1407cafeaa1SWarner Losh int
1417cafeaa1SWarner Losh feof(FILE *stream)
1427cafeaa1SWarner Losh {
1437cafeaa1SWarner Losh 
1447cafeaa1SWarner Losh 	if (stream == NULL)
1457cafeaa1SWarner Losh 		return 1;
1467cafeaa1SWarner Losh 
1477cafeaa1SWarner Losh 	return (stream->offset >= stream->size);
1487cafeaa1SWarner Losh }
1497cafeaa1SWarner Losh 
1507cafeaa1SWarner Losh int
1517cafeaa1SWarner Losh getc(FILE *stream)
1527cafeaa1SWarner Losh {
1537cafeaa1SWarner Losh 	char	ch;
1547cafeaa1SWarner Losh 	size_t	r;
1557cafeaa1SWarner Losh 
1567cafeaa1SWarner Losh 	if (stream == NULL)
1577cafeaa1SWarner Losh 		return EOF;
1587cafeaa1SWarner Losh 	r = read(stream->fd, &ch, 1);
1597cafeaa1SWarner Losh 	if (r == 1)
1607cafeaa1SWarner Losh 		return ch;
1617cafeaa1SWarner Losh 	return EOF;
1627cafeaa1SWarner Losh }
1637cafeaa1SWarner Losh 
164b216e997SConrad Meyer DIR *
165b216e997SConrad Meyer opendir(const char *name)
166b216e997SConrad Meyer {
167b216e997SConrad Meyer 	DIR *dp;
168b216e997SConrad Meyer 	int fd;
169b216e997SConrad Meyer 
170b216e997SConrad Meyer 	fd = open(name, O_RDONLY);
171b216e997SConrad Meyer 	if (fd < 0)
172b216e997SConrad Meyer 		return NULL;
173b216e997SConrad Meyer 	dp = fdopendir(fd);
174b216e997SConrad Meyer 	if (dp == NULL)
175b216e997SConrad Meyer 		close(fd);
176b216e997SConrad Meyer 	return dp;
177b216e997SConrad Meyer }
178b216e997SConrad Meyer 
179b216e997SConrad Meyer DIR *
180b216e997SConrad Meyer fdopendir(int fd)
181b216e997SConrad Meyer {
182b216e997SConrad Meyer 	DIR *dp;
183b216e997SConrad Meyer 
184b216e997SConrad Meyer 	dp = malloc(sizeof(*dp));
185b216e997SConrad Meyer 	if (dp == NULL)
186b216e997SConrad Meyer 		return NULL;
187b216e997SConrad Meyer 	dp->fd = fd;
188b216e997SConrad Meyer 	return dp;
189b216e997SConrad Meyer }
190b216e997SConrad Meyer 
191b216e997SConrad Meyer int
192b216e997SConrad Meyer closedir(DIR *dp)
193b216e997SConrad Meyer {
194b216e997SConrad Meyer 	close(dp->fd);
195b216e997SConrad Meyer 	dp->fd = -1;
196b216e997SConrad Meyer 	free(dp);
197b216e997SConrad Meyer 	return 0;
198b216e997SConrad Meyer }
199b216e997SConrad Meyer 
2007cafeaa1SWarner Losh void
2017cafeaa1SWarner Losh luai_writestring(const char *s, int i)
2027cafeaa1SWarner Losh {
2037cafeaa1SWarner Losh 
2047cafeaa1SWarner Losh 	while (i-- > 0)
2057cafeaa1SWarner Losh 		putchar(*s++);
2067cafeaa1SWarner Losh }
2077cafeaa1SWarner Losh 
2087cafeaa1SWarner Losh /*
2097cafeaa1SWarner Losh  * These routines from here on down are to implement the lua math
2107cafeaa1SWarner Losh  * library, but that's not presently included by default. They are
2117cafeaa1SWarner Losh  * little more than placeholders to allow compilation due to linkage
2127cafeaa1SWarner Losh  * issues with upstream Lua.
2137cafeaa1SWarner Losh  */
2147cafeaa1SWarner Losh 
2157cafeaa1SWarner Losh int64_t
2167cafeaa1SWarner Losh lstd_pow(int64_t x, int64_t y)
2177cafeaa1SWarner Losh {
2187cafeaa1SWarner Losh 	int64_t rv = 1;
2197cafeaa1SWarner Losh 
2207cafeaa1SWarner Losh 	if (y < 0)
2217cafeaa1SWarner Losh 		return 0;
2227cafeaa1SWarner Losh 	rv = x;
2237cafeaa1SWarner Losh 	while (--y)
2247cafeaa1SWarner Losh 		rv *= x;
2257cafeaa1SWarner Losh 
2267cafeaa1SWarner Losh 	return rv;
2277cafeaa1SWarner Losh }
2287cafeaa1SWarner Losh 
2297cafeaa1SWarner Losh int64_t
2307cafeaa1SWarner Losh lstd_floor(int64_t x)
2317cafeaa1SWarner Losh {
2327cafeaa1SWarner Losh 
2337cafeaa1SWarner Losh 	return (x);
2347cafeaa1SWarner Losh }
2357cafeaa1SWarner Losh 
2367cafeaa1SWarner Losh int64_t
2377cafeaa1SWarner Losh lstd_fmod(int64_t a, int64_t b)
2387cafeaa1SWarner Losh {
2397cafeaa1SWarner Losh 
2407cafeaa1SWarner Losh 	return (a % b);
2417cafeaa1SWarner Losh }
2427cafeaa1SWarner Losh 
2437cafeaa1SWarner Losh /*
2447cafeaa1SWarner Losh  * This can't be implemented, so maybe it should just abort.
2457cafeaa1SWarner Losh  */
2467cafeaa1SWarner Losh int64_t
2477cafeaa1SWarner Losh lstd_frexp(int64_t a, int *y)
2487cafeaa1SWarner Losh {
2497cafeaa1SWarner Losh 	*y = 0;
2507cafeaa1SWarner Losh 
2517cafeaa1SWarner Losh 	return 0;
2527cafeaa1SWarner Losh }
253