delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/09/18/06:14:30

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Date: Sat, 18 Sep 2004 13:06:59 +0300
From: "Eli Zaretskii" <eliz AT gnu DOT org>
Sender: halo1 AT zahav DOT net DOT il
To: djgpp AT delorie DOT com
Message-ID: <01c49d67$Blat.v2.2.2$625a64a0@zahav.net.il>
X-Mailer: emacs 21.3.50 (via feedmail 8 I) and Blat ver 2.2.2
In-reply-to: <cielml$fln$1@nsnmpen3-gest.nuria.telefonica-data.net> (message
from =?ISO-8859-1?Q?Rafael_Garc=EDa?= on Fri, 17 Sep 2004 14:33:19 +0200)
Subject: Re: system() return value if no program present
References: <cielml$fln$1 AT nsnmpen3-gest DOT nuria DOT telefonica-data DOT net>
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> From: =?ISO-8859-1?Q?Rafael_Garc=EDa?= <rafael_no_spam_no_ AT geninfor DOT com>
> Date: Fri, 17 Sep 2004 14:33:19 +0200
> 
> I'm trying to decompress an archive from a C program and I use
> 
> system("unzip prueba.zip");
> 
> I intend to have an "unzip.bat" that will do the work, containing the 
> details about format.
> 
> but when there is no "unzip.bat" nor "unzip.exe" I get an interactive 
> shell launched. I need to get an error code from system() and not to 
> stop execution of main program.
> 
> I'm running WinXP

I don't have XP around where I'm typing this, but on Windows 98 a
simple test program below works as I expect.  (One difference between
XP and 98 is that the default shell is cmd.exe on XP while it is
command.com on 98, but I think XP should invoke command.com for DJGPP
program, since it thinks they are DOS programs.  So I think you should
get the same output on XP as well.)

So please post what you get on your system from the program I wrote.
If my program gives the same output on XP, you will need to tell more
about your program, since obviously `system' works as expected.

--------------- Test program tsys.c ----------------------------
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
  int ret;
  char *prog;

  if (argc <= 1)
    prog = "foobar foo bar";
  else
    prog = argv[1];

  ret = system (prog);
  printf ("system(\"%s\") returned %d\n", prog, ret);
  return 0;
}
------------------------ Compilation -------------------------
    gcc -Wall -g -o tsys.exe tsys.c
------------------------ Running -----------------------------
    D:\usr\djgpp\tmp>tsys
    Bad command or file name
    system("foobar foo bar") returned 0

The "Bad command or file name" message comes from command.com, since
there's no program named `foobar' on my system.  The return value of
zero is also expected, since command.com always returns zero, even if
the command it tried to invoke failed.

    D:\usr\djgpp\tmp>tsys "foobar.exe foo bar"
    system("foobar.exe foo bar") returned -1

Note that this time, there's no "Bad command or file name" message and
the return value is -1.  That is because DJGPP's `system' does not
call the shell when the command has the ".exe" extension; instead, it
looks for the program along PATH and if not found, returns a failure
code right away.

Please see if you get the same results on your machine with this test
program.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019