通过命令传递参数查看环境变量
/************************************************************************* > File Name: env.c > Author: > Mail: > Created Time: Tue 24 Feb 2015 10:42:21 PM PST ************************************************************************/ #include<stdio.h> #include<stdlib.h> int main(int argc,char**argv) { char*p,*str ; if(argc==1||argc>3) { printf("Parameter Num is wrong!\n"); } else if(argc==2) { if((p=getenv(argv[1]))) { printf("Environment variable %s = %s\n",argv[1],p) ; } } else if(argc==3) { p=(char*)malloc(strlen(argv[1])+strlen(argv[2])+2) ; strcpy(p,argv[1]); strcat(p,"="); strcat(p,argv[2]); if(putenv(p)==0) { printf("put env successful\n"); } if(str=getenv(argv[1])) { printf("Put Env %s:%s\n",argv[1],str); }else { printf("Get Env Error!\n"); } //can not free on the front of this position free(p) ; } exit(0); }
/************************************************************************* > File Name: showenv.c > Author: > Mail: > Created Time: Tue 24 Feb 2015 11:17:39 PM PST ************************************************************************/ #include<stdio.h> #include<stdlib.h> extern char ** environ ; int main(int argc,char**argv) { char **penv=environ ; while(*penv) { printf("%s\n",*penv) ; penv++; } exit(0); }
/************************************************************************* > File Name: time.c > Author: > Mail: > Created Time: Wed 25 Feb 2015 12:11:00 AM PST ************************************************************************/ #include<stdio.h> #include<time.h> int main(int argc,char**argv) { char timeBuf[256]; time_t tmt=time((time_t*)NULL) ; printf("unformat time :%d\n",tmt); printf("format time :%s\n",ctime(localtime(&tmt))) ; strftime(timeBuf,256,"%m:%d",localtime(&tmt)); printf("format time:%s\n",timeBuf); exit(0); }
/************************************************************************* > File Name: uid.c > Author: > Mail: > Created Time: Wed 25 Feb 2015 12:51:53 AM PST ************************************************************************/ #include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { printf("UID:%d\n",getuid()); printf("GID:%d\n",getgid()); printf("CurrentLogined:%s\n",getlogin()); printf("GetHostID:%d\n",gethostid()); exit(0) ; }
/************************************************************************* > File Name: option.c > Author: > Mail: > Created Time: Tue 24 Feb 2015 06:30:00 PM PST ************************************************************************/ #include<stdio.h> #include<unistd.h> int main(int argc,char**argv) { int opt ; while((opt=getopt(argc,argv,":if:x"))!=-1) { switch(opt) { case '?': printf("Unknow Option:%c \n",optopt) ; break; case ':': printf("Option:%c need Value\n",optopt) ; break; case 'f': printf("File is %s \n",optarg) ; break; case 'i': case 'x': printf("Option : %c\n",opt) ; break ; } } for(;optind<argc;optind++) { printf("arguments: %s \n",argv[optind]) ; } exit(0); }
/************************************************************************* > File Name: option2.c > Author: > Mail: > Created Time: Tue 24 Feb 2015 09:16:58 PM PST ************************************************************************/ #include<stdio.h> #include<unistd.h> #include<getopt.h> #define _GNU_SOURCE int main(int argc,char**argv) { int opt ; struct option optlist[]={ {"add",0,NULL,'a'}, {"file",1,NULL,'f'}, {0,0,0,0} }; while((opt=getopt_long(argc,argv,":af:xcv",optlist,NULL))!=-1) { switch(opt) { case 'a': case 'x': case 'c': case 'v': printf("Option:%c\n",opt) ; break; case ':': printf("Option Have Value!\n"); break; case 'f': printf("File is %s\n",optarg) ; break ; case '?': printf("Option is unknow!\n") ; break ; } } for(;optind<argc;optind++) { printf("Remained Parameter :%s\n",argv[optind]) ; } exit(0); }
Linux下C程序设计(4)----操作环境变量 、程序传递参数getopt getopt_long操作、获取时间
原文地址:http://blog.csdn.net/yue7603835/article/details/43956763