Linux下C程序设计(4)----操作环境变量 、程序传递参数getopt getopt_long操作、获取时间

通过命令传递参数查看环境变量 

/*************************************************************************
	> 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);
}

获取登陆uid gid  user

/*************************************************************************
	> 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);
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。