![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 2
| Adding directories to PATH 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, "");
}
Anyone have any ideas? |
| kyle-shaggy is offline | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #3 |
| Cat without Hat 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 | |
| | #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/ Code:
if (-1 == putenv("PATH=/:/home/userid"))
{
printf("putenv failed \n");
return EXIT_FAILURE;
}
|
| kyle-shaggy is offline | |
| | #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 | |
| | #6 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,762
| Quote:
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |