1 /* Various utilities - Unix variants
2 Copyright (C) 1998 the Free Software Foundation.
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. */
23 #include <sys/types.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>
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h> /* my_system */
38 #include <errno.h> /* my_system */
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
48 #include <gdk/gdkprivate.h>
51 int my_system (int flags
, const char *shell
, const char *command
)
54 struct sigaction ignore
, save_intr
, save_quit
, save_stop
;
57 ignore
.sa_handler
= SIG_IGN
;
58 sigemptyset (&ignore
.sa_mask
);
61 sigaction (SIGINT
, &ignore
, &save_intr
);
62 sigaction (SIGQUIT
, &ignore
, &save_quit
);
64 if ((pid
= fork ()) < 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
);
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
++)
86 /* Setup the file descriptor for the child */
89 open ("/dev/null", O_RDONLY
);
92 open ("/dev/null", O_WRONLY
);
95 open ("/dev/null", O_WRONLY
);
97 if (!(flags
& EXECUTE_WAIT
))
101 if (flags
& EXECUTE_AS_SHELL
)
102 execl (shell
, shell
, "-c", command
, (char *) 0);
104 execlp (shell
, shell
, command
, (char *) 0);
105 /* See note below for why we use _exit () */
106 _exit (127); /* Exec error */
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
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
);