develooper Front page | perl.perl5.porters | Postings from March 2011

Allow for empty argv in exec/system

Thread Next
Sebastian Schmidt
March 7, 2011 12:35
Allow for empty argv in exec/system
Message ID:
Hi porters,

I'm not sure whether this is a bug or not so I'm putting it for
discussion here.

Consider the following C program, "a":
#include <stdio.h>
int main(int argc, char **argv) {
    printf("argc: %d, argv[0]: %s\n", argv[0]);
    return 0;

And another one, "b":
#include <unistd.h>
int main(void) {
    char *av = { NULL };
    char *ep = { NULL };
    execve("a", av, ep);
    return 0;

When b calls a, at least on my linux box, b behaves as expected and gets
an argv with { NULL } in it - whether that makes sense or not. I don't
think it's specified in POSIX but there may be valid reasons for it. And
at least on AmigaOS, as I were told, argv[0] means the program was
called from the UI.

Now trying to accomplish that from perl doesn't work. The obvious
solution would be perl -e 'exec { "a" } ()' but that's a no-op
currently. perl -e 'exec "a"' sets argv to { "a", NULL } which isn't
what the b program did either.

I'm not sure whether this is a bug or not - also I'm confident many
implementations won't expect that; my coreutil's echo on linux e.g. does
set_program_name which expects argv[0] to be set of course. OTOH it's not a
feature anyone would use by accident so I think it shoud be fine to make
perl -e 'system { "a" } ()' work. I have attached the quite trivial patch



diff --git a/doio.c b/doio.c
index cecc574..74fea19 100644
--- a/doio.c
+++ b/doio.c
@@ -1394,7 +1394,7 @@ Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
     Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
-    if (sp > mark) {
+    if (really || sp > mark) {
        const char **a;
        const char *tmps = NULL;
        Newx(PL_Argv, sp - mark + 1, const char*);

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About