elmazzun
elmazzun

Reputation: 1086

C - execl(): can't execute file located in another directory

I'm trying to execute a binary file named "helloworld" (compiled from a source.c) located in another directory with my program notify.c. Here's where my files are located:

/home/morts/Desktop/helloworld

/home/morts/Desktop/Homeworks/notify

I tried with:

const char *cmd = "/home/morts/Desktop/helloworld";
execl(cmd, "LOL", NULL);
perror("execl()");
exit(EXIT_FAILURE);

but I get:

execl(): Permission denied

If helloworld would be located in the same directory of notify, I'd simply put "./helloworld", but since they are in different directories, how can I fix this?

Thanks and regards.

Upvotes: 3

Views: 8036

Answers (2)

user2689108
user2689108

Reputation: 96

if you want to execute an file hello under path /bin/test by execl without any parameters try following:

execl("/bin/test/hello", "hello", (char*)NULL);

Check following otherwise you may get a permission denial error:

  1. The first argument must be an /path/your_file without any space
  2. The second argument must be "your_file" the same as in first argument
  3. the last argument must be (char*)NULL

Upvotes: 1

zakinster
zakinster

Reputation: 10698

You should check that the path is correct and you have the right permissions to execute this file by trying to run the command directly in the terminal :

/home/morts/Desktop/helloworld

If it works then you have to check that the user running your program also have the permission to execute that file.

Upvotes: 0

Related Questions