I have codes to get environment variable name from stand input and call library function getenv()
to get the value and output.
Here are codes.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLEN 256
int main(void)
{
char *envName;
char *envValue;
char *envEntry;
char *envVar=(char*)malloc(sizeof(char)*MAXLEN);
while(NULL!=(envName=gets(envVar))){
envValue=getenv(envName);
if(envValue){
printf("%s\n",envValue);
envEntry=strtok(envValue,";");
while(envEntry){
printf("%s\n",envEntry);
envEntry=strtok(NULL,";");
}
}else{
printf("not found\n");
}
}
return 0;
}
If the environment variable "path"
contains "a;b;c;d"
.
While it runs,if type a string "path" first time,it works well,but if type "path" again it only gets the first string like"a"
or "a;b"
.