agendafs
A filesystem for your calendar.
git clone git://mccd.space/agendafs| Log | Files | Refs | README | LICENSE | Mail | Website |
util.c (1026B)
1 // Copyright © 2019-2024 Michael Forney
2 // util.c from cproc
3 #include "util.h"
4 #include <assert.h>
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 void *
14 xmalloc(size_t len)
15 {
16 void *buf;
17
18 buf = malloc(len);
19 if (!buf && len) {
20 LOG("FAILED TO MALLOC FOR %zu", len);
21 exit(1);
22 }
23
24 return buf;
25 }
26
27 void *
28 xcalloc(size_t memb, size_t s)
29 {
30 void *buf;
31
32 buf = calloc(memb, s);
33 if (!buf)
34 exit(1);
35
36 return buf;
37 }
38
39 char *
40 xstrdup(const char *s)
41 {
42 char *cpy = strdup(s);
43 if (!cpy)
44 exit(1);
45 return cpy;
46 }
47
48 void *
49 reallocarray(void *buf, size_t n, size_t m)
50 {
51 if (n > 0 && SIZE_MAX / n < m) {
52 errno = ENOMEM;
53 return NULL;
54 }
55 return realloc(buf, n * m);
56 }
57
58 void *
59 xreallocarray(void *buf, size_t n, size_t m)
60 {
61 buf = reallocarray(buf, n, m);
62 if (!buf && n && m)
63 exit(1);
64
65 return buf;
66 }
67
68 bool
69 starts_with_str(const char *str, const char *prefix)
70 {
71 size_t len = strlen(prefix);
72 return strncmp(str, prefix, len) == 0;
73 }
74