1 /* Implementation of the EXECUTE_COMMAND_LINE intrinsic.
2 Copyright (C) 2009-2016 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert.
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 Libgfortran is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 #include "libgfortran.h"
27 #include <string.h>
28 #include <stdlib.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_SYS_WAIT_H
34 #include <sys/wait.h>
35 #endif
36
37
38 enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
39 EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
40 static const char *cmdmsg_values[] =
41 { "",
42 "Termination status of the command-language interpreter cannot be obtained",
43 "Execution of child process impossible",
44 "Invalid command line" };
45
46
47
48 static void
set_cmdstat(int * cmdstat,int value)49 set_cmdstat (int *cmdstat, int value)
50 {
51 if (cmdstat)
52 *cmdstat = value;
53 else if (value > EXEC_NOERROR)
54 {
55 #define MSGLEN 200
56 char msg[MSGLEN] = "EXECUTE_COMMAND_LINE: ";
57 strncat (msg, cmdmsg_values[value], MSGLEN - strlen(msg) - 1);
58 runtime_error ("%s", msg);
59 }
60 }
61
62
63 static void
execute_command_line(const char * command,bool wait,int * exitstat,int * cmdstat,char * cmdmsg,gfc_charlen_type command_len,gfc_charlen_type cmdmsg_len)64 execute_command_line (const char *command, bool wait, int *exitstat,
65 int *cmdstat, char *cmdmsg,
66 gfc_charlen_type command_len,
67 gfc_charlen_type cmdmsg_len)
68 {
69 /* Transform the Fortran string to a C string. */
70 char *cmd = fc_strdup (command, command_len);
71
72 /* Flush all I/O units before executing the command. */
73 flush_all_units();
74
75 #if defined(HAVE_FORK)
76 if (!wait)
77 {
78 /* Asynchronous execution. */
79 pid_t pid;
80
81 set_cmdstat (cmdstat, EXEC_NOERROR);
82
83 if ((pid = fork()) < 0)
84 set_cmdstat (cmdstat, EXEC_CHILDFAILED);
85 else if (pid == 0)
86 {
87 /* Child process. */
88 int res = system (cmd);
89 _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res);
90 }
91 }
92 else
93 #endif
94 {
95 /* Synchronous execution. */
96 int res = system (cmd);
97
98 if (res == -1)
99 set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
100 #ifndef HAVE_FORK
101 else if (!wait)
102 set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
103 #endif
104 else if (res == 127 || res == 126
105 #if defined(WEXITSTATUS) && defined(WIFEXITED)
106 || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
107 || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
108 #endif
109 )
110 /* Shell return codes 126 and 127 mean that the command line could
111 not be executed for various reasons. */
112 set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);
113 else
114 set_cmdstat (cmdstat, EXEC_NOERROR);
115
116 if (res != -1)
117 {
118 #if defined(WEXITSTATUS) && defined(WIFEXITED)
119 *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
120 #else
121 *exitstat = res;
122 #endif
123 }
124 }
125
126 free (cmd);
127
128 /* Now copy back to the Fortran string if needed. */
129 if (cmdstat && *cmdstat > EXEC_NOERROR && cmdmsg)
130 fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
131 strlen (cmdmsg_values[*cmdstat]));
132 }
133
134
135 extern void
136 execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait,
137 GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat,
138 char *cmdmsg, gfc_charlen_type command_len,
139 gfc_charlen_type cmdmsg_len);
140 export_proto(execute_command_line_i4);
141
142 void
execute_command_line_i4(const char * command,GFC_LOGICAL_4 * wait,GFC_INTEGER_4 * exitstat,GFC_INTEGER_4 * cmdstat,char * cmdmsg,gfc_charlen_type command_len,gfc_charlen_type cmdmsg_len)143 execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait,
144 GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat,
145 char *cmdmsg, gfc_charlen_type command_len,
146 gfc_charlen_type cmdmsg_len)
147 {
148 bool w = wait ? *wait : true;
149 int estat, estat_initial, cstat;
150
151 if (exitstat)
152 estat_initial = estat = *exitstat;
153
154 execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL,
155 cmdmsg, command_len, cmdmsg_len);
156
157 if (exitstat && estat != estat_initial)
158 *exitstat = estat;
159 if (cmdstat)
160 *cmdstat = cstat;
161 }
162
163
164 extern void
165 execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait,
166 GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat,
167 char *cmdmsg, gfc_charlen_type command_len,
168 gfc_charlen_type cmdmsg_len);
169 export_proto(execute_command_line_i8);
170
171 void
execute_command_line_i8(const char * command,GFC_LOGICAL_8 * wait,GFC_INTEGER_8 * exitstat,GFC_INTEGER_8 * cmdstat,char * cmdmsg,gfc_charlen_type command_len,gfc_charlen_type cmdmsg_len)172 execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait,
173 GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat,
174 char *cmdmsg, gfc_charlen_type command_len,
175 gfc_charlen_type cmdmsg_len)
176 {
177 bool w = wait ? *wait : true;
178 int estat, estat_initial, cstat;
179
180 if (exitstat)
181 estat_initial = estat = *exitstat;
182
183 execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL,
184 cmdmsg, command_len, cmdmsg_len);
185
186 if (exitstat && estat != estat_initial)
187 *exitstat = estat;
188 if (cmdstat)
189 *cmdstat = cstat;
190 }
191