webben在main函数中使用了getopt_long函数获得main函数的输入参数,可以看到文档中的参数定义
int getopt(int argc, char * const argv[], const char *optstring);
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
getopt 和 getopt_long的区别在于getopt_long比getopt函数多两个参数,首先介绍getopt函数的三个参数定义:
argc 和argv就main函数的输入,optstring 是用户自定义的字符串,下面举例说明
int main(int argc, char *argv[])
{
int opt;
while((opt=getopt(argc, argv, "c:d:e:"))!=EOF)
{
printf("opt is %c",opt);
printf("\n optarg %s",optarg);
printf("\n optind %d", optind);
printf("\n optargv[] %s", argv[optind-1]);
}
}
"c:d:e:"时,getopt函数依次检查输入中是否有-c -d -e,:的意思是在这些参数后面有其他的输入,如-c 100 -d 200 -e 300
函数输出
[gaowei@iZ28n8zvqheZ ctest]$ ./a.out -c 100 -d 200 -e 300
opt is c
optarg 100
optind 3
optargv[] 100
opt is d
optarg 200
optind 5
optargv[] 200
opt is e
optarg 300
optind 7
optargv[] 300
getopt_long函数的longopts的参数是一个 struct option 的结构体,结构体定义
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
name 是参数的名称
has_arg 指明是否带参数值,其数值可选:
no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
flag 当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0
val 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
webbench程序中的定义为
static const struct option long_options[]=
{
{"force",no_argument,&force,1},
{"reload",no_argument,&force_reload,1},
{"time",required_argument,NULL,'t'},
{"help",no_argument,NULL,'?'},
{"http09",no_argument,NULL,'9'},
{"http10",no_argument,NULL,'1'},
{"http11",no_argument,NULL,'2'},
{"get",no_argument,&method,METHOD_GET},
{"head",no_argument,&method,METHOD_HEAD},
{"options",no_argument,&method,METHOD_OPTIONS},
{"trace",no_argument,&method,METHOD_TRACE},
{"version",no_argument,NULL,'V'},
{"proxy",required_argument,NULL,'p'},
{"clients",required_argument,NULL,'c'},
{NULL,0,NULL,0}
};
另一个参数longindex,如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。
综上所述,webbench程序在进入main函数后就判断了main函数的参数
while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF )
{
switch(opt)
{
case 0 : break;
case 'f': force=1;break;
case 'r': force_reload=1;break;
case '9': http10=0;break;
case '1': http10=1;break;
case '2': http10=2;break;
case 'V': printf(PROGRAM_VERSION"\n");exit(0);
case 't': benchtime=atoi(optarg);break;
case 'p':
/* proxy server parsing server:port */
tmp=strrchr(optarg,':');
proxyhost=optarg;
if(tmp==NULL)
{
break;
}
if(tmp==optarg)
{
fprintf(stderr,"Error in option --proxy %s: Missing hostname.\n",optarg);
return 2;
}
if(tmp==optarg+strlen(optarg)-1)
{
fprintf(stderr,"Error in option --proxy %s Port number is missing.\n",optarg);
return 2;
}
*tmp='\0';
proxyport=atoi(tmp+1);break;
case ':':
case 'h':
case '?': usage();return 2;break;
case 'c': clients=atoi(optarg);break;
}
}
如果读入的opt为c 或者 t时,使用atoi函数,将读入的字符串转换成整数赋值给变量。
函数功能就是将数字表示的字符串转换成整数,识别的数字是从字符串起始位置到第一位非数字字符。
int main()
{
char *p = "123.456";
int x = atoi(p);
printf("%d",x);
}
输出为:
123