libroot: Replace getsubopt implementation with musl's.

It looks to be a bit more optimized, and one less thing for
us to have to maintain from POSIX.
This commit is contained in:
Augustin Cavalier 2022-06-09 16:06:56 -04:00
parent 092b6d4a98
commit 22b1d791b2
4 changed files with 24 additions and 40 deletions

View File

@ -12,6 +12,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
MergeObject <$(architecture)>posix_musl_misc.o :
a64l.c
ffs.c
getsubopt.c
;
}
}

View File

@ -0,0 +1,23 @@
#include <stdlib.h>
#include <string.h>
int getsubopt(char **opt, char *const *keys, char **val)
{
char *s = *opt;
int i;
*val = NULL;
*opt = strchr(s, ',');
if (*opt) *(*opt)++ = 0;
else *opt = s + strlen(s);
for (i=0; keys[i]; i++) {
size_t l = strlen(keys[i]);
if (strncmp(keys[i], s, l)) continue;
if (s[l] == '=')
*val = s + l + 1;
else if (s[l]) continue;
return i;
}
return -1;
}

View File

@ -21,7 +21,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
div.c
env.cpp
exit.cpp
getsubopt.cpp
heapsort.c
merge.c
mktemp.c

View File

@ -1,39 +0,0 @@
/*
* Copyright 2010, Oliver Tappe <zooey@hirschkaefer.de>.
* Distributed under the terms of the MIT License.
*/
#include <stdlib.h>
#include <string.h>
int
getsubopt(char** optionPtr, char* const* keys, char** valuePtr)
{
if (optionPtr == NULL || valuePtr == NULL)
return -1;
char* option = *optionPtr;
if (*option == '\0')
return -1;
char* startOfNextOption = strchr(option, ',');
if (startOfNextOption != NULL)
*startOfNextOption++ = '\0';
else
startOfNextOption = option + strlen(option);
*optionPtr = startOfNextOption;
char* startOfValue = strchr(option, '=');
if (startOfValue != NULL)
*startOfValue++ = '\0';
*valuePtr = startOfValue;
for (int keyIndex = 0; keys[keyIndex] != NULL; ++keyIndex) {
if (strcmp(option, keys[keyIndex]) == 0)
return keyIndex;
}
return -1;
}