2 * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #ifdef LOADER_VERIEXEC
35 #include <verify_file.h>
39 fopen(const char *filename, const char *mode)
49 case 'r': /* open for reading */
54 case 'w': /* open for writing */
56 /* These are not actually implemented yet */
57 o = O_CREAT | O_TRUNC;
60 default: /* illegal mode */
67 fd = open(filename, m | o);
71 f = malloc(sizeof(FILE));
77 if (fstat(fd, &st) != 0) {
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) {
103 freopen(const char *filename, const char *mode, FILE *stream)
106 return (fopen(filename, mode));
110 fread(void *ptr, size_t size, size_t count, FILE *stream)
116 r = (size_t)read(stream->fd, ptr, size * count);
123 fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
127 if (stream == NULL || ptr == NULL)
129 w = write(stream->fd, ptr, size * count);
152 return (stream == NULL || stream->fd < 0);
162 return (stream->offset >= stream->size);
173 r = read(stream->fd, &ch, 1);
180 opendir(const char *name)
185 fd = open(name, O_RDONLY);
199 dp = malloc(sizeof(*dp));
216 luai_writestring(const char *s, int i)
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.
231 lstd_pow(int64_t x, int64_t y)
245 lstd_floor(int64_t x)
252 lstd_fmod(int64_t a, int64_t b)
259 * This can't be implemented, so maybe it should just abort.
262 lstd_frexp(int64_t a, int *y)