LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2004, 01:57 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Rep: Reputation: 15
Enviornment variables in C...


Hi,

I know theres a function out there in C to read and write enviornment variables. Could someone tell me what it is and describe its basic use? This would be for a linux system of course!

Thanks again,
Scrag
 
Old 12-16-2004, 02:02 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
See these man pages:

getenv(3) clearenv(3), putenv(3), setenv(3), unsetenv(3), environ(7)
 
Old 12-16-2004, 02:29 PM   #3
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Thanks!
 
Old 12-16-2004, 04:31 PM   #4
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Well,
I used the following line of code to set the environment variable, but when I use the "env" commnad from console, I dont see it.
Code:
setenv ("PATHENV", "/home", 0);
Is there something im doing wrong?

Thanks!
 
Old 12-16-2004, 04:39 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Once your C program ends, the environment your program was running in ends also and reverts back to the environment that you were using before you ran your program.
 
Old 12-16-2004, 05:18 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
An attempt to clarify that some more: each process has it's own set of environment vars. But each process inherits (gets a copy) of the environment of its parent process.

When you start a program from a shell, the shell will be the parent of the process the program is running in. So the program gets a copy of the environment the shell has. Now if the program creates/changes environment vars, it does so in its own copy of the environment, which is discarded when the program exits.

There's no single way around this. If it were possible to get around this, it would be a security hole.

What you can do, is setting environment vars in your program and start another shell from your program with execl("/bin/bash") for example. The new shell then gets a copy of the environment from your program.

Last edited by Hko; 12-16-2004 at 05:21 PM.
 
Old 12-16-2004, 06:14 PM   #7
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Thanks for the explanation. You guys kick @ss!

 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
100% linux enviornment in the corporate world admstng Linux - Enterprise 1 01-04-2005 08:16 AM
Setting enviornment variables in FreeBSD nodger *BSD 1 11-26-2004 05:44 AM
Desktop enviornment/window manager q's JiggaJerry Red Hat 3 11-02-2004 07:00 PM
problem using socket in perl for linux enviornment hitesh29 Linux - Networking 0 04-08-2004 12:11 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration