BLP-chap-4 Linux环境
时间:2009-05-18 来源:mercurial-hl
程序参数
getopt用法:
运行:
getopt_long略
环境变量
环境变量详解:环境变量
environ:
getenv与putenv:
运行:
时间日期
time函数:
运行:
localtime函数:
运行:
ctime函数:
运行:
getopt用法:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { int opt; while ((opt=getopt(argc, argv, "if:lr")) != -1) { switch(opt) { case 'i': case 'l': case 'r': printf("option:%c\n", opt); break; case 'f': printf("filename: %s\n", optarg); break; case ':': printf("option needs a value\n"); break; case '?': printf("unknow option: %c\n", optopt); break; } } for (; optind<argc; optind++) { printf("argument: %s \n", argv[optind]); } exit(0); } |
运行:
[root@localhost chap4]# ./argopt -i -lr 'hi here' -f fred.c -q option:i option:l option:r filename: fred.c ./argopt: invalid option -- q unknow option: q argument: hi here |
getopt_long略
环境变量
环境变量详解:环境变量
environ:
#include <stdio.h> #include <stdlib.h> extern char **environ; int main(void) { char **env = environ; while (*env) { printf("%s\n", *env); env++; } exit(0); } |
getenv与putenv:
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { char *var; /* 环境变量名 */ char *value; /* 环境变量值 */ if (argc ==1 || argc > 3) { fprintf(stderr, "useage: ./environ var [value]\n"); exit(1); } var = argv[1]; value = getenv(var); /* 获取环境变量的值 */ if (value) printf("variable %s has value %s \n", var, value); else printf("variable %s has no value\n"); /* 在程序中设置环境变量的值 */ if (argc == 3) { char *string; value = argv[2]; if (!(string = malloc(strlen(var) + strlen(value) + 2))) { fprintf(stderr, "overflow\n"); exit(1); } strcpy(string, var); strcat(string, "="); strcat(string, value); printf("calling putenv to %s \n", string); if (putenv(string) != 0) /* 设置环境变量 */ { fprintf(stderr, "putenv faile\n"); free(string); exit(1); } value = getenv(var); /* 获取新值 */ if (value) printf("new value of %s is %s\n", var, value); else printf("new value of %s is null???\n", var); } exit(0); } |
运行:
[root@localhost chap4]# ./environ HOME variable HOME has value /root [root@localhost chap4]# ./environ NOSUCHENV variable has no value [root@localhost chap4]# ./environ NOSUCHENV hello variable has no value calling putenv to NOSUCHENV=hello new value of NOSUCHENV is hello [root@localhost chap4]# ./environ NOSUCHENV variable has no value |
时间日期
time函数:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(void) { time_t the_time; int i; for (i=0; i<5; i++) { the_time = time((time_t *)0); printf("The time is : %ld\n", the_time); sleep(3); } exit(0); } |
运行:
[root@localhost chap4]# ./envtime The time is : 1242628048 The time is : 1242628051 The time is : 1242628054 The time is : 1242628057 The time is : 1242628060 |
localtime函数:
#include <unistd.h> #include <stdio.h> #include <time.h> #include <stdlib.h> int main(void) { struct tm *tm_ptr; time_t the_time; (void)time(&the_time); tm_ptr = localtime(&the_time); printf("Raw time is %ld\n", the_time); printf("localtime gives:\n"); printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year+1900, 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); exit(0); } |
运行:
[root@localhost chap4]# ./localtime; date Raw time is 1242637706 localtime gives: date: 2009/05/18 time:17:08:26 Mon May 18 17:08:26 HKT 2009 |
ctime函数:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { time_t timeval; time(&timeval); printf("the date is:%s\n", ctime(&timeval)); exit(0); } |
运行:
[root@localhost chap4]# ./ctime the date is:Mon May 18 17:12:37 2009 |
相关阅读 更多 +