tutil.c - ics2txt - convert icalendar .ics file to plain text | |
git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
tutil.c (1205B) | |
--- | |
1 #include "util.h" | |
2 | |
3 #include <errno.h> | |
4 #include <stdint.h> | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 | |
8 size_t | |
9 strlcpy(char *buf, char const *str, size_t sz) | |
10 { | |
11 size_t len, cpy; | |
12 | |
13 len = strlen(str); | |
14 cpy = (len > sz) ? (sz) : (len); | |
15 memcpy(buf, str, cpy + 1); | |
16 buf[sz - 1] = '\0'; | |
17 return len; | |
18 } | |
19 | |
20 char * | |
21 strsep(char **str_p, char const *sep) | |
22 { | |
23 char *s, *prev; | |
24 | |
25 if (*str_p == NULL) | |
26 return NULL; | |
27 | |
28 for (s = prev = *str_p; strchr(sep, *s) == NULL; s++) | |
29 continue; | |
30 | |
31 if (*s == '\0') { | |
32 *str_p = NULL; | |
33 } else { | |
34 *s = '\0'; | |
35 *str_p = s + 1; | |
36 } | |
37 return prev; | |
38 } | |
39 | |
40 void | |
41 strchomp(char *line) | |
42 { | |
43 size_t len; | |
44 | |
45 len = strlen(line); | |
46 if (len > 0 && line[len - 1] == '\n') | |
47 line[len-- - 1] = '\0'; | |
48 if (len > 0 && line[len - 1] == '\r') | |
49 line[len-- - 1] = '\0'; | |
50 } | |
51 | |
52 int | |
53 strappend(char **base_p, char const *s) | |
54 { | |
55 size_t base_len, s_len; | |
56 void *v; | |
57 | |
58 base_len = (*base_p == NULL) ? (0) : (strlen(*base_p)); | |
59 s_len = strlen(s); | |
60 | |
61 if ((v = realloc(*base_p, base_len + s_len + 1)) == NULL) | |
62 return -1; | |
63 | |
64 *base_p = v; | |
65 memcpy(*base_p + base_len, s, s_len + 1); | |
66 return 0; | |
67 } | |
68 | |
69 void * | |
70 reallocarray(void *buf, size_t len, size_t sz) | |
71 { | |
72 if (SIZE_MAX / len < sz) | |
73 return errno=ERANGE, NULL; | |
74 return realloc(buf, len * sz); | |
75 } |