[GRASS-dev] trying to set environment vars from a C program

hi all,

I am working on a set of modules to integrate GRASS with R.
For this I need to read and set some global variables for R scripts to
use.
I would like to use shell environment variables for this and
set them through a C module.
However, if I use the C function setenv() in program foo.c, it seems to
only add variables to the environment of foo.c.
When foo.c terminates, all variables set with senenv() will be gone.

I have not found a way to modify the environment of the shell that
starts foo.c so that the new variables will be available in the calling
shell after foo.c has exited.

Is this somehow possible or will I have to fall back to storing
variables and values in an ASCII file?

Thanks,

Benjamin

--
Benjamin Ducke, M.A.
Archäoinformatik
(Archaeoinformation Science)
Institut für Ur- und Frühgeschichte
(Inst. of Prehistoric and Historic Archaeology)
Christian-Albrechts-Universität zu Kiel
Johanna-Mestorf-Straße 2-6
D 24098 Kiel
Germany

Tel.: ++49 (0)431 880-3378 / -3379
Fax : ++49 (0)431 880-7300
www.uni-kiel.de/ufg

On Dec 17 [16:39], Benjamin Ducke wrote:

hi all,

I would like to use shell environment variables for this and
set them through a C module.

How about
system("export VAR=value");
?
Needs some adaptation for different shells and cross-platform-use, though.

\Flo.

On Dec 17 [16:39], Benjamin Ducke wrote:

I would like to use shell environment variables for this and
set them through a C module.

Just seen: there is a G_system(), besides system()

Is this somehow possible or will I have to fall back to storing
variables and values in an ASCII file?

afaik, G_setenv() writes to your .grassrc

\Flo.

Benjamin Ducke wrote:

I am working on a set of modules to integrate GRASS with R.
For this I need to read and set some global variables for R scripts to
use.
I would like to use shell environment variables for this and
set them through a C module.
However, if I use the C function setenv() in program foo.c, it seems to
only add variables to the environment of foo.c.
When foo.c terminates, all variables set with senenv() will be gone.

I have not found a way to modify the environment of the shell that
starts foo.c so that the new variables will be available in the calling
shell after foo.c has exited.

Is this somehow possible

No. One process cannot modify the environment of another process
(other than by directly modifying that process' address space, which
isn't really a viable option).

That's why export (sh) and setenv (csh) are shell built-in functions;
a separate program wouldn't work.

or will I have to fall back to storing variables and values in an
ASCII file?

That's why GRASS has $GISRC for "GRASS variables", i.e. so that you
can have programs which record information for subsequent use by other
programs.

--
Glynn Clements <glynn@gclements.plus.com>

Yes, G_getenv/G_setenv seems to be the best solution.
Thanks a lot for the hint!

Benjamin

Florian Kindl wrote:

On Dec 17 [16:39], Benjamin Ducke wrote:

I would like to use shell environment variables for this and
set them through a C module.

Just seen: there is a G_system(), besides system()

Is this somehow possible or will I have to fall back to storing
variables and values in an ASCII file?

afaik, G_setenv() writes to your .grassrc

\Flo.

_______________________________________________
grass-dev mailing list
grass-dev@grass.itc.it
http://grass.itc.it/mailman/listinfo/grass-dev

--
Benjamin Ducke, M.A.
Archäoinformatik
(Archaeoinformation Science)
Institut für Ur- und Frühgeschichte
(Inst. of Prehistoric and Historic Archaeology)
Christian-Albrechts-Universität zu Kiel
Johanna-Mestorf-Straße 2-6
D 24098 Kiel
Germany

Tel.: ++49 (0)431 880-3378 / -3379
Fax : ++49 (0)431 880-7300
www.uni-kiel.de/ufg

Well, I never stop learning new things with GRASS ...
Right, I should have just looked into the GRASS API, it's
always amazing to see how many things are already covered
in there.
Florian pointed out G_getenv/G_setenv to me and that -- of
course -- works nicely.

Thanks, everybody.

Glynn Clements wrote:

Benjamin Ducke wrote:

I am working on a set of modules to integrate GRASS with R.
For this I need to read and set some global variables for R scripts to
use.
I would like to use shell environment variables for this and
set them through a C module.
However, if I use the C function setenv() in program foo.c, it seems to
only add variables to the environment of foo.c.
When foo.c terminates, all variables set with senenv() will be gone.

I have not found a way to modify the environment of the shell that
starts foo.c so that the new variables will be available in the calling
shell after foo.c has exited.

Is this somehow possible

No. One process cannot modify the environment of another process
(other than by directly modifying that process' address space, which
isn't really a viable option).

That's why export (sh) and setenv (csh) are shell built-in functions;
a separate program wouldn't work.

or will I have to fall back to storing variables and values in an
ASCII file?

That's why GRASS has $GISRC for "GRASS variables", i.e. so that you
can have programs which record information for subsequent use by other
programs.

--
Benjamin Ducke, M.A.
Archäoinformatik
(Archaeoinformation Science)
Institut für Ur- und Frühgeschichte
(Inst. of Prehistoric and Historic Archaeology)
Christian-Albrechts-Universität zu Kiel
Johanna-Mestorf-Straße 2-6
D 24098 Kiel
Germany

Tel.: ++49 (0)431 880-3378 / -3379
Fax : ++49 (0)431 880-7300
www.uni-kiel.de/ufg