Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c - Why does `execvp` take a `char *const argv[]`?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To quote the page you link:

The statement about argv[] and envp[] 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 of const- qualification for the argv[] and envp[] 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...