playing with enviornment variables..

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
it returns a null pointer and you aren't checking.
i also know that its returning a null pointer.. but why... when i normally give
echo $OSTYPE iget back my os??


regards
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.");
    }
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
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.