Here is the prototype of the function execlp:
int execlp(const char *file, const char *arg, ...);
The man page says that the first argument of arg
(i.e. arg0), "by convention, should point to the filename associated with the file being executed."
Then I did these experiments:
/*These three lines all produce the expected result:
. .. a.out main.c */
execlp("ls", "ls", "-a", 0);
execlp("ls", "arg0 is meaningless", "-a" , 0);
execlp("ls", "", "-a" , 0);
/*But this one does not work:
a.out main.c */
execlp("ls", "-a" , 0);
So the question is, is the arg0 parameter meaningful under any circumstances? Why the interface was designed like this?
The main function signature is
int main(int argc, char ** argv);
Where argv[0] is the name of the executable (arg0 in your case), so the application expects its command line from argv[1].
In some cases single binary can have multiple names (busybox, for example, sometimes uses symbolic links with the different names, pointing to the single binary). In such cases argv[0] is used to determine which link was used to call the binary.
Programs can use argv[0]
to behave differently depending on how they are called.
For example, see this snippet from args.c from xz-utils:
const char *name = strrchr(argv[0], '/');
if (name == NULL)
name = argv[0];
else
++name;
// Look for full command names instead of substrings like
// "un", "cat", and "lz" to reduce possibility of false
// positives when the programs have been renamed.
if (strstr(name, "xzcat") != NULL) {
opt_mode = MODE_DECOMPRESS;
opt_stdout = true;
} else if (strstr(name, "unxz") != NULL) {
opt_mode = MODE_DECOMPRESS;
} else if (strstr(name, "lzcat") != NULL) {
opt_format = FORMAT_LZMA;
opt_mode = MODE_DECOMPRESS;
opt_stdout = true;
} else if (strstr(name, "unlzma") != NULL) {
opt_format = FORMAT_LZMA;
opt_mode = MODE_DECOMPRESS;
} else if (strstr(name, "lzma") != NULL) {
opt_format = FORMAT_LZMA;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With