I'm wondering if there is a reason between two exec functions differing in const
-ness, of if this is just a bug in the Single Unix Spec:
Excerpting from the Linux manpage, which appears to align with the Single Unix Specification, here are a two versions of exec
:
int execlp(const char *file, const char *arg, ...);
int execvp(const char *file, char *const argv[]);
execlp
takes its arguments as const char *
, and it takes two or more of them. const
in C is a promise that the function will not change the pointed-to data, in this case the actual characters (char
) that make up the string.
execvp
instead takes its arguments as an array of pointers. However, instead of an array of pointers to const char *
as you'd expect, the const
keyword is in a different spot—and this matters quite a bit to C. execvp
is saying it may well modify the characters in the strings, but it promises not to modify the array—that is, the pointers to the strings. So, in other words,
int fake_execvp(const char *file, char *const argv[]) {
argv[0] = "some other string"; /* this is an error */
argv[0][0] = 'f'; /* change first letter to 'f': this is perfectly OK! */
/* ⋮ */
}
In particular, this makes it hard (technically, prohibited) to call execvp using C++'s std::string
's to_cstr()
method, which returns const char *
.
It seems like execvp
really ought to take const char *const argv[]
, in other words, it ought to promise not to do either of the above changes.
The execvp() function replaces the current process image with a new process image specified by file. The new image is constructed from a regular, executable file called the new process image file. No return is made because the calling process image is replaced by the new process image.
execvp() returns a negative value if the execution fails (e.g., the request file does not exist).
To quote the page you link:
The statement about
argv[]
andenvp[]
being constants is included to make explicit to future writers of language bindings that these objects are completely constant. Due to a limitation of the ISO C standard, it is not possible to state that idea in standard C. Specifying two levels ofconst
- qualification for theargv[]
andenvp[]
parameters for the exec functions may seem to be the natural choice, given that these functions do not modify either the array of pointers or the characters to which the function points, but this would disallow existing correct code.
Basically the const
qualification on execlp
and execvp
are completely compatible in the sense that they specify identical limitations on the corresponding arguments.
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