Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / simpos.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <errno.h>
25
26 #include "libguile/_scm.h"
27
28 #include "libguile/scmsigs.h"
29 #include "libguile/strings.h"
30
31 #include "libguile/validate.h"
32 #include "libguile/simpos.h"
33
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 \f
42 extern int system();
43 \f
44
45 #ifdef HAVE_SYSTEM
46 SCM_DEFINE (scm_system, "system", 0, 1, 0,
47 (SCM cmd),
48 "Execute @var{cmd} using the operating system's \"command\n"
49 "processor\". Under Unix this is usually the default shell\n"
50 "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
51 "returned by @code{waitpid}, which can be interpreted using the\n"
52 "functions above.\n"
53 "\n"
54 "If @code{system} is called without arguments, return a boolean\n"
55 "indicating whether the command processor is available.")
56 #define FUNC_NAME s_scm_system
57 {
58 int rv;
59
60 if (SCM_UNBNDP (cmd))
61 {
62 rv = system (NULL);
63 return SCM_BOOL(rv);
64 }
65 SCM_VALIDATE_STRING (1, cmd);
66 SCM_DEFER_INTS;
67 errno = 0;
68 rv = system (SCM_STRING_CHARS (cmd));
69 if (rv == -1 || (rv == 127 && errno != 0))
70 SCM_SYSERROR;
71 SCM_ALLOW_INTS;
72 return SCM_MAKINUM (rv);
73 }
74 #undef FUNC_NAME
75 #endif /* HAVE_SYSTEM */
76
77 extern char *getenv();
78 SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
79 (SCM nam),
80 "Looks up the string @var{name} in the current environment. The return\n"
81 "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
82 "found, in which case the string @code{VALUE} is returned.")
83 #define FUNC_NAME s_scm_getenv
84 {
85 char *val;
86 SCM_VALIDATE_STRING (1, nam);
87 val = getenv (SCM_STRING_CHARS (nam));
88 return val ? scm_mem2string (val, strlen (val)) : SCM_BOOL_F;
89 }
90 #undef FUNC_NAME
91
92 /* simple exit, without unwinding the scheme stack or flushing ports. */
93 SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
94 (SCM status),
95 "Terminate the current process without unwinding the Scheme stack.\n"
96 "This is would typically be useful after a fork. The exit status\n"
97 "is @var{status} if supplied, otherwise zero.")
98 #define FUNC_NAME s_scm_primitive_exit
99 {
100 int cstatus = 0;
101 if (!SCM_UNBNDP (status))
102 {
103 SCM_VALIDATE_INUM (1, status);
104 cstatus = SCM_INUM (status);
105 }
106 exit (cstatus);
107 }
108 #undef FUNC_NAME
109
110
111 void
112 scm_init_simpos ()
113 {
114 #include "libguile/simpos.x"
115 }
116
117
118 /*
119 Local Variables:
120 c-file-style: "gnu"
121 End:
122 */