1 /* Author: Mark Goldman   <mgoldman@tresys.com>
2  *
3  * Copyright (C) 2007 Tresys Technology, LLC
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library 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 GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 /* This file contains helper functions that are loosely based off of what is
21  * available from the python script genhomedircon.  Also this file contains
22  * c implementations of a couple of python functions so that genhomedircon will
23  * look/act like the python script.
24  */
25 #ifndef _SEMANAGE_UTILITIES_H_
26 #define _SEMANAGE_UTILITIES_H_
27 
28 #include <stdio.h>
29 
30 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
31 #define WARN_UNUSED \
32 	__attribute__ ((__warn_unused_result__))
33 #else
34 # define WARN_UNUSED		/* nothing */
35 #endif
36 
37 typedef struct list {
38 	char *data;
39 	struct list *next;
40 } semanage_list_t;
41 
42 /**
43  * @param file  the path to the file to look for a variable in
44  * @param var   the variable that you want the value of
45  * @param delim the value that separates the part you care about from the part
46  *	       that you don't.
47  * @return for the first instance of var in the file, returns everything after
48  *	   delim.
49  *	   returns "" if not found IE if(*(semanage_findval(f,v,d)) == '\0'){
50  *					  printf("%s not found in file", v);
51  *				       }
52  *
53  *	   NULL for error (out of memory, etc)
54  */
55 char *semanage_findval(const char *file, const char *var, const char *delim) WARN_UNUSED;
56 
57 /**
58  * @param str   string to test
59  * @param	 val   prefix
60  * @return  1 if val is the prefix of str
61  *	    0 if val is not the prefix of str
62  *
63  * note: if str == NULL, returns false
64  *	 if val == NULL, returns true --nothing can always be the prefix of
65  *				        something
66  *	 if (*val) == "" returns true same as above.
67  */
68 int semanage_is_prefix(const char *str, const char *val) WARN_UNUSED;
69 
70 /**
71  * @param str   the string to semanage_split
72  * @return     malloc'd string after the first run of charachters that aren't whitespace
73  */
74 char *semanage_split_on_space(const char *str) WARN_UNUSED;
75 
76 /**
77  * @param	 str   the string to semanage_split
78  * @param	 delim the string delimiter.  NOT a set of charachters that can be
79  *	       a delimiter.
80  *	       if *delim == '\0' behaves as semanage_splitOnSpace()
81  * @return   a ptr to the first charachter past the delimiter.
82  *	    if delim doesn't appear in the string, returns a ptr to the
83  *	    trailing null in the string
84  */
85 char *semanage_split(const char *str, const char *delim) WARN_UNUSED;
86 
87 /* linked list string functions
88  * Functions allocate memory.  Must be free'd with
89  * either semanage_list_pop until list == NULL or semanage_list_destroy()
90  */
91 int semanage_list_push(semanage_list_t ** list, const char *data) WARN_UNUSED;
92 char *semanage_list_pop(semanage_list_t ** list);
93 void semanage_list_destroy(semanage_list_t ** list);
94 semanage_list_t *semanage_list_find(semanage_list_t * l,
95 				    const char *data) WARN_UNUSED;
96 int semanage_list_sort(semanage_list_t ** l) WARN_UNUSED;
97 /* function to compare 2 semanage_list_t nodes,
98  * returns strcmp(x->data, y->data)
99  * used internally by semanage_list_sort()
100  */
101 int semanage_cmp_plist_t(const semanage_list_t ** x,
102 			 const semanage_list_t ** y);
103 /**
104  * @param      data a target string
105  * @param      what  a charachter
106  * @returns    the number of times the char appears in the string
107  */
108 int semanage_str_count(char *data, char what);
109 /**
110  * @param      - a string
111  * @param            the charachter to trim to
112  * @return   - mangles the string, converting the first
113  *             occurrance of the charachter to a '\0' from
114  *             the end of the string.
115  */
116 void semanage_rtrim(char *str, char trim_to);
117 
118 /**
119  * @param data    some string
120  * @return  modifies the string such that the first whitespace char becomes
121  *	    '\0', ending the string.
122  */
123 void semanage_keep_until_space(char *data);
124 
125 /**
126  * @param    file    - an open FILE to read from
127  * @param    pred    - a function taking a string that
128  *                    returns 1 if the string should be
129  *                    kept and 0 otherwise
130  * @return  a list of lines from the file (empty lines become
131  *          empty strings) in the file order where pred(line)
132  *          returns > 0
133  */
134 semanage_list_t *semanage_slurp_file_filter(FILE * file,
135 					    int (*pred) (const char *))
136     WARN_UNUSED;
137 #endif
138