Defuzzied one entry
[midnight-commander.git] / gnome / gutil.c
blob2b91e79309cfa917d6c9b1cef479c74fcad3ec31
1 /* Various utilities - Unix variants
2 Copyright (C) 1998 the Free Software Foundation.
4 Written 1998 by:
5 Miguel de Icaza
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <fcntl.h>
28 #include <signal.h> /* my_system */
29 #include <limits.h> /* INT_MAX */
30 #include <sys/time.h> /* select: timeout */
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <stdarg.h>
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h> /* my_system */
37 #endif
38 #include <errno.h> /* my_system */
39 #include <pwd.h>
40 #include <grp.h>
41 #include <string.h>
42 #include <ctype.h>
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
45 #endif
46 #include <gnome.h>
47 #include <X11/Xlib.h>
48 #include <gdk/gdkprivate.h>
49 #include "global.h"
51 int my_system (int flags, const char *shell, const char *command)
53 pid_t pid;
54 struct sigaction ignore, save_intr, save_quit, save_stop;
55 int status = 0, i;
57 ignore.sa_handler = SIG_IGN;
58 sigemptyset (&ignore.sa_mask);
59 ignore.sa_flags = 0;
61 sigaction (SIGINT, &ignore, &save_intr);
62 sigaction (SIGQUIT, &ignore, &save_quit);
64 if ((pid = fork ()) < 0){
65 return -1;
67 if (pid == 0){
68 const int top = max_open_files ();
69 struct sigaction default_pipe;
71 sigaction (SIGINT, &save_intr, NULL);
72 sigaction (SIGQUIT, &save_quit, NULL);
75 * reset sigpipe
77 default_pipe.sa_handler = SIG_DFL;
78 sigemptyset (&default_pipe.sa_mask);
79 default_pipe.sa_flags = 0;
81 sigaction (SIGPIPE, &default_pipe, NULL);
83 for (i = 0; i < top; i++)
84 close (i);
86 /* Setup the file descriptor for the child */
88 /* stdin */
89 open ("/dev/null", O_RDONLY);
91 /* stdout */
92 open ("/dev/null", O_WRONLY);
94 /* stderr */
95 open ("/dev/null", O_WRONLY);
97 if (!(flags & EXECUTE_WAIT))
98 pid = fork ();
100 if (pid == 0){
101 if (flags & EXECUTE_AS_SHELL)
102 execl (shell, shell, "-c", command, (char *) 0);
103 else
104 execlp (shell, shell, command, (char *) 0);
105 /* See note below for why we use _exit () */
106 _exit (127); /* Exec error */
107 } else {
108 int status;
110 if (flags & EXECUTE_WAIT)
111 waitpid (pid, &status, 0);
113 /* We need to use _exit instead of exit to avoid
114 * calling the atexit handlers (specifically the gdk atexit
115 * handler
117 _exit (0);
119 waitpid (pid, &status, 0);
120 sigaction (SIGINT, &save_intr, NULL);
121 sigaction (SIGQUIT, &save_quit, NULL);
122 sigaction (SIGTSTP, &save_stop, NULL);
124 return WEXITSTATUS(status);