* add missing getsubopt() POSIX-function
* added corresponding test


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38650 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe 2010-09-14 18:43:24 +00:00
parent d21b55880b
commit 338ddd836f
5 changed files with 82 additions and 0 deletions

View File

@ -168,6 +168,9 @@ extern size_t wcstombs(char *string, const wchar_t *pwcs, size_t maxSize);
/* 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);

View File

@ -12,6 +12,7 @@ MergeObject posix_stdlib.o :
div.c
env.cpp
exit.c
getsubopt.cpp
heapsort.c
merge.c
mktemp.c

View File

@ -0,0 +1,39 @@
/*
* 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;
}

View File

@ -12,6 +12,7 @@ SimpleTest SyslogTest : SyslogTest.cpp syslog.cpp ;
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 ;

View File

@ -0,0 +1,38 @@
/*
* Copyright 2010, Oliver Tappe <zooey@hirschkaefer.de>.
* 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;
}