source: libabac/abac_util.c @ 756011e

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

Now we pass -Wall with no warnings.

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[461541a]1
2/* abac_util.c */
3
[7f25a67f]4#include <err.h>
5#include <stdlib.h>
6#include <string.h>
[461541a]7#include <assert.h>
8#include <ctype.h>
[c5720ad]9#include <stdio.h>
[7f25a67f]10
[f2622ee]11#include <abac_util.h>
12
[7f25a67f]13/**
14 * Malloc, fatal on error.
15 */
[3c251d0]16void *abac_xmalloc(size_t size) {
[7f25a67f]17    void *ret;
18   
19    ret = malloc(size);
20    if (ret == NULL)
21        err(1, "malloc");
22
23    return ret;
24}
25
[44c8505]26void abac_chunk_free(abac_chunk_t *chunk)
[f2622ee]27{
[44c8505]28   if(chunk->len)
29       free(chunk->ptr);
30   chunk->len=0;
31   chunk->ptr=NULL;
[f2622ee]32}
[44c8505]33/*
[8164e70]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}               
[44c8505]59*/
[8164e70]60
[44c8505]61int abac_chunk_null(abac_chunk_t *chunk)
[8164e70]62{
[44c8505]63    if(chunk->ptr==NULL) return 1;
[8164e70]64    return 0;
65}
66
[44c8505]67int abac_chunk_show(abac_chunk_t *chunk)
[c5720ad]68{
[756011e]69    if(chunk->ptr==NULL) {
[c5720ad]70        fprintf(stderr,"abac_chunk_show: chunk is NULL\n");
[756011e]71        return 1;
72    }
[44c8505]73    fprintf(stderr,"abac_chunk_show: chunk is not NULL (%ld)\n",(long)chunk->ptr);
[756011e]74    return 0;
[c5720ad]75}
76
[7f25a67f]77/**
78 * strdup fatal on error
79 */
[3c251d0]80char *abac_xstrdup(char *source) {
[7f25a67f]81    char *ret;
82
[ff3d104]83    if (source == NULL)
84        return NULL;
85
[7f25a67f]86    ret = strdup(source);
87    if (ret == NULL)
88        err(1, "strdup");
89
90    return ret;
91}
[9a411d7]92
[461541a]93void *abac_xrealloc(void *ptr, size_t size) {
94    void *ret = realloc(ptr, size);
95    if (ret == NULL)
[1bf0f03]96        err(1, "couldn't realloc %zu bytes\n", size);
[461541a]97    return ret;
98}
99
[9a411d7]100/**
[9f78e4c]101 * Split a string based on the given delimiter.  The substrings are pointers
102 * into string, which has string terminators added to slice it into substrings.
103 * Do not free the returned substrings.  num passes in the maximum number of
104 * substrings to create and is returned as the number actually created.
[9a411d7]105 */
[9f78e4c]106#define MAXSPLIT    1024
[9a411d7]107void abac_split(char *string, char *delim, char **ret, int *num) {
108    int len = strlen(delim);
109    char *start = string;
110    int count = 0;
[9f78e4c]111    int lim = (num && *num > 0) ? *num : MAXSPLIT;
[9a411d7]112
[9f78e4c]113    // split the string by the delim.  Split into at most lim parts
114    while ((start = strstr(string, delim)) != NULL && count < lim-1) {
[9a411d7]115        *start = 0;
116        ret[count++] = string;
117        string = start + len;
118    }
119    ret[count++] = string;
120
121    *num = count;
122}
[461541a]123
124int abac_clean_name(char *string) {
125    int i;
126
127    assert(string != NULL);
128
129    // must start with a letter/number
130    if (!isalnum(string[0])) return 0;
131
132    // Name must be alphanumeric or - or _ or :
133    for (i = 1; string[i] != '\0'; ++i)
134        if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_' &&
135                string[i] != ':')
136            return 0;
137
138    return 1;
139}
140
Note: See TracBrowser for help on using the repository browser.