Optimize the common cases (init == NULL or init == "") a bit. replace uses
[platform/upstream/glib.git] / glib / gspawn-win32-helper.c
1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
2  *
3  *  Copyright 2000 Red Hat, Inc.
4  *  Copyright 2000 Tor Lillqvist
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #undef G_LOG_DOMAIN
25 #include "glib.h"
26 #define GSPAWN_HELPER
27 #include "gspawn-win32.c"       /* For shared definitions */
28
29
30 static GString *debugstring;
31
32 static void
33 write_err_and_exit (gint fd,
34                     gint msg)
35 {
36   gint en = errno;
37   
38   if (debug)
39     {
40       debugstring = g_string_new (NULL);
41       g_string_append (debugstring,
42                        g_strdup_printf ("writing error code %d and errno %d",
43                                         msg, en));
44       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
45     }
46
47   write (fd, &msg, sizeof(msg));
48   write (fd, &en, sizeof(en));
49   
50   _exit (1);
51 }
52
53 #ifdef __GNUC__
54 #  ifndef _stdcall
55 #    define _stdcall  __attribute__((stdcall))
56 #  endif
57 #endif
58
59 /* We build gspawn-win32-helper.exe as a Windows GUI application
60  * to avoid any temporarily flashing console windows in case
61  * the gspawn function is invoked by a GUI program. Thus, no main()
62  * but a WinMain(). We do, however, still use argc and argv tucked
63  * away in the global __argc and __argv by the C runtime startup code.
64  */
65
66 int _stdcall
67 WinMain (struct HINSTANCE__ *hInstance,
68          struct HINSTANCE__ *hPrevInstance,
69          char               *lpszCmdLine,
70          int                 nCmdShow)
71 {
72   int child_err_report_fd;
73   int i;
74   int fd;
75   int mode;
76   int handle;
77   int no_error = CHILD_NO_ERROR;
78   int zero = 0;
79   gchar **new_argv;
80
81   SETUP_DEBUG();
82
83   if (debug)
84     {
85       debugstring = g_string_new (NULL);
86
87       g_string_append (debugstring,
88                        g_strdup_printf ("g-spawn-win32-helper: "
89                                         "argc = %d, argv: ",
90                                         __argc));
91       for (i = 0; i < __argc; i++)
92         {
93           if (i > 0)
94             g_string_append (debugstring, " ");
95           g_string_append (debugstring, __argv[i]);
96         }
97       
98       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
99     }
100
101   g_assert (__argc >= ARG_COUNT);
102
103   /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor onto which
104    * write error messages.
105    */
106   child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
107
108   /* argv[ARG_STDIN..ARG_STDERR] are the file descriptors that should
109    * be dup2'd to stdin, stdout and stderr, '-' if the corresponding
110    * std* should be let alone, and 'z' if it should be connected to
111    * the bit bucket NUL:.
112    */
113   if (__argv[ARG_STDIN][0] == '-')
114     ; /* Nothing */
115   else if (__argv[ARG_STDIN][0] == 'z')
116     {
117       fd = open ("NUL:", O_RDONLY);
118       if (fd != 0)
119         {
120           dup2 (fd, 0);
121           close (fd);
122         }
123     }
124   else
125     {
126       fd = atoi (__argv[ARG_STDIN]);
127       if (fd != 0)
128         {
129           dup2 (fd, 0);
130           close (fd);
131         }
132     }
133
134   if (__argv[ARG_STDOUT][0] == '-')
135     ; /* Nothing */
136   else if (__argv[ARG_STDOUT][0] == 'z')
137     {
138       fd = open ("NUL:", O_WRONLY);
139       if (fd != 1)
140         {
141           dup2 (fd, 1);
142           close (fd);
143         }
144     }
145   else
146     {
147       fd = atoi (__argv[ARG_STDOUT]);
148       if (fd != 1)
149         {
150           dup2 (fd, 1);
151           close (fd);
152         }
153     }
154
155   if (__argv[ARG_STDERR][0] == '-')
156     ; /* Nothing */
157   else if (__argv[ARG_STDERR][0] == 'z')
158     {
159       fd = open ("NUL:", O_WRONLY);
160       if (fd != 2)
161         {
162           dup2 (fd, 2);
163           close (fd);
164         }
165     }
166   else
167     {
168       fd = atoi (__argv[ARG_STDERR]);
169       if (fd != 2)
170         {
171           dup2 (fd, 2);
172           close (fd);
173         }
174     }
175
176   /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
177    * process.  If "-", don't change directory.
178    */
179   if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
180       __argv[ARG_WORKING_DIRECTORY][1] == 0)
181     ; /* Nothing */
182   else if (chdir (__argv[ARG_WORKING_DIRECTORY]) < 0)
183     write_err_and_exit (child_err_report_fd,
184                         CHILD_CHDIR_FAILED);
185
186   /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
187    *  upwards should be closed
188    */
189
190   if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
191     for (i = 3; i < 1000; i++)  /* FIXME real limit? */
192       if (i != child_err_report_fd)
193         close (i);
194
195   /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
196
197   if (__argv[ARG_WAIT][0] == 'w')
198     mode = P_WAIT;
199   else
200     mode = P_NOWAIT;
201
202   /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
203
204   /* __argv[ARG_PROGRAM] is program file to run,
205    * __argv[ARG_PROGRAM+1]... is its __argv.
206    */
207
208   protect_argv (__argv, &new_argv);
209
210   /* For the program name passed to spawnv(), don't use the quoted
211    * version. */
212
213   if (debug)
214     {
215       debugstring = g_string_new (NULL);
216       g_string_append (debugstring,
217                        g_strdup_printf ("calling %s %s mode=%s argv: ",
218                                         (__argv[ARG_USE_PATH][0] == 'y' ?
219                                          "spawnvp" : "spawnv"),
220                                         __argv[ARG_PROGRAM],
221                                         (mode == P_WAIT ?
222                                          "P_WAIT" : "P_NOWAIT")));
223       i = ARG_PROGRAM+1;
224       while (new_argv[i])
225         {
226           g_string_append (debugstring, new_argv[i++]);
227           if (new_argv[i])
228             g_string_append (debugstring, " ");
229         }
230       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
231     }
232
233   if (new_argv[ARG_USE_PATH][0] == 'y')
234     handle = spawnvp (mode, __argv[ARG_PROGRAM], new_argv+ARG_PROGRAM);
235   else
236     handle = spawnv (mode, __argv[ARG_PROGRAM], new_argv+ARG_PROGRAM);
237
238   if (debug)
239     {
240       debugstring = g_string_new (NULL);
241       g_string_append (debugstring,
242                        g_strdup_printf ("%s returned %#x",
243                                         (__argv[ARG_USE_PATH][0] == 'y' ?
244                                          "spawnvp" : "spawnv"),
245                                         handle));
246       MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
247     }
248
249   if (handle < 0)
250     write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
251
252   write (child_err_report_fd, &no_error, sizeof (no_error));
253   if (mode == P_NOWAIT)
254     write (child_err_report_fd, &handle, sizeof (handle));
255   else
256     write (child_err_report_fd, &zero, sizeof (zero));
257   return 0;
258 }
259