xref: /aosp_15_r20/external/selinux/restorecond/stringslist.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
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 <[email protected]>
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 	if (!newptr->string)
52 		exitApp("Out of Memory");
53 	newptr->next = ptr;
54 	if (prev)
55 		prev->next = newptr;
56 	else
57 		*list = newptr;
58 }
59 
strings_list_find(struct stringsList * ptr,const char * string,int * exact)60 int strings_list_find(struct stringsList *ptr, const char *string, int *exact)
61 {
62 	while (ptr) {
63 		*exact = strcmp(ptr->string, string) == 0;
64 		int cmp = fnmatch(ptr->string, string, 0);
65 		if (cmp == 0)
66 			return 0;	/* Match found */
67 		ptr = ptr->next;
68 	}
69 	return -1;
70 }
71 
strings_list_free(struct stringsList * ptr)72 void strings_list_free(struct stringsList *ptr)
73 {
74 	struct stringsList *prev = NULL;
75 	while (ptr) {
76 		free(ptr->string);
77 		prev = ptr;
78 		ptr = ptr->next;
79 		free(prev);
80 	}
81 }
82 
strings_list_diff(struct stringsList * from,struct stringsList * to)83 int strings_list_diff(struct stringsList *from, struct stringsList *to)
84 {
85 	while (from != NULL && to != NULL) {
86 		if (strcmp(from->string, to->string) != 0)
87 			return 1;
88 		from = from->next;
89 		to = to->next;
90 	}
91 	if (from != NULL || to != NULL)
92 		return 1;
93 	return 0;
94 }
95 
strings_list_print(struct stringsList * ptr)96 void strings_list_print(struct stringsList *ptr)
97 {
98 	while (ptr) {
99 		printf("%s\n", ptr->string);
100 		ptr = ptr->next;
101 	}
102 }
103 
104 #ifdef TEST
exitApp(const char * msg)105 void exitApp(const char *msg)
106 {
107 	perror(msg);
108 	exit(-1);
109 }
110 
main(int argc,char ** argv)111 int main(int argc, char **argv)
112 {
113 	struct stringsList *list = NULL;
114 	struct stringsList *list1 = NULL;
115 	strings_list_add(&list, "/etc/resolv.conf");
116 	strings_list_add(&list, "/etc/walsh");
117 	strings_list_add(&list, "/etc/mtab");
118 	strings_list_add(&list, "/etc/walsh");
119 	if (strings_list_diff(list, list) != 0)
120 		printf("strings_list_diff test1 bug\n");
121 	strings_list_add(&list1, "/etc/walsh");
122 	if (strings_list_diff(list, list1) == 0)
123 		printf("strings_list_diff test2 bug\n");
124 	strings_list_add(&list1, "/etc/walsh");
125 	strings_list_add(&list1, "/etc/walsh/*");
126 	strings_list_add(&list1, "/etc/resolv.conf");
127 	strings_list_add(&list1, "/etc/mtab1");
128 	if (strings_list_diff(list, list1) == 0)
129 		printf("strings_list_diff test3 bug\n");
130 	printf("strings list\n");
131 	strings_list_print(list);
132 	printf("strings list1\n");
133 	strings_list_find(list1, "/etc/walsh/dan");
134 	strings_list_print(list1);
135 	strings_list_free(list);
136 	strings_list_free(list1);
137 }
138 #endif
139