os: move string functions to separate source file

Unclutter the huge utils.c a bit, by moving out string functions to
their own source file.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1336>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-02-27 18:01:17 +01:00 committed by Marge Bot
parent 3e9ac852bc
commit 22a67f7818
3 changed files with 74 additions and 62 deletions

View File

@ -12,6 +12,7 @@ srcs_os = [
'oscolor.c',
'osinit.c',
'ospoll.c',
'string.c',
'utils.c',
'xdmauth.c',
'xsha1.c',

73
os/string.c Normal file
View File

@ -0,0 +1,73 @@
/* SPDX-License-Identifier: MIT OR X11
*
* Copyright © 1987, 1998 The Open Group
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
*/
#include <dix-config.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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.
*/