Hello,

How can i change the root directory to the current working directory.. I need to use some form of an exec call to run a program found in the current working directory but the problem is that the path of that program is required to start with '/'.... my code works fine if its just the name of the program:

if ( execv(path, arguments) == -1 ){
     printf("Exec caused an error\n");
     return EXIT_FAILURE;
}

I tried execl, execlp, and execvp, all end up with the same result:
"Directory or file cannot be found."

Any idea? Thanks!

put a dot in from of the / character, e.g. "./blabla"

put a dot in from of the / character, e.g. "./blabla"

Tried that, did not work...gave me the same error... No directory or file ..

Post the code that shows how you constructed the contents of that path variable and argv[] array of strings.

Post the code that shows how you constructed the contents of that path variable and argv[] array of strings.

Alright, well the program I'm trying to exec doesn't really need any arguments..so you can think of the arguments array as being:
char **arguments_array = {program_name, NULL};

and the path is the following:
char *path = "/path/to/program";

Now, the 'program', which I'm trying to exec, would normally reside in the current working directory. When I call exec with the path being just the program name (i.e. : char *path = "program_name";), the exec call seems to find the program and runs perfectly. It's only when I add the '/' (backslash), that's when the exec produces the "Directory or File not found" message...

Post the code that shows how you constructed the contents of that path variable and argv[] array of strings.

Alright, well the program I'm trying to exec doesn't really need any arguments..so you can think of the arguments array as being:
char **arguments_array = {program_name, NULL};

and the path is the following:
char *path = "/path/to/program";

Now, the 'program', which I'm trying to exec, would normally reside in the current working directory. When I call exec with the path being just the program name (i.e. : char *path = "program_name";), the exec call seems to find the program and runs perfectly. It's only when I add the '/' (backslash), that's when the exec produces the "Directory or File not found" message...

Did I miss the code somewhere? Is it in another thread? Or did you completely ignore what AD asked for?

Of course exe will not find "/program" when the program does not reside in the root directory. As I said previously you need to make it "./program". If you don't want to post actual code, that's ok, but will limit the amount of help any of us can give you.

Of course exe will not find "/program" when the program does not reside in the root directory. As I said previously you need to make it "./program". If you don't want to post actual code, that's ok, but will limit the amount of help any of us can give you.

Its all good...It worked...I had to add the "." to my PATH variable and use the "exec*p*" system call...and so it found the program and ran it... One question though: How can I prevent the exec call from running other programs (located in the same directory)...Any ideas?

Adding the "." to your path is not a desirable way to fix the problem. It may work for you, but not for anyone else that tries to run your program.


>>How can I prevent the exec call from running other programs (located in the same directory)...Any ideas?
It won't. It only runs the program that's named in the first argument to that function.

commented: Very true +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.