Thread: Adding directories to PATH

  1. #1
    Registered User
    Join Date
    Nov 2008

    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:
    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    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!

    "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

  4. #4
    Registered User
    Join Date
    Nov 2008
    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:

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

    if (-1 == putenv("PATH=/:/home/userid")) 
          printf("putenv failed \n");
          return EXIT_FAILURE;

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    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

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    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.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

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