美文网首页
二、linux环境

二、linux环境

作者: zhile_doing | 来源:发表于2018-08-14 14:54 被阅读0次
    1. argc and argv
      main函数原型
      int main(int argc, char* argv[])
      argc代表命令行参数的个数,argv保存命令行参数内容

        #include<stdio.h>
        int main(int argc, char* argv[]){
            printf("program name:%s\n", argv[0]);
            for(int i = 1; i < argc ; i++){
                if(argv[i][0] == '-'){
                    printf("option:%s\n", argv[i] + 1);
                }else{
                    printf("argument:%s\n", argv[i]);
                }
        
            }
            return 0;
        }
      
    2. 使用getopt处理命令行参数


      #include<stdio.h>
      #include<unistd.h>
      extern char *optarg;
      extern int opterr, optind, optopt;
      int main(int argc, char *argv[]){
          int opt;
          while((opt = getopt(argc, argv, "if:rs")) != -1){
              switch(opt){
                  case 'i': 
                  case 'r': 
                  case 's': 
                      printf("option:%c\n", opt);
                      break;
                  case 'f': 
                      printf("filename:%s\n", optarg);
                  default: 
                      printf("unknown argument!\n");
                      break;
              }
          }
          for(;optind < argc; optind++){
              printf("argument:%s\n", argv[optind]);
          }
          return 0;
      }
      

      使用getopt_long处理长参数类型

      #include<stdio.h>
      #include<unistd.h>
      #include<getopt.h>
      #define _GNU_SOURCE
      extern char* optarg;
      extern int optopt, optind, opterr;
      int main(int argc, char* argv[]){
          int opt;
          struct option options[] = {
              {"initialize", 0, NULL, 'i'},
              {"restart", 0, NULL, 'r'},
              {"stop", 0, NULL, 's'},
              {"file", 1, NULL, 'f'}
          };
          while((opt = getopt_long(argc, argv, "irsf:", options, NULL)) != -1){
              switch(opt){
                  case 'i': 
                  case 'r': 
                  case 's': 
                      printf("option:%c\n", opt);
                      break;
                  case 'f': 
                      printf("filename:%s\n", optarg);
                      break;
                  default:
                      printf("unknown arguments!");
                      break;
              }
          }
          for(;optind < argc; optind++){
              printf("other argument:%s\n", argv[optind]);
          }
          return 0;
      }
      
    3. 使用getenv与putenv获取并设置环境变量

      #include<stdio.h>
      #include<stdlib.h>
      #include<string.h>
      int main(int argc, char* argv[]){
          if(argc == 1 || argc > 3){
              fprintf(stderr, "invalid argument!");
              exit(1);
          }
          char *var = argv[1];
          char *value = getenv(var);
          if(value){
              printf("The environment variable %s has value %s\n", var, value);
          }else{
              printf("The environment variable %s has no value!\n", var);
          }
      
          if(argc == 3){
              value = argv[2];
              char *string = (char*)malloc(strlen(var) + strlen(value) + 2);
              if(string != NULL){
                  strcpy(string, var);
                  strcat(string, "=");
                  strcat(string, value);
              }
              printf("calling putenv with string %s\n", string);
              if(putenv(string) != 0){
                  fprintf(stderr, "putenv failed!\n");
                  free(string);
                  exit(1);
              }
              value = getenv(var);
              if(value){
                  printf("new value of %s is %s\n", var, value);
              }else{
                  printf("calling get env failed!\n");
              }
          }
          return 0;
      }
      

      获取所有环境变量

      #include<stdlib.h>
      #include<stdio.h>
      extern char** environ;
      int main(int argc, char *argv[]){
          char **env = environ;
          while(*env != NULL){
              printf("%s\n", *env);
              env++;
          }
          return 0;
      }
      
    4. 日期与时间

      • time函数获取时间,返回time_t类型的长整型数,表示某一特定时间到现在的秒数


        #include<time.h>
        #include<stdio.h>
        #include<unistd.h>
        int main(int argc, char *argv[]){
            int i;
            time_t the_time;
            for(i = 0;i<10; i++){
                the_time = time((time_t*)0);
                printf("time is:%ld\n", the_time);
                sleep(2);
            }
            return 0;
        }
        
      • gmtime可以从time_t类型的数据中获取当前时间数据



        返回tm结构体类型的指针,其中包含了我们需要的各种信息


        #include<stdio.h>
        #include<time.h>
        int main(){
            time_t the_time;
            time(&the_time);
            struct tm* tm_ptr = gmtime(&the_time);
            printf("the row time is:%ld\n", the_time);
            printf("the gmtime gives:\n");
            printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon + 1, tm_ptr->tm_mday);
            printf("time: %02d:%02d:%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
            return 0;
        }
        
      • localtime 用来返回time_t类型代表的本地时间
        #include<stdio.h>
        #include<time.h>
        int main(){
            time_t the_time;
            time(&the_time);
            struct tm* tm_ptr = localtime(&the_time);
            printf("the row time is:%ld\n", the_time);
            printf("the localtime gives:\n");
            printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon + 1, tm_ptr->tm_mday);
            printf("time: %02d:%02d:%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
            return 0;
        }
        
      • mktime从一个tm结构体构造一个等价的time_t值,失败时返回-1


      • asctime and ctime


        #include<time.h>
        #include<stdio.h>
        int main(){
            struct tm* tm_ptr;
            time_t the_time;
            time(&the_time);
            printf("asctime gives:%s\n", asctime(localtime(&the_time)));
            printf("ctime gives:%s\n", ctime(&the_time));
            return 0;
        }
        
    5. 临时文件

      • 使用tmpnam生成临时文件名称



        使用tmpfile打开临时文件


        #include<stdio.h>
        int main(){
            char tmpname[L_tmpnam];
            char* filename;
            FILE* tempfile;
            filename = tmpnam(tmpname);
            printf("the tmp file name is:%s\n", filename);
            tempfile = tmpfile();
            if(tempfile){
                puts("open stream to tmpfile successfully!");
            }else{
                puts("open file failed!");
            }
            return 0;
        }
        

      使用mktemp与mkstemp


    6. 使用syslog记录日志

      #include<syslog.h>
      #include<stdio.h>
      int main(){
          FILE* f = fopen("no_exist", "r");
          if(!f){
              syslog(LOG_ALERT | LOG_USER, "oops - %m\n");
              puts("log successfully!\n");
          }
          return 0;
      }
      

      掌握理解,openlog, setlogmask

      #include<syslog.h>
      #include<stdio.h>
      #include<unistd.h>
      int main(){
          int logmask;
          openlog("test logmask", LOG_PID|LOG_CONS, LOG_USER);
          syslog(LOG_DEBUG, "test debug log, this message should exist!");
          syslog(LOG_INFO, "program pid is %d\n", getpid());
          logmask = setlogmask(LOG_UPTO(LOG_INFO));
          syslog(LOG_DEBUG, "this message should not exist!\n");
          return 0;
      }
      

    相关文章

      网友评论

          本文标题:二、linux环境

          本文链接:https://www.haomeiwen.com/subject/uuokbftx.html