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