diff --git a/os/meson.build b/os/meson.build index 00375d17c..67c7140ed 100644 --- a/os/meson.build +++ b/os/meson.build @@ -12,6 +12,7 @@ srcs_os = [ 'oscolor.c', 'osinit.c', 'ospoll.c', + 'string.c', 'utils.c', 'xdmauth.c', 'xsha1.c', diff --git a/os/string.c b/os/string.c new file mode 100644 index 000000000..f0e34e40b --- /dev/null +++ b/os/string.c @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: MIT OR X11 + * + * Copyright © 1987, 1998 The Open Group + * Copyright © 2024 Enrico Weigelt, metux IT consult + */ +#include + +#include +#include + +#include "os.h" + +char * +Xstrdup(const char *s) +{ + if (s == NULL) + return NULL; + return strdup(s); +} + +char * +XNFstrdup(const char *s) +{ + char *ret; + + if (s == NULL) + return NULL; + + ret = strdup(s); + if (!ret) + FatalError("XNFstrdup: Out of memory"); + return ret; +} + +/* + * 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 = reallocarray(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; +} diff --git a/os/utils.c b/os/utils.c index 467fc3b91..ba8ebf97a 100644 --- a/os/utils.c +++ b/os/utils.c @@ -1131,28 +1131,6 @@ set_font_authorizations(char **authorizations, int *authlen, void *client) #endif /* TCPCONN */ } -char * -Xstrdup(const char *s) -{ - if (s == NULL) - return NULL; - return strdup(s); -} - -char * -XNFstrdup(const char *s) -{ - char *ret; - - if (s == NULL) - return NULL; - - ret = strdup(s); - if (!ret) - FatalError("XNFstrdup: Out of memory"); - return ret; -} - void SmartScheduleStopTimer(void) { @@ -1916,46 +1894,6 @@ 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 = reallocarray(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; -} - /* Format a signed number into a string in a signal safe manner. The string * should be at least 21 characters in order to handle all int64_t values. */