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 void 131 luai_writestring(const char *s, int i) 132 { 133 134 while (i-- > 0) 135 putchar(*s++); 136 } 137 138 /* 139 * These routines from here on down are to implement the lua math 140 * library, but that's not presently included by default. They are 141 * little more than placeholders to allow compilation due to linkage 142 * issues with upstream Lua. 143 */ 144 145 int64_t 146 lstd_pow(int64_t x, int64_t y) 147 { 148 int64_t rv = 1; 149 150 if (y < 0) 151 return 0; 152 rv = x; 153 while (--y) 154 rv *= x; 155 156 return rv; 157 } 158 159 int64_t 160 lstd_floor(int64_t x) 161 { 162 163 return (x); 164 } 165 166 int64_t 167 lstd_fmod(int64_t a, int64_t b) 168 { 169 170 return (a % b); 171 } 172 173 /* 174 * This can't be implemented, so maybe it should just abort. 175 */ 176 int64_t 177 lstd_frexp(int64_t a, int *y) 178 { 179 *y = 0; 180 181 return 0; 182 } 183