Author: zooey Date: 2010-09-14 20:43:24 +0200 (Tue, 14 Sep 2010) New Revision: 38650 Changeset: http://dev.haiku-os.org/changeset/38650 Ticket: http://dev.haiku-os.org/ticket/6499 Added: haiku/trunk/src/system/libroot/posix/stdlib/getsubopt.cpp haiku/trunk/src/tests/system/libroot/posix/getsubopt_test.cpp Modified: haiku/trunk/headers/posix/stdlib.h haiku/trunk/src/system/libroot/posix/stdlib/Jamfile haiku/trunk/src/tests/system/libroot/posix/Jamfile Log: Closing #6499: * add missing getsubopt() POSIX-function * added corresponding test Modified: haiku/trunk/headers/posix/stdlib.h =================================================================== --- haiku/trunk/headers/posix/stdlib.h 2010-09-14 18:37:37 UTC (rev 38649) +++ haiku/trunk/headers/posix/stdlib.h 2010-09-14 18:43:24 UTC (rev 38650) @@ -168,6 +168,9 @@ /* crypt */ extern void setkey(const char *key); +/* sub-option parsing */ +extern int getsubopt(char **optionp, char * const *keylistp, + char **valuep); /* pty functions */ extern int posix_openpt(int openFlags); Modified: haiku/trunk/src/system/libroot/posix/stdlib/Jamfile =================================================================== --- haiku/trunk/src/system/libroot/posix/stdlib/Jamfile 2010-09-14 18:37:37 UTC (rev 38649) +++ haiku/trunk/src/system/libroot/posix/stdlib/Jamfile 2010-09-14 18:43:24 UTC (rev 38650) @@ -12,6 +12,7 @@ div.c env.cpp exit.c + getsubopt.cpp heapsort.c merge.c mktemp.c Added: haiku/trunk/src/system/libroot/posix/stdlib/getsubopt.cpp =================================================================== --- haiku/trunk/src/system/libroot/posix/stdlib/getsubopt.cpp (rev 0) +++ haiku/trunk/src/system/libroot/posix/stdlib/getsubopt.cpp 2010-09-14 18:43:24 UTC (rev 38650) @@ -0,0 +1,39 @@ +/* + * Copyright 2010, Oliver Tappe <zooey@xxxxxxxxxxxxxxx>. + * 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; +} Modified: haiku/trunk/src/tests/system/libroot/posix/Jamfile =================================================================== --- haiku/trunk/src/tests/system/libroot/posix/Jamfile 2010-09-14 18:37:37 UTC (rev 38649) +++ haiku/trunk/src/tests/system/libroot/posix/Jamfile 2010-09-14 18:43:24 UTC (rev 38650) @@ -12,6 +12,7 @@ SimpleTest clearenv : clearenv.cpp ; SimpleTest dirent_test : dirent_test.cpp ; SimpleTest flock_test : flock_test.cpp ; +SimpleTest getsubopt_test : getsubopt_test.cpp ; SimpleTest locale_test : locale_test.cpp ; SimpleTest memalign_test : memalign_test.cpp ; SimpleTest mprotect_test : mprotect_test.cpp ; Added: haiku/trunk/src/tests/system/libroot/posix/getsubopt_test.cpp =================================================================== --- haiku/trunk/src/tests/system/libroot/posix/getsubopt_test.cpp (rev 0) +++ haiku/trunk/src/tests/system/libroot/posix/getsubopt_test.cpp 2010-09-14 18:43:24 UTC (rev 38650) @@ -0,0 +1,38 @@ +/* + * Copyright 2010, Oliver Tappe <zooey@xxxxxxxxxxxxxxx>. + * Distributed under the terms of the MIT License. + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +int +main() +{ + char* const keys[] = { + "mode", "be-lenient", NULL + }; + + char* option = strdup("mode=readwrite,unknown,be-lenient"); + char* value = NULL; + int result = getsubopt(&option, keys, &value); + if (result != 0) + fprintf(stderr, "failed 1: result=%d, expected %d\n", result, 0); + if (value == NULL || strcmp(value, "readwrite") != 0) + fprintf(stderr, "failed 2: value=%s, expected 'readwrite'\n", value); + result = getsubopt(&option, keys, &value); + if (result != -1) + fprintf(stderr, "failed 3: result=%d, expected %d\n", result, -1); + if (value != NULL) + fprintf(stderr, "failed 4: value=%p, expected NULL\n", value); + result = getsubopt(&option, keys, &value); + if (result != 1) + fprintf(stderr, "failed 5: result=%d, expected %d\n", result, 1); + if (value != NULL) + fprintf(stderr, "failed 6: value=%p, expected NULL\n", value); + + return 0; +}