xref: /freebsd/stand/liblua/lstd.c (revision 07c17b2b00d8c1c8a2d58d4d8f99e64ec1182476)
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 FILE *
35 fopen(const char *filename, const char *mode)
36 {
37 	struct stat	st;
38 	int		fd;
39 	FILE		*f;
40 
41 	if (mode == NULL || mode[0] != 'r')
42 		return NULL;
43 
44 	fd = open(filename, O_RDONLY);
45 	if (fd < 0)
46 		return NULL;
47 
48 	f = malloc(sizeof(FILE));
49 	if (f == NULL) {
50 		close(fd);
51 		return NULL;
52 	}
53 
54 	if (fstat(fd, &st) != 0) {
55 		free(f);
56 		close(fd);
57 		return (NULL);
58 	}
59 
60 	f->fd = fd;
61 	f->offset = 0;
62 	f->size = st.st_size;
63 
64 	return (f);
65 }
66 
67 
68 FILE *
69 freopen(const char *filename, const char *mode, FILE *stream)
70 {
71 	fclose(stream);
72 	return (fopen(filename, mode));
73 }
74 
75 size_t
76 fread(void *ptr, size_t size, size_t count, FILE *stream)
77 {
78 	size_t r;
79 
80 	if (stream == NULL)
81 		return 0;
82 	r = (size_t)read(stream->fd, ptr, size * count);
83 	stream->offset += r;
84 
85 	return (r);
86 }
87 
88 int
89 fclose(FILE *stream)
90 {
91 	if (stream == NULL)
92 		return EOF;
93 	close(stream->fd);
94 	free(stream);
95 
96 	return (0);
97 }
98 
99 int
100 ferror(FILE *stream)
101 {
102 
103 	return (stream == NULL || stream->fd < 0);
104 }
105 
106 int
107 feof(FILE *stream)
108 {
109 
110 	if (stream == NULL)
111 		return 1;
112 
113 	return (stream->offset >= stream->size);
114 }
115 
116 int
117 getc(FILE *stream)
118 {
119 	char	ch;
120 	size_t	r;
121 
122 	if (stream == NULL)
123 		return EOF;
124 	r = read(stream->fd, &ch, 1);
125 	if (r == 1)
126 		return ch;
127 	return EOF;
128 }
129 
130 DIR *
131 opendir(const char *name)
132 {
133 	DIR *dp;
134 	int fd;
135 
136 	fd = open(name, O_RDONLY);
137 	if (fd < 0)
138 		return NULL;
139 	dp = fdopendir(fd);
140 	if (dp == NULL)
141 		close(fd);
142 	return dp;
143 }
144 
145 DIR *
146 fdopendir(int fd)
147 {
148 	DIR *dp;
149 
150 	dp = malloc(sizeof(*dp));
151 	if (dp == NULL)
152 		return NULL;
153 	dp->fd = fd;
154 	return dp;
155 }
156 
157 int
158 closedir(DIR *dp)
159 {
160 	close(dp->fd);
161 	dp->fd = -1;
162 	free(dp);
163 	return 0;
164 }
165 
166 void
167 luai_writestring(const char *s, int i)
168 {
169 
170 	while (i-- > 0)
171 		putchar(*s++);
172 }
173 
174 /*
175  * These routines from here on down are to implement the lua math
176  * library, but that's not presently included by default. They are
177  * little more than placeholders to allow compilation due to linkage
178  * issues with upstream Lua.
179  */
180 
181 int64_t
182 lstd_pow(int64_t x, int64_t y)
183 {
184 	int64_t rv = 1;
185 
186 	if (y < 0)
187 		return 0;
188 	rv = x;
189 	while (--y)
190 		rv *= x;
191 
192 	return rv;
193 }
194 
195 int64_t
196 lstd_floor(int64_t x)
197 {
198 
199 	return (x);
200 }
201 
202 int64_t
203 lstd_fmod(int64_t a, int64_t b)
204 {
205 
206 	return (a % b);
207 }
208 
209 /*
210  * This can't be implemented, so maybe it should just abort.
211  */
212 int64_t
213 lstd_frexp(int64_t a, int *y)
214 {
215 	*y = 0;
216 
217 	return 0;
218 }
219