Correct way to call execve?

Leo Bicknell bicknell at ufp.org
Mon Jul 21 11:59:31 PDT 2003


You know, my own thinking made me figure this one out:

% cat exec.c

#include <unistd.h>
#include <paths.h>
#include <string.h>

int main(int argc, char *const argv[], char *const envp[]) {
  char *const execargv[] = { _PATH_BSHELL, NULL };

  execve(_PATH_BSHELL,execargv,NULL);

  return 0;
}
% cc -fwritable-strings -Wcast-qual -Wwrite-strings exec.c
%

There you go.  By default strings are read-only, and indeed smart
compilers use that to compress them and do other nifty tricks.  However,
in this case you really want a string to be "char *", eg writable.
So, tell the compiler to do that with "-fwritable-strings", poof,
strings are now "char *", the cast away the cost problem goes away,
"-Wcast-qual" works fine.

It always seemed to me a lot of things included -fwritable-strings for
no good reason, maybe this is part of the reason. :)

-- 
       Leo Bicknell - bicknell at ufp.org - CCIE 3440
        PGP keys at http://www.ufp.org/~bicknell/
Read TMBG List - tmbg-list-request at tmbg.org, www.tmbg.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20030721/6d9b585c/attachment.bin


More information about the freebsd-hackers mailing list