28

i have vlc (program to reproduce videos) if i type in a shell:

/home/vlc "/home/my movies/the movie i want to see.mkv"

it opens up an reproduces the movie.

however, when I run the following program:

#include <unistd.h>

int main(void) {

  execl("/home/vlc", "/home/my movies/the movie i want to see.mkv",NULL);

  return 0;
}

vlc opens up but doesn't reproduce anything. How can I solve this?

Things I tried:

I guessed

execl("/home/vlc", "/home/my movies/the movie i want to see.mkv",NULL);

was equivalent to typing in the shell:

/home/vlc /home/my movies/the movie i want to see.mkv

which doesn't work, so i tried

 execl("/home/vlc", "\"/home/my movies/the movie i want to see.mkv\"",NULL);

and vlc opens up but doesn't reproduce either.

Instead of writing NULL at the end I tried 0, (char*) 0, 1 .... not helpful. Help!!!!

Jens
  • 65,924
  • 14
  • 115
  • 171
Matias Morant
  • 493
  • 2
  • 5
  • 12
  • 1
    Why didn't you look at `errno` after the failed execl? It would have told you more about your problem. What has become of reading the man page for execl carefully? :-) – Jens Sep 26 '12 at 07:53

2 Answers2

46
execl("/home/vlc", 
  "/home/vlc", "/home/my movies/the movie i want to see.mkv", 
  (char*) NULL);

You need to specify all arguments, included argv[0] which isn't taken from the executable.

Also make sure the final NULL gets cast to char*.

Details are here: http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html

alk
  • 68,300
  • 10
  • 92
  • 234
AProgrammer
  • 49,582
  • 8
  • 87
  • 140
  • 6
    Why does /home/vlc need to be specified twice? – LazerSharks Jul 17 '14 at 21:38
  • 11
    first one is the full path, second is process name, generally "vlc" is enough in the second parameter – Tanguy Jul 27 '14 at 22:06
  • 1
    It should be `... , (char*) NULL);` – alk Aug 14 '16 at 11:40
  • @alk, indeed. (But I wonder how many current implementations are there out which won't work with a naked NULL as defining NULL as (void*)0 or 0L became popular to make the absence of cast on NULL to work in practice with variadic functions; even passing a naked 0 will work on most if not all 32-bit ABI and, IIRC, some 64-bit one such as the one used by Linux). – AProgrammer Aug 14 '16 at 12:34
  • Why do we have to cast NULL? – Imago Feb 19 '19 at 18:32
  • 1
    In case NULL is defined as 0 and sizeof(char*) != sizeof(int) – AProgrammer Feb 19 '19 at 18:38
0

If you need just to execute your VLC playback process and only give control back to your application process when it is done and nothing more complex, then i suppose you can use just:

system("The same thing you type into console");

Erik Kaju
  • 3,117
  • 3
  • 16
  • 27
  • 1
    Between fixing the slight misunderstanding in the execl arguments and writing a function which will quote correctly so that the shell can undo the quote, I know what I prefer. – AProgrammer Sep 26 '12 at 08:00
  • I think i get your point and your preferment is reasonable. But when dealing with such simple needs, is there any other benefits of using execl() instead of system(), besides avoidance of messing with escape sequence backslashes in system()? – Erik Kaju Sep 26 '12 at 08:34
  • 3
    There could be security reasons, if you have for example a string created like, `sprintf(buf, "ls -la %s", var)` which you execute with `system(buf)`, someone could give you a filename, `"somefile; rm -rf ~"`, for example. – netdigger Jul 29 '14 at 12:02