1 /*
2 * Copyright (C) 2006, 2008 Red Hat
3 * see file 'COPYING' for use and warranty information
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 .*
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 * 02111-1307 USA
19 *
20 * Authors:
21 * Dan Walsh <dwalsh@redhat.com>
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 /* Sorted lists */
strings_list_add(struct stringsList ** list,const char * string)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; /* Not on list break out to add */
42 if (cmp == 0)
43 return; /* Already on list */
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
strings_list_find(struct stringsList * ptr,const char * string,int * exact)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; /* Match found */
65 ptr = ptr->next;
66 }
67 return -1;
68 }
69
strings_list_free(struct stringsList * ptr)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
strings_list_diff(struct stringsList * from,struct stringsList * to)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
strings_list_print(struct stringsList * ptr)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
exitApp(const char * msg)103 void exitApp(const char *msg)
104 {
105 perror(msg);
106 exit(-1);
107 }
108
main(int argc,char ** argv)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
137