Friday, February 4, 2011

"getting" path in linux

I am writing a c program in linux. Commands like execv() require a path in the form of a c string. Is there a command that will return the current path in the form of a c style string?

  • getcwd()

  • I am not a professional programmer so this isn't an exact answer.

    What you need to do is grab the environment variable PWD (present working directory)

    I'm not sure what the library it is in but it is a standard linux header.

    I'll look around and see if I can find it.


    I was thinking of ,getenv() which would help if you also need to run system commands and need the various bin paths located in PATH

    From Tanj
  • This is not ANSI C:

    #include <unistd.h>
    char path[MAXPATHLEN];
    getcwd(path, MAXPATHLEN);
    printf("pwd -> %s\n", path);
    From akr
  • The path argument to execv() is the path to the application you wish to execute, not the current working directory (which will be returned by getcwd()) or the shell search path (which will be returned by getenv("PATH")).

    Depending on what you're doing, you may get more mileage out of the system() function in the C library rather than the lower-level exec() family.

  • If the path can be a relative path, you should be able to use '.' or './' as the path. I'm not sure if it will work, but you could try it.

    From yjerem
  • gah this place is so fast that if it isn't on the tip of your tongue you'll get down voted while editting your post with a better answer

    Artelius : This isn't a forum. You should only post answers to questions. This doesn't qualify as an answer and you should delete it.
    Tanj : this was posted pre-comments era :)
    From Tanj


Post a Comment