simpos.c 4.1 KB

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