From 22b1d791b2aa81e134295b60a2471fb621793c37 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 9 Jun 2022 16:06:56 -0400 Subject: [PATCH] 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. --- src/system/libroot/posix/musl/misc/Jamfile | 1 + .../libroot/posix/musl/misc/getsubopt.c | 23 +++++++++++ src/system/libroot/posix/stdlib/Jamfile | 1 - src/system/libroot/posix/stdlib/getsubopt.cpp | 39 ------------------- 4 files changed, 24 insertions(+), 40 deletions(-) create mode 100644 src/system/libroot/posix/musl/misc/getsubopt.c delete mode 100644 src/system/libroot/posix/stdlib/getsubopt.cpp diff --git a/src/system/libroot/posix/musl/misc/Jamfile b/src/system/libroot/posix/musl/misc/Jamfile index 739adabb61..e8e6c29e8b 100644 --- a/src/system/libroot/posix/musl/misc/Jamfile +++ b/src/system/libroot/posix/musl/misc/Jamfile @@ -12,6 +12,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { MergeObject <$(architecture)>posix_musl_misc.o : a64l.c ffs.c + getsubopt.c ; } } diff --git a/src/system/libroot/posix/musl/misc/getsubopt.c b/src/system/libroot/posix/musl/misc/getsubopt.c new file mode 100644 index 0000000000..53ee9573f2 --- /dev/null +++ b/src/system/libroot/posix/musl/misc/getsubopt.c @@ -0,0 +1,23 @@ +#include +#include + +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; +} diff --git a/src/system/libroot/posix/stdlib/Jamfile b/src/system/libroot/posix/stdlib/Jamfile index e97b601fee..2beea8ef8c 100644 --- a/src/system/libroot/posix/stdlib/Jamfile +++ b/src/system/libroot/posix/stdlib/Jamfile @@ -21,7 +21,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { div.c env.cpp exit.cpp - getsubopt.cpp heapsort.c merge.c mktemp.c diff --git a/src/system/libroot/posix/stdlib/getsubopt.cpp b/src/system/libroot/posix/stdlib/getsubopt.cpp deleted file mode 100644 index 9346bcef32..0000000000 --- a/src/system/libroot/posix/stdlib/getsubopt.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2010, Oliver Tappe . - * Distributed under the terms of the MIT License. - */ - - -#include -#include - - -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; -}