post  playing with enviornment variables..

CD4 (28)   Link to this post
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
jsmith (3802)   Link to this post
it returns a null pointer and you aren't checking.
CD4 (28)   Link to this post
i also know that its returning a null pointer.. but why... when i normally give
echo $OSTYPE iget back my os??


regards
Seph (2)   Link to this post
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)   Link to this post
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 (832)   Link to this post
echo itself is a program which takes care of all these things, your program should take care of these errors.

This topic is archived - New replies not allowed.