source: libabac/abac_util.c @ 9937351

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since 9937351 was 7727f26, checked in by Mei <mei@…>, 12 years ago

1) add environment variables DUMP_DB, ABAC_CN.

ABAC_CN will switch to using CNs for keyid insead of SHAs

2) add/modified couple of doc files.

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <err.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdio.h>
5
6static int debug=0;
7
8/**
9 * Malloc, fatal on error.
10 */
11void *abac_xmalloc(size_t size) {
12    void *ret;
13   
14    ret = malloc(size);
15    if (ret == NULL)
16        err(1, "malloc");
17
18    return ret;
19}
20
21/**
22 * strdup fatal on error
23 */
24char *abac_xstrdup(char *source) {
25    char *ret;
26
27    if (source == NULL) {
28        if(debug) printf("BAD -> duping null string..\n");
29        return NULL;
30    }
31
32    ret = strdup(source);
33    if (ret == NULL)
34        err(1, "strdup");
35
36    return ret;
37}
38
39/* make a brand new string with or without being trimmed */
40char *abac_trim_quotes(char *string) {
41    char *tmp=abac_xstrdup(string);
42    int len=strlen(tmp);
43    if( (tmp[len-1] == '\'' || tmp[len-1] == '"') &&
44                       (tmp[0] == '\'' || tmp[0] == '"')) {
45       tmp[len-1]='\0';
46       char *ntmp=abac_xstrdup(&tmp[1]);
47       free(tmp);
48       return ntmp;
49    }
50    return tmp;
51}
52
53/**
54 * Split a string based on the given delimiter.
55 */
56void abac_split(char *string, char *delim, char **ret, int *num) {
57    int len = strlen(delim);
58    char *start = string;
59    int count = 0;
60
61    // split the string by the delim
62    while ((start = strstr(string, delim)) != NULL) {
63        *start = 0;
64        ret[count++] = string;
65        string = start + len;
66    }
67    ret[count++] = string;
68
69    *num = count;
70}
Note: See TracBrowser for help on using the repository browser.