| 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 |
printf(" \033[31;01m*\033[0m " _args); \
|
| 50 |
errno = old_errno; \
|
| 51 |
} while (0)
|
| 52 |
|
| 53 |
/* Return true if filename '_name' ends in '_ext' */
|
| 54 |
#define CHECK_FILE_EXTENSION(_name, _ext) \
|
| 55 |
(strlen(_name) > strlen(_ext) && \
|
| 56 |
0 == strncmp(&_name[strlen(_name) - strlen(_ext)], \
|
| 57 |
_ext, strlen(_ext)))
|
| 58 |
|
| 59 |
/* For each '_char' in '_string', inc '_count' */
|
| 60 |
#define COUNT_CHAR_UP(_string, _char, _count) \
|
| 61 |
do { \
|
| 62 |
int _i; \
|
| 63 |
for (_i = 0;_i < strlen(_string);_i++) \
|
| 64 |
if (_string[_i] == _char) \
|
| 65 |
_count++; \
|
| 66 |
} while (0)
|
| 67 |
|
| 68 |
/* For each '_char' in '_string', dec '_count' */
|
| 69 |
#define COUNT_CHAR_DN(_string, _char, _count) \
|
| 70 |
do { \
|
| 71 |
int _i; \
|
| 72 |
for (_i = 0;_i < strlen(_string);_i++) \
|
| 73 |
if (_string[_i] == _char) \
|
| 74 |
_count--; \
|
| 75 |
} while (0)
|
| 76 |
|
| 77 |
/* Add a new item to a string list. If the pointer to the list is NULL,
|
| 78 |
* allocate enough memory for the amount of entries needed. Ditto for
|
| 79 |
* when it already exists, but we add one more entry than it can
|
| 80 |
* contain. The list is NULL terminated.
|
| 81 |
* NOTE: _only_ memory for the list are allocated, and not for the items - that
|
| 82 |
* should be done by relevant code (unlike STRING_LIST_DEL that will
|
| 83 |
* free the memory) */
|
| 84 |
#define STRING_LIST_ADD(_string_list, _item, _error) \
|
| 85 |
do { \
|
| 86 |
char **_tmp_p; \
|
| 87 |
int _i = 0; \
|
| 88 |
if ((NULL == _item) || (0 == strlen(_item))) { \
|
| 89 |
DBG_MSG("Invalid argument passed!\n"); \
|
| 90 |
errno = EINVAL; \
|
| 91 |
goto _error; \
|
| 92 |
} \
|
| 93 |
while ((NULL != _string_list) && (NULL != _string_list[_i])) { \
|
| 94 |
_i++; \
|
| 95 |
} \
|
| 96 |
/* Amount of entries + new + terminator */ \
|
| 97 |
_tmp_p = realloc(_string_list, sizeof(char *) * (_i + 2)); \
|
| 98 |
if (NULL == _tmp_p) { \
|
| 99 |
DBG_MSG("Failed to reallocate list!\n"); \
|
| 100 |
goto _error; \
|
| 101 |
} \
|
| 102 |
_string_list = _tmp_p; \
|
| 103 |
_string_list[_i] = _item; \
|
| 104 |
/* Terminator */ \
|
| 105 |
_string_list[_i+1] = NULL; \
|
| 106 |
} while (0)
|
| 107 |
|
| 108 |
/* Add a new item to a string list (foundamental the same as above), but make
|
| 109 |
* sure we have all the items alphabetically sorted. */
|
| 110 |
#define STRING_LIST_ADD_SORT(_string_list, _item, _error) \
|
| 111 |
do { \
|
| 112 |
char **_tmp_p; \
|
| 113 |
char *_str_p1; \
|
| 114 |
char *_str_p2; \
|
| 115 |
int _i = 0; \
|
| 116 |
if ((NULL == _item) || (0 == strlen(_item))) { \
|
| 117 |
DBG_MSG("Invalid argument passed!\n"); \
|
| 118 |
errno = EINVAL; \
|
| 119 |
goto _error; \
|
| 120 |
} \
|
| 121 |
while ((NULL != _string_list) && (NULL != _string_list[_i])) \
|
| 122 |
_i++; \
|
| 123 |
/* Amount of entries + new + terminator */ \
|
| 124 |
_tmp_p = realloc(_string_list, sizeof(char *) * (_i + 2)); \
|
| 125 |
if (NULL == _tmp_p) { \
|
| 126 |
DBG_MSG("Failed to reallocate list!\n"); \
|
| 127 |
goto _error; \
|
| 128 |
} \
|
| 129 |
_string_list = _tmp_p; \
|
| 130 |
if (0 == _i) \
|
| 131 |
/* Needed so that the end NULL will propagate
|
| 132 |
* (iow, make sure our 'NULL != _str_p1' test below
|
| 133 |
* do not fail) */ \
|
| 134 |
_string_list[_i] = NULL; \
|
| 135 |
/* Actual terminator that needs adding */ \
|
| 136 |
_string_list[_i+1] = NULL; \
|
| 137 |
_i = 0; \
|
| 138 |
/* See where we should insert the new item to have it all \
|
| 139 |
* alphabetically sorted */ \
|
| 140 |
while (NULL != _string_list[_i]) { \
|
| 141 |
if (strcmp(_string_list[_i], _item) > 0) { \
|
| 142 |
break; \
|
| 143 |
} \
|
| 144 |
_i++; \
|
| 145 |
} \
|
| 146 |
/* Now just insert the new item, and shift the rest one over.
|
| 147 |
* '_str_p2' is temporary storage to swap the indexes in a loop,
|
| 148 |
* and 'str_p1' is used to store the old value across the loop */ \
|
| 149 |
_str_p1 = _string_list[_i]; \
|
| 150 |
_string_list[_i] = _item; \
|
| 151 |
do { \
|
| 152 |
_i++;\
|
| 153 |
_str_p2 = _string_list[_i]; \
|
| 154 |
_string_list[_i] = _str_p1; \
|
| 155 |
_str_p1 = _str_p2; \
|
| 156 |
} while (NULL != _str_p1); \
|
| 157 |
} while (0)
|
| 158 |
|
| 159 |
/* Delete one entry from the string list, and shift the rest down if the entry
|
| 160 |
* was not at the end. For now we do not resize the amount of entries the
|
| 161 |
* string list can contain, and free the memory for the matching item */
|
| 162 |
#define STRING_LIST_DEL(_string_list, _item, _error) \
|
| 163 |
do { \
|
| 164 |
int _i = 0; \
|
| 165 |
if ((NULL == _item) || (0 == strlen(_item)) || \
|
| 166 |
(NULL == _string_list)) { \
|
| 167 |
DBG_MSG("Invalid argument passed!\n"); \
|
| 168 |
errno = EINVAL; \
|
| 169 |
goto _error; \
|
| 170 |
} \
|
| 171 |
while (NULL != _string_list[_i]) { \
|
| 172 |
if (0 == strcmp(_item, _string_list[_i])) \
|
| 173 |
break; \
|
| 174 |
else \
|
| 175 |
_i++; \
|
| 176 |
} \
|
| 177 |
if (NULL == _string_list[_i]) { \
|
| 178 |
DBG_MSG("Invalid argument passed!\n"); \
|
| 179 |
errno = EINVAL; \
|
| 180 |
goto _error; \
|
| 181 |
} \
|
| 182 |
free(_string_list[_i]); \
|
| 183 |
/* Shift all the following items one forward */ \
|
| 184 |
do { \
|
| 185 |
_string_list[_i] = _string_list[_i+1]; \
|
| 186 |
/* This stupidity is to shutup gcc */ \
|
| 187 |
_i++; \
|
| 188 |
} while (NULL != _string_list[_i]); \
|
| 189 |
} while (0)
|
| 190 |
|
| 191 |
/* Step through each entry in the string list, setting '_pos' to the
|
| 192 |
* beginning of the entry. '_counter' is used by the macro as index,
|
| 193 |
* but should not be used by code as index (or if really needed, then
|
| 194 |
* it should usually by +1 from what you expect, and should only be
|
| 195 |
* used in the scope of the macro) */
|
| 196 |
#define STRING_LIST_FOR_EACH(_string_list, _pos, _counter) \
|
| 197 |
if ((NULL != _string_list) && (0 == (_counter = 0))) \
|
| 198 |
while (NULL != (_pos = _string_list[_counter++]))
|
| 199 |
|
| 200 |
/* Same as above (with the same warning about '_counter'). Now we just
|
| 201 |
* have '_next' that are also used for indexing. Once again rather refrain
|
| 202 |
* from using it if not absolutely needed. The major difference to above,
|
| 203 |
* is that it should be safe from having the item removed from under you. */
|
| 204 |
#define STRING_LIST_FOR_EACH_SAFE(_string_list, _pos, _next, _counter) \
|
| 205 |
if ((NULL != _string_list) && (0 == (_counter = 0))) \
|
| 206 |
/* Just set both '_pos' and '_next', and make sure _pos
|
| 207 |
* at least are not NULL */ \
|
| 208 |
while ((NULL != (_pos = _string_list[_counter])) && \
|
| 209 |
(_pos != (_next = _string_list[_counter+1]))) \
|
| 210 |
/* This is plain ugly. The basic idea is to
|
| 211 |
* first check for NULL, and that things have
|
| 212 |
* not changed from under us */ \
|
| 213 |
while (((NULL != _string_list[_counter]) && \
|
| 214 |
(_next == _string_list[++_counter])) || \
|
| 215 |
/* If things have changed, once again verify
|
| 216 |
* the absence of NULL, and then see how many
|
| 217 |
* times we should shift back. Not that safe,
|
| 218 |
* but we should at least be able to backup
|
| 219 |
* once, which should be more than enough with
|
| 220 |
* the code I use it in. */ \
|
| 221 |
((NULL != _next) && \
|
| 222 |
(_next == _string_list[--_counter-1]) && \
|
| 223 |
(-1 == (_counter--))))
|
| 224 |
|
| 225 |
/* Just free the whole string list */
|
| 226 |
#define STRING_LIST_FREE(_string_list) \
|
| 227 |
do { \
|
| 228 |
if (NULL != _string_list) { \
|
| 229 |
int _i = 0; \
|
| 230 |
while (NULL != _string_list[_i]) \
|
| 231 |
free(_string_list[_i++]); \
|
| 232 |
free(_string_list); \
|
| 233 |
_string_list = NULL; \
|
| 234 |
} \
|
| 235 |
} while (0)
|
| 236 |
|
| 237 |
/* String functions. Return a string on success, or NULL on error
|
| 238 |
* or no action taken. On error errno will be set.*/
|
| 239 |
char *memrepchr(char **str, char old, char _new, size_t size);
|
| 240 |
/* Concat two paths adding '/' if needed. Memory will be allocated
|
| 241 |
* with the malloc() call. */
|
| 242 |
char *strcatpaths(const char *pathname1, const char *pathname2);
|
| 243 |
|
| 244 |
/* Compat functions for GNU extensions */
|
| 245 |
char *strndup(const char *str, size_t size);
|
| 246 |
/* Same as basename(3), but do not modify path */
|
| 247 |
char *gbasename(const char *path);
|
| 248 |
|
| 249 |
/* The following functions do not care about errors - they only return
|
| 250 |
* 1 if 'pathname' exist, and is the type requested, or else 0.
|
| 251 |
* They also might clear errno */
|
| 252 |
int exists(const char *pathname);
|
| 253 |
int is_file(const char *pathname, int follow_link);
|
| 254 |
int is_link(const char *pathname);
|
| 255 |
int is_dir(const char *pathname, int follow_link);
|
| 256 |
|
| 257 |
/* The following function do not care about errors - it only returns
|
| 258 |
* the mtime of 'pathname' if it exists, and is the type requested,
|
| 259 |
* or else 0. It also might clear errno */
|
| 260 |
time_t get_mtime(const char *pathname, int follow_link);
|
| 261 |
|
| 262 |
/* The following functions return 0 on success, or -1 with errno set on error. */
|
| 263 |
int mktree(const char *pathname, mode_t mode);
|
| 264 |
int rmtree(const char *pathname);
|
| 265 |
|
| 266 |
/* The following return a pointer on success, or NULL with errno set on error.
|
| 267 |
* If it returned NULL, but errno is not set, then there was no error, but
|
| 268 |
* there is nothing to return. */
|
| 269 |
char **ls_dir(const char *pathname, int hidden);
|
| 270 |
char *get_cnf_entry(const char *pathname, const char *entry);
|
| 271 |
|
| 272 |
/* Below three functions (file_map, file_unmap and buf_get_line) are from
|
| 273 |
* udev-050 (udev_utils.c). Please see misc.c for copyright info.
|
| 274 |
* (Some are slightly modified, please check udev for originals.) */
|
| 275 |
int file_map(const char *filename, char **buf, size_t *bufsize);
|
| 276 |
void file_unmap(char *buf, size_t bufsize);
|
| 277 |
size_t buf_get_line(char *buf, size_t buflen, size_t cur);
|
| 278 |
|
| 279 |
#endif /* _MISC_H */
|
| 280 |
|