delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/09/18/17:44:04

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Date: Sun, 19 Sep 2004 00:41:43 +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: <01c49dc8$Blat.v2.2.2$6fa7bb60@zahav.net.il>
X-Mailer: emacs 21.3.50 (via feedmail 8 I) and Blat ver 2.2.2
In-reply-to: <f7d84cf3.0409180201.713ca0b7@posting.google.com>
(iunknown2k4 AT yahoo DOT co DOT in)
Subject: Re: Problem using system( )
References: <f7d84cf3 DOT 0409180201 DOT 713ca0b7 AT posting DOT google DOT com>
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: iunknown2k4 AT yahoo DOT co DOT in (anonymous)
> Date: 18 Sep 2004 03:01:51 -0700
> 
> I want to know whether the command passed to the system()
> was successfully run or not.

I tried to explain that (with an example), but I obviously failed.
Let me try again.

For you to be able to tell whether a command passed to `system'
succeded or not, follow these steps:

  . Name a program with an explicit executable extension: `foo.exe'
    rather than just `foo'.

  . Avoid batch files and commands that are built into the shell.  Use
    .exe programs instead (e.g., use cp.exe instead of COPY).

  . Check the return value of `system'.  If it is -1, it couldn't find
    the program you wanted.  If it is anything other than zero, the
    program was found, but failed to run successfully (i.e. exited
    with a non-zero exit status).

Does this work for you?

> I am not able to understand the following:
>  
> "The behavior of `system' can be customized at run time by defining the
> variable `DJSYSFLAGS' in the environment.  The value of that variable
> should be the numerical value of `__system_flags' that you'd like to
> set; it will override the value of `__system_flags' specified when the
> program was compiled."
> 
> Does it mean that the `DJSYSFLAGS' should be defined something like 
> 
> #define DJSYSFLAGS  (  __system_emulate_command | __system_use_shell \
>                                   |__system_call_cmdproc)

No.  The fragment you cited talks about the _environment_ variable
DJSYSFLAGS, not about a program variable.  So to get the same effect
as the #define above, you need to type the following from the shell's
command prompt, before you invoke your program:

	set DJSYSFLAGS=38

38 is the decimal equivalent of 0x26, the bitwise OR of the flags you
mentioned above.

> Also the return value is in the lower 8 bits, 
> so I'm checking it like
> 
> struct retVal
>  {
>   char  r[2];
>  }*ret;
> 
> ...
> 
> int temp = system(cmd);
> ret = (struct retVal *) &temp;
> printf ("\n %d %d", ret->r[0], ret->r[1]);

There's no need to do that, the lower 8 bits are correctly
sign-extended into the return value (which is an int).  See the test
program I posted for the much simpler way of testing the return value.

> But, the vales are always zero even for system() called with
> an invalid command.

As I explained earlier, if the invalid command has no .exe extension,
`system' will think that it might be a shell built-in or some other
shell-dependent trick that it doesn't know about, and will invoke the
shell.  And command.com, the shell it invokes by default, always
returns a zero exit code, so that's what you get.

To avoid this lossage, always try to use an explicit .exe file-name
extension.

- Raw text -


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