Thread: Using the search path

  1. #1
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13

    Using the search path

    Just wondering... does the execl() function use the PATH environment variable? (Or whatever it's called.)

    If not, how can I get it to? Would I have to split up the search path string manually, and try each section of it induvidually?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Yes.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The exec() functions with an e in their name let you provide the enviroment variables, including PATH: http://www.opengroup.org/onlinepubs/...ions/exec.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    > f not, how can I get it to? Would I have to split up the search path string manually, and try each section of it induvidually?

    You can use the function below

    Code:
    /* Gets the user path and assigns a pointer for each PATH */
    int parsePath(char *dirs[])
    {
    	char *pathEnvVar;
    	char *thePath;
    	int i;
    	
    	for (i=0;i<MAX_ARGS;i++)
    		dirs[i]=NULL;
    	pathEnvVar=(char *)getenv("PATH");
    	thePath=(char *)malloc(strlen(pathEnvVar)+1);
    	strcpy(thePath,pathEnvVar);
    	i=0;
    	dirs[i]=thePath;
    	
    	for(i=1;i<MAX_PATHS;i++)
    	{
    		if (i==1)
    			dirs[i]=strtok(thePath,":");
    		dirs[i]=strtok(NULL,":");
    	}
    	return 1;
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why does that function return a value? Why are you casting malloc()? Why aren't you using free()? Why don't you include <string.h>? Why does the comment include "Gets the user path" (should you maybe use getcwd()?)?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Tones of questions...

    > Why does that function return a value?
    Yes, this is actually unnecessary...Since it expects a pointer as a parameter to change its value

    > Why are you casting malloc()?
    I did not know the issue in FAQ about the malloc casting when I wrote the code.

    > Why aren't you using free()? Why don't you include <string.h>?
    This is a function, not the program itself. If somebody wants to use it, (and if he knows a little about C) ,I think, he will be smart enough to include string.h. If I use free, how can I keep the value of *dirs[]

    > Why does the comment include "Gets the user path" (should you maybe use getcwd()?)?
    Actually the function gets the user PATH and assigns each individual path to a *dirs[] variable. Like *dir[0]="/usr/bin", *dirs[1]="/bin", etc...

    And you did not write the whole comment I wrote originally! (Gets the user path...) getcwd returns the user working directory, thes function returns each path found in the PATH variable.
    Last edited by fnoyan; 05-09-2006 at 12:35 AM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If I use free, how can I keep the value of *dirs[]
    You passed dirs[] as a pointer. After you assigned thePath to dirs[] there is no reason to keep thePath's memory around, as it's value will still be available after the function returns. Just somewhere else

  8. #8
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    > You passed dirs[] as a pointer. After you assigned thePath to dirs[] there is no reason to keep thePath's memory around, as it's value will still be available after the function returns. Just somewhere else

    So, this means, even if I free() the memory allocated, it is still possible to use array of *char[]?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes exactly, you can free thePath.

    *dirs[] is still available because you passed it in from the calling function. *dirs[] doesn't fall out of scope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  2. Binary Search Tree Cost of Path
    By indi_kila in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2006, 08:20 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM