1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2000 Tor Lillqvist
6 * GLib is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * GLib is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GLib; see the file COPYING.LIB. If not, write
18 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gspawn-win32.c" /* For shared definitions */
26 write_err_and_exit (gint fd
,
31 write (fd
, &msg
, sizeof(msg
));
32 write (fd
, &en
, sizeof(en
));
38 write_no_error (gint fd
)
40 gint msg
= CHILD_NO_ERROR
;
43 write (fd
, &msg
, sizeof(msg
));
44 write (fd
, &en
, sizeof(en
));
49 # define _stdcall __attribute__((stdcall))
53 /* We build gspawn-win32-helper.exe as a Windows GUI application
54 * to avoid any temporarily flashing console windows in case
55 * the gspawn function is invoked by a GUI program. Thus, no main()
56 * but a WinMain(). We do, however, still use argc and argv tucked
57 * away in the global __argc and __argv by the C runtime startup code.
61 WinMain (struct HINSTANCE__
*hInstance
,
62 struct HINSTANCE__
*hPrevInstance
,
66 int child_err_report_fd
;
76 debugstring
= g_string_new ("");
78 g_string_append (debugstring
,
79 g_strdup_printf ("g-spawn-win32-helper: "
82 for (i
= 0; i
< __argc
; i
++)
85 g_string_append (debugstring
, " ");
86 g_string_append (debugstring
, __argv
[i
]);
89 MessageBox (NULL
, debugstring
->str
, "gspawn-win32-helper", 0);
92 g_assert (__argc
>= ARG_COUNT
);
94 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor onto which
95 * write error messages.
97 child_err_report_fd
= atoi (__argv
[ARG_CHILD_ERR_REPORT
]);
99 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptors that should
100 * be dup2'd to stdin, stdout and stderr, '-' if the corresponding
101 * std* should be let alone, and 'z' if it should be connected to
102 * the bit bucket NUL:.
104 if (__argv
[ARG_STDIN
][0] == '-')
106 else if (__argv
[ARG_STDIN
][0] == 'z')
108 fd
= open ("NUL:", O_RDONLY
);
117 fd
= atoi (__argv
[ARG_STDIN
]);
125 if (__argv
[ARG_STDOUT
][0] == '-')
127 else if (__argv
[ARG_STDOUT
][0] == 'z')
129 fd
= open ("NUL:", O_WRONLY
);
138 fd
= atoi (__argv
[ARG_STDOUT
]);
146 if (__argv
[ARG_STDERR
][0] == '-')
148 else if (__argv
[ARG_STDERR
][0] == 'z')
150 fd
= open ("NUL:", O_WRONLY
);
159 fd
= atoi (__argv
[ARG_STDERR
]);
167 /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
168 * process. If "-", don't change directory.
170 if (__argv
[ARG_WORKING_DIRECTORY
][0] == '-' &&
171 __argv
[ARG_WORKING_DIRECTORY
][1] == 0)
173 else if (chdir (__argv
[ARG_WORKING_DIRECTORY
]) < 0)
174 write_err_and_exit (child_err_report_fd
,
177 /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
178 * upwards should be closed
181 if (__argv
[ARG_CLOSE_DESCRIPTORS
][0] == 'y')
182 for (i
= 3; i
< 1000; i
++) /* FIXME real limit? */
183 if (i
!= child_err_report_fd
)
186 /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
188 if (__argv
[ARG_WAIT
][0] == 'w')
193 /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
195 /* __argv[ARG_PROGRAM] is program file to run,
196 * __argv[ARG_PROGRAM+1]... is its __argv.
201 debugstring
= g_string_new ("");
202 g_string_append (debugstring
,
203 g_strdup_printf ("calling %s on program %s, __argv: ",
204 (__argv
[ARG_USE_PATH
][0] == 'y' ?
205 "spawnvp" : "spawnv"),
206 __argv
[ARG_PROGRAM
]));
209 g_string_append (debugstring
, __argv
[i
++]);
210 MessageBox (NULL
, debugstring
->str
, "gspawn-win32-helper", 0);
213 if (__argv
[ARG_USE_PATH
][0] == 'y')
215 if (spawnvp (mode
, __argv
[ARG_PROGRAM
], __argv
+ARG_PROGRAM
) < 0)
216 write_err_and_exit (child_err_report_fd
, CHILD_SPAWN_FAILED
);
220 if (spawnv (mode
, __argv
[ARG_PROGRAM
], __argv
+ARG_PROGRAM
) < 0)
221 write_err_and_exit (child_err_report_fd
, CHILD_SPAWN_FAILED
);