develooper Front page | perl.perl5.porters | Postings from April 2004

Problem with system() on Win9x and command.com (perl 5.8.x-5.9.x)

Thread Next
From:
bilbo
Date:
April 27, 2004 14:23
Subject:
Problem with system() on Win9x and command.com (perl 5.8.x-5.9.x)
Message ID:
611491036.20040428001955@ua.fm
Hello ,

  I have problem with perl 5.8.x-5.9.x on Windows98SE (Win9x). When I
  run some executable via system() - all work ok. But when I try to
  run .bat file with system(), .bat file does not receive command line
  parameters. Example:

test_bin.pl:
for ($i=0; $i<=$#ARGV; $i++) { print $i+1, " - $ARGV[$i]\n" }

test_bat.bat:
@echo off
echo 1 - %1
echo 2 - %2
echo 3 - %3
echo 4 - %4
echo 5 - %5
echo 6 - %6
echo 7 - %7
echo 8 - %8
echo 9 - %9

test.pl:
system('perl test_bin.pl 1 2 3 4 5 6 7 8 9');
system('test_bat.bat 1 2 3 4 5 6 7 8 9');

  When I run test.pl on Win9x, test_bin.pl print:

1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 7
8 - 8
9 - 9

  But test_bat.bat print:

1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -

Perls earlier 5.8.x work fine with this example.

I also found source of this bug, in file win32/win32.c:

    /* The NT cmd.exe shell has the following peculiarity that needs to be
     * worked around.  It strips a leading and trailing dquote when any
     * of the following is true:
     *    1. the /S switch was used
     *    2. there are more than two dquotes
     *    3. there is a special character from this set: &<>()@^|
     *    4. no whitespace characters within the two dquotes
     *    5. string between two dquotes isn't an executable file
>     * To work around this, we always add a leading and trailing dquote
>     * to the string, if the first argument is either "cmd.exe" or "cmd",
>     * and there were at least two or more arguments passed to cmd.exe
>     * (not including switches).
     * XXX the above rules (from "cmd /?") don't seem to be applied
     * always, making for the convolutions below :-(
     */

Because of this, system('test_bat.bat 1 2 3 4 5 6 7 8 9') really run
'"test_bat.bat 1 2 3 4 5 6 7 8 9"', and command.com can't understand this.

I make patch for this bug.

--- win32/win32.c       Wed Feb 04 04:22:18 2004
+++ win32/win32.c       Sat Apr 17 15:02:42 2004
@@ -3538,11 +3538,12 @@
        if (clen > 4
            && (stricmp(&cname[clen-4], ".bat") == 0
                || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
        {
            bat_file = TRUE;
-           len += 3;
+           if (!IsWin95())
+               len += 3;
        }
        else {
            char *exe = strrchr(cname, '/');
            char *exe2 = strrchr(cname, '\\');
            if (exe2 > exe)
@@ -3575,11 +3576,11 @@
 
     argc = index;
     New(1310, cmd, len, char);
     ptr = cmd;
 
-    if (bat_file) {
+    if (bat_file && !IsWin95()) {
        *ptr++ = '"';
        extra_quotes = TRUE;
     }
 
     for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {




ps: I also report this bug to activestate & perl bug tracker (#28512)
some time ago, but I can't see any activity for this ticket. Therefore
I post information about this bug directly to perl5-porters list.

--




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About