Linux Programming by Exemple(第二章 参数、选项..
时间:2006-04-11 来源:gameboytxt
课后练习:
1。假设程序接受选项-a、-b和 -c,并且-b 需要参数。请为这个程序写一段手工解析参数的代码,不能使用getopt()或者getopt_long(),接收到-- 结束选项处理。确保-ab、
-bYANKEES、-b YANKEES 和-abYANKEES都能运行。测试你的程序。
--------------------CODE-------------------------
/* exer-01.c
author: gameboytxt
2006.3
*/
#include <stdio.h>
int aflg,bflg,cflg; /* a, b ,c选项的标记变量 */
char* optarg; /*保存选项参数*/
main( argc, argv )
int argc;
char* argv[];
{
if ( --argc > 0 && *argv[1] == '-' ) {
argv++;
while ( *++*argv ) switch( **argv ) {
case 'a' :
aflg++;
continue;
case 'b' :
bflg++;
if (*++*argv){
optarg=*argv;
goto display_opt;
}
else if (--argc ==0){
printf("b option require argument\n");
return -1;
}else {
optarg=*++argv;
goto display_opt;
}
case 'c' :
cflg++;
continue;
case '-':
goto display_opt;
default :
continue;
}
}
display_opt : {
if (aflg) printf ( "This is -a option\n");
if (bflg) printf ( "This is -b option\nits argument is %s\n", optarg);
if (cflg) printf ("This is -c option\n");
}
return 0;
} -----------------END CODE-------------------------- 2。实现getop()。第一个版本不用担心'optstring[0]==':'' 的情况,也可以忽略 opterr。 3。在你的getopt()中增加处理'optstring[0]==':''和opterr的代码。 ----------------CODE------------------------------- #include <stdio.h>
char *optarg = NULL; //如果选项接受参数的话,那么它就是选项参数。
int optind = 1; //argv的当前索引。
int opterr = 1; //当它为零,getopt为无效选项,和缺少选项参数。
int optopt = '?'; //当发现无效字符的时候,getopt或者返回‘?’或者返回‘:’
//并且optopt包含了所发现的无效选项字符。
static char *
my_index (str, chr)
const char *str;
int chr;
{
while (*str)
{
if (*str == chr)
return (char *) str;
str++;
}
return 0;
} int getopt (int argc, char *const argv[], const char *optstring){
static char *nextchar = NULL; if (optind != argc && !strcmp (argv[optind], "--"))
optind = argc;
if (optind == argc)
{
return -1;
}
if (nextchar == NULL || *nextchar == '\0') {
if ( argv[optind][0] != '-' || argv[optind][1] == '\0' ){
optarg = argv[optind++];
return 1;
}
nextchar = (argv[optind] + 1
+ (argv[optind][1] == '-'));
}
char c = *nextchar++;
char *temp = my_index (optstring, c); if ( *nextchar == '\0' )
++optind;
if ( temp == NULL || c == ':' )
{
if (opterr)
{
printf ("%s: invalid option -- %c\n",
argv[0], c);
}
optopt = c;
return '?';
}
if (temp[1] == ':')
{
if (temp[2] == ':')
{
if (*nextchar != '\0')
{
optarg = nextchar;
optind++;
}
else
optarg = NULL;
nextchar = NULL;
}
else
{
if (*nextchar != '\0')
{
optarg = nextchar;
optind++;
}
else if (optind == argc)
{
if (opterr)
{
printf ("%s: option requires an argument -- %c\n",
argv[0], c);
}
optopt = c;
if (optstring[0] == ':')
c = ':';
else
c = '?';
}
else
optarg = argv[optind++];
nextchar = NULL;
} }
return c;
}
int main( int argc, char* argv[])
{
int oc;
int aflg ,cflg ;
aflg=cflg=0;
char* b_opt_arg = NULL;
while ( (oc = getopt(argc, argv, ":ab:c")) !=-1) {
switch (oc) {
case 'a' :
aflg++;
break;
case 'b' :
b_opt_arg = optarg;
break;
case 'c' :
cflg++;
break;
case '?' ://无效选项 break;
case ':' ://选项缺少参数 break;
default:
break;
}
}
if (aflg)
printf("This is -a option\n");
if (b_opt_arg)
printf("This is -b option\nits argument is %s\n", b_opt_arg);
if (cflg)
printf("This is -c option\n");
return 0;
}
-----------------END CODE---------------------------
author: gameboytxt
2006.3
*/
#include <stdio.h>
int aflg,bflg,cflg; /* a, b ,c选项的标记变量 */
char* optarg; /*保存选项参数*/
main( argc, argv )
int argc;
char* argv[];
{
if ( --argc > 0 && *argv[1] == '-' ) {
argv++;
while ( *++*argv ) switch( **argv ) {
case 'a' :
aflg++;
continue;
case 'b' :
bflg++;
if (*++*argv){
optarg=*argv;
goto display_opt;
}
else if (--argc ==0){
printf("b option require argument\n");
return -1;
}else {
optarg=*++argv;
goto display_opt;
}
case 'c' :
cflg++;
continue;
case '-':
goto display_opt;
default :
continue;
}
}
display_opt : {
if (aflg) printf ( "This is -a option\n");
if (bflg) printf ( "This is -b option\nits argument is %s\n", optarg);
if (cflg) printf ("This is -c option\n");
}
return 0;
} -----------------END CODE-------------------------- 2。实现getop()。第一个版本不用担心'optstring[0]==':'' 的情况,也可以忽略 opterr。 3。在你的getopt()中增加处理'optstring[0]==':''和opterr的代码。 ----------------CODE------------------------------- #include <stdio.h>
char *optarg = NULL; //如果选项接受参数的话,那么它就是选项参数。
int optind = 1; //argv的当前索引。
int opterr = 1; //当它为零,getopt为无效选项,和缺少选项参数。
int optopt = '?'; //当发现无效字符的时候,getopt或者返回‘?’或者返回‘:’
//并且optopt包含了所发现的无效选项字符。
static char *
my_index (str, chr)
const char *str;
int chr;
{
while (*str)
{
if (*str == chr)
return (char *) str;
str++;
}
return 0;
} int getopt (int argc, char *const argv[], const char *optstring){
static char *nextchar = NULL; if (optind != argc && !strcmp (argv[optind], "--"))
optind = argc;
if (optind == argc)
{
return -1;
}
if (nextchar == NULL || *nextchar == '\0') {
if ( argv[optind][0] != '-' || argv[optind][1] == '\0' ){
optarg = argv[optind++];
return 1;
}
nextchar = (argv[optind] + 1
+ (argv[optind][1] == '-'));
}
char c = *nextchar++;
char *temp = my_index (optstring, c); if ( *nextchar == '\0' )
++optind;
if ( temp == NULL || c == ':' )
{
if (opterr)
{
printf ("%s: invalid option -- %c\n",
argv[0], c);
}
optopt = c;
return '?';
}
if (temp[1] == ':')
{
if (temp[2] == ':')
{
if (*nextchar != '\0')
{
optarg = nextchar;
optind++;
}
else
optarg = NULL;
nextchar = NULL;
}
else
{
if (*nextchar != '\0')
{
optarg = nextchar;
optind++;
}
else if (optind == argc)
{
if (opterr)
{
printf ("%s: option requires an argument -- %c\n",
argv[0], c);
}
optopt = c;
if (optstring[0] == ':')
c = ':';
else
c = '?';
}
else
optarg = argv[optind++];
nextchar = NULL;
} }
return c;
}
int main( int argc, char* argv[])
{
int oc;
int aflg ,cflg ;
aflg=cflg=0;
char* b_opt_arg = NULL;
while ( (oc = getopt(argc, argv, ":ab:c")) !=-1) {
switch (oc) {
case 'a' :
aflg++;
break;
case 'b' :
b_opt_arg = optarg;
break;
case 'c' :
cflg++;
break;
case '?' ://无效选项 break;
case ':' ://选项缺少参数 break;
default:
break;
}
}
if (aflg)
printf("This is -a option\n");
if (b_opt_arg)
printf("This is -b option\nits argument is %s\n", b_opt_arg);
if (cflg)
printf("This is -c option\n");
return 0;
}
-----------------END CODE---------------------------
相关阅读 更多 +