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
31 #ifdef LOADER_VERIEXEC
32 #include <verify_file.h>
36 fopen(const char *filename, const char *mode)
46 case 'r': /* open for reading */
51 case 'w': /* open for writing */
53 /* These are not actually implemented yet */
54 o = O_CREAT | O_TRUNC;
57 default: /* illegal mode */
64 fd = open(filename, m | o);
68 f = malloc(sizeof(FILE));
74 if (fstat(fd, &st) != 0) {
80 #ifdef LOADER_VERIEXEC
81 /* only regular files and only reading makes sense */
82 if (S_ISREG(st.st_mode) && !(m & O_WRONLY)) {
83 if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
100 freopen(const char *filename, const char *mode, FILE *stream)
103 return (fopen(filename, mode));
107 fread(void *ptr, size_t size, size_t count, FILE *stream)
113 r = (size_t)read(stream->fd, ptr, size * count);
120 fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
124 if (stream == NULL || ptr == NULL)
126 w = write(stream->fd, ptr, size * count);
149 return (stream == NULL || stream->fd < 0);
159 return (stream->offset >= stream->size);
170 r = read(stream->fd, &ch, 1);
177 opendir(const char *name)
182 fd = open(name, O_RDONLY);
196 dp = malloc(sizeof(*dp));
213 luai_writestring(const char *s, int i)
221 * These routines from here on down are to implement the lua math
222 * library, but that's not presently included by default. They are
223 * little more than placeholders to allow compilation due to linkage
224 * issues with upstream Lua.
228 lstd_pow(int64_t x, int64_t y)
242 lstd_floor(int64_t x)
249 lstd_fmod(int64_t a, int64_t b)
256 * This can't be implemented, so maybe it should just abort.
259 lstd_frexp(int64_t a, int *y)