From patchwork Wed Jan 18 15:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 45340 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2407173wrn; Wed, 18 Jan 2023 07:43:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXuFSiCbro/3b1rN4Za156oe2TZBoqIc5qhkCtdjgW8NWHCWBv10s2/d6SeFmDEoyA3yLkrG X-Received: by 2002:a17:906:40e:b0:873:4311:d30c with SMTP id d14-20020a170906040e00b008734311d30cmr5832223eja.13.1674056607063; Wed, 18 Jan 2023 07:43:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674056607; cv=none; d=google.com; s=arc-20160816; b=xCkvt8P1KzwA4cv4eeG4g51aIRFriXHPBgXMt9LZjOTeaS60f7+5ePvvfS6vGTeltS t7EQ7MTxuEOvT5WPvlMeGE6P88G73o8dYSYIPplx9in6p6rYOZB3aAAsNGgdF7xOKlUZ LyvI64aL5Q7u9gIFJPM2Ag+Mb3X4NKiCbK47QkcNK1Z/rhs5PQGzGaLl2nOFKT8oYxZ7 OYCVig9QUzzmU0gDojAlxL1mZKyyFVAXaw492gG91SweTRe75fboTQ1Wql+Hmqwp/c9z 94N/Kxw5iYamKP/ZIleP5Uhzhg7ls8VglMT4P4qV+oS3iE4xHYDjoAa8UiYMocOhn80W 0n0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:cc:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=x5bhDCBqWOe961T94Vt1DoMuJCbrXVdBuX4GrOlwEZs=; b=de3driZoPhPDvYymL4W1sEUwBFT87vhOAI7GtwUq0mn/BFGETd3+yn1jMrAHTZx6TT yajD3vBy0QZDwwFecAGLrqxIw/SURdoRZQ9IvdaNE2eky4ur+hi4SBNqS0mSHjI6iMNu o5GMOcBzK00eM/XKDJ9MJX+uJXAoqRaXMMEDQvii6jRJhUrwJGxXtTb4mIng07NGztlv D6ifJDcF9IZqTOKpv+y0SD6wGWSYgeVb8imRqPl+AVnXb7rSq9VbgfCIc/5q9d9QKVF+ qVHVIS6CXNR+HsXcFuUzBq+62jo4oa0zr17SFlKgT8me3Omg277tbpiEWo2ho+LVg3ux XsAw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hs9-20020a1709073e8900b0086a9cbb1c34si13871344ejc.361.2023.01.18.07.43.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 07:43:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C6933858439 for ; Wed, 18 Jan 2023 15:43:05 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 539EB3858D28; Wed, 18 Jan 2023 15:42:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 539EB3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.97,226,1669104000"; d="diff'?scan'208";a="93787896" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa3.mentor.iphmx.com with ESMTP; 18 Jan 2023 07:42:35 -0800 IronPort-SDR: 65ZiQVs1psN/XyZM1apGwWgn6Fj2+cxufJCRgA7R3M02WhWnmNftpwCghndA5t+eT6QrDgW42S INTh5sKBZ791WLfdxkILvTEsVfKvfyMUlVsgr2uTlbz2523j8UTRjQJkuVUlLfCy+hWG0vgji0 1x/PoGV6RCDoiKeR2ojCSxGl2851qC4A782ckxwIm8ZyXKk+Dwvpa1wHn0HbkvkhJgEnng+xKf x0mU3JZeUo4JNTZVHJml01wdv9qlmanT1IjFjsqw0MRKDnCUHXTCPfS3JPGG02x/uyTlq0dczL W68= Message-ID: <0b95e5f1-142e-e13a-7d77-272073e25c2a@codesourcery.com> Date: Wed, 18 Jan 2023 16:42:24 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Content-Language: en-US To: gcc-patches , fortran From: Tobias Burnus CC: NightStrike Subject: [Patch] libfortran: Fix execute_command_line for Windows X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-11.mgc.mentorg.com (139.181.222.11) To svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755375580951217140?= X-GMAIL-MSGID: =?utf-8?q?1755375580951217140?= 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 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);