linux/unix下命令行开关与环境变量

Posted on 2008年5月13日 04:20

1.命令行开关标准编程接口 : getopt函数,putopt函数

练习:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.         int opt;
  7.  
  8.         while((opt = getopt(argc, argv, "if:lr")) != -1) {
  9.                 switch(opt) {
  10.                 case 'i' :
  11.                 case 'l' :
  12.                 case 'r' :
  13.                         printf("option: %c\n", opt);
  14.                         break;
  15.                 case 'f' :
  16.                         printf("filename: %s\n", optarg);
  17.                         break;
  18.                 case ':' :
  19.                         printf("options needs a value\n");
  20.                         break;
  21.                 case '?' :
  22.                         printf("unknown option : %c\n", optopt);
  23.                         break;
  24.                 }
  25.         }
  26.         for(; optind < argc; optind++)
  27.                 printf("argument: %s\n", argv[optind]);
  28.  
  29.         exit(0);
  30. }
  31.  

2.环境变量:getenv函数,putenv函数

练习:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.         char *var, *value;
  8.  
  9.         if (argc ==1 || argc > 3) {
  10.                 fprintf(stderr, "usage: environ var [value]\n");
  11.                 exit(1);
  12.         }
  13.  
  14.         var = argv[1];
  15.         value = getenv(var);
  16.         if (value)
  17.                 printf("Variable %s has value %s\n", var, value);
  18.         else
  19.                 printf("Varuable %s has no value\n", var);
  20.  
  21.         if (argc == 3) {
  22.                 char *string;
  23.                 value = argv[2];
  24.                 string = malloc(strlen(var)+strlen(value)+2);
  25.                 strcpy(string, var);
  26.                 strcat(string, "=");
  27.                 strcat(string, value);
  28.                 printf("Calling putenv with: %s\n", string);
  29.                 if (putenv(string) != 0) {
  30.                         fprintf(stderr, "putenv failed\n");
  31.                         free(string);
  32.                         exit(1);
  33.                 }
  34.                 value = getenv(argv[1]);
  35.                 if(value)
  36.                         printf("New value of %s is %s\n", var, value);
  37.                 else
  38.                         printf("New value of %s is null??\n", var);
  39.         }       
  40.         exit(0);
  41. }
  42.  

总结:注意optarg,optopt,optind关键字全局变量

  

linux/unix目录操作的实例

Posted on 2008年5月11日 20:54

目录扫描程序:

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4.  
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10. void printdir(char *dir, int depth);
  11.  
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15.         char *topdir = ".";
  16.         if (argc >= 2)
  17.                 topdir = argv[1];
  18.  
  19.         printf("Dirrctory scan of %s\n", topdir);
  20.         printdir(topdir, 0);
  21.         printf("done.\n");
  22.  
  23.         exit(0);
  24. }
  25.  
  26.  
  27. void printdir(char *dir, int depth)
  28. {
  29.         DIR *dp;
  30.         struct stat statbuf;
  31.         struct dirent *entry;
  32.  
  33.         if ((dp = opendir(dir)) == NULL) {
  34.                 fprintf(stderr, "cannot open directory : %s\n", dir);
  35.                 return;
  36.         }
  37.         chdir(dir);
  38.         while ((entry = readdir(dp)) != NULL) {
  39.                 lstat(entry->d_name, &statbuf);
  40.                 if (S_ISDIR(statbuf.st_mode)) {
  41.                         if (strcmp(".", entry->d_name) == 0 ||
  42.                                 strcmp("..", entry->d_name) == 0)
  43.                                 continue;
  44.                         printf("%*s%s/\n", depth,"", entry->d_name);
  45.                         printdir(entry->d_name, depth+4);
  46.                 }
  47.                 else printf("%*s%s\n", depth,"", entry->d_name);
  48.         }
  49.         chdir("..");
  50.         closedir(dp);   
  51. }
  52.  
  53.  

总结:

1.printf%*s的应用

2.递归应用

程序运行截图: