#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc,char **argv){ char *var,*value; if(argc==1||argc>3) { 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",var); if(argc==3) { char *string; value=argv[2]; string=malloc(strlen(var)+strlen(value)+2); if(!string) { fprintf(stderr,"out of memoryy\n"); exit(1); } strcpy(string,var); strcat(string,"="); strcat(string,value); printf("Calling putenv with: %s\n",string); if(putenv(string)!=0) { 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);}
Function prototype:
# Include <stdlib. h>
Char * getenv (const char * Name); if the environment variable does not exist, null is returned.
Int putenv (const char * string);-1 is returned if an environment variable fails to be added.