simpos.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef HAVE_SYSTEM
  27. SCM_DEFINE (scm_system, "system", 0, 1, 0,
  28. (SCM cmd),
  29. "Execute @var{cmd} using the operating system's \"command\n"
  30. "processor\". Under Unix this is usually the default shell\n"
  31. "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
  32. "returned by @code{waitpid}, which can be interpreted using\n"
  33. "@code{status:exit-val} and friends.\n"
  34. "\n"
  35. "If @code{system} is called without arguments, return a boolean\n"
  36. "indicating whether the command processor is available.")
  37. #define FUNC_NAME s_scm_system
  38. {
  39. int rv, eno;
  40. char *c_cmd;
  41. if (SCM_UNBNDP (cmd))
  42. {
  43. rv = system (NULL);
  44. return scm_from_bool (rv);
  45. }
  46. SCM_VALIDATE_STRING (1, cmd);
  47. errno = 0;
  48. c_cmd = scm_to_locale_string (cmd);
  49. rv = system (c_cmd);
  50. eno = errno; free (c_cmd); errno = eno;
  51. if (rv == -1 || (rv == 127 && errno != 0))
  52. SCM_SYSERROR;
  53. return scm_from_int (rv);
  54. }
  55. #undef FUNC_NAME
  56. #endif /* HAVE_SYSTEM */
  57. SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
  58. (SCM nam),
  59. "Looks up the string @var{nam} in the current environment. The return\n"
  60. "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
  61. "found, in which case the string @code{VALUE} is returned.")
  62. #define FUNC_NAME s_scm_getenv
  63. {
  64. char *val;
  65. char *var = scm_to_locale_string (nam);
  66. val = getenv (var);
  67. free (var);
  68. return val ? scm_from_locale_string (val) : SCM_BOOL_F;
  69. }
  70. #undef FUNC_NAME
  71. /* Get an integer from an environment variable. */
  72. int
  73. scm_getenv_int (const char *var, int def)
  74. {
  75. char *end = 0;
  76. char *val = getenv (var);
  77. long res = def;
  78. if (!val)
  79. return def;
  80. res = strtol (val, &end, 10);
  81. if (end == val)
  82. return def;
  83. return res;
  84. }
  85. /* simple exit, without unwinding the scheme stack or flushing ports. */
  86. SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
  87. (SCM status),
  88. "Terminate the current process without unwinding the Scheme\n"
  89. "stack. The exit status is @var{status} if supplied, otherwise\n"
  90. "zero.")
  91. #define FUNC_NAME s_scm_primitive_exit
  92. {
  93. int cstatus = 0;
  94. if (!SCM_UNBNDP (status))
  95. cstatus = scm_to_int (status);
  96. exit (cstatus);
  97. }
  98. #undef FUNC_NAME
  99. SCM_DEFINE (scm_primitive__exit, "primitive-_exit", 0, 1, 0,
  100. (SCM status),
  101. "Terminate the current process using the _exit() system call and\n"
  102. "without unwinding the Scheme stack. The exit status is\n"
  103. "@var{status} if supplied, otherwise zero.\n"
  104. "\n"
  105. "This function is typically useful after a fork, to ensure no\n"
  106. "Scheme cleanups or @code{atexit} handlers are run (those\n"
  107. "usually belonging in the parent rather than the child).")
  108. #define FUNC_NAME s_scm_primitive__exit
  109. {
  110. int cstatus = 0;
  111. if (!SCM_UNBNDP (status))
  112. cstatus = scm_to_int (status);
  113. _exit (cstatus);
  114. }
  115. #undef FUNC_NAME
  116. void
  117. scm_init_simpos ()
  118. {
  119. #include "simpos.x"
  120. }