Environment Variable - Parent Process --- TIA

J

Jimmy Cracker

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
 
T

TTroy

Jimmy said:
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.
 
M

Michael Mair

Jimmy said:
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

Eric Sosman

TTroy said:
Jimmy said:
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.
 
C

CBFalconer

Eric said:
TTroy said:
Jimmy said:
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)
 
M

Michael Mair

CBFalconer said:
Eric said:
TTroy said:
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
 
S

S.Tobias

glen herrmannsfeldt said:
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).
 
J

Jimmy Cracker

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

Thanks all!


Jimmy said:
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
 
C

CBFalconer

Jimmy said:
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.
 
R

Randy Howard

Eric said:
TTroy said:
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 :)
 
C

CBFalconer

Randy said:
Eric said:
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. :)
 
L

Lawrence Kirby

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
 
K

Keith Thompson

glen herrmannsfeldt said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top