| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include "stringslist.h" |
| 29 |
#include "restorecond.h" |
| 30 |
#include <fnmatch.h> |
| 31 |
|
| 32 |
|
| 33 |
void strings_list_add(struct stringsList **list, const char *string) |
| 34 |
{ |
| 35 |
struct stringsList *ptr = *list; |
| 36 |
struct stringsList *prev = NULL; |
| 37 |
struct stringsList *newptr = NULL; |
| 38 |
while (ptr) { |
| 39 |
int cmp = strcmp(string, ptr->string); |
| 40 |
if (cmp < 0) |
| 41 |
break; |
| 42 |
if (cmp == 0) |
| 43 |
return; |
| 44 |
prev = ptr; |
| 45 |
ptr = ptr->next; |
| 46 |
} |
| 47 |
newptr = calloc(1, sizeof(struct stringsList)); |
| 48 |
if (!newptr) |
| 49 |
exitApp("Out of Memory"); |
| 50 |
newptr->string = strdup(string); |
| 51 |
newptr->next = ptr; |
| 52 |
if (prev) |
| 53 |
prev->next = newptr; |
| 54 |
else |
| 55 |
*list = newptr; |
| 56 |
} |
| 57 |
|
| 58 |
int strings_list_find(struct stringsList *ptr, const char *string, int *exact) |
| 59 |
{ |
| 60 |
while (ptr) { |
| 61 |
*exact = strcmp(ptr->string, string) == 0; |
| 62 |
int cmp = fnmatch(ptr->string, string, 0); |
| 63 |
if (cmp == 0) |
| 64 |
return 0; |
| 65 |
ptr = ptr->next; |
| 66 |
} |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
void strings_list_free(struct stringsList *ptr) |
| 71 |
{ |
| 72 |
struct stringsList *prev = NULL; |
| 73 |
while (ptr) { |
| 74 |
free(ptr->string); |
| 75 |
prev = ptr; |
| 76 |
ptr = ptr->next; |
| 77 |
free(prev); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
int strings_list_diff(struct stringsList *from, struct stringsList *to) |
| 82 |
{ |
| 83 |
while (from != NULL && to != NULL) { |
| 84 |
if (strcmp(from->string, to->string) != 0) |
| 85 |
return 1; |
| 86 |
from = from->next; |
| 87 |
to = to->next; |
| 88 |
} |
| 89 |
if (from != NULL || to != NULL) |
| 90 |
return 1; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
void strings_list_print(struct stringsList *ptr) |
| 95 |
{ |
| 96 |
while (ptr) { |
| 97 |
printf("%s\n", ptr->string); |
| 98 |
ptr = ptr->next; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#ifdef TEST |
| 103 |
void exitApp(const char *msg) |
| 104 |
{ |
| 105 |
perror(msg); |
| 106 |
exit(-1); |
| 107 |
} |
| 108 |
|
| 109 |
int main(int argc, char **argv) |
| 110 |
{ |
| 111 |
struct stringsList *list = NULL; |
| 112 |
struct stringsList *list1 = NULL; |
| 113 |
strings_list_add(&list, "/etc/resolv.conf"); |
| 114 |
strings_list_add(&list, "/etc/walsh"); |
| 115 |
strings_list_add(&list, "/etc/mtab"); |
| 116 |
strings_list_add(&list, "/etc/walsh"); |
| 117 |
if (strings_list_diff(list, list) != 0) |
| 118 |
printf("strings_list_diff test1 bug\n"); |
| 119 |
strings_list_add(&list1, "/etc/walsh"); |
| 120 |
if (strings_list_diff(list, list1) == 0) |
| 121 |
printf("strings_list_diff test2 bug\n"); |
| 122 |
strings_list_add(&list1, "/etc/walsh"); |
| 123 |
strings_list_add(&list1, "/etc/walsh/*"); |
| 124 |
strings_list_add(&list1, "/etc/resolv.conf"); |
| 125 |
strings_list_add(&list1, "/etc/mtab1"); |
| 126 |
if (strings_list_diff(list, list1) == 0) |
| 127 |
printf("strings_list_diff test3 bug\n"); |
| 128 |
printf("strings list\n"); |
| 129 |
strings_list_print(list); |
| 130 |
printf("strings list1\n"); |
| 131 |
strings_list_find(list1, "/etc/walsh/dan"); |
| 132 |
strings_list_print(list1); |
| 133 |
strings_list_free(list); |
| 134 |
strings_list_free(list1); |
| 135 |
} |
| 136 |
#endif |