mirror of
https://github.com/topjohnwu/selinux.git
synced 2024-11-24 12:09:50 +00:00
libsemanage: simplify string utilities functions
Use string functions from C standard library instead of ustr. This makes the code simpler and make utilities.c no longer depend on ustr library. This changes how semanage_split() behaves when delim is not empty (NULL or "") and the input string contains several successive delimiters: semanage_split("foo::::bar", ":") returned "bar" and now returns ":bar". This would not have any impact in the current code as semanage_split() is only called with delim="=" (through semanage_findval(), in libsemanage/src/genhomedircon.c), in order to split a "key=value" statement. Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
parent
fd6bc593b8
commit
a228bb3736
@ -26,7 +26,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <ustr.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
@ -74,64 +73,32 @@ char *semanage_split_on_space(const char *str)
|
||||
{
|
||||
/* as per the man page, these are the isspace() chars */
|
||||
const char *seps = "\f\n\r\t\v ";
|
||||
size_t slen = strlen(seps);
|
||||
size_t off = 0, rside_len = 0;
|
||||
char *retval = NULL;
|
||||
Ustr *ustr = USTR_NULL, *temp = USTR_NULL;
|
||||
size_t off = 0;
|
||||
|
||||
if (!str)
|
||||
goto done;
|
||||
if (!(ustr = ustr_dup_cstr(str)))
|
||||
goto done;
|
||||
temp =
|
||||
ustr_split_spn_chrs(ustr, &off, seps, slen, USTR_NULL,
|
||||
USTR_FLAG_SPLIT_DEF);
|
||||
if (!temp)
|
||||
goto done;
|
||||
/* throw away the left hand side */
|
||||
ustr_sc_free(&temp);
|
||||
return NULL;
|
||||
|
||||
rside_len = ustr_len(ustr) - off;
|
||||
temp = ustr_dup_subustr(ustr, off + 1, rside_len);
|
||||
if (!temp)
|
||||
goto done;
|
||||
retval = strdup(ustr_cstr(temp));
|
||||
ustr_sc_free(&temp);
|
||||
|
||||
done:
|
||||
ustr_sc_free(&ustr);
|
||||
return retval;
|
||||
/* skip one token and the spaces before and after it */
|
||||
off = strspn(str, seps);
|
||||
off += strcspn(str + off, seps);
|
||||
off += strspn(str + off, seps);
|
||||
return strdup(str + off);
|
||||
}
|
||||
|
||||
char *semanage_split(const char *str, const char *delim)
|
||||
{
|
||||
Ustr *ustr = USTR_NULL, *temp = USTR_NULL;
|
||||
size_t off = 0, rside_len = 0;
|
||||
char *retval = NULL;
|
||||
char *retval;
|
||||
|
||||
if (!str)
|
||||
goto done;
|
||||
return NULL;
|
||||
if (!delim || !(*delim))
|
||||
return semanage_split_on_space(str);
|
||||
ustr = ustr_dup_cstr(str);
|
||||
temp =
|
||||
ustr_split_cstr(ustr, &off, delim, USTR_NULL, USTR_FLAG_SPLIT_DEF);
|
||||
if (!temp)
|
||||
goto done;
|
||||
/* throw away the left hand side */
|
||||
ustr_sc_free(&temp);
|
||||
|
||||
rside_len = ustr_len(ustr) - off;
|
||||
retval = strstr(str, delim);
|
||||
if (retval == NULL)
|
||||
return NULL;
|
||||
|
||||
temp = ustr_dup_subustr(ustr, off + 1, rside_len);
|
||||
if (!temp)
|
||||
goto done;
|
||||
retval = strdup(ustr_cstr(temp));
|
||||
ustr_sc_free(&temp);
|
||||
|
||||
done:
|
||||
ustr_sc_free(&ustr);
|
||||
return retval;
|
||||
return strdup(retval + strlen(delim));
|
||||
}
|
||||
|
||||
int semanage_list_push(semanage_list_t ** list, const char *data)
|
||||
|
Loading…
Reference in New Issue
Block a user