public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] libfortran: Fix execute_command_line for Windows
@ 2023-01-18 15:42 Tobias Burnus
  2023-01-18 20:47 ` Harald Anlauf
  2023-01-19  1:54 ` Jerry D
  0 siblings, 2 replies; 3+ messages in thread
From: Tobias Burnus @ 2023-01-18 15:42 UTC (permalink / raw)
  To: gcc-patches, fortran; +Cc: NightStrike

[-- Attachment #1: Type: text/plain, Size: 1262 bytes --]

Reported by nightstrike, who also tested this patch.

On Windows, we call system() which works as described at
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170

Namely, it only fails with "-1" if the command interpreter
could not be started. Otherwise, it has the return value.
(Same on Linux.) On POSIX systems, 'sh' calls exit(127) or
_exit(127) if it cannot execute the program of the passed string,
as documented. Cf. https://www.unix.com/man-page/posix/3p/system/

Thus, the question is what happens on Windows. Our experiments, several
webpages (like stackoverflow) and the source code of WINE for cmd.exe indicate
that Windows returns 9009 in that case. See for instance
https://github.com/wine-mirror/wine/blob/master/programs/cmd/wcmdmain.c#L1262-L1269

Thus, we now do likewise. The code is for MINGW; Cygwin does not set that that
var and is likely to use return values closer to POSIX.

OK for mainline?

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

[-- Attachment #2: win-cmd-exec.diff --]
[-- Type: text/x-patch, Size: 1369 bytes --]

libfortran: Fix execute_command_line for Windows

On Windows, 'system' is called - that fails with -1 if the command
interpreter could not be started; on POSIX systems, if the child
process could not be started by the shell, exit(127)/_exit(127) is
called/returned. On Windows, cmd.exe (and also the PowerShell) return
errorlevel 9009.

libgfortran/ChangeLog:

	* intrinsics/execute_command_line.c (execute_command_line): On
	Windows, regard system()'s return value of 9009 as EXEC_INVALIDCOMMAND.

diff --git a/libgfortran/intrinsics/execute_command_line.c b/libgfortran/intrinsics/execute_command_line.c
index 305f067d973..0d1688400c2 100644
--- a/libgfortran/intrinsics/execute_command_line.c
+++ b/libgfortran/intrinsics/execute_command_line.c
@@ -142,10 +142,15 @@ execute_command_line (const char *command, bool wait, int *exitstat,
 #endif
       else if (res == 127 || res == 126
 #if defined(WEXITSTATUS) && defined(WIFEXITED)
 	       || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
 	       || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
+#endif
+#ifdef __MINGW32__
+		  /* cmd.exe sets the errorlevel to 9009,
+		     if the command could not be executed.  */
+		|| res == 9009
 #endif
 	       )
 	/* Shell return codes 126 and 127 mean that the command line could
 	   not be executed for various reasons.  */
 	set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Patch] libfortran: Fix execute_command_line for Windows
  2023-01-18 15:42 [Patch] libfortran: Fix execute_command_line for Windows Tobias Burnus
@ 2023-01-18 20:47 ` Harald Anlauf
  2023-01-19  1:54 ` Jerry D
  1 sibling, 0 replies; 3+ messages in thread
From: Harald Anlauf @ 2023-01-18 20:47 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches, fortran; +Cc: NightStrike

Hi Tobias,

Am 18.01.23 um 16:42 schrieb Tobias Burnus:
> Reported by nightstrike, who also tested this patch.
>
> On Windows, we call system() which works as described at
> https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170
>
> Namely, it only fails with "-1" if the command interpreter
> could not be started. Otherwise, it has the return value.
> (Same on Linux.) On POSIX systems, 'sh' calls exit(127) or
> _exit(127) if it cannot execute the program of the passed string,
> as documented. Cf. https://www.unix.com/man-page/posix/3p/system/
>
> Thus, the question is what happens on Windows. Our experiments, several
> webpages (like stackoverflow) and the source code of WINE for cmd.exe
> indicate
> that Windows returns 9009 in that case. See for instance
> https://github.com/wine-mirror/wine/blob/master/programs/cmd/wcmdmain.c#L1262-L1269
>
> Thus, we now do likewise. The code is for MINGW; Cygwin does not set
> that that
> var and is likely to use return values closer to POSIX.

I don't use Windows, but this LGTM.

> OK for mainline?

Yes, and thanks for the patch!

Harald

>
> Tobias
> -----------------
> Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201,
> 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer:
> Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München;
> Registergericht München, HRB 106955


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Patch] libfortran: Fix execute_command_line for Windows
  2023-01-18 15:42 [Patch] libfortran: Fix execute_command_line for Windows Tobias Burnus
  2023-01-18 20:47 ` Harald Anlauf
@ 2023-01-19  1:54 ` Jerry D
  1 sibling, 0 replies; 3+ messages in thread
From: Jerry D @ 2023-01-19  1:54 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches, fortran; +Cc: NightStrike

On 1/18/23 7:42 AM, Tobias Burnus wrote:
> Reported by nightstrike, who also tested this patch.
> 
> On Windows, we call system() which works as described at
> https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170
> 
> Namely, it only fails with "-1" if the command interpreter
> could not be started. Otherwise, it has the return value.
> (Same on Linux.) On POSIX systems, 'sh' calls exit(127) or
> _exit(127) if it cannot execute the program of the passed string,
> as documented. Cf. https://www.unix.com/man-page/posix/3p/system/
> 
> Thus, the question is what happens on Windows. Our experiments, several
> webpages (like stackoverflow) and the source code of WINE for cmd.exe 
> indicate
> that Windows returns 9009 in that case. See for instance
> https://github.com/wine-mirror/wine/blob/master/programs/cmd/wcmdmain.c#L1262-L1269
> 
> Thus, we now do likewise. The code is for MINGW; Cygwin does not set 
> that that
> var and is likely to use return values closer to POSIX.
> 
> OK for mainline?
> 
> Tobias

OK, thanks fir fix.

Jerry


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-01-19  1:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-18 15:42 [Patch] libfortran: Fix execute_command_line for Windows Tobias Burnus
2023-01-18 20:47 ` Harald Anlauf
2023-01-19  1:54 ` Jerry D

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).