By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

Environment Variable - Parent Process --- TIA

P: n/a
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this:

main(int argc, char *argv[])
{
char *var;
var = (char *)malloc(1028);

strcpy(var, "VAR=");
strcat(var, argv[1]);

putenv(var);
return(0);
}

The above I just did now on the fly so it may not be perfect but it
will get the idea across. Now once this exits, I want to see this
variable in my environment. Is this possible?

Thank you
Nov 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment variable to the parent shell? I would like to do something like this:

main(int argc, char *argv[])
{
char *var;
var = (char *)malloc(1028);

strcpy(var, "VAR=");
strcat(var, argv[1]);

putenv(var);
return(0);
}

The above I just did now on the fly so it may not be perfect but it
will get the idea across. Now once this exits, I want to see this
variable in my environment. Is this possible?

Thank you


Watch out, you're going to get it from clc's wannabe enforcer Keith
Thompson.

Nov 14 '05 #2

P: n/a
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this:

main(int argc, char *argv[])
{
char *var;
var = (char *)malloc(1028);

strcpy(var, "VAR=");
strcat(var, argv[1]);

putenv(var);
return(0);
}

The above I just did now on the fly so it may not be perfect but it
will get the idea across. Now once this exits, I want to see this
variable in my environment. Is this possible?


This is more of a Unix question. C does not know anything about shells
and environment variables.

Try comp.unix.programmer

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3

P: n/a
TTroy wrote:
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this: [...]


Watch out, you're going to get it from clc's wannabe enforcer Keith
Thompson.


No, no: My turn! Let me! Meee!

A quick quiz for Jimmy C.: What is the output of the
following program?

#include <stdio.h>
#include <string.h>
static void test(const char *p, const char *q) {
printf ("%s %c= %s\n",
p, strcmp(p, q) ? '!' : '=', q);
}
int main(void) {
test ("C", "UNIX");
test ("comp.lang.c", "comp.unix.programmer");
printf ("Off-topic: ");
test ("What you ask", "possible");
return 0;
}

Answer and advice, all in one neat little package.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #4

P: n/a
Eric Sosman wrote:
TTroy wrote:
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment
variable to the parent shell? I would like to do something
like this: [...]
Watch out, you're going to get it from clc's wannabe enforcer
Keith Thompson.


No, no: My turn! Let me! Meee!

A quick quiz for Jimmy C.: What is the output of the
following program?

.... snip code ...
Answer and advice, all in one neat little package.


I have taken the liberty of reformatting your package:

#define O00 static
#define O0O void
#define O0o test
#define OO0 const
#define OOO char
#define OOo printf
#define Oo0 strcmp
#define OoO int
#define Ooo main
#define o00 return
#include <stdio.h>
#include <string.h>

O00 O0O O0o(OO0 OOO *p, OO0 OOO *q) { OOo ("%s %c= %s\n", p,
Oo0(p, q) ? '!' : '=', q); } OoO Ooo(O0O) { O0o ("C", "UNIX");
O0o ("comp.lang.c", "comp.unix.programmer"); OOo ("Off-topic: ");
O0o ("What you ask", "possible"); o00 0; }

(my id2id package comes in handy here)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5

P: n/a
CBFalconer wrote:
Eric Sosman wrote:
TTroy wrote:
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment
variable to the parent shell? I would like to do something
like this: [...]

Watch out, you're going to get it from clc's wannabe enforcer
Keith Thompson.


No, no: My turn! Let me! Meee!

A quick quiz for Jimmy C.: What is the output of the
following program?


... snip code ...
Answer and advice, all in one neat little package.

I have taken the liberty of reformatting your package:

#define O00 static
#define O0O void
#define O0o test
#define OO0 const
#define OOO char
#define OOo printf
#define Oo0 strcmp
#define OoO int
#define Ooo main
#define o00 return
#include <stdio.h>
#include <string.h>

O00 O0O O0o(OO0 OOO *p, OO0 OOO *q) { OOo ("%s %c= %s\n", p,
Oo0(p, q) ? '!' : '=', q); } OoO Ooo(O0O) { O0o ("C", "UNIX");
O0o ("comp.lang.c", "comp.unix.programmer"); OOo ("Off-topic: ");
O0o ("What you ask", "possible"); o00 0; }

(my id2id package comes in handy here)


Aw, please do not propagate this further -- in the end some
newbie thinks "Wow, this is kinda cool"... And making it into
the annals of c.l.c as the one who instigated the "O0o" style
is probably not your intention ;-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #6

P: n/a
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this: [...]


It is possible in unix, but not in C.

See the tset command, and the X11 resize command.

-- glen

Nov 14 '05 #7

P: n/a
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
Jimmy Cracker wrote:

Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this: [...]
It is possible in unix, but not in C.
Please, don't give OT answers. Your answer cannot be cross-verified here.
Eric Sosman's answer contains a hint where this question should
be asked and answers given.
See the tset command, and the X11 resize command.


[OT] I believe your answer is wrong (I haven't seen a *nix system
where a child process could modify the environment of its parent).
The above examples are not appropriate for OP's question; you might
not understand what those commands do.
AFAICT only the shell can change its environment variables; it can
be invoked by a special command, or by other means (eg. a signal).

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #8

P: n/a
Sorry, I didn't realize. I will go to comp.unix.programmer. I didn't
mean to cause trouble on this group.

Thanks all!
On Sat, 05 Feb 2005 22:55:04 +0100, Michael Mair
<Mi**********@invalid.invalid> wrote:
Jimmy Cracker wrote:
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this:

main(int argc, char *argv[])
{
char *var;
var = (char *)malloc(1028);

strcpy(var, "VAR=");
strcat(var, argv[1]);

putenv(var);
return(0);
}

The above I just did now on the fly so it may not be perfect but it
will get the idea across. Now once this exits, I want to see this
variable in my environment. Is this possible?


This is more of a Unix question. C does not know anything about shells
and environment variables.

Try comp.unix.programmer

Cheers
Michael


Nov 14 '05 #9

P: n/a
Jimmy Cracker wrote:

Sorry, I didn't realize. I will go to comp.unix.programmer. I
didn't mean to cause trouble on this group.


When you get there do not top-post. Your answer belongs after (or
intermixed with) the material to which you reply, with all
non-germane matter snipped out.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #10

P: n/a
In article <42***************@yahoo.com>, cb********@yahoo.com says...
Eric Sosman wrote:
TTroy wrote:
Jimmy Cracker wrote:

Is it completely impossible in UNIX to push an environment
variable to the parent shell? I would like to do something
like this: [...]

Watch out, you're going to get it from clc's wannabe enforcer
Keith Thompson.


No, no: My turn! Let me! Meee!

A quick quiz for Jimmy C.: What is the output of the
following program?

... snip code ...

Answer and advice, all in one neat little package.


I have taken the liberty of reformatting your package:

#define O00 static
#define O0O void
#define O0o test
#define OO0 const
#define OOO char
#define OOo printf
#define Oo0 strcmp
#define OoO int
#define Ooo main
#define o00 return
#include <stdio.h>
#include <string.h>

O00 O0O O0o(OO0 OOO *p, OO0 OOO *q) { OOo ("%s %c= %s\n", p,
Oo0(p, q) ? '!' : '=', q); } OoO Ooo(O0O) { O0o ("C", "UNIX");
O0o ("comp.lang.c", "comp.unix.programmer"); OOo ("Off-topic: ");
O0o ("What you ask", "possible"); o00 0; }

(my id2id package comes in handy here)


I wonder if gnu indent will support this...

indent -falconer foo.c :-)

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #11

P: n/a
Randy Howard wrote:

In article <42***************@yahoo.com>, cb********@yahoo.com says...
Eric Sosman wrote:
TTroy wrote:
> Jimmy Cracker wrote:
>
>> Is it completely impossible in UNIX to push an environment
>> variable to the parent shell? I would like to do something
>> like this: [...]
>
> Watch out, you're going to get it from clc's wannabe enforcer
> Keith Thompson.

No, no: My turn! Let me! Meee!

A quick quiz for Jimmy C.: What is the output of the
following program?

... snip code ...

Answer and advice, all in one neat little package.


I have taken the liberty of reformatting your package:

#define O00 static
#define O0O void
#define O0o test
#define OO0 const
#define OOO char
#define OOo printf
#define Oo0 strcmp
#define OoO int
#define Ooo main
#define o00 return
#include <stdio.h>
#include <string.h>

O00 O0O O0o(OO0 OOO *p, OO0 OOO *q) { OOo ("%s %c= %s\n", p,
Oo0(p, q) ? '!' : '=', q); } OoO Ooo(O0O) { O0o ("C", "UNIX");
O0o ("comp.lang.c", "comp.unix.programmer"); OOo ("Off-topic: ");
O0o ("What you ask", "possible"); o00 0; }

(my id2id package comes in handy here)


I wonder if gnu indent will support this...

indent -falconer foo.c :-)


works fine (v2.2.9):

[1] c:\c\ohohohs>indent -st ohohoh.c
#define O00 static
#define O0O void
#define O0o test
#define OO0 const
#define OOO char
#define OOo printf
#define Oo0 strcmp
#define OoO int
#define Ooo main
#define o00 return
#include <stdio.h>
#include <string.h>

O00 O0O
O0o(OO0 OOO * p, OO0 OOO * q)
{
OOo("%s %c= %s\n", p, Oo0(p, q) ? '!' : '=', q);
}

OoO
Ooo(O0O)
{
O0o("C", "UNIX");
O0o("comp.lang.c", "comp.unix.programmer");
OOo("Off-topic: ");
O0o("What you ask", "possible");
o00 0;
}

In fact, with the file idpairs (related to the #defines) around:

[1] c:\c\ohohohs>type idpairs
O00 static
O0O void
O0o test
OO0 const
OOO char
OOo printf
Oo0 strcmp
OoO int
Ooo main
o00 return

you can do a complete conversion in one line:

[1] c:\c\ohohohs>indent -st ohohoh.c | id2id
#define static static
#define void void
#define test test
#define const const
#define char char
#define printf printf
#define strcmp strcmp
#define int int
#define main main
#define return return
#include <stdio.h>
#include <string.h>

static void
test(const char * p, const char * q)
{
printf("%s %c= %s\n", p, strcmp(p, q) ? '!' : '=', q);
}

int
main(void)
{
test("C", "UNIX");
test("comp.lang.c", "comp.unix.programmer");
printf("Off-topic: ");
test("What you ask", "possible");
return 0;
}

which is much easier than the gyrations I had to go through to make
it in the first place. :-)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #12

P: n/a
On Sun, 06 Feb 2005 13:24:17 +0000, S.Tobias wrote:
command, and the X11 resize command.

[OT] I believe your answer is wrong (I haven't seen a *nix system
where a child process could modify the environment of its parent).


Such a system would be terminally broken from a security viewpoint.

Lawrence
Nov 14 '05 #13

P: n/a
glen herrmannsfeldt <ga*@ugcs.caltech.edu> writes:
Jimmy Cracker wrote:

Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this: [...]


It is possible in unix, but not in C.

See the tset command, and the X11 resize command.


<OT>
Those commands are examples of ways a parent process can set its own
environment variables based in information from a child process. They
do not illustrate propagating an environment variable directly to a
parent process, which is generally not possible in Unix.
</OT>

The only facility C provides is the getenv() function, whose behavior
is almost entirely system-specific. It allows querying a specific
environment variable; there's no portable way either to set a variable
or to get a list of all environment variables. A conforming C
implementation could be perfectly happy on a system on which child
processes can propagate environment variables to their parents, or
even to unrelated processes. The C standard says nothing about
processes at all.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.