Closing #6499:
* 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:
parent
d21b55880b
commit
338ddd836f
@ -168,6 +168,9 @@ extern size_t wcstombs(char *string, const wchar_t *pwcs, size_t maxSize);
|
|||||||
/* crypt */
|
/* crypt */
|
||||||
extern void setkey(const char *key);
|
extern void setkey(const char *key);
|
||||||
|
|
||||||
|
/* sub-option parsing */
|
||||||
|
extern int getsubopt(char **optionp, char * const *keylistp,
|
||||||
|
char **valuep);
|
||||||
|
|
||||||
/* pty functions */
|
/* pty functions */
|
||||||
extern int posix_openpt(int openFlags);
|
extern int posix_openpt(int openFlags);
|
||||||
|
@ -12,6 +12,7 @@ MergeObject posix_stdlib.o :
|
|||||||
div.c
|
div.c
|
||||||
env.cpp
|
env.cpp
|
||||||
exit.c
|
exit.c
|
||||||
|
getsubopt.cpp
|
||||||
heapsort.c
|
heapsort.c
|
||||||
merge.c
|
merge.c
|
||||||
mktemp.c
|
mktemp.c
|
||||||
|
39
src/system/libroot/posix/stdlib/getsubopt.cpp
Normal file
39
src/system/libroot/posix/stdlib/getsubopt.cpp
Normal 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;
|
||||||
|
}
|
@ -12,6 +12,7 @@ SimpleTest SyslogTest : SyslogTest.cpp syslog.cpp ;
|
|||||||
SimpleTest clearenv : clearenv.cpp ;
|
SimpleTest clearenv : clearenv.cpp ;
|
||||||
SimpleTest dirent_test : dirent_test.cpp ;
|
SimpleTest dirent_test : dirent_test.cpp ;
|
||||||
SimpleTest flock_test : flock_test.cpp ;
|
SimpleTest flock_test : flock_test.cpp ;
|
||||||
|
SimpleTest getsubopt_test : getsubopt_test.cpp ;
|
||||||
SimpleTest locale_test : locale_test.cpp ;
|
SimpleTest locale_test : locale_test.cpp ;
|
||||||
SimpleTest memalign_test : memalign_test.cpp ;
|
SimpleTest memalign_test : memalign_test.cpp ;
|
||||||
SimpleTest mprotect_test : mprotect_test.cpp ;
|
SimpleTest mprotect_test : mprotect_test.cpp ;
|
||||||
|
38
src/tests/system/libroot/posix/getsubopt_test.cpp
Normal file
38
src/tests/system/libroot/posix/getsubopt_test.cpp
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user