views:

317

answers:

6

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?

+9  A: 

getcwd()

John Millikin
A: 

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.

edit:

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

Tanj
+1  A: 

This is not ANSI C:

#include <unistd.h>

char path[MAXPATHLEN];
getcwd(path, MAXPATHLEN);
printf("pwd -> %s\n", path);
akr
+4  A: 

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.

Commodore Jaeger
A: 

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.

yjerem
A: 

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

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