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>
32 #ifdef LOADER_VERIEXEC
33 #include <verify_file.h>
37 fopen(const char *filename, const char *mode)
47 case 'r': /* open for reading */
52 case 'w': /* open for writing */
54 /* These are not actually implemented yet */
55 o = O_CREAT | O_TRUNC;
58 default: /* illegal mode */
65 fd = open(filename, m | o);
69 f = malloc(sizeof(FILE));
75 if (fstat(fd, &st) != 0) {
81 #ifdef LOADER_VERIEXEC
82 /* only regular files and only reading makes sense */
83 if (S_ISREG(st.st_mode) && !(m & O_WRONLY)) {
84 if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
101 freopen(const char *filename, const char *mode, FILE *stream)
104 return (fopen(filename, mode));
108 fread(void *ptr, size_t size, size_t count, FILE *stream)
114 r = (size_t)read(stream->fd, ptr, size * count);
121 fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
125 if (stream == NULL || ptr == NULL)
127 w = write(stream->fd, ptr, size * count);
150 return (stream == NULL || stream->fd < 0);
160 return (stream->offset >= stream->size);
171 r = read(stream->fd, &ch, 1);
178 opendir(const char *name)
183 fd = open(name, O_RDONLY);
197 dp = malloc(sizeof(*dp));
214 luai_writestring(const char *s, int i)
222 * These routines from here on down are to implement the lua math
223 * library, but that's not presently included by default. They are
224 * little more than placeholders to allow compilation due to linkage
225 * issues with upstream Lua.
229 lstd_pow(int64_t x, int64_t y)
243 lstd_floor(int64_t x)
250 lstd_fmod(int64_t a, int64_t b)
257 * This can't be implemented, so maybe it should just abort.
260 lstd_frexp(int64_t a, int *y)