Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is argv parameter to execvp not const?

Tags:

c

libc

execvp

execvp is defined thus:

int execvp(const char *file, char *const argv[]);

Which precludes code such as this from being used:

const char* argv[] = {"/bin/my", "command", "here", NULL};
execvp(argv[0], argv);

Was this an accidental omission? Is it safe to const_cast around this? Or do some execvp implementations actually scribble on that memory?

like image 881
Jonathan Mayer Avatar asked May 04 '12 20:05

Jonathan Mayer


1 Answers

The POSIX spec says (http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html):

The argv[] and envp[] arrays of pointers and the strings to which those arrays point shall not be modified by a call to one of the exec functions, except as a consequence of replacing the process image.

I think the missing (or misplaced) const is simply an historical oddity.

like image 63
Michael Burr Avatar answered Sep 29 '22 12:09

Michael Burr