simpos.c 6.9 KB

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