View environment variables with command pass parameters
/*************************************************************************> File name:env.c> Author: > Mail: > Created time:tue 10:42:21 PM PST **************************************************************** /#include <stdio.h> #include <stdlib.h>int main (int argc,char**argv) {char*p,*str; if (argc==1| | argc>3) {printf ("Parameter Num is wrong!\n"); } else if (argc==2) {if ((P=getenv (argv[1))) {printf ("environment variable%s =%s\n", argv[ 1],P); }} else if (argc==3) {p= (char*) malloc (strlen (argv[1]) +strlen (argv[2]) +2); strcpy (p,argv[1]); Strcat (p, "="); strcat (p,argv[2]); if (Putenv (p) ==0) {printf ("Put env successful\n"); } if (Str=getenv (argv[1])) {printf ("Put Env%s:%s\n", argv[1],str); }else {printf ("Get Env error!\n"); }//can not free on the front of This position free (p); } exit (0);}
View Global Environment variables
/*************************************************************************> File name:showenv.c> Author: > Mail: > Created time:tue 11:17:39 PM PST ************************************************************ /#include <stdio.h> #include <stdlib.h>extern char * * environ; int main (int argc,char** argv) { char **penv=environ; while (*penv) { printf ("%s\n", *penv); penv++; } Exit (0);}
Time operation
/*************************************************************************> File name:time.c> Author: > Mail: > Created time:wed 12:11:00 AM PST **************************************************************** /#include <stdio.h> #include <time.h>int main (int argc,char**argv) { char timebuf[256]; time_t Tmt=time ((time_t*) NULL); printf ("Unformat Time:%d\n", TMT); printf ("Format time:%s\n", CTime (LocalTime (&tmt))); Strftime (timebuf,256, "%m:%d", LocalTime (&tmt)); printf ("Format time:%s\n", timebuf); Exit (0);}
Get login uid GID user
/*************************************************************************> File name:uid.c> Author: > Mail: > Created time:wed 12:51:53 AM PST **************************************************************** /#include <stdio.h> #include <unistd.h> #include <sys/types.h>int main () { printf ("UID: %d\n ", Getuid ()); printf ("gid:%d\n", Getgid ()); printf ("currentlogined:%s\n", GetLogin ()); printf ("gethostid:%d\n", Gethostid ()); exit (0);}
Pass a simple program parameter
/************ > File name:option.c> Author: > Mail: > Created time:tue 06:30:00 PM PST ************************************************************************/ #include <stdio.h> #include <unistd.h>int main (int argc,char**argv) {int opt; while ((Opt=getopt (ARGC,ARGV, ": if:x")!=-1) {switch (opt) {case '? ': printf ("Unknow optio n:%c \ n ", optopt); Break Case ': ': printf ("Option:%c Need Value\n", optopt); Break Case ' F ': printf ("File is%s \ n", Optarg); Break Case ' I ': Case ' x ': printf ("Option:%c\n", opt); break; }} for (; optind<argc;optind++) {printf ("arguments:%s \ n", Argv[optind]); } exit (0);}
more complete Transfer parameters
/*************************************************************************> File name:option2.c> Author: > Mail: > Created time:tue 09:16:58 PM PST ************************************************************ /#include <stdio.h> #include <unistd.h> #include <getopt.h> #define _gnu_source int Main ( int argc,char**argv) {int opt; struct option optlist[]={{"Add", 0,null, ' a '}, {"File", 1,null, ' f '}, {0,0,0,0}}; while ((Opt=getopt_long (ARGC,ARGV, ": AF:XCV", Optlist,null))!=-1) {switch (opt) {case ' a ': Case ' x ': Case ' C ': Case ' V ': printf ("option:%c\n", opt); Break Case ': ': printf ("Option has value!\n"); Break Case ' F ': printf ("File is%s\n", optarg); break; Case '? ': printf ("Option is unknow!\n"); break; } } for (; optind<argc;optind++) {printf ("remained Parameter:%s\n", Argv[optind]); } exit (0);}
Linux C Programming (4)----Operating environment variables, program pass parameters getopt Getopt_long operation, acquisition time