#include #include #include #include /* FUNCTION: Find all environmental variables beginning with specified prefix and print them in the syntax of a csh(1) setenv command; like the printenv(1) command does for sh(1). USAGE: cprintenv # print entire environment cprintenv -p PREFIX # print environmental variables beginning with PREFIX PURPOSE: env(1) and printenv(1) print the environment table in Bourne shell syntax; need capability to write variables in csh/tcsh syntax. AUTHOR: John S. Urban, May 2009 DOES YOUR SYSTEM ALLOW?: setenv '\!::' 'string' setenv '::' 'string' */ extern char **environ; printquoted(char *p){ int i; char c; putchar('\''); for (i = 0; i < strlen(p); i++){ c = *(p+i); switch (c){ case '\'': putchar ('\''); putchar ('\\'); putchar ('\''); putchar ('\''); break; case '!': putchar ('\\'); putchar (c); break; default: putchar (c); break; } } putchar('\''); } main( int argc, char **argv){ char **ep = environ; char *p; char *e; int i; long int plen; char c; int p_opt=0; /* specify prefix */ char *prefix=NULL; int Z_opt=0; /* filter mode */ int l_opt=0; /* list mode */ /* getopt */ static char usage[]="usage: cprintenv [-p PREFIX]\n"; extern char *optarg; int s; plen=0; while ((s = getopt(argc,argv,"Zp:l?uh")) != EOF){ switch(s){ case 'p': p_opt=1; prefix = optarg; plen=strlen(prefix); break; case 'Z': Z_opt=1; break; case 'l': l_opt=1; break; case '?': case 'u': case 'h': fprintf(stderr,"%s",usage); exit(2); break; } } while ((p = *ep++)){ if(plen==0 | strncmp(prefix,p,plen)==0 ){ e=index(p,'='); *e='\0'; printf("setenv "); printquoted(p); putchar(' '); printquoted(e+1); putchar('\n'); } } return 0; } /* ================================================================================================ */ /* If system does not support environ variable then in general, the environ variable is also passed as the third, optional, parameter to main(); that is, for example: #include #include int main(int argc, char **argv, char **envp) { char *p; char *e; while ((p = *envp++)) if(strncmp("CPE_",p,4)==0){ e=index(p,'='); *e='\0'; printf("setenv %s '%s'\n", p,e+1); } return 0; } */