Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do I export a environmental variable to a C program?

Reply
Thread Tools

How do I export a environmental variable to a C program?

 
 
iavian
Guest
Posts: n/a
 
      05-09-2008
Hi all ,

I want to set a env variable in a C program? . Not through shell env
variables. Any help?

Thanks
Vijay
 
Reply With Quote
 
 
 
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      05-09-2008
In article <(E-Mail Removed)>,
iavian <(E-Mail Removed)> wrote:
>Hi all ,
>
>I want to set a env variable in a C program? . Not through shell env
>variables. Any help?


How an environment variable gets set is beyond the scope of the C
language.
They're typically assigned through an OS-defined mechanism when the
process is created. A newsgroup whose scope includes how to start
processes on your OS may be able to provide more specific guidance.

Many C implementations also provide extensions that allow environment
variables to be set from within the program. Your implementation's
documentation may shed some light on that.


dave

--
Dave Vandervies dj3vande at eskimo dot com
He'll be buried face down, nine edge first. Unlike most people of whom
that is said, in his case, it's a compliment.
--Richard Bos, on John Backus, in comp.lang.c
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      05-09-2008
On 9 May 2008 at 17:26, iavian wrote:
> I want to set a env variable in a C program? . Not through shell env
> variables. Any help?


What do you mean "not through shell env variables"?

If you're on a POSIX system, check out setenv() and putenv().

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-09-2008
In article <(E-Mail Removed)>,
iavian <(E-Mail Removed)> wrote:

>I want to set a env variable in a C program? . Not through shell env
>variables. Any help?


C does not provide any mechanism to set environment variables.

It is common for OSes to provide a setenv() routine in their libraries.

Note that when OSes do so provide, often setenv() only affects
the environment of the running program and any programs that it
invokes after that point, such as by system() or by OS routines
such as exec*() . Unix-like systems often have *no* mechanism
that would allow a program to change the environment variables of
other programs that are already running.

--
"All is vanity." -- Ecclesiastes
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-09-2008
iavian <(E-Mail Removed)> wrote:
> I want to set a env variable in a C program? . Not through shell env
> variables. Any help?


This is not a C question since this only depends on how your
shell handles things. But, that out of the way, you simply
can't do that (at least with no shell I ever heard of). Your
C program is a process started by your shell and such a child-
process can never change anything in its "parent" process.

The only thing you can do is to have the the program output
something the shell then explicitely executes, like (e.g.
using bash):

---- env_cmd.c -----

#include <stdio.h>

int main( void )
{
puts( "export X=AAA" );
return 0;
}

--------------------

and then do e.g.

> gcc -o env_cmd env_cmd.c
> `./env_cmd` # note the backticks
> echo $X

AAA
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-09-2008
(E-Mail Removed) (Jens Thoms Toerring) writes:
> iavian <(E-Mail Removed)> wrote:
>> I want to set a env variable in a C program? . Not through shell env
>> variables. Any help?

>
> This is not a C question since this only depends on how your
> shell handles things. But, that out of the way, you simply
> can't do that (at least with no shell I ever heard of). Your
> C program is a process started by your shell and such a child-
> process can never change anything in its "parent" process.


The OP didn't seem to be asking about propagating the setting to the
parent process (assuming that "parent process" is meaningful). As far
as I can tell, the POSIX setenv() function (not standard C) would
satisfy the requirement he stated. Whether it would satisfy his
actual requirement is another question.

<OT>In Unix-like systems, there are various ways for a process to set
one of its own environment variables in response to information from a
child process. You demonstrated one of them in part of your article
that I ruthlessly snipped; other ways are possible.
comp.unix.programmer would be a better place to discuss it -- after
checking any FAQ lists, since this kind of question comes up
frequently.</OT>

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
iavian
Guest
Posts: n/a
 
      05-11-2008
Thanks for all who replied ..

I am writing a function for postgres in C which connect to Oracle
db .. postgres accepts a shared object to be loaded . So, when the .so
is loaded inside postgres , .so could find oracle environments.. I am
not sure what should be the best way to do it ..

whenever a row is inserted in postgres , a trigger will call this C
function which connects to oracle and inserts the record in Oracle db

Any pointers?

My problem is
On May 9, 2:52 pm, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Jens Thoms Toerring) writes:
>
> > iavian <(E-Mail Removed)> wrote:
> >> I want to set a env variable in a C program? . Not through shell env
> >> variables. Any help?

>
> > This is not a C question since this only depends on how your
> > shell handles things. But, that out of the way, you simply
> > can't do that (at least with no shell I ever heard of). Your
> > C program is a process started by your shell and such a child-
> > process can never change anything in its "parent" process.

>
> The OP didn't seem to be asking about propagating the setting to the
> parent process (assuming that "parent process" is meaningful). As far
> as I can tell, the POSIX setenv() function (not standard C) would
> satisfy the requirement he stated. Whether it would satisfy his
> actual requirement is another question.
>
> <OT>In Unix-like systems, there are various ways for a process to set
> one of its own environment variables in response to information from a
> child process. You demonstrated one of them in part of your article
> that I ruthlessly snipped; other ways are possible.
> comp.unix.programmer would be a better place to discuss it -- after
> checking any FAQ lists, since this kind of question comes up
> frequently.</OT>
>
> --
> Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
> Nokia
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-11-2008
iavian <(E-Mail Removed)> writes:
> I am writing a function for postgres in C which connect to Oracle
> db .. postgres accepts a shared object to be loaded . So, when the .so
> is loaded inside postgres , .so could find oracle environments.. I am
> not sure what should be the best way to do it ..
>
> whenever a row is inserted in postgres , a trigger will call this C
> function which connects to oracle and inserts the record in Oracle db
>
> Any pointers?


I think you've already been advised to ask in comp.unix.programmer (I
assume you're using a Unix-like system). That's the best advice
you're going to get here.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
is it possible to override a 'read only' environmental variable like$" ? timr Ruby 2 01-20-2009 10:47 AM
problem in setting environmental variable rameshotn3@gmail.com Perl Misc 4 03-05-2007 09:19 PM
possible to use an environmental variable to address the XSL file path? Kourosh XML 2 05-20-2006 03:35 AM
Environmental variable for CGI proxy John Smith Perl Misc 1 05-16-2006 02:47 PM



Advertisments