simpos.c 6.3 KB

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