1 | |
---|
2 | /* abac_util.c */ |
---|
3 | |
---|
4 | #include <err.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <string.h> |
---|
7 | #include <assert.h> |
---|
8 | #include <ctype.h> |
---|
9 | |
---|
10 | #include <abac_util.h> |
---|
11 | |
---|
12 | /** |
---|
13 | * Malloc, fatal on error. |
---|
14 | */ |
---|
15 | void *abac_xmalloc(size_t size) { |
---|
16 | void *ret; |
---|
17 | |
---|
18 | ret = malloc(size); |
---|
19 | if (ret == NULL) |
---|
20 | err(1, "malloc"); |
---|
21 | |
---|
22 | return ret; |
---|
23 | } |
---|
24 | |
---|
25 | void abac_chunk_free(abac_chunk_t chunk) |
---|
26 | { |
---|
27 | if(chunk.len) |
---|
28 | free(chunk.ptr); |
---|
29 | chunk.len=0; |
---|
30 | chunk.ptr=NULL; |
---|
31 | } |
---|
32 | |
---|
33 | abac_chunk_t abac_chunk_dup(char *ptr, int len) |
---|
34 | { |
---|
35 | abac_chunk_t chunk; |
---|
36 | if(len!=0) { |
---|
37 | chunk.ptr=(unsigned char *) abac_xstrdup(ptr); |
---|
38 | chunk.len=len; |
---|
39 | } else { |
---|
40 | chunk.ptr=NULL; |
---|
41 | chunk.len=0; |
---|
42 | } |
---|
43 | return chunk; |
---|
44 | } |
---|
45 | |
---|
46 | abac_chunk_t abac_chunk_new(char *ptr, int len) |
---|
47 | { |
---|
48 | abac_chunk_t chunk; |
---|
49 | if(len!=0) { |
---|
50 | chunk.ptr=(unsigned char *) ptr; |
---|
51 | chunk.len=len; |
---|
52 | } else { |
---|
53 | chunk.ptr=NULL; |
---|
54 | chunk.len=0; |
---|
55 | } |
---|
56 | return chunk; |
---|
57 | } |
---|
58 | |
---|
59 | int abac_chunk_null(abac_chunk_t chunk) |
---|
60 | { |
---|
61 | if(chunk.ptr==NULL) return 1; |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * strdup fatal on error |
---|
67 | */ |
---|
68 | char *abac_xstrdup(char *source) { |
---|
69 | char *ret; |
---|
70 | |
---|
71 | if (source == NULL) |
---|
72 | return NULL; |
---|
73 | |
---|
74 | ret = strdup(source); |
---|
75 | if (ret == NULL) |
---|
76 | err(1, "strdup"); |
---|
77 | |
---|
78 | return ret; |
---|
79 | } |
---|
80 | |
---|
81 | void *abac_xrealloc(void *ptr, size_t size) { |
---|
82 | void *ret = realloc(ptr, size); |
---|
83 | if (ret == NULL) |
---|
84 | err(1, "couldn't realloc %zu bytes\n", size); |
---|
85 | return ret; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Split a string based on the given delimiter. The substrings are pointers |
---|
90 | * into string, which has string terminators added to slice it into substrings. |
---|
91 | * Do not free the returned substrings. num passes in the maximum number of |
---|
92 | * substrings to create and is returned as the number actually created. |
---|
93 | */ |
---|
94 | #define MAXSPLIT 1024 |
---|
95 | void abac_split(char *string, char *delim, char **ret, int *num) { |
---|
96 | int len = strlen(delim); |
---|
97 | char *start = string; |
---|
98 | int count = 0; |
---|
99 | int lim = (num && *num > 0) ? *num : MAXSPLIT; |
---|
100 | |
---|
101 | // split the string by the delim. Split into at most lim parts |
---|
102 | while ((start = strstr(string, delim)) != NULL && count < lim-1) { |
---|
103 | *start = 0; |
---|
104 | ret[count++] = string; |
---|
105 | string = start + len; |
---|
106 | } |
---|
107 | ret[count++] = string; |
---|
108 | |
---|
109 | *num = count; |
---|
110 | } |
---|
111 | |
---|
112 | int abac_clean_name(char *string) { |
---|
113 | int i; |
---|
114 | |
---|
115 | assert(string != NULL); |
---|
116 | |
---|
117 | // must start with a letter/number |
---|
118 | if (!isalnum(string[0])) return 0; |
---|
119 | |
---|
120 | // Name must be alphanumeric or - or _ or : |
---|
121 | for (i = 1; string[i] != '\0'; ++i) |
---|
122 | if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_' && |
---|
123 | string[i] != ':') |
---|
124 | return 0; |
---|
125 | |
---|
126 | return 1; |
---|
127 | } |
---|
128 | |
---|