/* Implementation of the EXECUTE_COMMAND_LINE intrinsic.
- Copyright (C) 2009 Free Software Foundation, Inc.
- Contributed by FranÃ\83§ois-Xavier Coudert.
+ Copyright (C) 2009-2018 Free Software Foundation, Inc.
+ Contributed by François-Xavier Coudert.
This file is part of the GNU Fortran runtime library (libgfortran).
#include "libgfortran.h"
#include <string.h>
-#include <stdbool.h>
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
-enum { EXEC_NOERROR = 0, EXEC_SYSTEMFAILED };
+enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
+ EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
static const char *cmdmsg_values[] =
- { "", "Execution of child process impossible" };
+ { "",
+ "Termination status of the command-language interpreter cannot be obtained",
+ "Execution of child process impossible",
+ "Invalid command line" };
{
if (cmdstat)
*cmdstat = value;
- else if (value != 0)
- runtime_error ("Could not execute command line");
+ else if (value > EXEC_NOERROR)
+ {
+#define MSGLEN 200
+ char msg[MSGLEN] = "EXECUTE_COMMAND_LINE: ";
+ strncat (msg, cmdmsg_values[value], MSGLEN - strlen(msg) - 1);
+ runtime_error ("%s", msg);
+ }
}
gfc_charlen_type cmdmsg_len)
{
/* Transform the Fortran string to a C string. */
- char cmd[command_len + 1];
- memcpy (cmd, command, command_len);
- cmd[command_len] = '\0';
+ char *cmd = fc_strdup (command, command_len);
/* Flush all I/O units before executing the command. */
flush_all_units();
/* Asynchronous execution. */
pid_t pid;
- set_cmdstat (cmdstat, 0);
+ set_cmdstat (cmdstat, EXEC_NOERROR);
if ((pid = fork()) < 0)
- set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
+ set_cmdstat (cmdstat, EXEC_CHILDFAILED);
else if (pid == 0)
{
/* Child process. */
/* Synchronous execution. */
int res = system (cmd);
- if (!wait)
- set_cmdstat (cmdstat, -2);
- else if (res == -1)
+ if (res == -1)
set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
+#ifndef HAVE_FORK
+ else if (!wait)
+ set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
+#endif
+ else if (res == 127 || res == 126
+#if defined(WEXITSTATUS) && defined(WIFEXITED)
+ || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
+ || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
+#endif
+ )
+ /* Shell return codes 126 and 127 mean that the command line could
+ not be executed for various reasons. */
+ set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);
else
+ set_cmdstat (cmdstat, EXEC_NOERROR);
+
+ if (res != -1)
{
- set_cmdstat (cmdstat, 0);
#if defined(WEXITSTATUS) && defined(WIFEXITED)
*exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
#else
}
}
+ free (cmd);
+
/* Now copy back to the Fortran string if needed. */
- if (cmdstat && *cmdstat > 0)
- {
- if (cmdmsg)
- fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
+ if (cmdstat && *cmdstat > EXEC_NOERROR && cmdmsg)
+ fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
strlen (cmdmsg_values[*cmdstat]));
- else
- runtime_error ("Failure in EXECUTE_COMMAND_LINE: %s",
- cmdmsg_values[*cmdstat]);
- }
}