Use `scm_gc malloc_pointerless ()' in `scm_i allocate_string_pointers ()'.
[bpt/guile.git] / libguile / simpos.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2009 Free Software
2 * Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <errno.h>
26 #include <signal.h> /* for SIG constants */
27 #include <stdlib.h> /* for getenv */
28
29 #include "libguile/_scm.h"
30
31 #include "libguile/scmsigs.h"
32 #include "libguile/strings.h"
33
34 #include "libguile/validate.h"
35 #include "libguile/simpos.h"
36 #include "libguile/dynwind.h"
37
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #if HAVE_SYS_WAIT_H
45 # include <sys/wait.h>
46 #endif
47
48 #include "posix.h"
49
50 \f
51 extern int system();
52 \f
53
54 #ifdef HAVE_SYSTEM
55 SCM_DEFINE (scm_system, "system", 0, 1, 0,
56 (SCM cmd),
57 "Execute @var{cmd} using the operating system's \"command\n"
58 "processor\". Under Unix this is usually the default shell\n"
59 "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
60 "returned by @code{waitpid}, which can be interpreted using\n"
61 "@code{status:exit-val} and friends.\n"
62 "\n"
63 "If @code{system} is called without arguments, return a boolean\n"
64 "indicating whether the command processor is available.")
65 #define FUNC_NAME s_scm_system
66 {
67 int rv, eno;
68 char *c_cmd;
69
70 if (SCM_UNBNDP (cmd))
71 {
72 rv = system (NULL);
73 return scm_from_bool(rv);
74 }
75 SCM_VALIDATE_STRING (1, cmd);
76 errno = 0;
77 c_cmd = scm_to_locale_string (cmd);
78 rv = system (c_cmd);
79 eno = errno; free (c_cmd); errno = eno;
80 if (rv == -1 || (rv == 127 && errno != 0))
81 SCM_SYSERROR;
82 return scm_from_int (rv);
83 }
84 #undef FUNC_NAME
85 #endif /* HAVE_SYSTEM */
86
87
88 #ifdef HAVE_SYSTEM
89 #ifdef HAVE_WAITPID
90
91
92 SCM_DEFINE (scm_system_star, "system*", 0, 0, 1,
93 (SCM args),
94 "Execute the command indicated by @var{args}. The first element must\n"
95 "be a string indicating the command to be executed, and the remaining\n"
96 "items must be strings representing each of the arguments to that\n"
97 "command.\n"
98 "\n"
99 "This function returns the exit status of the command as provided by\n"
100 "@code{waitpid}. This value can be handled with @code{status:exit-val}\n"
101 "and the related functions.\n"
102 "\n"
103 "@code{system*} is similar to @code{system}, but accepts only one\n"
104 "string per-argument, and performs no shell interpretation. The\n"
105 "command is executed using fork and execlp. Accordingly this function\n"
106 "may be safer than @code{system} in situations where shell\n"
107 "interpretation is not required.\n"
108 "\n"
109 "Example: (system* \"echo\" \"foo\" \"bar\")")
110 #define FUNC_NAME s_scm_system_star
111 {
112 if (scm_is_null (args))
113 SCM_WRONG_NUM_ARGS ();
114
115 if (scm_is_pair (args))
116 {
117 SCM oldint;
118 SCM oldquit;
119 SCM sig_ign;
120 SCM sigint;
121 SCM sigquit;
122 int pid;
123 char **execargv;
124
125 /* allocate before fork */
126 execargv = scm_i_allocate_string_pointers (args);
127
128 /* make sure the child can't kill us (as per normal system call) */
129 sig_ign = scm_from_long ((unsigned long) SIG_IGN);
130 sigint = scm_from_int (SIGINT);
131 sigquit = scm_from_int (SIGQUIT);
132 oldint = scm_sigaction (sigint, sig_ign, SCM_UNDEFINED);
133 oldquit = scm_sigaction (sigquit, sig_ign, SCM_UNDEFINED);
134
135 pid = fork ();
136 if (pid == 0)
137 {
138 /* child */
139 execvp (execargv[0], execargv);
140 SCM_SYSERROR;
141 /* not reached. */
142 return SCM_BOOL_F;
143 }
144 else
145 {
146 /* parent */
147 int wait_result, status;
148
149 if (pid == -1)
150 SCM_SYSERROR;
151
152 SCM_SYSCALL (wait_result = waitpid (pid, &status, 0));
153 if (wait_result == -1)
154 SCM_SYSERROR;
155 scm_sigaction (sigint, SCM_CAR (oldint), SCM_CDR (oldint));
156 scm_sigaction (sigquit, SCM_CAR (oldquit), SCM_CDR (oldquit));
157
158 return scm_from_int (status);
159 }
160 }
161 else
162 SCM_WRONG_TYPE_ARG (1, args);
163 }
164 #undef FUNC_NAME
165 #endif /* HAVE_WAITPID */
166 #endif /* HAVE_SYSTEM */
167
168
169 SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
170 (SCM nam),
171 "Looks up the string @var{name} in the current environment. The return\n"
172 "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
173 "found, in which case the string @code{VALUE} is returned.")
174 #define FUNC_NAME s_scm_getenv
175 {
176 char *val;
177 char *var = scm_to_locale_string (nam);
178 val = getenv (var);
179 free (var);
180 return val ? scm_from_locale_string (val) : SCM_BOOL_F;
181 }
182 #undef FUNC_NAME
183
184 /* simple exit, without unwinding the scheme stack or flushing ports. */
185 SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
186 (SCM status),
187 "Terminate the current process without unwinding the Scheme\n"
188 "stack. The exit status is @var{status} if supplied, otherwise\n"
189 "zero.")
190 #define FUNC_NAME s_scm_primitive_exit
191 {
192 int cstatus = 0;
193 if (!SCM_UNBNDP (status))
194 cstatus = scm_to_int (status);
195 exit (cstatus);
196 }
197 #undef FUNC_NAME
198
199 SCM_DEFINE (scm_primitive__exit, "primitive-_exit", 0, 1, 0,
200 (SCM status),
201 "Terminate the current process using the _exit() system call and\n"
202 "without unwinding the Scheme stack. The exit status is\n"
203 "@var{status} if supplied, otherwise zero.\n"
204 "\n"
205 "This function is typically useful after a fork, to ensure no\n"
206 "Scheme cleanups or @code{atexit} handlers are run (those\n"
207 "usually belonging in the parent rather than the child).")
208 #define FUNC_NAME s_scm_primitive__exit
209 {
210 int cstatus = 0;
211 if (!SCM_UNBNDP (status))
212 cstatus = scm_to_int (status);
213 _exit (cstatus);
214 }
215 #undef FUNC_NAME
216
217
218
219 void
220 scm_init_simpos ()
221 {
222 #include "libguile/simpos.x"
223 }
224
225
226 /*
227 Local Variables:
228 c-file-style: "gnu"
229 End:
230 */