【发布时间】:2012-09-14 08:54:17
【问题描述】:
while ((c = getopt(ac, av, "r:u:p:h:P:s:S:t:iRbo:n:")) != EOF)
{
switch(c)
{
case 'b':
isbsd++;
break;
case 'R':
detectos++;
break;
case 'r':
root = strdup(optarg);
break;
case 'i':
is_scramble = 1;
break;
case 's':
saddr = strtoul(optarg, 0, 0);
break;
case 't':
timeout = strtoul(optarg, 0, 0);
break;
case 'S':
size = strtoul(optarg, 0, 0);
break;
case 'u':
user = strdup(optarg);
break;
case 'p':
pass = strdup(optarg);
break;
case 'h':
host = strdup(optarg);
break;
case 'P':
port = strtoul(optarg, 0, 0);
break;
case 'o':
heapbase = strtoul(optarg, 0, 0);
break;
case 'n':
scnum = strtoul(optarg, 0, 0);
break;
default:
usage(av[0]);
}
}
我对 getopt 功能很陌生,从未使用过它。 (ac 是 argc,av 是 argv)
那么,这段代码是如何运作的呢?我感到困惑的是“r:u ...”的工作原理。
另外,strdup 和 strtoul 在这段代码中的作用是什么?
谢谢。
【问题讨论】:
-
"strdup 和 strtoul 在这段代码中做了什么?"您是否尝试过阅读文档以了解这些函数的作用?
-
你读过docs for getopt 吗?那里都有解释。
标签: c