mirror of
https://github.com/X11Libre/xserver.git
synced 2026-04-14 17:18:09 +00:00
Add xstrtokenize to the dix.
Move tokenize out of the parser, make it a dix util function instead. Splitting a string into multiple substrings is useful by other places, so let's use it across the line. Future users include config/hal, config/udev and of course the parser. Example usage: char **substrings = xstrtokenize(my_string, "\n"); Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Dan Nicholson <dbn.lists@gmail.com>
This commit is contained in:
40
os/utils.c
40
os/utils.c
@@ -1870,6 +1870,46 @@ CheckUserAuthorization(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Tokenize a string into a NULL terminated array of strings. Always returns
|
||||
* an allocated array unless an error occurs.
|
||||
*/
|
||||
char**
|
||||
xstrtokenize(const char *str, const char *separators)
|
||||
{
|
||||
char **list, **nlist;
|
||||
char *tok, *tmp;
|
||||
unsigned num = 0, n;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
list = calloc(1, sizeof(*list));
|
||||
if (!list)
|
||||
return NULL;
|
||||
tmp = strdup(str);
|
||||
if (!tmp)
|
||||
goto error;
|
||||
for (tok = strtok(tmp, separators); tok; tok = strtok(NULL, separators)) {
|
||||
nlist = realloc(list, (num + 2) * sizeof(*list));
|
||||
if (!nlist)
|
||||
goto error;
|
||||
list = nlist;
|
||||
list[num] = strdup(tok);
|
||||
if (!list[num])
|
||||
goto error;
|
||||
list[++num] = NULL;
|
||||
}
|
||||
free(tmp);
|
||||
return list;
|
||||
|
||||
error:
|
||||
free(tmp);
|
||||
for (n = 0; n < num; n++)
|
||||
free(list[n]);
|
||||
free(list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef __SCO__
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user