0

Hi have this line of code in a C program that I must understand as a whole, I understand everything except for the line below. I have been looking into the documentation at it seems to me that it is just as if user introduced the ls command in Ubuntu. However I am not sure about it and I do not know why there is a second ls, the -l option and the NULL. Any help is very much appreciated!

execlp ("ls","ls","-l", NULL);

Miguel Garcia
  • 97
  • 1
  • 8
  • Does this answer your question? [I do not understand how execlp() works in Linux](https://stackoverflow.com/questions/21558937/i-do-not-understand-how-execlp-works-in-linux) –  Dec 07 '21 at 14:31
  • Hello. Well it does to a certain extent but not completely. I had , in fact, read this entry before posting my question. Still do not get why we need two "ls" arguments and also do not understand the NULL. So the command this line runs is ls -l or what? – Miguel Garcia Dec 07 '21 at 14:48
  • tl;dr: the first ls is the binary you want to run, the rest are arguments that are handed to the new proces, those include `argv[0]` which is the name of the binary again, if you replace the second ls with "foo", nothing much will happen, but the process may appear in `ps` as foo –  Dec 07 '21 at 15:21
  • 1
    The null is to mark the end of arguments (it should be `(char *)NULL`, btw, as [the manpage](https://www.man7.org/linux/man-pages/man3/exec.3.html) points out) – Shawn Dec 07 '21 at 15:34
  • 1
    Create a program named `myprog` that just prints its arguments. Execute it with `execlp("myprog", "arg0", "arg1", (char *)NULL)` – William Pursell Dec 07 '21 at 16:13

0 Answers0