getsubopt(3) - wariua/manpages-ko GitHub Wiki
getsubopt - ๋ฌธ์์ด์์ ํ์ ์ต์ ์ธ์ ํ์ฑ
#include <stdlib.h>
int getsubopt(char **optionp, char * const *tokens, char **valuep);
glibc ๊ธฐ๋ฅ ํ์ธ ๋งคํฌ๋ก ์๊ฑด (feature_test_macros(7) ์ฐธ๊ณ ):
-
getsubopt()
: -
_XOPEN_SOURCE >= 500
|| /* glibc 2.12๋ถํฐ: */ _POSIX_C_SOURCE >= 200809L
getsubopt()
๋ optionp
๋ก ๋ฐ์ ์ผํ ๊ตฌ๋ถ ํ์ ์ต์
๋ชฉ๋ก์ ํ์ฑ ํ๋ค. (๋ณดํต getopt(3)๋ฅผ ์จ์ ๋ช
๋ นํ์ ํ์ฑ ํ ๋ ๊ทธ๋ฐ ํ์ ์ต์
๋ชฉ๋ก์ด ์๊ธด๋ค. ๊ฐ๋ น mount(8)์ -o
์ต์
์ฐธ๊ณ .) ๊ฐ ํ์ ์ต์
์ ๊ฐ์ด ์์ ์๋ ์์ผ๋ฉฐ ๊ทธ ๊ฒฝ์ฐ ๋ฑํธ๋ฅผ ์จ์ ํ์ ์ต์
์ด๋ฆ๊ณผ ๊ตฌ๋ณํ๋ค. ๋ค์์ optionp
๋ก ์ ๋ฌ๋ ์ ์์ ์ข
๋ฅ์ ๋ฌธ์์ด ์์์ด๋ค.
ro,name=xyz
tokens
์ธ์๋ ํ ํฐ๋ค์ ๋ํ ํฌ์ธํฐ๋ค๋ก ๋ NULL ์ข
๋ฃ ๋ฐฐ์ด์ ๋ํ ํฌ์ธํฐ์ด๋ค. getsubopt()
๊ฐ optionp
์์ ๊ทธ ํ ํฐ๋ค์ ์ฐพ๊ฒ ๋๋ค. ํ ํฐ์ ์ ์ด๋ ํ ๋ฌธ์๋ฅผ ๋ด์ ์๋ก ๊ตฌ๋ณ๋๋ ๋ ์ข
๋ฃ ๋ฌธ์์ด์ด์ด์ผ ํ๋ฉฐ ๋ฑํธ๋ ์ผํ๊ฐ ๋ค์ด ์์ง ์์์ผ ํ๋ค.
๊ฐ getsubopt()
ํธ์ถ์ ๋ค์ ๋ฏธ์ฒ๋ฆฌ ํ์ ์ต์
์ ๋ํ ์ ๋ณด๋ฅผ optionp
์ผ๋ก ๋ฐํํ๋ค. ์๋ธ ์ต์
์ ์ฒซ ๋ฒ์งธ ๋ฑํธ๋ (์๋ค๋ฉด) ๊ทธ ํ์ ์ต์
์ ์ด๋ฆ๊ณผ ๊ฐ ์ฌ์ด ๊ตฌ๋ถ์๋ก ํด์ํ๋ค. ๊ฐ์ ๋ค์ ์ฝํ๊น์ง ๋๋ (๋ง์ง๋ง ํ์ ์ต์
์ธ ๊ฒฝ์ฐ) ๋ฌธ์์ด ๋๊น์ง ์ด์ด์ง๋ค. ํ์ ์ต์
์ ์ด๋ฆ์ด tokens
์ ์๋ ์ด๋ค ์ด๋ฆ๊ณผ ์ผ์นํ๊ณ ๊ฐ ๋ฌธ์์ด์ด ์๋ ๊ฒฝ์ฐ์ getsubopt()
๋ ๊ทธ ๋ฌธ์์ด์ ์ฃผ์๋ฅผ *valuep
์ ์ค์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ optionp
์ ์ฒซ ๋ฒ์งธ ์ผํ๋ฅผ ๋ ๋ฐ์ดํธ๋ก ๋ฎ์ด ์จ์ *valuep
๊ฐ ์ ํํ๊ฒ ๊ทธ ํ์ ์ต์
์ "๊ฐ ๋ฌธ์์ด"์ด ๋๋ค.
ํ์ ์ต์
์ ์ธ์ํ์ง๋ง ๊ฐ ๋ฌธ์์ด์ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ์๋ *valuep
๋ฅผ NULL๋ก ์ค์ ํ๋ค.
getsubopt()
๋ฐํ ์ optionp
๋ ๋ค์ ํ์ ์ต์
์ ๊ฐ๋ฆฌํค๊ฑฐ๋, ๋ง์ง๋ง ํ์ ์ต์
์ ๋ฐฉ๊ธ ์ฒ๋ฆฌํ ๊ฒฝ์ฐ ๋ฌธ์์ด ๋์ ์๋ ๋ ๋ฐ์ดํธ('\0')๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
optionp
์ ์ฒซ ๋ฒ์งธ ํ์ ์ต์
์ ์ธ์ํ ๊ฒฝ์ฐ getsubopt()
๋ tokens
์์ ์ผ์นํ๋ ํ์ ์ต์
ํญ๋ชฉ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค. ์๋๋ผ๋ฉด -1์ ๋ฐํํ๋ฉฐ *valuep
๋ name[=value]
๋ฌธ์์ด ์ ์ฒด์ด๋ค.
*optionp
๊ฐ ๋ฐ๋๊ธฐ ๋๋ฌธ์ getsubopt()
ํธ์ถ ์ ์ ์ฒซ ๋ฒ์งธ ํ์ ์ต์
์ getsubopt()
ํ์ ์ฒซ ๋ฒ์งธ ํ์ ์ต์
๊ณผ (๋ฐ๋์) ๊ฐ์ง ์๋ค.
์ด ์ ์์ ์ฌ์ฉํ๋ ์ฉ์ด๋ค์ ๋ํ ์ค๋ช ์ attributes(7)๋ฅผ ๋ณด๋ผ.
์ธํฐํ์ด์ค | ์์ฑ | ๊ฐ |
---|---|---|
getsubopt() |
์ค๋ ๋ ์์ ์ฑ | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
๋ฌธ์์ด *optionp
์์ ์ฐพ์ ์ผํ๋ฅผ getsubopt()
์์ ๋ฎ์ด ์ฐ๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ฌธ์์ด์ด ์ฐ๊ธฐ ๊ฐ๋ฅํด์ผ ํ๋ค. ์ฆ ๋ฌธ์์ด ์์์ผ ์ ์๋ค.
๋ค์ ํ๋ก๊ทธ๋จ์ "-o" ์ต์ ๋ค์์ ํ์ ์ต์ ์ ๋ฐ๋๋ค.
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
enum {
RO_OPT = 0,
RW_OPT,
NAME_OPT
};
char *const token[] = {
[RO_OPT] = "ro",
[RW_OPT] = "rw",
[NAME_OPT] = "name",
NULL
};
char *subopts;
char *value;
int opt;
int readonly = 0;
int readwrite = 0;
char *name = NULL;
int errfnd = 0;
while ((opt = getopt(argc, argv, "o:")) != -1) {
switch (opt) {
case 'o':
subopts = optarg;
while (*subopts != '\0' && !errfnd) {
switch (getsubopt(&subopts, token, &value)) {
case RO_OPT:
readonly = 1;
break;
case RW_OPT:
readwrite = 1;
break;
case NAME_OPT:
if (value == NULL) {
fprintf(stderr, "Missing value for "
"suboption '%s'\n", token[NAME_OPT]);
errfnd = 1;
continue;
}
name = value;
break;
default:
fprintf(stderr, "No match found "
"for token: /%s/\n", value);
errfnd = 1;
break;
}
}
if (readwrite && readonly) {
fprintf(stderr, "Only one of '%s' and '%s' can be "
"specified\n", token[RO_OPT], token[RW_OPT]);
errfnd = 1;
}
break;
default:
errfnd = 1;
}
}
if (errfnd || argc == 1) {
fprintf(stderr, "\nUsage: %s -o <suboptstring>\n", argv[0]);
fprintf(stderr, "suboptions are 'ro', 'rw', "
"and 'name=<value>'\n");
exit(EXIT_FAILURE);
}
/* ํ๋ก๊ทธ๋จ ๋๋จธ์ง... */
exit(EXIT_SUCCESS);
}
2019-03-06