simpos.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <errno.h>
  22. #include <signal.h> /* for SIG constants */
  23. #include <stdlib.h> /* for getenv */
  24. #include "libguile/_scm.h"
  25. #include "libguile/scmsigs.h"
  26. #include "libguile/strings.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/simpos.h"
  29. #include "libguile/dynwind.h"
  30. #ifdef HAVE_STRING_H
  31. #include <string.h>
  32. #endif
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #if HAVE_SYS_WAIT_H
  37. # include <sys/wait.h>
  38. #endif
  39. #include "posix.h"
  40. extern int system();
  41. #ifdef HAVE_SYSTEM
  42. SCM_DEFINE (scm_system, "system", 0, 1, 0,
  43. (SCM cmd),
  44. "Execute @var{cmd} using the operating system's \"command\n"
  45. "processor\". Under Unix this is usually the default shell\n"
  46. "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
  47. "returned by @code{waitpid}, which can be interpreted using\n"
  48. "@code{status:exit-val} and friends.\n"
  49. "\n"
  50. "If @code{system} is called without arguments, return a boolean\n"
  51. "indicating whether the command processor is available.")
  52. #define FUNC_NAME s_scm_system
  53. {
  54. int rv, eno;
  55. char *c_cmd;
  56. if (SCM_UNBNDP (cmd))
  57. {
  58. rv = system (NULL);
  59. return scm_from_bool(rv);
  60. }
  61. SCM_VALIDATE_STRING (1, cmd);
  62. errno = 0;
  63. c_cmd = scm_to_locale_string (cmd);
  64. rv = system (c_cmd);
  65. eno = errno; free (c_cmd); errno = eno;
  66. if (rv == -1 || (rv == 127 && errno != 0))
  67. SCM_SYSERROR;
  68. return scm_from_int (rv);
  69. }
  70. #undef FUNC_NAME
  71. #endif /* HAVE_SYSTEM */
  72. #ifdef HAVE_SYSTEM
  73. #ifdef HAVE_WAITPID
  74. static void
  75. free_string_pointers (void *data)
  76. {
  77. scm_i_free_string_pointers ((char **)data);
  78. }
  79. SCM_DEFINE (scm_system_star, "system*", 0, 0, 1,
  80. (SCM args),
  81. "Execute the command indicated by @var{args}. The first element must\n"
  82. "be a string indicating the command to be executed, and the remaining\n"
  83. "items must be strings representing each of the arguments to that\n"
  84. "command.\n"
  85. "\n"
  86. "This function returns the exit status of the command as provided by\n"
  87. "@code{waitpid}. This value can be handled with @code{status:exit-val}\n"
  88. "and the related functions.\n"
  89. "\n"
  90. "@code{system*} is similar to @code{system}, but accepts only one\n"
  91. "string per-argument, and performs no shell interpretation. The\n"
  92. "command is executed using fork and execlp. Accordingly this function\n"
  93. "may be safer than @code{system} in situations where shell\n"
  94. "interpretation is not required.\n"
  95. "\n"
  96. "Example: (system* \"echo\" \"foo\" \"bar\")")
  97. #define FUNC_NAME s_scm_system_star
  98. {
  99. if (scm_is_null (args))
  100. SCM_WRONG_NUM_ARGS ();
  101. if (scm_is_pair (args))
  102. {
  103. SCM oldint;
  104. SCM oldquit;
  105. SCM sig_ign;
  106. SCM sigint;
  107. SCM sigquit;
  108. int pid;
  109. char **execargv;
  110. scm_dynwind_begin (0);
  111. /* allocate before fork */
  112. execargv = scm_i_allocate_string_pointers (args);
  113. scm_dynwind_unwind_handler (free_string_pointers, execargv,
  114. SCM_F_WIND_EXPLICITLY);
  115. /* make sure the child can't kill us (as per normal system call) */
  116. sig_ign = scm_from_long ((unsigned long) SIG_IGN);
  117. sigint = scm_from_int (SIGINT);
  118. sigquit = scm_from_int (SIGQUIT);
  119. oldint = scm_sigaction (sigint, sig_ign, SCM_UNDEFINED);
  120. oldquit = scm_sigaction (sigquit, sig_ign, SCM_UNDEFINED);
  121. pid = fork ();
  122. if (pid == 0)
  123. {
  124. /* child */
  125. execvp (execargv[0], execargv);
  126. SCM_SYSERROR;
  127. /* not reached. */
  128. scm_dynwind_end ();
  129. return SCM_BOOL_F;
  130. }
  131. else
  132. {
  133. /* parent */
  134. int wait_result, status;
  135. if (pid == -1)
  136. SCM_SYSERROR;
  137. SCM_SYSCALL (wait_result = waitpid (pid, &status, 0));
  138. if (wait_result == -1)
  139. SCM_SYSERROR;
  140. scm_sigaction (sigint, SCM_CAR (oldint), SCM_CDR (oldint));
  141. scm_sigaction (sigquit, SCM_CAR (oldquit), SCM_CDR (oldquit));
  142. scm_dynwind_end ();
  143. return scm_from_int (status);
  144. }
  145. }
  146. else
  147. SCM_WRONG_TYPE_ARG (1, args);
  148. }
  149. #undef FUNC_NAME
  150. #endif /* HAVE_WAITPID */
  151. #endif /* HAVE_SYSTEM */
  152. SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
  153. (SCM nam),
  154. "Looks up the string @var{name} in the current environment. The return\n"
  155. "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
  156. "found, in which case the string @code{VALUE} is returned.")
  157. #define FUNC_NAME s_scm_getenv
  158. {
  159. char *val;
  160. char *var = scm_to_locale_string (nam);
  161. val = getenv (var);
  162. free (var);
  163. return val ? scm_from_locale_string (val) : SCM_BOOL_F;
  164. }
  165. #undef FUNC_NAME
  166. /* simple exit, without unwinding the scheme stack or flushing ports. */
  167. SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
  168. (SCM status),
  169. "Terminate the current process without unwinding the Scheme\n"
  170. "stack. The exit status is @var{status} if supplied, otherwise\n"
  171. "zero.")
  172. #define FUNC_NAME s_scm_primitive_exit
  173. {
  174. int cstatus = 0;
  175. if (!SCM_UNBNDP (status))
  176. cstatus = scm_to_int (status);
  177. exit (cstatus);
  178. }
  179. #undef FUNC_NAME
  180. SCM_DEFINE (scm_primitive__exit, "primitive-_exit", 0, 1, 0,
  181. (SCM status),
  182. "Terminate the current process using the _exit() system call and\n"
  183. "without unwinding the Scheme stack. The exit status is\n"
  184. "@var{status} if supplied, otherwise zero.\n"
  185. "\n"
  186. "This function is typically useful after a fork, to ensure no\n"
  187. "Scheme cleanups or @code{atexit} handlers are run (those\n"
  188. "usually belonging in the parent rather than the child).")
  189. #define FUNC_NAME s_scm_primitive__exit
  190. {
  191. int cstatus = 0;
  192. if (!SCM_UNBNDP (status))
  193. cstatus = scm_to_int (status);
  194. _exit (cstatus);
  195. }
  196. #undef FUNC_NAME
  197. void
  198. scm_init_simpos ()
  199. {
  200. #include "libguile/simpos.x"
  201. }
  202. /*
  203. Local Variables:
  204. c-file-style: "gnu"
  205. End:
  206. */