Couldn't execute: /bin/ls
: No such file or directory
Notice that the error message is on a new line, but you tried to remove the end-of-line character from the input. So your removal appears not to be working.
The reason for that is:
if (buf[strlen(buf) - 1] == "\n")
You're comparing a character (buf[x]
) with a string ("\n"
), i.e. a pointer. Turn on your compiler's warnings and read them carefully.
Try this instead:
if (buf[strlen(buf) - 1] == '\n')
The arguments for execlp
are described in its man page:
The first argument, by convention, should point to the filename associated with the file being executed.
Or in POSIX here.
(char *)0
is a null-pointer of type char*
. Nothing magic going on here, an integral constant of value 0 is a null-pointer constant. But since execlp
is a variadic function, the cast is necessary - the constant zero would be treated as an int
otherwise, which might not have the same size as a pointer (and the internal representation of a null-pointer might not be the same as that of the integer 0), leading to undefined behavior.