playing with enviornment variables..
CD4 (28)
Nov 17, 2009 at 12:14pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char *envstrn;
int main( int argc,char *argv[]) {
if (argc==2) {
envstrn=getenv(argv[1]);
printf("%s\n" ,envstrn);
}
return 0;
}
when the above program is rum with arguments HOME OR SHELL OR PATH , it gives results. But when OSTYPE is used i gives segmentation fault??
regards
Last edited on Nov 17, 2009 at 12:15pm UTC
jsmith (5804)
Nov 17, 2009 at 1:16pm UTC
it returns a null pointer and you aren't checking.
CD4 (28)
Nov 17, 2009 at 2:01pm UTC
i also know that its returning a null pointer.. but why... when i normally give
echo $OSTYPE iget back my os??
regards
Seph (2)
Nov 17, 2009 at 3:12pm UTC
Hello !!
It's because echo work on diferent form, I think...
You can add these lines to fix this problem in your program:
( The lines in bold ).
if(argc==2) {
envstrn=getenv(argv[1]);
if(envstrn != NULL)
printf("%s\n",envstrn);
else
printf("%s\n", "Environtment variable not found.");
}
CD4 (28)
Nov 17, 2009 at 3:58pm UTC
hello seph.. so you mean that otherwise the program is fine.. actually i dint prepare for that NULL because i dint think that OSTYPE wont return a value as it was doing in the console.. Thanks for ur help.. i was great.. :-)
regards
writetonsharma (1181)
Nov 18, 2009 at 9:09am UTC
echo itself is a program which takes care of all these things, your program should take care of these errors.
Topic archived. No new replies allowed.