simpos.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright 1995-1998,2000-2001,2003-2004,2009-2014,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <errno.h>
  19. #include <stdlib.h> /* for getenv, system, exit, free */
  20. #include <unistd.h> /* for _exit */
  21. #include "boolean.h"
  22. #include "gsubr.h"
  23. #include "numbers.h"
  24. #include "strings.h"
  25. #include "simpos.h"
  26. SCM_DEFINE (scm_system, "system", 0, 1, 0,
  27. (SCM cmd),
  28. "Execute @var{cmd} using the operating system's \"command\n"
  29. "processor\". Under Unix this is usually the default shell\n"
  30. "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
  31. "returned by @code{waitpid}, which can be interpreted using\n"
  32. "@code{status:exit-val} and friends.\n"
  33. "\n"
  34. "If @code{system} is called without arguments, return a boolean\n"
  35. "indicating whether the command processor is available.")
  36. #define FUNC_NAME s_scm_system
  37. {
  38. int rv, eno;
  39. char *c_cmd;
  40. if (SCM_UNBNDP (cmd))
  41. {
  42. rv = system (NULL);
  43. return scm_from_bool (rv);
  44. }
  45. SCM_VALIDATE_STRING (1, cmd);
  46. errno = 0;
  47. c_cmd = scm_to_locale_string (cmd);
  48. rv = system (c_cmd);
  49. eno = errno; free (c_cmd); errno = eno;
  50. if (rv == -1 || (rv == 127 && errno != 0))
  51. SCM_SYSERROR;
  52. return scm_from_int (rv);
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
  56. (SCM nam),
  57. "Looks up the string @var{nam} in the current environment. The return\n"
  58. "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
  59. "found, in which case the string @code{VALUE} is returned.")
  60. #define FUNC_NAME s_scm_getenv
  61. {
  62. char *val;
  63. char *var = scm_to_locale_string (nam);
  64. val = getenv (var);
  65. free (var);
  66. return val ? scm_from_locale_string (val) : SCM_BOOL_F;
  67. }
  68. #undef FUNC_NAME
  69. /* Get an integer from an environment variable. */
  70. int
  71. scm_getenv_int (const char *var, int def)
  72. {
  73. char *end = 0;
  74. char *val = getenv (var);
  75. long res = def;
  76. if (!val)
  77. return def;
  78. res = strtol (val, &end, 10);
  79. if (end == val)
  80. return def;
  81. return res;
  82. }
  83. /* simple exit, without unwinding the scheme stack or flushing ports. */
  84. SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
  85. (SCM status),
  86. "Terminate the current process without unwinding the Scheme\n"
  87. "stack. The exit status is @var{status} if supplied, otherwise\n"
  88. "zero.")
  89. #define FUNC_NAME s_scm_primitive_exit
  90. {
  91. int cstatus = 0;
  92. if (!SCM_UNBNDP (status))
  93. cstatus = scm_to_int (status);
  94. exit (cstatus);
  95. }
  96. #undef FUNC_NAME
  97. SCM_DEFINE (scm_primitive__exit, "primitive-_exit", 0, 1, 0,
  98. (SCM status),
  99. "Terminate the current process using the _exit() system call and\n"
  100. "without unwinding the Scheme stack. The exit status is\n"
  101. "@var{status} if supplied, otherwise zero.\n"
  102. "\n"
  103. "This function is typically useful after a fork, to ensure no\n"
  104. "Scheme cleanups or @code{atexit} handlers are run (those\n"
  105. "usually belonging in the parent rather than the child).")
  106. #define FUNC_NAME s_scm_primitive__exit
  107. {
  108. int cstatus = 0;
  109. if (!SCM_UNBNDP (status))
  110. cstatus = scm_to_int (status);
  111. _exit (cstatus);
  112. }
  113. #undef FUNC_NAME
  114. void
  115. scm_init_simpos ()
  116. {
  117. #include "simpos.x"
  118. }