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