source: libabac/abac_util.c @ b8a6c918

abac0-leak
Last change on this file since b8a6c918 was 44c8505, checked in by Ted Faber <faber@…>, 11 years ago

Make chunk operations take pointers.

  • Property mode set to 100644
File size: 3.5 KB
Line 
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 */
16void *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
26void 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/*
34abac_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
47abac_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
61int abac_chunk_null(abac_chunk_t *chunk)
62{
63    if(chunk->ptr==NULL) return 1;
64    return 0;
65}
66
67int abac_chunk_show(abac_chunk_t *chunk)
68{
69    if(chunk->ptr==NULL)
70        fprintf(stderr,"abac_chunk_show: chunk is NULL\n");
71    fprintf(stderr,"abac_chunk_show: chunk is not NULL (%ld)\n",(long)chunk->ptr);
72}
73
74/**
75 * strdup fatal on error
76 */
77char *abac_xstrdup(char *source) {
78    char *ret;
79
80    if (source == NULL)
81        return NULL;
82
83    ret = strdup(source);
84    if (ret == NULL)
85        err(1, "strdup");
86
87    return ret;
88}
89
90void *abac_xrealloc(void *ptr, size_t size) {
91    void *ret = realloc(ptr, size);
92    if (ret == NULL)
93        err(1, "couldn't realloc %zu bytes\n", size);
94    return ret;
95}
96
97/**
98 * Split a string based on the given delimiter.  The substrings are pointers
99 * into string, which has string terminators added to slice it into substrings.
100 * Do not free the returned substrings.  num passes in the maximum number of
101 * substrings to create and is returned as the number actually created.
102 */
103#define MAXSPLIT    1024
104void abac_split(char *string, char *delim, char **ret, int *num) {
105    int len = strlen(delim);
106    char *start = string;
107    int count = 0;
108    int lim = (num && *num > 0) ? *num : MAXSPLIT;
109
110    // split the string by the delim.  Split into at most lim parts
111    while ((start = strstr(string, delim)) != NULL && count < lim-1) {
112        *start = 0;
113        ret[count++] = string;
114        string = start + len;
115    }
116    ret[count++] = string;
117
118    *num = count;
119}
120
121int abac_clean_name(char *string) {
122    int i;
123
124    assert(string != NULL);
125
126    // must start with a letter/number
127    if (!isalnum(string[0])) return 0;
128
129    // Name must be alphanumeric or - or _ or :
130    for (i = 1; string[i] != '\0'; ++i)
131        if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_' &&
132                string[i] != ':')
133            return 0;
134
135    return 1;
136}
137
Note: See TracBrowser for help on using the repository browser.