|
prtenv命令行选项:
-i:不区分大小写
- [color=red]prtenv -i path[/color]
- path:
- /home/lbc/sh:/home/lbc/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:
- /usr/games:/usr/j2sdk141_01/bin:
复制代码
-m:分行显示,有点问题,最后的.(当前目录)没显示出来
- [color=red]prtenv -im path[/color]
- path:
- /home/lbc/sh
- /home/lbc/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/bin/X11
- /usr/games
- /usr/j2sdk141_01/bin
复制代码
-n:打印环境变量名
- [color=red]prtenv -n[/color]
- HZ, SSH_AGENT_PID, KDE_MULTIHEAD, SHELL, TERM, GS_LIB, GTK_RC_FILES,
- WINDOWID, HUSHLOGIN, QTDIR, KDE_STARTUP_ENV, USER, LS_COLORS,
- SSH_AUTH_SOCK, KDEDIR, SESSION_MANAGER, XPSERVERLIST, MAIL, PATH, PWD,
- JAVA_HOME, XMODIFIERS, LANG, COLORFGBG, SHLVL, HOME, LOGNAME, LC_CTYPE,
- DISPLAY, COLORTERM, XAUTHORITY, _, OLDPWD
复制代码
- /* prtenv.c
- * Binary Li
- * 2003-3-28
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- extern char **environ;
- int o_i, o_m, o_n; /* 命令行选项imn对应的变量 */
- char *o_d = ":"; /* 本来想加上命令行-d,自定义分隔符,后来发现用处不大,
- * 就没有实现 */
- void printenv(char *);
- void printname(void);
- int
- main(int argc, char *argv[])
- {
- extern int optind;
- char c, **en;
- int i;
- en = environ;
- if(argc == 1){ /* 无命令行参数时打印全部环境变量 */
- while(*en)
- printf("%s\n", *en++);
- }else{
- while((c = getopt(argc, argv, "imn")) != -1){
- switch(c){
- case 'i':
- o_i = 1;
- break;
- case 'm':
- o_m = 1;
- break;
- case 'n':
- o_n = 1;
- break;
- default:
- fprintf(stderr, "usage: %s [-i|-m|-n] [environment]\n", argv[0]);
- exit(-1);
- }
- }
- if(o_n)
- printname();
- for(i = optind; i < argc; i++)
- printenv(argv[i]);
- }
-
- exit(0);
- }
- void
- printenv(char *s) /* 打印单个环境变量 */
- {
- int (*cmp)(const char *, const char *, size_t);
- char **en;
- char *tmp, *t;
-
- en = environ;
-
- if(o_i) /* true:不区分大小写 */
- cmp = strncasecmp;
- else
- cmp = strncmp;
- while(*en){
- if(!(*cmp)(s, *en, strlen(s)))
- break;
- en++;
- }
-
- printf("%s:\n", s);
- if(*en == NULL){
- printf(" not found!\n");
- return;
- }
-
- tmp = (char *)malloc(strlen(*en) + 1); /* strtok会改变传入的字符串,
- * 所以必须对副本操作 */
- strcpy(tmp, *en);
- t = strtok(tmp, "=");
- if(o_m)
- while((t = strtok(NULL, o_d)) != NULL)
- printf(" %s\n", t);
- else
- printf(" %s\n", tmp + strlen(t) + 1);
- free(tmp);
- }
- void
- printname(void) /* 打印所有环境变量名 */
- {
- char **en, *t, tmp[80];
- int f = 0;
-
- en = environ;
- while(*en){
- if(f) /* 避免输出最后一个变量名时后跟", " */
- printf(", ");
- else
- f = 1;
- strncpy(tmp, *en, 79);
- t = strtok(tmp, "=");
- printf("%s", t);
- en++;
- }
- printf("\n");
- }
复制代码 |
|