C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2008, 09:41 PM   #1
Registered User
 
Join Date: Nov 2008
Posts: 2
Adding directories to PATH

If anyone is familiar with exec maybe you could help me out. I have a basic shell that I am trying to personalize. One of the features I want to include is that when the user inputs the command PATH they are then prompted for a directory which then is cat'ed to the end of linux's PATH variable. Here is a snippet of what I have so far:
Code:
char *t_PATH = (char *)malloc(sizeof(char) * 100);
char c;
	if (strcmp("PATH", *g_argv) == 0)
	{
		printf("Please enter the directory you wish to add to PATH\n");
		while(c != '\n') 		             
		{
			c = getchar();
			strncat(t_PATH, c, 1);
		}
		execve ("/bin/set PATH=$PATH:", t_PATH, "");
	}
It compiles fine and when I enter PATH it allows me to enter a variable but upon pressing return, I get a segmentation fault. I believe I am incorrectly using execve somehow, but I am still new to it so I am quite confused as to how to fix it.

Anyone have any ideas?
kyle-shaggy is offline   Reply With Quote
Old 11-18-2008, 12:29 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,676
> while(c != '\n')
c isn't initialised, so who knows whether this will pass the first time around.

> strncat(t_PATH, c, 1);
- t_PATH isn't initialised, so who knows where in the string your char is appended.
- c should have been passed as &c
- strncat does NOT always append a \0, so you could still end up with a broken string.

> execve ("/bin/set PATH=$PATH:", t_PATH, "");
Variable length exec calls end with a NULL parameter, not a "" parameter.


I suppose it's worth noting that exec is the wrong thing to be calling to set the path. But you should try anyway, you'll learn something in the process.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 11-18-2008, 03:11 AM   #3
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
Writing any progrem to append to the path is wrong. But that's another good thing to learn.

Here's a hint. Write a shell script that sets an environment variable, call it, and then look at the environment.
Then source the shell script instead of calling it.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 11-18-2008, 09:46 AM   #4
Registered User
 
Join Date: Nov 2008
Posts: 2
Yah I could easily make a shell script to change the PATH env variable, but that is not how I want to go about doing it. I wanted to see if I would be able to change it with C code.

Essentially I was asking is there a C equivalent to:

Code:
 PATH = PATH=$PATH:/myfolder/
Researching more about setting environment variables in C I found this piece of code to help me out substantially:

Code:
   
if (-1 == putenv("PATH=/:/home/userid")) 
{
      printf("putenv failed \n");
      return EXIT_FAILURE;
}
kyle-shaggy is offline   Reply With Quote
Old 11-18-2008, 10:41 AM   #5
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,278
Why not use the setenv() function?
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
cpjust is offline   Reply With Quote
Old 11-18-2008, 02:40 PM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,762
Quote:
Originally Posted by cpjust View Post
Why not use the setenv() function?
That won't work either. Changing the parent's environment is impossible. The only way to do this is with a shell script that gets evaluated in the environment which needs to be modified.

There's no way to affect a different process's PATH.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a directory to a dynamic library path vivharv Windows Programming 3 09-20-2007 07:09 AM
Adding MinGW Search Directories Matt3000 Windows Programming 1 04-23-2007 05:47 AM
SVN Import Causes Crash Tonto Tech Board 6 11-01-2006 03:44 PM
Adding include directories [VS2003] aker_y3k C++ Programming 1 10-24-2004 02:41 PM
Adding folders to multiple directories Smee Windows Programming 2 09-14-2003 06:18 AM


All times are GMT -6. The time now is 01:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22