Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

environmental variable in C program

Status
Not open for further replies.

sramki

Programmer
May 8, 2001
27
0
0
IN
hi all

i would like to know how i can use some the environmental variable inside a c program .
iam working in linux platform .
If u can give me an idea it would be great

thanks

ramki
 
You can use the getenv/putenv C functions
to get/set an environment variable.

The environment variables are available to your
program through an argument to main as char** envp;

getenv(char *str) gets the value of the environement
variable whose name is str;

putenv(char *str) sets the environment variable
where str is a name=value pair.

main(int argc, char** argc, char** environp)
{

char* env1;
char envstr[80];

env1=getenv("MYVAR1");
printf("%s\n", env1);
env1="NEW VALUE";
sprintf(envstr,"MYVAR1=%s", env1);
putenv(envstr);
}

For more info, try the man pages of getenv/putenv.

abp :cool:
 
hi abp

i have given the code here .
somewhere there is a segmentation fault .
iam unable to find out .could u help me .

the main aim of the code is to run a particular process
"get" at an interval as specified in the ENv variable "TIME" .
The process is getting executed once but the second time iam getting a segmentation fault error .
iam not able to pass through it .
if can put ur idea it would be useful.

ramki

THE CODE :
-----------


#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include &quot;/home/ipcpe/process/lib/ERR_logerror.c&quot;
#define CONFIG &quot;/home/ipcpe/process/config.ini&quot;
#define YES 1
#define NO 0
#define TRUE 1
#define FALSE 0
/*********Global Variables ***********/
int CDRcoll_pid = -1; /* pid of the get process */
int CDRdaemon_pid; /* pid of this daemon */
FILE *fp1;
/******************Function Declaration *******************/
int collt();
void sigtrap();
void sigcld();
void sigalrm();
main()
{
char *ptr,*getenv();
char c;
int sig,i;
/********** Change to the root ************/
chdir(&quot;/&quot;);
/************ Mask the file permissions*******/
umask(0);
/**************** set the effective gid and pid ************/
setpgrp();
/**********The Following Signals are ignored *********/
signal(SIGHUP,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);

/***********For the following Signals execute sigtrap ************/

signal(SIGILL,sigtrap);
signal(SIGIO,sigtrap);
signal(SIGIOT,sigtrap);
signal(SIGPOLL,sigtrap);


signal(SIGCLD,sigcld);
signal(SIGALRM,sigalrm);
while(1)
{
/*
fp1=fopen(&quot;/home/ipcpe/process/test&quot;,&quot;r&quot;);

if(fp1==NULL)
{
printf(&quot;Error opening file \n&quot;);
exit(1);
}

rewind(fp1);
for(c=fgetc(fp1); !feof(fp1) && c != '=';c=fgetc(fp1));
fscanf(fp1,&quot;%d&quot;,&i);
fclose(fp1);
*/
if( ( ptr = getenv(&quot;TIME&quot;)) == (char *) 0)
{
printf(&quot;TIME is not defined \n&quot;);
}
else
{
i=atoi(ptr);
printf(&quot;%d\n&quot;,i);
}
alarm(i);
sleep(i);
}
}

 
Hi sramki

I tried to compile your program on a Sun Sparc but
i got the following unresolveds.

sigtrap
sigcld
sigalrm.

You are including a file which is not included in your
program. If I remove references to the above symbols I dont
get a segmentation fault when I run a 2nd time.

abp :&quot;>
 
hey

i solved it by just giving a NULL in the wait function .
could someone tell me what is the difference between wait();
and wait(NULL);

thanks abp for ur efforts

ramki
 
I dont know if it escaped me, but I cant find a wait()
used in your program!

abp :cool:
 
sorry friends i forgot to give u the rest of the code where i have used a wait .

i had given wait with a parameter ie wait();

there ocured a segmentation fault because of this .
then i tried wait(NULL); it worked

but tell me what is the difference btw these .

anyway thanks for ur effort .
if u would like to see the whole code here it is :

CODE
-----
#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include &quot;/home/ipcpe/process/lib/ERR_logerror.c&quot;

#define CONFIG &quot;/home/ipcpe/process/config.ini&quot;
#define YES 1
#define NO 0

#define TRUE 1
#define FALSE 0

/*********Global Variables ***********/

int CDRcoll_pid = -1; /* pid of the get process */

int CDRdaemon_pid; /* pid of this daemon */

FILE *fp1;

/******************Function Declaration *******************/
int collt();
void sigtrap();
void sigcld();
void sigalrm();

main()
{
char *ptr,*getenv();
char c;
int sig,i;

/********** Change to the root ************/

chdir(&quot;/&quot;);

/************ Mask the file permissions*******/

umask(0);

/**************** set the effective gid and pid *************/

setpgrp();

/**********The Following Signals are ignored *********/
signal(SIGHUP,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);

/***********For the following Signals execute sigtrap ************/

signal(SIGILL,sigtrap);
signal(SIGIO,sigtrap);
signal(SIGIOT,sigtrap);
signal(SIGPOLL,sigtrap);


signal(SIGCLD,sigcld);
signal(SIGALRM,sigalrm);


while(1)
{
/*
fp1=fopen(&quot;/home/ipcpe/process/test&quot;,&quot;r&quot;);

if(fp1==NULL)
{
printf(&quot;Error opening file \n&quot;);
exit(1);
}

rewind(fp1);
for(c=fgetc(fp1); !feof(fp1) && c != '=';c=fgetc(fp1));
fscanf(fp1,&quot;%d&quot;,&i);
fclose(fp1);
*/
if( ( ptr = getenv(&quot;TIME&quot;)) == (char *) 0)
{
printf(&quot;TIME is not defined \n&quot;);
}
else
{
i=atoi(ptr);
printf(&quot;%d\n&quot;,i);
}

alarm(i);
sleep(i);

}

}

void sigtrap(int sig )
{
}

void sigcld()
{
}

void sigalrm(int sig)
{

static int k=0;

if((CDRcoll_pid = fork()) == -1)
{
ERR_LOGERROR(&quot;7000&quot;,errno,&quot;&quot;);
exit(1);
}
if(CDRcoll_pid == 0)
{
execl(&quot;/home/ipcpe/process/get&quot;,(char *)0,(char *)0);
ERR_LOGERROR(&quot;7001&quot;,errno,&quot;&quot;);
exit(1);
}

k=k+1;
printf(&quot;waiting for child to die for %d time \n&quot;,k);
wait(NULL);
printf(&quot; child is dead for %d time \n&quot;,k);
return;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top