作者在 2008-08-22 14:52:10 发布以下内容
因为学校Linux小组纳新的要求,写了一个自制的Echo程序.代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/*Two long-options:"help" && "version".*/
const struct option long_options[]={
{"help",0,NULL,'h'},
{"version",0,NULL,'v'},
{NULL,0,NULL,0}
};
int main(int argc,char *argv[]){
int n_on=0,e_on=0;
int strstart;
int value;
char charb;
int nl=1;
/*Analysis parameters of the program.*/
while((value=getopt_long(argc,argv,"enhvE",long_options,NULL))!=-1){
switch(value){
case 'e':
e_on=1;
break;
case 'n':
n_on=1;
break;
case 'E':
e_on=0;
break;
case 'h':
system("man echo");
return 0;
case 'v':
printf("Version:0.1\nBulit by SnakeWind\nJune,29,2008\n");
return 0;
case '?':
printf("Parameter no found.\nPlease check you input.\n");
return 1;
}
}
/*Look for the start point of string.*/
/* for(strstart=1;*argv[strstart]=='-';strstart++);
*/
/*String output.*/
for(strstart=1;strstart<argc;strstart++){
if(*argv[strstart]==45)
continue;
for(int i=0;*(argv[strstart]+i)!='\0';i++){
charb=*(argv[strstart]+i);
if(charb=='\\' && e_on==1){
i++;
charb=*(argv[strstart]+i);
if(charb>='0' && charb<='9'){
i+=2;
charb=*(argv[strstart]+i-2)*64+*(argv[strstart]+i-1)*8+*(argv[strstart]+i)*1;
printf("%c",charb);
}
else if(charb=='a') {printf("\a");}
else if(charb=='b') {printf("\b \b");}
/* else if(charb=='c') {nl=0;}*/
else if(charb=='f') {printf("\n\b");}
else if(charb=='n') {printf("\n");}
else if(charb=='r') {printf("\r");}
else if(charb=='t') {printf("\t");}
else if(charb=='\\') {printf("\\");}
else if(charb=='\"') {printf("\"");}
else printf("%c",charb);
}
printf("%c",charb);
}
printf(" ");
}
if(n_on==0) printf("\n");
}
自己不管怎么看,都觉得没啥问题,并且运行结果也和Fedora8的Echo命令是完全一样的.
但是Fedora8的Echo本身好像有问题!就是不管使不使用-e开关,都会无视转义字符,简单地把'\'去掉而已...
以上代码在Fedora8 x86_64下面也是这个问题-_-|||
补:字符串之间没有空格,大谬。已修正。
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/*Two long-options:"help" && "version".*/
const struct option long_options[]={
{"help",0,NULL,'h'},
{"version",0,NULL,'v'},
{NULL,0,NULL,0}
};
int main(int argc,char *argv[]){
int n_on=0,e_on=0;
int strstart;
int value;
char charb;
int nl=1;
/*Analysis parameters of the program.*/
while((value=getopt_long(argc,argv,"enhvE",long_options,NULL))!=-1){
switch(value){
case 'e':
e_on=1;
break;
case 'n':
n_on=1;
break;
case 'E':
e_on=0;
break;
case 'h':
system("man echo");
return 0;
case 'v':
printf("Version:0.1\nBulit by SnakeWind\nJune,29,2008\n");
return 0;
case '?':
printf("Parameter no found.\nPlease check you input.\n");
return 1;
}
}
/*Look for the start point of string.*/
/* for(strstart=1;*argv[strstart]=='-';strstart++);
*/
/*String output.*/
for(strstart=1;strstart<argc;strstart++){
if(*argv[strstart]==45)
continue;
for(int i=0;*(argv[strstart]+i)!='\0';i++){
charb=*(argv[strstart]+i);
if(charb=='\\' && e_on==1){
i++;
charb=*(argv[strstart]+i);
if(charb>='0' && charb<='9'){
i+=2;
charb=*(argv[strstart]+i-2)*64+*(argv[strstart]+i-1)*8+*(argv[strstart]+i)*1;
printf("%c",charb);
}
else if(charb=='a') {printf("\a");}
else if(charb=='b') {printf("\b \b");}
/* else if(charb=='c') {nl=0;}*/
else if(charb=='f') {printf("\n\b");}
else if(charb=='n') {printf("\n");}
else if(charb=='r') {printf("\r");}
else if(charb=='t') {printf("\t");}
else if(charb=='\\') {printf("\\");}
else if(charb=='\"') {printf("\"");}
else printf("%c",charb);
}
printf("%c",charb);
}
printf(" ");
}
if(n_on==0) printf("\n");
}
自己不管怎么看,都觉得没啥问题,并且运行结果也和Fedora8的Echo命令是完全一样的.
但是Fedora8的Echo本身好像有问题!就是不管使不使用-e开关,都会无视转义字符,简单地把'\'去掉而已...
以上代码在Fedora8 x86_64下面也是这个问题-_-|||
补:字符串之间没有空格,大谬。已修正。