new level test
[deeprace] / src / util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "util.h"
6
7 void *malloc_nf_impl(size_t sz, const char *file, int line)
8 {
9         void *p;
10         if(!(p = malloc(sz))) {
11                 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)sz);
12                 abort();
13         }
14         return p;
15 }
16
17 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line)
18 {
19         void *p;
20         if(!(p = calloc(num, sz))) {
21                 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)(num * sz));
22                 abort();
23         }
24         return p;
25 }
26
27 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line)
28 {
29         if(!(p = realloc(p, sz))) {
30                 fprintf(stderr, "%s:%d failed to realloc %lu bytes\n", file, line, (unsigned long)sz);
31                 abort();
32         }
33         return p;
34 }
35
36 char *strdup_nf_impl(const char *s, const char *file, int line)
37 {
38         int len;
39         char *res;
40
41         len = strlen(s);
42         if(!(res = malloc(len + 1))) {
43                 fprintf(stderr, "%s:%d failed to duplicate string\n", file, line);
44                 abort();
45         }
46         memcpy(res, s, len + 1);
47         return res;
48 }
49
50
51 int match_prefix(const char *str, const char *prefix)
52 {
53         while(*str && *prefix) {
54                 if(*str++ != *prefix++) {
55                         return 0;
56                 }
57         }
58         return *prefix ? 0 : 1;
59 }