1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004 Free Software
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <signal.h> /* for SIG constants */
27 #include <stdlib.h> /* for getenv */
29 #include "libguile/_scm.h"
31 #include "libguile/scmsigs.h"
32 #include "libguile/strings.h"
34 #include "libguile/validate.h"
35 #include "libguile/simpos.h"
44 # include <sys/wait.h>
54 SCM_DEFINE (scm_system
, "system", 0, 1, 0,
56 "Execute @var{cmd} using the operating system's \"command\n"
57 "processor\". Under Unix this is usually the default shell\n"
58 "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
59 "returned by @code{waitpid}, which can be interpreted using\n"
60 "@code{status:exit-val} and friends.\n"
62 "If @code{system} is called without arguments, return a boolean\n"
63 "indicating whether the command processor is available.")
64 #define FUNC_NAME s_scm_system
71 return scm_from_bool(rv
);
73 SCM_VALIDATE_STRING (1, cmd
);
75 rv
= system (SCM_STRING_CHARS (cmd
));
76 if (rv
== -1 || (rv
== 127 && errno
!= 0))
78 return SCM_MAKINUM (rv
);
81 #endif /* HAVE_SYSTEM */
87 /* return a newly allocated array of char pointers to each of the strings
88 in args, with a terminating NULL pointer. */
89 /* Note: a similar function is defined in dynl.c, but we don't necessarily
92 allocate_string_pointers (SCM args
)
95 int n_args
= scm_ilength (args
);
98 SCM_ASSERT (n_args
>= 0, args
, SCM_ARGn
, "allocate_string_pointers");
99 result
= (char **) scm_malloc ((n_args
+ 1) * sizeof (char *));
100 result
[n_args
] = NULL
;
101 for (i
= 0; i
< n_args
; i
++)
103 SCM car
= SCM_CAR (args
);
105 if (!SCM_STRINGP (car
))
108 scm_wrong_type_arg ("allocate_string_pointers", SCM_ARGn
, car
);
110 result
[i
] = SCM_STRING_CHARS (SCM_CAR (args
));
111 args
= SCM_CDR (args
);
116 SCM_DEFINE (scm_system_star
, "system*", 0, 0, 1,
118 "Execute the command indicated by @var{args}. The first element must\n"
119 "be a string indicating the command to be executed, and the remaining\n"
120 "items must be strings representing each of the arguments to that\n"
123 "This function returns the exit status of the command as provided by\n"
124 "@code{waitpid}. This value can be handled with @code{status:exit-val}\n"
125 "and the related functions.\n"
127 "@code{system*} is similar to @code{system}, but accepts only one\n"
128 "string per-argument, and performs no shell interpretation. The\n"
129 "command is executed using fork and execlp. Accordingly this function\n"
130 "may be safer than @code{system} in situations where shell\n"
131 "interpretation is not required.\n"
133 "Example: (system* \"echo\" \"foo\" \"bar\")")
134 #define FUNC_NAME s_scm_system_star
136 if (SCM_NULLP (args
))
137 SCM_WRONG_NUM_ARGS ();
139 if (SCM_CONSP (args
))
149 SCM_VALIDATE_STRING (1, SCM_CAR (args
));
150 /* allocate before fork */
151 execargv
= allocate_string_pointers (args
);
153 /* make sure the child can't kill us (as per normal system call) */
154 sig_ign
= scm_long2num ((long) SIG_IGN
);
155 sigint
= scm_long2num (SIGINT
);
156 sigquit
= scm_long2num (SIGQUIT
);
157 oldint
= scm_sigaction (sigint
, sig_ign
, SCM_UNDEFINED
);
158 oldquit
= scm_sigaction (sigquit
, sig_ign
, SCM_UNDEFINED
);
164 execvp (SCM_STRING_CHARS (SCM_CAR (args
)), execargv
);
165 scm_remember_upto_here_1 (args
);
173 int wait_result
, status
, save_errno
;
181 SCM_SYSCALL (wait_result
= waitpid (pid
, &status
, 0));
182 if (wait_result
== -1) SCM_SYSERROR
;
183 scm_sigaction (sigint
, SCM_CAR (oldint
), SCM_CDR (oldint
));
184 scm_sigaction (sigquit
, SCM_CAR (oldquit
), SCM_CDR (oldquit
));
185 scm_remember_upto_here_2 (oldint
, oldquit
);
186 return SCM_MAKINUM (0L + status
);
190 SCM_WRONG_TYPE_ARG (1, SCM_CAR (args
));
193 #endif /* HAVE_WAITPID */
194 #endif /* HAVE_SYSTEM */
197 SCM_DEFINE (scm_getenv
, "getenv", 1, 0, 0,
199 "Looks up the string @var{name} in the current environment. The return\n"
200 "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
201 "found, in which case the string @code{VALUE} is returned.")
202 #define FUNC_NAME s_scm_getenv
205 SCM_VALIDATE_STRING (1, nam
);
206 val
= getenv (SCM_STRING_CHARS (nam
));
207 return val
? scm_mem2string (val
, strlen (val
)) : SCM_BOOL_F
;
211 /* simple exit, without unwinding the scheme stack or flushing ports. */
212 SCM_DEFINE (scm_primitive_exit
, "primitive-exit", 0, 1, 0,
214 "Terminate the current process without unwinding the Scheme stack.\n"
215 "This is would typically be useful after a fork. The exit status\n"
216 "is @var{status} if supplied, otherwise zero.")
217 #define FUNC_NAME s_scm_primitive_exit
220 if (!SCM_UNBNDP (status
))
222 SCM_VALIDATE_INUM (1, status
);
223 cstatus
= SCM_INUM (status
);
233 #include "libguile/simpos.x"