/[vps]/baselayout-vserver/trunk/src/core/librcscripts/misc.h
Gentoo

Contents of /baselayout-vserver/trunk/src/core/librcscripts/misc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 127 - (show annotations) (download) (as text)
Fri Dec 2 10:56:06 2005 UTC (12 years, 9 months ago) by phreak
File MIME type: text/x-chdr
File size: 9873 byte(s)
Import the latest baselayout changes. Merging revision 1658.
1 /*
2 * misc.h
3 *
4 * Miscellaneous macro's and functions.
5 *
6 * Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * $Header$
23 */
24
25 #ifndef _MISC_H
26 #define _MISC_H
27
28 #include <sys/stat.h>
29 #include <sys/types.h>
30
31 /* Gentoo style e* printing macro's */
32 #define EINFO(_args...) \
33 do { \
34 int old_errno = errno; \
35 printf(" \033[32;01m*\033[0m " _args); \
36 errno = old_errno; \
37 } while (0)
38
39 #define EWARN(_args...) \
40 do { \
41 int old_errno = errno; \
42 printf(" \033[33;01m*\033[0m " _args); \
43 errno = old_errno; \
44 } while (0)
45
46 #define EERROR(_args...) \
47 do { \
48 int old_errno = errno; \
49 fprintf(stderr, " \033[31;01m*\033[0m " _args); \
50 errno = old_errno; \
51 } while (0)
52
53 /* Min/Max macro's */
54 #ifdef MAX
55 # undef MAX
56 #endif
57 #define MAX(_a, _b) (((_a) > (_b)) ? (_a) : (_b))
58 #ifdef MIN
59 # undef MIN
60 #endif
61 #define MIN(_a, _b) ((_a) > (_b) ? (_b) : (_a))
62
63 /* Return true if filename '_name' ends in '_ext' */
64 #define CHECK_FILE_EXTENSION(_name, _ext) \
65 ((strlen(_name) > strlen(_ext)) \
66 && (0 == strncmp(&(_name[strlen(_name) - strlen(_ext)]), \
67 _ext, strlen(_ext))))
68
69 /* Add a new item to a string list. If the pointer to the list is NULL,
70 * allocate enough memory for the amount of entries needed. Ditto for
71 * when it already exists, but we add one more entry than it can
72 * contain. The list is NULL terminated.
73 * NOTE: _only_ memory for the list are allocated, and not for the items - that
74 * should be done by relevant code (unlike STRING_LIST_DEL that will
75 * free the memory) */
76 #define STRING_LIST_ADD(_string_list, _item, _error) \
77 do { \
78 char **_tmp_p; \
79 int _i = 0; \
80 if ((NULL == _item) || (0 == strlen(_item))) { \
81 DBG_MSG("Invalid argument passed!\n"); \
82 errno = EINVAL; \
83 goto _error; \
84 } \
85 while ((NULL != _string_list) && (NULL != _string_list[_i])) { \
86 _i++; \
87 } \
88 /* Amount of entries + new + terminator */ \
89 _tmp_p = realloc(_string_list, sizeof(char *) * (_i + 2)); \
90 if (NULL == _tmp_p) { \
91 DBG_MSG("Failed to reallocate list!\n"); \
92 goto _error; \
93 } \
94 _string_list = _tmp_p; \
95 _string_list[_i] = _item; \
96 /* Terminator */ \
97 _string_list[_i+1] = NULL; \
98 } while (0)
99
100 /* Add a new item to a string list (foundamental the same as above), but make
101 * sure we have all the items alphabetically sorted. */
102 #define STRING_LIST_ADD_SORT(_string_list, _item, _error) \
103 do { \
104 char **_tmp_p; \
105 char *_str_p1; \
106 char *_str_p2; \
107 int _i = 0; \
108 if ((NULL == _item) || (0 == strlen(_item))) { \
109 DBG_MSG("Invalid argument passed!\n"); \
110 errno = EINVAL; \
111 goto _error; \
112 } \
113 while ((NULL != _string_list) && (NULL != _string_list[_i])) \
114 _i++; \
115 /* Amount of entries + new + terminator */ \
116 _tmp_p = realloc(_string_list, sizeof(char *) * (_i + 2)); \
117 if (NULL == _tmp_p) { \
118 DBG_MSG("Failed to reallocate list!\n"); \
119 goto _error; \
120 } \
121 _string_list = _tmp_p; \
122 if (0 == _i) \
123 /* Needed so that the end NULL will propagate
124 * (iow, make sure our 'NULL != _str_p1' test below
125 * do not fail) */ \
126 _string_list[_i] = NULL; \
127 /* Actual terminator that needs adding */ \
128 _string_list[_i+1] = NULL; \
129 _i = 0; \
130 /* See where we should insert the new item to have it all \
131 * alphabetically sorted */ \
132 while (NULL != _string_list[_i]) { \
133 if (strcmp(_string_list[_i], _item) > 0) { \
134 break; \
135 } \
136 _i++; \
137 } \
138 /* Now just insert the new item, and shift the rest one over.
139 * '_str_p2' is temporary storage to swap the indexes in a loop,
140 * and 'str_p1' is used to store the old value across the loop */ \
141 _str_p1 = _string_list[_i]; \
142 _string_list[_i] = _item; \
143 do { \
144 _i++;\
145 _str_p2 = _string_list[_i]; \
146 _string_list[_i] = _str_p1; \
147 _str_p1 = _str_p2; \
148 } while (NULL != _str_p1); \
149 } while (0)
150
151 /* Delete one entry from the string list, and shift the rest down if the entry
152 * was not at the end. For now we do not resize the amount of entries the
153 * string list can contain, and free the memory for the matching item */
154 #define STRING_LIST_DEL(_string_list, _item, _error) \
155 do { \
156 int _i = 0; \
157 if ((NULL == _item) \
158 || (0 == strlen(_item)) \
159 || (NULL == _string_list)) { \
160 DBG_MSG("Invalid argument passed!\n"); \
161 errno = EINVAL; \
162 goto _error; \
163 } \
164 while (NULL != _string_list[_i]) { \
165 if (0 == strcmp(_item, _string_list[_i])) \
166 break; \
167 else \
168 _i++; \
169 } \
170 if (NULL == _string_list[_i]) { \
171 DBG_MSG("Invalid argument passed!\n"); \
172 errno = EINVAL; \
173 goto _error; \
174 } \
175 free(_string_list[_i]); \
176 /* Shift all the following items one forward */ \
177 do { \
178 _string_list[_i] = _string_list[_i+1]; \
179 /* This stupidity is to shutup gcc */ \
180 _i++; \
181 } while (NULL != _string_list[_i]); \
182 } while (0)
183
184 /* Step through each entry in the string list, setting '_pos' to the
185 * beginning of the entry. '_counter' is used by the macro as index,
186 * but should not be used by code as index (or if really needed, then
187 * it should usually by +1 from what you expect, and should only be
188 * used in the scope of the macro) */
189 #define STRING_LIST_FOR_EACH(_string_list, _pos, _counter) \
190 if ((NULL != _string_list) && (0 == (_counter = 0))) \
191 while (NULL != (_pos = _string_list[_counter++]))
192
193 /* Same as above (with the same warning about '_counter'). Now we just
194 * have '_next' that are also used for indexing. Once again rather refrain
195 * from using it if not absolutely needed. The major difference to above,
196 * is that it should be safe from having the item removed from under you. */
197 #define STRING_LIST_FOR_EACH_SAFE(_string_list, _pos, _next, _counter) \
198 if ((NULL != _string_list) \
199 && (0 == (_counter = 0))) \
200 /* First part of the while checks if this is the
201 * first loop, and if so setup _pos and _next
202 * and increment _counter */ \
203 while ((((0 == _counter) \
204 && (NULL != (_pos = _string_list[_counter])) \
205 && (_pos != (_next = _string_list[++_counter]))) \
206 /* Second part is when it is not the first loop
207 * and _pos was not removed from under us. We
208 * just increment _counter, and setup _pos and
209 * _next */ \
210 || ((0 != _counter) \
211 && (_pos == _string_list[_counter-1]) \
212 && (_next == _string_list[_counter]) \
213 && (NULL != (_pos = _string_list[_counter])) \
214 && (_pos != (_next = _string_list[++_counter]))) \
215 /* Last part is when _pos was removed from under
216 * us. We basically just setup _pos and _next,
217 * but leave _counter alone */ \
218 || ((0 != _counter) \
219 && (_pos != _string_list[_counter-1]) \
220 && (_next == _string_list[_counter-1]) \
221 && (NULL != (_pos = _string_list[_counter-1])) \
222 && (_pos != (_next = _string_list[_counter])))))
223
224 /* Just free the whole string list */
225 #define STRING_LIST_FREE(_string_list) \
226 do { \
227 if (NULL != _string_list) { \
228 int _i = 0; \
229 while (NULL != _string_list[_i]) \
230 free(_string_list[_i++]); \
231 free(_string_list); \
232 _string_list = NULL; \
233 } \
234 } while (0)
235
236 /* String functions. Return a string on success, or NULL on error
237 * or no action taken. On error errno will be set.*/
238 char *memrepchr(char **str, char old, char _new, size_t size);
239 /* Concat two paths adding '/' if needed. Memory will be allocated
240 * with the malloc() call. */
241 char *strcatpaths(const char *pathname1, const char *pathname2);
242
243 /* Compat functions for GNU extensions */
244 char *strndup(const char *str, size_t size);
245 /* Same as basename(3), but do not modify path */
246 char *gbasename(const char *path);
247
248 /* The following functions do not care about errors - they only return
249 * 1 if 'pathname' exist, and is the type requested, or else 0.
250 * They also might clear errno */
251 int exists(const char *pathname);
252 int is_file(const char *pathname, int follow_link);
253 int is_link(const char *pathname);
254 int is_dir(const char *pathname, int follow_link);
255
256 /* The following function do not care about errors - it only returns
257 * the mtime of 'pathname' if it exists, and is the type requested,
258 * or else 0. It also might clear errno */
259 time_t get_mtime(const char *pathname, int follow_link);
260
261 /* The following functions return 0 on success, or -1 with errno set on error. */
262 #ifdef __KLIBC__
263 int remove(const char *pathname);
264 #endif
265 int mktree(const char *pathname, mode_t mode);
266 int rmtree(const char *pathname);
267
268 /* The following return a pointer on success, or NULL with errno set on error.
269 * If it returned NULL, but errno is not set, then there was no error, but
270 * there is nothing to return. */
271 char **ls_dir(const char *pathname, int hidden);
272 char *get_cnf_entry(const char *pathname, const char *entry);
273
274 /* Below three functions (file_map, file_unmap and buf_get_line) are from
275 * udev-050 (udev_utils.c). Please see misc.c for copyright info.
276 * (Some are slightly modified, please check udev for originals.) */
277 int file_map(const char *filename, char **buf, size_t *bufsize);
278 void file_unmap(char *buf, size_t bufsize);
279 size_t buf_get_line(char *buf, size_t buflen, size_t cur);
280
281 #endif /* _MISC_H */
282

  ViewVC Help
Powered by ViewVC 1.1.20