comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: Program (not task) Activation
Date: 1998/04/28
Date: 1998-04-28T00:00:00+00:00	[thread overview]
Message-ID: <Es51yx.2o@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 6i2egl$o5$1@owens.ridgecrest.ca.us


Do-While Jones (do_while@ridgecrest.ca.us) wrote:

: Jerry van Dijk posted a useful demo program, which Michael Brenner
: expanded to include tests to make sure the expected error code were
: returned.

: What is important is that when he ran his test on NT 4.0, he got
: non-zero results when he should have.  When I ran his program on Windows
: 95, I got zero every time.  (I couldn't run his "exitcode" program because
: that must have been a C program he wrote but failed to include in
: the message.)

: So, my original question ("Does anyone know why Windows 95 did not return
: an error code?") still stands.

The answer seems to be: the system() call does not behave as specified,
it seems error returns from programs are caught by the system() code and
not passed back to the calling program.

I tried to verify this by first testing it with a program compiled with
the Microsoft Visual C++ v5.0 compiler:

	/***************************************************************/
	/*                                                             */
	/* The MS documentation specifies the following cases for the  */
	/* system() call:                                              */
	/*                                                             */
	/* 1. Command is ""                                            */
	/*                                                             */
	/*    a. Command interpreter not found -> returns 0, errno set */
	/*    b. Command interpreter found     -> returns non-zero     */
	/*                                                             */
	/*    This is meant to test for the presence of a command      */
	/*    interpreter                                              */
	/*                                                             */
	/* 2. Command is not ""                                        */
	/*                                                             */
	/*    a. Return value of command interpreter                   */
	/*    b. Returns -1 id canse of error -> errno set             */
	/*                                                             */
	/*    This is supposed to be the return code of the command    */
	/*                                                             */
	/***************************************************************/

	#include <stdio.h>
	#include <stdlib.h>

	int main(void)
	{
	   int rc;

	   /* Check for command interpreter  */
	   puts("\nCalling without argument:");
	   rc = system("");
	   if (rc == 0) {
	      perror("system() returned 0");
	   }
	   else {
	      printf("system() returned %d, command interpreter found\n", rc);
	   }
	
	   /* Run a legal command */
	   puts("\nCalling with legal argument:");
	   rc = system("dir > nul");
	    if (rc == -1) {
	      perror("system() returned -1");
	   }
	   else {
	      printf("system() returned %d, command succeded\n", rc);
	   }

	   /* Run an illegal command */
	   puts("\nCalling with illegal argument:");
	   rc = system("xyyz1 > nul");
	    if (rc == -1) {
	      perror("system() returned -1");
	   }
	   else {
	      printf("system() returned %d, command succeded\n", rc);
	   }

	   return EXIT_SUCCESS;
	}

Next, I did the same using Ada:

	-----------------------------------------------------------------
	--
	-- The MS documentation specifies the following cases for the
	-- system() call:
	--
	-- 1. Command is ""
	--
	--    a. Command interpreter not found -> returns 0, errno set
	--    b. Command interpreter found     -> returns non-zero
	--
	--    This is meant to test for the presence of a command
	--    interpreter
	--
	-- 2. Command is not ""
	--
	--    a. Return value of command interpreter
	--    b. Returns -1 id canse of error -> errno set
	--
	--    This is supposed to be the return code of the command
	--
	-----------------------------------------------------------------
	
	with Ada.Text_IO;      use Ada.Text_IO;
	with Interfaces.C;     use Interfaces.C;
	with Ada.Command_Line; use Ada.Command_Line;
	
	procedure Test is
	
	   function System (Argument : String) return Integer is
	      function c_system (Argument : char_array) return int;
	      pragma Import (C, c_system, "system");
	   begin
	      return Integer (c_system (To_C (Argument)));
	   end System;
	
	   procedure Perror (Text : String) is
	      function c_perror (Text : char_array) return int;
	      pragma Import (C, c_perror, "perror");
	      Temp : int;
	   begin
	      Temp := c_perror (To_C (Text));
	   end Perror;
	
	   Return_Value : Integer;

	begin

	   -- Check for command interpreter
	   New_Line;
	   Put_Line ("Calling without argument:");
	   Return_Value := System("");
	   if Return_Value = 0 then
	      Perror("system() returned 0");
	   else
	      Put_Line ("system() returned" &
	                Integer'Image (Return_Value) &
	                "command interpreter not found");
	   end if;
	
	   -- Run a legal command
	   New_Line;
	   Put_Line ("Calling with legal argument:");
	   Return_Value := System("");
	   if Return_Value = -1 then
	      Perror ("system() returned -1");
	   else
	      Put_Line ("system() returned" &
	                Integer'Image (Return_Value) &
	                ", command succeded");
	   end if;
	
	   -- Run an illegal command
	   New_Line;
	   Put_Line ("Calling with illegal argument:");
	   Return_Value := System("xyyz1 > nul");
	   if Return_Value = -1 then
	      Perror ("system() returned -1");
	   else
	      Put_Line ("system() returned" &
	                Integer'Image (Return_Value) &
	                ", command succeded");
	   end if;
	
	   Set_Exit_Status (Success);
	
	end;

Both programs produced identical output:

	Calling without argument:
	system() returned 0: No error

	Calling with legal argument:
	system() returned 0, command succeded

	Calling with illegal argument:
	De opdracht of bestandsnaam is onjuist.
	system() returned 0, command succeded

: Jerry asked, "Also, since you are using Ada 95, why the difficult code?"

: The short answer is that this was legacy Ada 83 code written for UNIX.  I
: just compiled it on Windows 95 and was surprised that it worked at all.

: The long answer is that I wanted to make the system call safer and more
: Ada-like.

Yes, I understand using the exception mechanism, I do the same in my code.
I just wondered why you didn't use the Interfaces.C package.

: If I knew the values of E2BIG, ENOENT, etc.

Look in /usr/mingw32/include/errno.h

regards,
Jerry.

-- 
-- Jerry van Dijk  | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada




  reply	other threads:[~1998-04-28  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-17  0:00 Program (not task) Activation Jeremy T Smith
1998-04-21  0:00 ` Robert I. Eachus
1998-04-21  0:00   ` Do-While Jones
1998-04-22  0:00     ` Jerry van Dijk
1998-04-24  0:00       ` Michael F Brenner
1998-04-27  0:00       ` Do-While Jones
1998-04-28  0:00         ` Jerry van Dijk [this message]
1998-04-29  0:00           ` Do-While Jones
1998-04-29  0:00             ` Do-While Jones
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox