From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a1d846d0e0466c9b X-Google-Attributes: gid103376,public From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk) Subject: Re: Program (not task) Activation Date: 1998/04/28 Message-ID: X-Deja-AN: 348569684 References: <#CDmPrma9GA.300@ntawwabp.compuserve.com> <6hjtju$kiq$1@owens.ridgecrest.ca.us> <6i2egl$o5$1@owens.ridgecrest.ca.us> Organization: * JerryWare *, Leiden, Holland Newsgroups: comp.lang.ada Date: 1998-04-28T00:00:00+00:00 List-Id: 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 #include 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