LinuxQuestions.org
Review your favorite Linux distribution.
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 02-26-2004, 04:15 AM   #1
c_coder
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Rep: Reputation: 0
How do I set $PATH using C code


Hi people.


How do I set $PATH permanently using C code? Using putenv() alters the PATh env for the current child process only. How does one alter the $PATH of the existing shell from which one executes the C program?
I cannot use the '. ' (dot space filename) shell command or 'source' shell command to execute my C program due to certain reasons.

urgent help required please...


Thanx.

Last edited by c_coder; 02-26-2004 at 04:19 AM.
 
Old 02-26-2004, 04:44 AM   #2
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434
Blog Entries: 1

Rep: Reputation: 55
you could try something like:

system("PATH=/wherever/file/bin:$PATH ");
 
Old 02-26-2004, 04:52 AM   #3
c_coder
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Original Poster
Rep: Reputation: 0
I Tried it exactly that way... However same problem.. The altered PATH is valid only in that process. I guess it is because system() forks a new shell to fire the command-- the original shell still has the same PATH. Is there another way?
 
Old 02-26-2004, 04:56 AM   #4
doraiashok
LQ Newbie
 
Registered: Nov 2003
Posts: 19

Rep: Reputation: 0
I don't know about the system() call but one thing is clear that every process maintains it own separate path variables.

so try exporting the variable and making it global by using the export command.

May be you can use,
Code:
system("export PATH");
But still it may be unsure because system() itself forks a new process to execute the given command. so a code below might work incase "export" works.

Code:
if(fork()==0)
{
//Set the PATH variable
//Use exec to execute the export command.
}
 
Old 02-26-2004, 05:02 AM   #5
c_coder
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Nope. 'Export' did not work either. I guess export must be sending vars to the current environment... not the parents one.

What now? there must be SOME way..
 
Old 02-26-2004, 05:15 AM   #6
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
Heheh yes, there is some way (I will put only a few lines, not whole code):


FILE *file;
fputs("PATH=/your/path\n export PATH", file);
exec(file);

;-)
Take this as a joke...but it would also do exactly what you want.

Last edited by krajzega; 02-26-2004 at 05:18 AM.
 
Old 02-26-2004, 07:40 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Nice. A dirty hack for a dirty job... :-)
 
  


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
Set the path systemwide/Set the path for a user with Slackware jayhel Slackware 1 06-12-2005 12:24 AM
9.1 set $PATH seingoku Slackware 6 02-06-2004 03:12 PM
How to set PATH How do I set PATH environment variable? Tranquil Linux - Newbie 3 11-02-2003 02:52 AM
$PATH!? how to set these PATH(s)!? sirpelidor Red Hat 5 10-25-2003 04:00 PM
Set Path reaky Linux - Newbie 2 10-09-2003 07:54 PM

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

All times are GMT -5. The time now is 02:24 AM.

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