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