Thread: Exec and paths

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Exec and paths

    I want to use execlp for a child process and because it automatically searches the path environment form the root for the command, makes life easier.

    However, if I want to execute any command than I need to use execl and get the absolute path.

    Is there a better a way given my limited knowledge of C? I read the code posted in the FAQ about recursive searching to get the path of a file. Had compile errors using MinGW 5 and Eclipse.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why would you search to run an arbitrary command?

    If there are multiple executable commands with the same name on the file system, you have no idea which one is going to execute.

    Running commands "off the path" usually means getting some input from the user to unambiguously specify the one they want.

    Eg, I might do
    ./project/debug/mytest

    I wouldn't want to type in mytest by itself and hope it would find what I wanted.
    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
    Registered User
    Join Date
    May 2010
    Posts
    178
    Reason being is because if I desire to run a program as a child process and my home directory is not listed under path, then I have to specify the entire path from the root as ./username/project_folder/executable does not work in execlp.

    The entire path has to be specified form the root and the two exec functions that auto search path will not autosearch on an appended path string. Your recursive directory search for the absolute path of a file looks nice but I can't get it to compile under Eclipse using MinGW.

    I will try under Unix itself. This may sound nuts and is not a requirement of my Systems Programming project but I thought adding code to do this alleviates unnecessary typing.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But if you already know ./something/or/other and you know the path to your home (or even the program you're running, look in argv[0]), then it's simple string manipulation.

    Not a full-blown tree walk of the file system.
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    178
    Good point Salem and logic. I can use execvp or lp with a full path or just the command and UNIX will accept that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exec with system(..), how to use relative paths?
    By sept in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2008, 01:22 PM