Mercurial > molko
comparison libcore/core/util.c @ 198:d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 09 Nov 2020 13:24:49 +0100 |
parents | 7103d6574062 |
children | befa2e855d3b |
comparison
equal
deleted
inserted
replaced
197:852d0b7817ce | 198:d6f217a5e4b1 |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <sys/stat.h> | |
19 #include <assert.h> | 20 #include <assert.h> |
21 #include <errno.h> | |
22 #include <fcntl.h> | |
20 #include <stdlib.h> | 23 #include <stdlib.h> |
24 #include <unistd.h> | |
21 | 25 |
22 #include <SDL.h> | 26 #include <SDL.h> |
23 | 27 |
28 #include "alloc.h" | |
29 #include "error.h" | |
24 #include "util.h" | 30 #include "util.h" |
25 | 31 |
26 void | 32 void |
27 delay(unsigned int ms) | 33 delay(unsigned int ms) |
28 { | 34 { |
29 SDL_Delay(ms); | 35 SDL_Delay(ms); |
36 } | |
37 | |
38 char * | |
39 readall(const char *path) | |
40 { | |
41 int fd; | |
42 struct stat st; | |
43 char *str = NULL; | |
44 ssize_t nr; | |
45 | |
46 if ((fd = open(path, O_RDONLY)) < 0 || fstat(fd, &st) < 0) | |
47 goto io_error; | |
48 if (!(str = alloc_zero(1, st.st_size + 1))) | |
49 goto alloc_error; | |
50 if ((nr = read(fd, str, st.st_size)) != st.st_size) | |
51 goto io_error; | |
52 | |
53 return str; | |
54 | |
55 io_error: | |
56 errorf("%s", strerror(errno)); | |
57 | |
58 alloc_error: | |
59 close(fd); | |
60 free(str); | |
61 | |
62 return NULL; | |
30 } | 63 } |
31 | 64 |
32 unsigned int | 65 unsigned int |
33 nrand(unsigned int lower, unsigned int upper) | 66 nrand(unsigned int lower, unsigned int upper) |
34 { | 67 { |