ENV 
Author Message
 ENV

Quote:

>Is it possible to change a enviroment varible within a program

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> int main(int argc, char **argv)
> {

>   char *done_buff;

>     done_buff=getenv("DONE");
>     printf("\n%s\n",done_buff);
>}
>     /* value of Done is 1 */
>     Can I change the value of the Enviroment Varible DONE
>      within  the this current program
>      so next call to getenv("DONE")returns a New
>      Value - I am writing this on an IBM AIX Box

This is a frequently-asked question, and the answer can be found in the
comp.lang.c Frequently-Asked-Question list (FAQ), at
http://www.*-*-*.com/ ~scs/C-faq/top.html.  You will be particularly interested
in question 19.33, "How can a process change an environment variable in its
caller?".

--

Kenan Systems Corporation



Sun, 22 Apr 2001 03:00:00 GMT  
 ENV

Quote:

> use putenv()

<Jack>

There is no putenv() function in standard C.  There is no way to do this in
standard C.

</Jack>



Mon, 23 Apr 2001 03:00:00 GMT  
 ENV

Quote:



> > use putenv()

> If it exists, a lot of people are suprised to learn that putenv is *not*
> defined in the standard C library.

And if you do use it, be *real* careful to read the entire man page first.
Pay special attention to the warning about using automatic variables as
parameters.  We recently burned lots of cycles trying to find out why our
environment was getting munged, and found a putenv with a local string
buffer as parameter.  Tough bug to find.  As in:

int foo()
{
   char buf[1024];

   strcpy( buf, "ORACLE_SID=database" );
   putenv( buf );

Quote:
}

int bar()
{
   char * ptr;
   ptr = getenv( "ORACLE_SID" );
   printf( "%s\n", ptr );

Quote:
}

void yada( int yada )
{
   char buf[1024];
   sprintf( buf, "%d", yada );

Quote:
}

int main( void )
{
   int yada = 0;

   foo();
   yada( yada );
   bar();

Quote:
}
>  core dumped

(hint - ptr is NULL when it hits the printf ).


Mon, 23 Apr 2001 03:00:00 GMT  
 ENV
Those compilers that I use that support the setting of an environment
variable only set the variable for the current running programs
session and its child processes.

Which means the environment changes don't stay after your program
terminates.  

In DOS this can be done only though an extremely cyptic procedure
involving calling and undocumented function and scanning though a list
of structures that contain program and environment block information.

Quote:

>     /* value of Done is 1 */
>     Can I change the value of the Enviroment Varible DONE
>      within  the this current program
>      so next call to getenv("DONE")returns a New
>      Value - I am writing this on an IBM AIX Box



Tue, 24 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. setjmp(jmpbuf env) and longjump(jmpbuf env, int value)

2. Help Required regarding VC++7 Web Service Env.

3. setting my env - linux programming question

4. Is Env-Variable support standard ANSI?

5. Is Env-Variable support standard ANSI?

6. good book on C and/or C++ (linux env)

7. Help parsing env

8. CGI in Perl calling C functions: env variable init issue

9. VC5: no error detail (env rather than lang q)

10. need account to compile on unix ENV

11. memory in system using c in dos env.

12. IDE env

 

 
Powered by phpBB® Forum Software