Reveal guile-tools's inner simplicity...
[bpt/guile.git] / libguile / posix.c
CommitLineData
b56c252b 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5 21#ifdef HAVE_CONFIG_H
2546b5c3
RB
22# include <config.h>
23#endif
0f2d19dd 24
3731192f 25#include <stdlib.h>
0f2d19dd 26#include <stdio.h>
e6e2e95a 27#include <errno.h>
889975e5 28#include <uniconv.h>
e6e2e95a 29
22072f21
LC
30#ifdef HAVE_SCHED_H
31# include <sched.h>
32#endif
33
a0599745 34#include "libguile/_scm.h"
b28f5b3c 35#include "libguile/dynwind.h"
a0599745
MD
36#include "libguile/fports.h"
37#include "libguile/scmsigs.h"
38#include "libguile/feature.h"
39#include "libguile/strings.h"
c44ca4fe 40#include "libguile/srfi-13.h"
a17d2654 41#include "libguile/srfi-14.h"
a0599745 42#include "libguile/vectors.h"
4ea9429e 43#include "libguile/values.h"
a0599745
MD
44
45#include "libguile/validate.h"
46#include "libguile/posix.h"
b89c4943 47#include "libguile/gettext.h"
9de87eea 48#include "libguile/threads.h"
0f2d19dd
JB
49\f
50
02b754d3
GH
51#ifdef HAVE_STRING_H
52#include <string.h>
53#endif
0f2d19dd
JB
54#ifdef TIME_WITH_SYS_TIME
55# include <sys/time.h>
56# include <time.h>
57#else
58# if HAVE_SYS_TIME_H
59# include <sys/time.h>
60# else
61# include <time.h>
62# endif
63#endif
64
65#ifdef HAVE_UNISTD_H
66#include <unistd.h>
95b88819
GH
67#else
68#ifndef ttyname
69extern char *ttyname();
70#endif
0f2d19dd
JB
71#endif
72
3594582b 73#ifdef LIBC_H_WITH_UNISTD_H
bab0f4e5
JB
74#include <libc.h>
75#endif
76
8cc71382 77#include <sys/types.h>
0f2d19dd
JB
78#include <sys/stat.h>
79#include <fcntl.h>
80
82893676 81#ifdef HAVE_PWD_H
0f2d19dd 82#include <pwd.h>
82893676
MG
83#endif
84#ifdef HAVE_IO_H
85#include <io.h>
86#endif
f87c105a 87#ifdef HAVE_WINSOCK2_H
82893676
MG
88#include <winsock2.h>
89#endif
90
91#ifdef __MINGW32__
92/* Some defines for Windows here. */
7beabedb 93# include <process.h>
82893676
MG
94# define pipe(fd) _pipe (fd, 256, O_BINARY)
95#endif /* __MINGW32__ */
0f2d19dd
JB
96
97#if HAVE_SYS_WAIT_H
98# include <sys/wait.h>
99#endif
100#ifndef WEXITSTATUS
101# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
102#endif
103#ifndef WIFEXITED
104# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
105#endif
106
107#include <signal.h>
108
82893676 109#ifdef HAVE_GRP_H
0f2d19dd 110#include <grp.h>
82893676
MG
111#endif
112#ifdef HAVE_SYS_UTSNAME_H
0f2d19dd 113#include <sys/utsname.h>
82893676 114#endif
0f2d19dd 115
0f2d19dd
JB
116#ifdef HAVE_SETLOCALE
117#include <locale.h>
118#endif
119
b89c4943
LC
120#if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
121# define USE_GNU_LOCALE_API
122#endif
123
a2f00b9b
LC
124#if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
125# include <xlocale.h>
126#endif
127
534bbcc1 128#ifdef HAVE_CRYPT_H
94e6d793
MG
129# include <crypt.h>
130#endif
131
0eaf1055
KR
132#ifdef HAVE_NETDB_H
133#include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
134#endif
135
136#ifdef HAVE_SYS_PARAM_H
137#include <sys/param.h> /* for MAXHOSTNAMELEN */
138#endif
139
94e6d793
MG
140#if HAVE_SYS_RESOURCE_H
141# include <sys/resource.h>
142#endif
143
837b0ae0 144#include <sys/file.h> /* from Gnulib */
eb7e1603 145
bab0f4e5
JB
146/* Some Unix systems don't define these. CPP hair is dangerous, but
147 this seems safe enough... */
148#ifndef R_OK
149#define R_OK 4
150#endif
151
152#ifndef W_OK
153#define W_OK 2
154#endif
155
156#ifndef X_OK
157#define X_OK 1
158#endif
159
160#ifndef F_OK
161#define F_OK 0
162#endif
398609a5 163
8ab3d8a0
KR
164/* No prototype for this on Solaris 10. The man page says it's in
165 <unistd.h> ... but it lies. */
166#if ! HAVE_DECL_SETHOSTNAME
167int sethostname (char *name, size_t namelen);
168#endif
169
398609a5
JB
170/* On NextStep, <utime.h> doesn't define struct utime, unless we
171 #define _POSIX_SOURCE before #including it. I think this is less
172 of a kludge than defining struct utimbuf ourselves. */
173#ifdef UTIMBUF_NEEDS_POSIX
174#define _POSIX_SOURCE
175#endif
176
177#ifdef HAVE_SYS_UTIME_H
178#include <sys/utime.h>
179#endif
180
181#ifdef HAVE_UTIME_H
182#include <utime.h>
183#endif
184
185/* Please don't add any more #includes or #defines here. The hack
186 above means that _POSIX_SOURCE may be #defined, which will
eb7e1603
KR
187 encourage header files to do strange things.
188
189 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
190
191 FIXME: Probably should do all the includes first, then all the fallback
192 declarations and defines, in case things are not in the header we
193 imagine. */
194
195
196
197
0f2d19dd 198\f
f015614a
MV
199
200/* Two often used patterns
201 */
202
203#define WITH_STRING(str,cstr,code) \
204 do { \
205 char *cstr = scm_to_locale_string (str); \
206 code; \
207 free (cstr); \
208 } while (0)
209
210#define STRING_SYSCALL(str,cstr,code) \
211 do { \
212 int eno; \
213 char *cstr = scm_to_locale_string (str); \
214 SCM_SYSCALL (code); \
215 eno = errno; free (cstr); errno = eno; \
216 } while (0)
217
218
219\f
bc45012d
JB
220SCM_SYMBOL (sym_read_pipe, "read pipe");
221SCM_SYMBOL (sym_write_pipe, "write pipe");
0f2d19dd 222
a1ec6916 223SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
1bbd0b84 224 (),
1e6808ea
MG
225 "Return a newly created pipe: a pair of ports which are linked\n"
226 "together on the local machine. The @emph{car} is the input\n"
227 "port and the @emph{cdr} is the output port. Data written (and\n"
228 "flushed) to the output port can be read from the input port.\n"
229 "Pipes are commonly used for communication with a newly forked\n"
230 "child process. The need to flush the output port can be\n"
231 "avoided by making it unbuffered using @code{setvbuf}.\n"
232 "\n"
233 "Writes occur atomically provided the size of the data in bytes\n"
234 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
235 "the output port is likely to block if too much data (typically\n"
236 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
237 "from the input port.")
1bbd0b84 238#define FUNC_NAME s_scm_pipe
0f2d19dd
JB
239{
240 int fd[2], rv;
0f2d19dd 241 SCM p_rd, p_wt;
02b754d3 242
0f2d19dd
JB
243 rv = pipe (fd);
244 if (rv)
1bbd0b84 245 SCM_SYSERROR;
ee149d03
JB
246
247 p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
248 p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
0f2d19dd
JB
249 return scm_cons (p_rd, p_wt);
250}
1bbd0b84 251#undef FUNC_NAME
0f2d19dd
JB
252
253
0e958795 254#ifdef HAVE_GETGROUPS
a1ec6916 255SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
1bbd0b84 256 (),
1e6808ea 257 "Return a vector of integers representing the current\n"
bb2c02f2 258 "supplementary group IDs.")
1bbd0b84 259#define FUNC_NAME s_scm_getgroups
0f2d19dd 260{
1d1559ce 261 SCM result;
66460dfb 262 int ngroups;
1be6b49c 263 size_t size;
66460dfb
DH
264 GETGROUPS_T *groups;
265
266 ngroups = getgroups (0, NULL);
267 if (ngroups <= 0)
1bbd0b84 268 SCM_SYSERROR;
66460dfb
DH
269
270 size = ngroups * sizeof (GETGROUPS_T);
4c9419ac 271 groups = scm_malloc (size);
634aa8de 272 ngroups = getgroups (ngroups, groups);
66460dfb 273
f360a962
MV
274 result = scm_c_make_vector (ngroups, SCM_BOOL_F);
275 while (--ngroups >= 0)
4057a3e0 276 SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups]));
66460dfb 277
4c9419ac 278 free (groups);
1d1559ce 279 return result;
1bbd0b84
GB
280}
281#undef FUNC_NAME
0e958795 282#endif
0f2d19dd 283
f360a962
MV
284#ifdef HAVE_SETGROUPS
285SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
286 (SCM group_vec),
fcc3adc2
KR
287 "Set the current set of supplementary group IDs to the integers\n"
288 "in the given vector @var{vec}. The return value is\n"
289 "unspecified.\n"
290 "\n"
291 "Generally only the superuser can set the process group IDs.")
f360a962
MV
292#define FUNC_NAME s_scm_setgroups
293{
294 size_t ngroups;
295 size_t size;
296 size_t i;
297 int result;
298 int save_errno;
299 GETGROUPS_T *groups;
300
301 SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
302
4057a3e0 303 ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
f360a962
MV
304
305 /* validate before allocating, so we don't have to worry about leaks */
306 for (i = 0; i < ngroups; i++)
307 {
308 unsigned long ulong_gid;
309 GETGROUPS_T gid;
4057a3e0
MV
310 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
311 ulong_gid);
f360a962
MV
312 gid = ulong_gid;
313 if (gid != ulong_gid)
4057a3e0 314 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
f360a962
MV
315 }
316
317 size = ngroups * sizeof (GETGROUPS_T);
2eb78d06 318 if (size / sizeof (GETGROUPS_T) != ngroups)
e11e83f3 319 SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups));
f360a962
MV
320 groups = scm_malloc (size);
321 for(i = 0; i < ngroups; i++)
4057a3e0 322 groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
f360a962
MV
323
324 result = setgroups (ngroups, groups);
325 save_errno = errno; /* don't let free() touch errno */
326 free (groups);
327 errno = save_errno;
328 if (result < 0)
329 SCM_SYSERROR;
330 return SCM_UNSPECIFIED;
331}
332#undef FUNC_NAME
333#endif
334
82893676 335#ifdef HAVE_GETPWENT
a1ec6916 336SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
1bbd0b84 337 (SCM user),
b380b885
MD
338 "Look up an entry in the user database. @var{obj} can be an integer,\n"
339 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
340 "or getpwent respectively.")
1bbd0b84 341#define FUNC_NAME s_scm_getpwuid
0f2d19dd 342{
0f2d19dd 343 struct passwd *entry;
0f2d19dd 344
1d1559ce 345 SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
7888309b 346 if (SCM_UNBNDP (user) || scm_is_false (user))
0f2d19dd 347 {
0f2d19dd 348 SCM_SYSCALL (entry = getpwent ());
3d781d49
JB
349 if (! entry)
350 {
3d781d49
JB
351 return SCM_BOOL_F;
352 }
0f2d19dd 353 }
e11e83f3 354 else if (scm_is_integer (user))
0f2d19dd 355 {
e11e83f3 356 entry = getpwuid (scm_to_int (user));
0f2d19dd
JB
357 }
358 else
359 {
f015614a
MV
360 WITH_STRING (user, c_user,
361 entry = getpwnam (c_user));
0f2d19dd
JB
362 }
363 if (!entry)
1bbd0b84 364 SCM_MISC_ERROR ("entry not found", SCM_EOL);
02b754d3 365
4057a3e0
MV
366 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name));
367 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd));
368 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid));
369 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid));
370 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos));
0f2d19dd 371 if (!entry->pw_dir)
4057a3e0 372 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
0f2d19dd 373 else
4057a3e0 374 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir));
0f2d19dd 375 if (!entry->pw_shell)
4057a3e0 376 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (""));
0f2d19dd 377 else
4057a3e0 378 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
1d1559ce 379 return result;
0f2d19dd 380}
1bbd0b84 381#undef FUNC_NAME
82893676 382#endif /* HAVE_GETPWENT */
0f2d19dd
JB
383
384
0e958795 385#ifdef HAVE_SETPWENT
a1ec6916 386SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
1bbd0b84 387 (SCM arg),
b380b885
MD
388 "If called with a true argument, initialize or reset the password data\n"
389 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
390 "@code{endpwent} procedures are implemented on top of this.")
1bbd0b84 391#define FUNC_NAME s_scm_setpwent
0f2d19dd 392{
7888309b 393 if (SCM_UNBNDP (arg) || scm_is_false (arg))
0f2d19dd
JB
394 endpwent ();
395 else
396 setpwent ();
397 return SCM_UNSPECIFIED;
398}
1bbd0b84 399#undef FUNC_NAME
0e958795 400#endif
0f2d19dd
JB
401
402
82893676 403#ifdef HAVE_GETGRENT
0f2d19dd 404/* Combines getgrgid and getgrnam. */
a1ec6916 405SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
1bbd0b84 406 (SCM name),
b380b885
MD
407 "Look up an entry in the group database. @var{obj} can be an integer,\n"
408 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
409 "or getgrent respectively.")
1bbd0b84 410#define FUNC_NAME s_scm_getgrgid
0f2d19dd 411{
0f2d19dd 412 struct group *entry;
1d1559ce 413 SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
34d19ef6 414
7888309b 415 if (SCM_UNBNDP (name) || scm_is_false (name))
3d781d49
JB
416 {
417 SCM_SYSCALL (entry = getgrent ());
418 if (! entry)
419 {
3d781d49
JB
420 return SCM_BOOL_F;
421 }
422 }
e11e83f3
MV
423 else if (scm_is_integer (name))
424 SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
0f2d19dd 425 else
f015614a
MV
426 STRING_SYSCALL (name, c_name,
427 entry = getgrnam (c_name));
0f2d19dd 428 if (!entry)
1bbd0b84 429 SCM_SYSERROR;
02b754d3 430
4057a3e0
MV
431 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name));
432 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd));
433 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid));
434 SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem));
1d1559ce 435 return result;
0f2d19dd 436}
1bbd0b84 437#undef FUNC_NAME
0f2d19dd
JB
438
439
440
a1ec6916 441SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
1bbd0b84 442 (SCM arg),
b380b885
MD
443 "If called with a true argument, initialize or reset the group data\n"
444 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
445 "@code{endgrent} procedures are implemented on top of this.")
1bbd0b84 446#define FUNC_NAME s_scm_setgrent
0f2d19dd 447{
7888309b 448 if (SCM_UNBNDP (arg) || scm_is_false (arg))
0f2d19dd
JB
449 endgrent ();
450 else
451 setgrent ();
452 return SCM_UNSPECIFIED;
453}
1bbd0b84 454#undef FUNC_NAME
82893676 455#endif /* HAVE_GETGRENT */
0f2d19dd
JB
456
457
4ea9429e
AW
458#ifdef HAVE_GETRLIMIT
459#ifdef RLIMIT_AS
460SCM_SYMBOL (sym_as, "as");
461#endif
462#ifdef RLIMIT_CORE
463SCM_SYMBOL (sym_core, "core");
464#endif
465#ifdef RLIMIT_CPU
466SCM_SYMBOL (sym_cpu, "cpu");
467#endif
468#ifdef RLIMIT_DATA
469SCM_SYMBOL (sym_data, "data");
470#endif
471#ifdef RLIMIT_FSIZE
472SCM_SYMBOL (sym_fsize, "fsize");
473#endif
474#ifdef RLIMIT_MEMLOCK
475SCM_SYMBOL (sym_memlock, "memlock");
476#endif
477#ifdef RLIMIT_MSGQUEUE
478SCM_SYMBOL (sym_msgqueue, "msgqueue");
479#endif
480#ifdef RLIMIT_NICE
481SCM_SYMBOL (sym_nice, "nice");
482#endif
483#ifdef RLIMIT_NOFILE
484SCM_SYMBOL (sym_nofile, "nofile");
485#endif
486#ifdef RLIMIT_NPROC
487SCM_SYMBOL (sym_nproc, "nproc");
488#endif
489#ifdef RLIMIT_RSS
490SCM_SYMBOL (sym_rss, "rss");
491#endif
492#ifdef RLIMIT_RTPRIO
493SCM_SYMBOL (sym_rtprio, "rtprio");
494#endif
495#ifdef RLIMIT_RTPRIO
496SCM_SYMBOL (sym_rttime, "rttime");
497#endif
498#ifdef RLIMIT_SIGPENDING
499SCM_SYMBOL (sym_sigpending, "sigpending");
500#endif
501#ifdef RLIMIT_STACK
502SCM_SYMBOL (sym_stack, "stack");
503#endif
504
505static int
506scm_to_resource (SCM s, const char *func, int pos)
507{
508 if (scm_is_number (s))
509 return scm_to_int (s);
510
511 SCM_ASSERT_TYPE (scm_is_symbol (s), s, pos, func, "symbol");
512
513#ifdef RLIMIT_AS
d223c3fc 514 if (scm_is_eq (s, sym_as))
4ea9429e
AW
515 return RLIMIT_AS;
516#endif
517#ifdef RLIMIT_CORE
d223c3fc 518 if (scm_is_eq (s, sym_core))
4ea9429e
AW
519 return RLIMIT_CORE;
520#endif
521#ifdef RLIMIT_CPU
d223c3fc 522 if (scm_is_eq (s, sym_cpu))
4ea9429e
AW
523 return RLIMIT_CPU;
524#endif
525#ifdef RLIMIT_DATA
d223c3fc 526 if (scm_is_eq (s, sym_data))
4ea9429e
AW
527 return RLIMIT_DATA;
528#endif
529#ifdef RLIMIT_FSIZE
d223c3fc 530 if (scm_is_eq (s, sym_fsize))
4ea9429e
AW
531 return RLIMIT_FSIZE;
532#endif
533#ifdef RLIMIT_MEMLOCK
d223c3fc 534 if (scm_is_eq (s, sym_memlock))
4ea9429e
AW
535 return RLIMIT_MEMLOCK;
536#endif
537#ifdef RLIMIT_MSGQUEUE
d223c3fc 538 if (scm_is_eq (s, sym_msgqueue))
4ea9429e
AW
539 return RLIMIT_MSGQUEUE;
540#endif
541#ifdef RLIMIT_NICE
d223c3fc 542 if (scm_is_eq (s, sym_nice))
4ea9429e
AW
543 return RLIMIT_NICE;
544#endif
545#ifdef RLIMIT_NOFILE
d223c3fc 546 if (scm_is_eq (s, sym_nofile))
4ea9429e
AW
547 return RLIMIT_NOFILE;
548#endif
549#ifdef RLIMIT_NPROC
d223c3fc 550 if (scm_is_eq (s, sym_nproc))
4ea9429e
AW
551 return RLIMIT_NPROC;
552#endif
553#ifdef RLIMIT_RSS
d223c3fc 554 if (scm_is_eq (s, sym_rss))
4ea9429e
AW
555 return RLIMIT_RSS;
556#endif
557#ifdef RLIMIT_RTPRIO
d223c3fc 558 if (scm_is_eq (s, sym_rtprio))
4ea9429e
AW
559 return RLIMIT_RTPRIO;
560#endif
561#ifdef RLIMIT_RTPRIO
d223c3fc 562 if (scm_is_eq (s, sym_rttime))
4ea9429e
AW
563 return RLIMIT_RTPRIO;
564#endif
565#ifdef RLIMIT_SIGPENDING
d223c3fc 566 if (scm_is_eq (s, sym_sigpending))
4ea9429e
AW
567 return RLIMIT_SIGPENDING;
568#endif
569#ifdef RLIMIT_STACK
d223c3fc 570 if (scm_is_eq (s, sym_stack))
4ea9429e
AW
571 return RLIMIT_STACK;
572#endif
573
574 scm_misc_error (func, "invalid rlimit resource ~A", scm_list_1 (s));
575 return 0;
576}
577
578SCM_DEFINE (scm_getrlimit, "getrlimit", 1, 0, 0,
579 (SCM resource),
580 "Get a resource limit for this process. @var{resource} identifies the resource,\n"
581 "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
582 "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
583 "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
584 "limit is set for the resource in question, the returned limit will be @code{#f}.")
585#define FUNC_NAME s_scm_getrlimit
586{
587 int iresource;
588 struct rlimit lim = { 0, 0 };
589
590 iresource = scm_to_resource (resource, FUNC_NAME, 1);
591
592 if (getrlimit (iresource, &lim) != 0)
593 scm_syserror (FUNC_NAME);
594
595 return scm_values (scm_list_2 ((lim.rlim_cur == RLIM_INFINITY) ? SCM_BOOL_F
596 : scm_from_long (lim.rlim_cur),
597 (lim.rlim_max == RLIM_INFINITY) ? SCM_BOOL_F
598 : scm_from_long (lim.rlim_max)));
599}
600#undef FUNC_NAME
601
602
603#ifdef HAVE_SETRLIMIT
604SCM_DEFINE (scm_setrlimit, "setrlimit", 3, 0, 0,
605 (SCM resource, SCM soft, SCM hard),
606 "Set a resource limit for this process. @var{resource} identifies the resource,\n"
607 "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
608 "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
609 "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
610 "limit to 150 kilobytes, with a hard limit of 300 kB.")
611#define FUNC_NAME s_scm_setrlimit
612{
613 int iresource;
614 struct rlimit lim = { 0, 0 };
615
616 iresource = scm_to_resource (resource, FUNC_NAME, 1);
617
393baa8a
AW
618 lim.rlim_cur = scm_is_false (soft) ? RLIM_INFINITY : scm_to_long (soft);
619 lim.rlim_max = scm_is_false (hard) ? RLIM_INFINITY : scm_to_long (hard);
4ea9429e
AW
620
621 if (setrlimit (iresource, &lim) != 0)
622 scm_syserror (FUNC_NAME);
623
624 return SCM_UNSPECIFIED;
625}
626#undef FUNC_NAME
627#endif /* HAVE_SETRLIMIT */
628#endif /* HAVE_GETRLIMIT */
629
630
a1ec6916 631SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
1bbd0b84 632 (SCM pid, SCM sig),
b380b885
MD
633 "Sends a signal to the specified process or group of processes.\n\n"
634 "@var{pid} specifies the processes to which the signal is sent:\n\n"
635 "@table @r\n"
636 "@item @var{pid} greater than 0\n"
637 "The process whose identifier is @var{pid}.\n"
638 "@item @var{pid} equal to 0\n"
639 "All processes in the current process group.\n"
640 "@item @var{pid} less than -1\n"
641 "The process group whose identifier is -@var{pid}\n"
642 "@item @var{pid} equal to -1\n"
643 "If the process is privileged, all processes except for some special\n"
644 "system processes. Otherwise, all processes with the current effective\n"
645 "user ID.\n"
646 "@end table\n\n"
647 "@var{sig} should be specified using a variable corresponding to\n"
648 "the Unix symbolic name, e.g.,\n\n"
649 "@defvar SIGHUP\n"
650 "Hang-up signal.\n"
651 "@end defvar\n\n"
652 "@defvar SIGINT\n"
653 "Interrupt signal.\n"
654 "@end defvar")
1bbd0b84 655#define FUNC_NAME s_scm_kill
0f2d19dd 656{
0f2d19dd 657 /* Signal values are interned in scm_init_posix(). */
82893676 658#ifdef HAVE_KILL
a55c2b68 659 if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
23d72566 660 SCM_SYSERROR;
82893676 661#else
23d72566
KR
662 /* Mingw has raise(), but not kill(). (Other raw DOS environments might
663 be similar.) Use raise() when the requested pid is our own process,
664 otherwise bomb. */
a55c2b68 665 if (scm_to_int (pid) == getpid ())
23d72566
KR
666 {
667 if (raise (scm_to_int (sig)) != 0)
668 {
669 err:
670 SCM_SYSERROR;
671 }
672 else
673 {
674 errno = ENOSYS;
675 goto err;
676 }
677 }
82893676 678#endif
02b754d3 679 return SCM_UNSPECIFIED;
0f2d19dd 680}
1bbd0b84 681#undef FUNC_NAME
0f2d19dd 682
d00ae47e 683#ifdef HAVE_WAITPID
a1ec6916 684SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
1bbd0b84 685 (SCM pid, SCM options),
b380b885
MD
686 "This procedure collects status information from a child process which\n"
687 "has terminated or (optionally) stopped. Normally it will\n"
688 "suspend the calling process until this can be done. If more than one\n"
689 "child process is eligible then one will be chosen by the operating system.\n\n"
690 "The value of @var{pid} determines the behaviour:\n\n"
691 "@table @r\n"
692 "@item @var{pid} greater than 0\n"
693 "Request status information from the specified child process.\n"
694 "@item @var{pid} equal to -1 or WAIT_ANY\n"
695 "Request status information for any child process.\n"
696 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
697 "Request status information for any child process in the current process\n"
698 "group.\n"
699 "@item @var{pid} less than -1\n"
700 "Request status information for any child process whose process group ID\n"
701 "is -@var{PID}.\n"
702 "@end table\n\n"
703 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
704 "values of zero or more of the following variables:\n\n"
705 "@defvar WNOHANG\n"
706 "Return immediately even if there are no child processes to be collected.\n"
707 "@end defvar\n\n"
708 "@defvar WUNTRACED\n"
709 "Report status information for stopped processes as well as terminated\n"
710 "processes.\n"
711 "@end defvar\n\n"
712 "The return value is a pair containing:\n\n"
713 "@enumerate\n"
714 "@item\n"
715 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
716 "specified and no process was collected.\n"
717 "@item\n"
718 "The integer status value.\n"
719 "@end enumerate")
1bbd0b84 720#define FUNC_NAME s_scm_waitpid
0f2d19dd
JB
721{
722 int i;
723 int status;
724 int ioptions;
0f2d19dd
JB
725 if (SCM_UNBNDP (options))
726 ioptions = 0;
727 else
728 {
0f2d19dd 729 /* Flags are interned in scm_init_posix. */
a55c2b68 730 ioptions = scm_to_int (options);
0f2d19dd 731 }
a55c2b68 732 SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
02b754d3 733 if (i == -1)
1bbd0b84 734 SCM_SYSERROR;
a55c2b68 735 return scm_cons (scm_from_int (i), scm_from_int (status));
0f2d19dd 736}
1bbd0b84 737#undef FUNC_NAME
d00ae47e 738#endif /* HAVE_WAITPID */
0f2d19dd 739
82893676 740#ifndef __MINGW32__
a1ec6916 741SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
1bbd0b84 742 (SCM status),
1e6808ea
MG
743 "Return the exit status value, as would be set if a process\n"
744 "ended normally through a call to @code{exit} or @code{_exit},\n"
745 "if any, otherwise @code{#f}.")
1bbd0b84 746#define FUNC_NAME s_scm_status_exit_val
67ec3667 747{
e67dc2be
JB
748 int lstatus;
749
e67dc2be 750 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
e11e83f3 751 go figure. */
a55c2b68 752 lstatus = scm_to_int (status);
e67dc2be 753 if (WIFEXITED (lstatus))
a55c2b68 754 return (scm_from_int (WEXITSTATUS (lstatus)));
67ec3667
GH
755 else
756 return SCM_BOOL_F;
757}
1bbd0b84 758#undef FUNC_NAME
e67dc2be 759
a1ec6916 760SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
1bbd0b84 761 (SCM status),
1e6808ea
MG
762 "Return the signal number which terminated the process, if any,\n"
763 "otherwise @code{#f}.")
1bbd0b84 764#define FUNC_NAME s_scm_status_term_sig
67ec3667 765{
e67dc2be
JB
766 int lstatus;
767
a55c2b68 768 lstatus = scm_to_int (status);
e67dc2be 769 if (WIFSIGNALED (lstatus))
a55c2b68 770 return scm_from_int (WTERMSIG (lstatus));
67ec3667
GH
771 else
772 return SCM_BOOL_F;
773}
1bbd0b84 774#undef FUNC_NAME
0f2d19dd 775
a1ec6916 776SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
1bbd0b84 777 (SCM status),
1e6808ea
MG
778 "Return the signal number which stopped the process, if any,\n"
779 "otherwise @code{#f}.")
1bbd0b84 780#define FUNC_NAME s_scm_status_stop_sig
67ec3667 781{
e67dc2be
JB
782 int lstatus;
783
a55c2b68 784 lstatus = scm_to_int (status);
e67dc2be 785 if (WIFSTOPPED (lstatus))
a55c2b68 786 return scm_from_int (WSTOPSIG (lstatus));
67ec3667
GH
787 else
788 return SCM_BOOL_F;
789}
1bbd0b84 790#undef FUNC_NAME
82893676 791#endif /* __MINGW32__ */
0f2d19dd 792
82893676 793#ifdef HAVE_GETPPID
a1ec6916 794SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
1bbd0b84 795 (),
1e6808ea
MG
796 "Return an integer representing the process ID of the parent\n"
797 "process.")
1bbd0b84 798#define FUNC_NAME s_scm_getppid
0f2d19dd 799{
e11e83f3 800 return scm_from_int (getppid ());
0f2d19dd 801}
1bbd0b84 802#undef FUNC_NAME
82893676 803#endif /* HAVE_GETPPID */
0f2d19dd
JB
804
805
82893676 806#ifndef __MINGW32__
a1ec6916 807SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
1bbd0b84 808 (),
1e6808ea 809 "Return an integer representing the current real user ID.")
1bbd0b84 810#define FUNC_NAME s_scm_getuid
0f2d19dd 811{
e11e83f3 812 return scm_from_int (getuid ());
0f2d19dd 813}
1bbd0b84 814#undef FUNC_NAME
0f2d19dd
JB
815
816
817
a1ec6916 818SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
1bbd0b84 819 (),
1e6808ea 820 "Return an integer representing the current real group ID.")
1bbd0b84 821#define FUNC_NAME s_scm_getgid
0f2d19dd 822{
e11e83f3 823 return scm_from_int (getgid ());
0f2d19dd 824}
1bbd0b84 825#undef FUNC_NAME
0f2d19dd
JB
826
827
828
a1ec6916 829SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
1bbd0b84 830 (),
1e6808ea 831 "Return an integer representing the current effective user ID.\n"
b380b885 832 "If the system does not support effective IDs, then the real ID\n"
480fa28d 833 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
1e6808ea 834 "system supports effective IDs.")
1bbd0b84 835#define FUNC_NAME s_scm_geteuid
0f2d19dd
JB
836{
837#ifdef HAVE_GETEUID
e11e83f3 838 return scm_from_int (geteuid ());
0f2d19dd 839#else
e11e83f3 840 return scm_from_int (getuid ());
0f2d19dd
JB
841#endif
842}
1bbd0b84 843#undef FUNC_NAME
0f2d19dd
JB
844
845
a1ec6916 846SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
1bbd0b84 847 (),
1e6808ea 848 "Return an integer representing the current effective group ID.\n"
b380b885 849 "If the system does not support effective IDs, then the real ID\n"
480fa28d 850 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
1e6808ea 851 "system supports effective IDs.")
1bbd0b84 852#define FUNC_NAME s_scm_getegid
0f2d19dd
JB
853{
854#ifdef HAVE_GETEUID
e11e83f3 855 return scm_from_int (getegid ());
0f2d19dd 856#else
e11e83f3 857 return scm_from_int (getgid ());
0f2d19dd
JB
858#endif
859}
1bbd0b84 860#undef FUNC_NAME
0f2d19dd
JB
861
862
a1ec6916 863SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
1bbd0b84 864 (SCM id),
b380b885
MD
865 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
866 "the process has appropriate privileges.\n"
867 "The return value is unspecified.")
1bbd0b84 868#define FUNC_NAME s_scm_setuid
0f2d19dd 869{
a55c2b68 870 if (setuid (scm_to_int (id)) != 0)
1bbd0b84 871 SCM_SYSERROR;
02b754d3 872 return SCM_UNSPECIFIED;
0f2d19dd 873}
1bbd0b84 874#undef FUNC_NAME
0f2d19dd 875
a1ec6916 876SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
1bbd0b84 877 (SCM id),
b380b885
MD
878 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
879 "the process has appropriate privileges.\n"
880 "The return value is unspecified.")
1bbd0b84 881#define FUNC_NAME s_scm_setgid
0f2d19dd 882{
a55c2b68 883 if (setgid (scm_to_int (id)) != 0)
1bbd0b84 884 SCM_SYSERROR;
02b754d3 885 return SCM_UNSPECIFIED;
0f2d19dd 886}
1bbd0b84 887#undef FUNC_NAME
0f2d19dd 888
a1ec6916 889SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
1bbd0b84 890 (SCM id),
b380b885
MD
891 "Sets the effective user ID to the integer @var{id}, provided the process\n"
892 "has appropriate privileges. If effective IDs are not supported, the\n"
480fa28d 893 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
b380b885
MD
894 "system supports effective IDs.\n"
895 "The return value is unspecified.")
1bbd0b84 896#define FUNC_NAME s_scm_seteuid
0f2d19dd 897{
02b754d3
GH
898 int rv;
899
0f2d19dd 900#ifdef HAVE_SETEUID
a55c2b68 901 rv = seteuid (scm_to_int (id));
0f2d19dd 902#else
a55c2b68 903 rv = setuid (scm_to_int (id));
0f2d19dd 904#endif
02b754d3 905 if (rv != 0)
1bbd0b84 906 SCM_SYSERROR;
02b754d3 907 return SCM_UNSPECIFIED;
0f2d19dd 908}
1bbd0b84 909#undef FUNC_NAME
7beabedb
MG
910#endif /* __MINGW32__ */
911
0f2d19dd 912
0e958795 913#ifdef HAVE_SETEGID
a1ec6916 914SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
1bbd0b84 915 (SCM id),
b380b885
MD
916 "Sets the effective group ID to the integer @var{id}, provided the process\n"
917 "has appropriate privileges. If effective IDs are not supported, the\n"
480fa28d 918 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
b380b885
MD
919 "system supports effective IDs.\n"
920 "The return value is unspecified.")
1bbd0b84 921#define FUNC_NAME s_scm_setegid
0f2d19dd 922{
02b754d3
GH
923 int rv;
924
0f2d19dd 925#ifdef HAVE_SETEUID
a55c2b68 926 rv = setegid (scm_to_int (id));
0f2d19dd 927#else
a55c2b68 928 rv = setgid (scm_to_int (id));
0f2d19dd 929#endif
02b754d3 930 if (rv != 0)
1bbd0b84 931 SCM_SYSERROR;
02b754d3
GH
932 return SCM_UNSPECIFIED;
933
0f2d19dd 934}
1bbd0b84 935#undef FUNC_NAME
0e958795 936#endif
0f2d19dd 937
82893676
MG
938
939#ifdef HAVE_GETPGRP
a1ec6916 940SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
1bbd0b84 941 (),
1e6808ea 942 "Return an integer representing the current process group ID.\n"
b380b885 943 "This is the POSIX definition, not BSD.")
1bbd0b84 944#define FUNC_NAME s_scm_getpgrp
0f2d19dd
JB
945{
946 int (*fn)();
4625e44f 947 fn = (int (*) ()) getpgrp;
e11e83f3 948 return scm_from_int (fn (0));
0f2d19dd 949}
1bbd0b84 950#undef FUNC_NAME
82893676
MG
951#endif /* HAVE_GETPGRP */
952
0f2d19dd 953
f25f761d 954#ifdef HAVE_SETPGID
a1ec6916 955SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
1bbd0b84 956 (SCM pid, SCM pgid),
b380b885
MD
957 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
958 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
959 "current process.\n"
960 "Fails on systems that do not support job control.\n"
961 "The return value is unspecified.")
1bbd0b84 962#define FUNC_NAME s_scm_setpgid
0f2d19dd 963{
02b754d3 964 /* FIXME(?): may be known as setpgrp. */
a55c2b68 965 if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
1bbd0b84 966 SCM_SYSERROR;
02b754d3 967 return SCM_UNSPECIFIED;
0f2d19dd 968}
1bbd0b84 969#undef FUNC_NAME
f25f761d 970#endif /* HAVE_SETPGID */
0f2d19dd 971
f25f761d 972#ifdef HAVE_SETSID
a1ec6916 973SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
1bbd0b84 974 (),
b380b885
MD
975 "Creates a new session. The current process becomes the session leader\n"
976 "and is put in a new process group. The process will be detached\n"
977 "from its controlling terminal if it has one.\n"
978 "The return value is an integer representing the new process group ID.")
1bbd0b84 979#define FUNC_NAME s_scm_setsid
0f2d19dd
JB
980{
981 pid_t sid = setsid ();
02b754d3 982 if (sid == -1)
1bbd0b84 983 SCM_SYSERROR;
02b754d3 984 return SCM_UNSPECIFIED;
0f2d19dd 985}
1bbd0b84 986#undef FUNC_NAME
f25f761d 987#endif /* HAVE_SETSID */
0f2d19dd 988
3e5aed1c
NJ
989#ifdef HAVE_GETSID
990SCM_DEFINE (scm_getsid, "getsid", 1, 0, 0,
991 (SCM pid),
992 "Returns the session ID of process @var{pid}. (The session\n"
993 "ID of a process is the process group ID of its session leader.)")
994#define FUNC_NAME s_scm_getsid
995{
996 return scm_from_int (getsid (scm_to_int (pid)));
997}
998#undef FUNC_NAME
999#endif /* HAVE_GETSID */
1000
e8a59063
KR
1001
1002/* ttyname returns its result in a single static buffer, hence
1003 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
1004 continuously calling ttyname will otherwise get an overwrite quite
1005 easily.
1006
1007 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
1008 there's probably little to be gained in either speed or parallelism. */
1009
82893676 1010#ifdef HAVE_TTYNAME
a1ec6916 1011SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
1bbd0b84 1012 (SCM port),
1e6808ea
MG
1013 "Return a string with the name of the serial terminal device\n"
1014 "underlying @var{port}.")
1bbd0b84 1015#define FUNC_NAME s_scm_ttyname
0f2d19dd 1016{
1d1559ce 1017 char *result;
e8a59063 1018 int fd, err;
a1ef7406 1019 SCM ret = SCM_BOOL_F;
78446828
MV
1020
1021 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1022 SCM_VALIDATE_OPPORT (1, port);
a98bddfd 1023 if (!SCM_FPORTP (port))
0f2d19dd 1024 return SCM_BOOL_F;
ee149d03 1025 fd = SCM_FPORT_FDES (port);
e8a59063 1026
9de87eea 1027 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
a1ef7406 1028
1d1559ce 1029 SCM_SYSCALL (result = ttyname (fd));
e8a59063 1030 err = errno;
a1ef7406
LC
1031 if (result != NULL)
1032 result = strdup (result);
1033
9de87eea 1034 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
e8a59063 1035
1d1559ce 1036 if (!result)
e8a59063
KR
1037 {
1038 errno = err;
1039 SCM_SYSERROR;
1040 }
a1ef7406
LC
1041 else
1042 ret = scm_take_locale_string (result);
1043
e8a59063 1044 return ret;
0f2d19dd 1045}
1bbd0b84 1046#undef FUNC_NAME
82893676 1047#endif /* HAVE_TTYNAME */
0f2d19dd 1048
e8a59063 1049
0a9d83b0
KR
1050/* For thread safety "buf" is used instead of NULL for the ctermid static
1051 buffer. Actually it's unlikely the controlling terminal will change
1052 during program execution, and indeed on glibc (2.3.2) it's always just
1053 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
1054 safety everywhere. */
f25f761d 1055#ifdef HAVE_CTERMID
a1ec6916 1056SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
1bbd0b84 1057 (),
1e6808ea
MG
1058 "Return a string containing the file name of the controlling\n"
1059 "terminal for the current process.")
1bbd0b84 1060#define FUNC_NAME s_scm_ctermid
0f2d19dd 1061{
0a9d83b0
KR
1062 char buf[L_ctermid];
1063 char *result = ctermid (buf);
02b754d3 1064 if (*result == '\0')
1bbd0b84 1065 SCM_SYSERROR;
cc95e00a 1066 return scm_from_locale_string (result);
0f2d19dd 1067}
1bbd0b84 1068#undef FUNC_NAME
f25f761d 1069#endif /* HAVE_CTERMID */
0f2d19dd 1070
f25f761d 1071#ifdef HAVE_TCGETPGRP
a1ec6916 1072SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
1bbd0b84 1073 (SCM port),
1e6808ea
MG
1074 "Return the process group ID of the foreground process group\n"
1075 "associated with the terminal open on the file descriptor\n"
1076 "underlying @var{port}.\n"
1077 "\n"
b380b885
MD
1078 "If there is no foreground process group, the return value is a\n"
1079 "number greater than 1 that does not match the process group ID\n"
1080 "of any existing process group. This can happen if all of the\n"
1081 "processes in the job that was formerly the foreground job have\n"
1082 "terminated, and no other job has yet been moved into the\n"
1083 "foreground.")
1bbd0b84 1084#define FUNC_NAME s_scm_tcgetpgrp
0f2d19dd
JB
1085{
1086 int fd;
1087 pid_t pgid;
78446828
MV
1088
1089 port = SCM_COERCE_OUTPORT (port);
1090
34d19ef6 1091 SCM_VALIDATE_OPFPORT (1, port);
ee149d03
JB
1092 fd = SCM_FPORT_FDES (port);
1093 if ((pgid = tcgetpgrp (fd)) == -1)
1bbd0b84 1094 SCM_SYSERROR;
e11e83f3 1095 return scm_from_int (pgid);
1bbd0b84
GB
1096}
1097#undef FUNC_NAME
f25f761d 1098#endif /* HAVE_TCGETPGRP */
0f2d19dd 1099
f25f761d 1100#ifdef HAVE_TCSETPGRP
a1ec6916 1101SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
1bbd0b84 1102 (SCM port, SCM pgid),
b380b885
MD
1103 "Set the foreground process group ID for the terminal used by the file\n"
1104 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
1105 "The calling process\n"
1106 "must be a member of the same session as @var{pgid} and must have the same\n"
1107 "controlling terminal. The return value is unspecified.")
1bbd0b84 1108#define FUNC_NAME s_scm_tcsetpgrp
0f2d19dd
JB
1109{
1110 int fd;
78446828
MV
1111
1112 port = SCM_COERCE_OUTPORT (port);
1113
34d19ef6 1114 SCM_VALIDATE_OPFPORT (1, port);
ee149d03 1115 fd = SCM_FPORT_FDES (port);
a55c2b68 1116 if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
1bbd0b84 1117 SCM_SYSERROR;
02b754d3 1118 return SCM_UNSPECIFIED;
1bbd0b84
GB
1119}
1120#undef FUNC_NAME
f25f761d 1121#endif /* HAVE_TCSETPGRP */
0f2d19dd 1122
a1ec6916 1123SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
1bbd0b84 1124 (SCM filename, SCM args),
b380b885
MD
1125 "Executes the file named by @var{path} as a new process image.\n"
1126 "The remaining arguments are supplied to the process; from a C program\n"
bb2c02f2 1127 "they are accessible as the @code{argv} argument to @code{main}.\n"
b380b885 1128 "Conventionally the first @var{arg} is the same as @var{path}.\n"
8f85c0c6 1129 "All arguments must be strings.\n\n"
b380b885
MD
1130 "If @var{arg} is missing, @var{path} is executed with a null\n"
1131 "argument list, which may have system-dependent side-effects.\n\n"
1132 "This procedure is currently implemented using the @code{execv} system\n"
1133 "call, but we call it @code{execl} because of its Scheme calling interface.")
1bbd0b84 1134#define FUNC_NAME s_scm_execl
0f2d19dd 1135{
7f03f970
MV
1136 char *exec_file;
1137 char **exec_argv;
1138
661ae7ab 1139 scm_dynwind_begin (0);
7f03f970
MV
1140
1141 exec_file = scm_to_locale_string (filename);
661ae7ab 1142 scm_dynwind_free (exec_file);
7f03f970
MV
1143
1144 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1145
8ab3d8a0
KR
1146 execv (exec_file,
1147#ifdef __MINGW32__
1148 /* extra "const" in mingw formals, provokes warning from gcc */
1149 (const char * const *)
1150#endif
1151 exec_argv);
1bbd0b84 1152 SCM_SYSERROR;
7f03f970 1153
02b754d3 1154 /* not reached. */
661ae7ab 1155 scm_dynwind_end ();
02b754d3 1156 return SCM_BOOL_F;
0f2d19dd 1157}
1bbd0b84 1158#undef FUNC_NAME
0f2d19dd 1159
a1ec6916 1160SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
1bbd0b84 1161 (SCM filename, SCM args),
b380b885
MD
1162 "Similar to @code{execl}, however if\n"
1163 "@var{filename} does not contain a slash\n"
1164 "then the file to execute will be located by searching the\n"
1165 "directories listed in the @code{PATH} environment variable.\n\n"
6bcd01fc 1166 "This procedure is currently implemented using the @code{execvp} system\n"
b380b885 1167 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1bbd0b84 1168#define FUNC_NAME s_scm_execlp
0f2d19dd 1169{
7f03f970
MV
1170 char *exec_file;
1171 char **exec_argv;
1172
661ae7ab 1173 scm_dynwind_begin (0);
7f03f970
MV
1174
1175 exec_file = scm_to_locale_string (filename);
661ae7ab 1176 scm_dynwind_free (exec_file);
7f03f970
MV
1177
1178 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1179
8ab3d8a0
KR
1180 execvp (exec_file,
1181#ifdef __MINGW32__
1182 /* extra "const" in mingw formals, provokes warning from gcc */
1183 (const char * const *)
1184#endif
1185 exec_argv);
1bbd0b84 1186 SCM_SYSERROR;
7f03f970 1187
02b754d3 1188 /* not reached. */
661ae7ab 1189 scm_dynwind_end ();
02b754d3 1190 return SCM_BOOL_F;
0f2d19dd 1191}
1bbd0b84 1192#undef FUNC_NAME
0f2d19dd 1193
6afcd3b2 1194
807f353f
KR
1195/* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1196 list strings the way environ_list_to_c gives. */
1197
a1ec6916 1198SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
1bbd0b84 1199 (SCM filename, SCM env, SCM args),
b380b885
MD
1200 "Similar to @code{execl}, but the environment of the new process is\n"
1201 "specified by @var{env}, which must be a list of strings as returned by the\n"
1202 "@code{environ} procedure.\n\n"
1203 "This procedure is currently implemented using the @code{execve} system\n"
1204 "call, but we call it @code{execle} because of its Scheme calling interface.")
1bbd0b84 1205#define FUNC_NAME s_scm_execle
6afcd3b2 1206{
7f03f970 1207 char **exec_argv;
6afcd3b2 1208 char **exec_env;
7f03f970 1209 char *exec_file;
6afcd3b2 1210
661ae7ab 1211 scm_dynwind_begin (0);
7f03f970
MV
1212
1213 exec_file = scm_to_locale_string (filename);
661ae7ab 1214 scm_dynwind_free (exec_file);
7f03f970
MV
1215
1216 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1217 exec_env = scm_i_allocate_string_pointers (env);
7f03f970 1218
8ab3d8a0
KR
1219 execve (exec_file,
1220#ifdef __MINGW32__
1221 /* extra "const" in mingw formals, provokes warning from gcc */
1222 (const char * const *)
1223#endif
1224 exec_argv,
1225#ifdef __MINGW32__
1226 /* extra "const" in mingw formals, provokes warning from gcc */
1227 (const char * const *)
1228#endif
1229 exec_env);
1bbd0b84 1230 SCM_SYSERROR;
7f03f970 1231
6afcd3b2 1232 /* not reached. */
661ae7ab 1233 scm_dynwind_end ();
6afcd3b2
GH
1234 return SCM_BOOL_F;
1235}
1bbd0b84 1236#undef FUNC_NAME
6afcd3b2 1237
82893676 1238#ifdef HAVE_FORK
a1ec6916 1239SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
1bbd0b84 1240 (),
b380b885
MD
1241 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1242 "In the child the return value is 0. In the parent the return value is\n"
1243 "the integer process ID of the child.\n\n"
1244 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1245 "with the scsh fork.")
1bbd0b84 1246#define FUNC_NAME s_scm_fork
0f2d19dd 1247{
bab0f4e5 1248 int pid;
0f2d19dd
JB
1249 pid = fork ();
1250 if (pid == -1)
1bbd0b84 1251 SCM_SYSERROR;
e11e83f3 1252 return scm_from_int (pid);
0f2d19dd 1253}
1bbd0b84 1254#undef FUNC_NAME
82893676 1255#endif /* HAVE_FORK */
0f2d19dd 1256
4f68365d
MV
1257#ifdef __MINGW32__
1258# include "win32-uname.h"
1259#endif
1260
1261#if defined (HAVE_UNAME) || defined (__MINGW32__)
a1ec6916 1262SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
1bbd0b84 1263 (),
1e6808ea
MG
1264 "Return an object with some information about the computer\n"
1265 "system the program is running on.")
1bbd0b84 1266#define FUNC_NAME s_scm_uname
0f2d19dd 1267{
0f2d19dd 1268 struct utsname buf;
1d1559ce 1269 SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
e1a191a8 1270 if (uname (&buf) < 0)
1bbd0b84 1271 SCM_SYSERROR;
4057a3e0
MV
1272 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname));
1273 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename));
1274 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release));
1275 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version));
1276 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine));
0f2d19dd 1277/*
02b754d3 1278 a linux special?
4057a3e0 1279 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
0f2d19dd 1280*/
1d1559ce 1281 return result;
0f2d19dd 1282}
1bbd0b84 1283#undef FUNC_NAME
f25f761d 1284#endif /* HAVE_UNAME */
0f2d19dd 1285
a1ec6916 1286SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1bbd0b84 1287 (SCM env),
1e6808ea
MG
1288 "If @var{env} is omitted, return the current environment (in the\n"
1289 "Unix sense) as a list of strings. Otherwise set the current\n"
1290 "environment, which is also the default environment for child\n"
1291 "processes, to the supplied list of strings. Each member of\n"
1292 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1293 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1294 "then the return value is unspecified.")
1bbd0b84 1295#define FUNC_NAME s_scm_environ
0f2d19dd
JB
1296{
1297 if (SCM_UNBNDP (env))
1298 return scm_makfromstrs (-1, environ);
1299 else
1300 {
2a776823 1301 environ = scm_i_allocate_string_pointers (env);
0f2d19dd
JB
1302 return SCM_UNSPECIFIED;
1303 }
1304}
1bbd0b84 1305#undef FUNC_NAME
0f2d19dd 1306
9ee5fce4
MD
1307#ifdef L_tmpnam
1308
a1ec6916 1309SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1bbd0b84 1310 (),
1e6808ea
MG
1311 "Return a name in the file system that does not match any\n"
1312 "existing file. However there is no guarantee that another\n"
1313 "process will not create the file after @code{tmpnam} is called.\n"
1314 "Care should be taken if opening the file, e.g., use the\n"
1315 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1bbd0b84 1316#define FUNC_NAME s_scm_tmpnam
9ee5fce4
MD
1317{
1318 char name[L_tmpnam];
6d163216
GH
1319 char *rv;
1320
1321 SCM_SYSCALL (rv = tmpnam (name));
1322 if (rv == NULL)
1323 /* not SCM_SYSERROR since errno probably not set. */
1324 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
cc95e00a 1325 return scm_from_locale_string (name);
9ee5fce4 1326}
0f981281 1327#undef FUNC_NAME
0f2d19dd 1328
1bbd0b84 1329#endif
1cc91f1b 1330
cd53bd36
TTN
1331SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0,
1332 (void),
1333 "Return an input/output port to a unique temporary file\n"
1334 "named using the path prefix @code{P_tmpdir} defined in\n"
1335 "@file{stdio.h}.\n"
1336 "The file is automatically deleted when the port is closed\n"
1337 "or the program terminates.")
1338#define FUNC_NAME s_scm_tmpfile
1339{
1340 FILE *rv;
1341
1342 if (! (rv = tmpfile ()))
1343 SCM_SYSERROR;
1344 return scm_fdes_to_port (fileno (rv), "w+", SCM_BOOL_F);
1345}
1346#undef FUNC_NAME
1347
06bfe276
AW
1348SCM_DEFINE (scm_utime, "utime", 1, 5, 0,
1349 (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens,
1350 SCM flags),
1e6808ea
MG
1351 "@code{utime} sets the access and modification times for the\n"
1352 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1353 "not supplied, then the current time is used. @var{actime} and\n"
1354 "@var{modtime} must be integer time values as returned by the\n"
06bfe276
AW
1355 "@code{current-time} procedure.\n\n"
1356 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1357 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
477e4219 1358 "only supported on some combinations of file systems and operating\n"
06bfe276 1359 "systems.\n"
1e6808ea 1360 "@lisp\n"
b380b885 1361 "(utime \"foo\" (- (current-time) 3600))\n"
1e6808ea
MG
1362 "@end lisp\n"
1363 "will set the access time to one hour in the past and the\n"
1364 "modification time to the current time.")
1bbd0b84 1365#define FUNC_NAME s_scm_utime
0f2d19dd
JB
1366{
1367 int rv;
06bfe276
AW
1368 time_t atim_sec, mtim_sec;
1369 long atim_nsec, mtim_nsec;
1370 int f;
1371
0f2d19dd 1372 if (SCM_UNBNDP (actime))
06bfe276 1373 {
adfb4284 1374#ifdef HAVE_UTIMENSAT
06bfe276
AW
1375 atim_sec = 0;
1376 atim_nsec = UTIME_NOW;
1377#else
1378 SCM_SYSCALL (time (&atim_sec));
1379 atim_nsec = 0;
1380#endif
1381 }
0f2d19dd 1382 else
06bfe276
AW
1383 {
1384 atim_sec = SCM_NUM2ULONG (2, actime);
1385 if (SCM_UNBNDP (actimens))
1386 atim_nsec = 0;
1387 else
1388 atim_nsec = SCM_NUM2LONG (4, actimens);
1389 }
1390
0f2d19dd 1391 if (SCM_UNBNDP (modtime))
06bfe276 1392 {
adfb4284 1393#ifdef HAVE_UTIMENSAT
06bfe276
AW
1394 mtim_sec = 0;
1395 mtim_nsec = UTIME_NOW;
1396#else
1397 SCM_SYSCALL (time (&mtim_sec));
1398 mtim_nsec = 0;
1399#endif
1400 }
0f2d19dd 1401 else
06bfe276
AW
1402 {
1403 mtim_sec = SCM_NUM2ULONG (3, modtime);
1404 if (SCM_UNBNDP (modtimens))
1405 mtim_nsec = 0;
1406 else
1407 mtim_nsec = SCM_NUM2LONG (5, modtimens);
1408 }
1409
1410 if (SCM_UNBNDP (flags))
1411 f = 0;
1412 else
1413 f = SCM_NUM2INT (6, flags);
1414
adfb4284 1415#ifdef HAVE_UTIMENSAT
06bfe276
AW
1416 {
1417 struct timespec times[2];
1418 times[0].tv_sec = atim_sec;
1419 times[0].tv_nsec = atim_nsec;
1420 times[1].tv_sec = mtim_sec;
1421 times[1].tv_nsec = mtim_nsec;
1422
1423 STRING_SYSCALL (pathname, c_pathname,
adfb4284 1424 rv = utimensat (AT_FDCWD, c_pathname, times, f));
06bfe276
AW
1425 }
1426#else
1427 {
1428 struct utimbuf utm;
1429 utm.actime = atim_sec;
1430 utm.modtime = mtim_sec;
1431
1432 STRING_SYSCALL (pathname, c_pathname,
1433 rv = utime (c_pathname, &utm));
1434 }
1435#endif
0f2d19dd 1436
02b754d3 1437 if (rv != 0)
1bbd0b84 1438 SCM_SYSERROR;
02b754d3 1439 return SCM_UNSPECIFIED;
0f2d19dd 1440}
1bbd0b84 1441#undef FUNC_NAME
0f2d19dd 1442
a1ec6916 1443SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1bbd0b84 1444 (),
1e6808ea 1445 "Return an integer representing the current process ID.")
1bbd0b84 1446#define FUNC_NAME s_scm_getpid
0f2d19dd 1447{
e11e83f3 1448 return scm_from_ulong (getpid ());
0f2d19dd 1449}
1bbd0b84 1450#undef FUNC_NAME
0f2d19dd 1451
a1ec6916 1452SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1bbd0b84 1453 (SCM str),
b380b885
MD
1454 "Modifies the environment of the current process, which is\n"
1455 "also the default environment inherited by child processes.\n\n"
1456 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1457 "directly into the environment, replacing any existing environment string\n"
1458 "with\n"
1459 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1460 "sign, then any existing string with name matching @var{string} will\n"
1461 "be removed.\n\n"
1462 "The return value is unspecified.")
1bbd0b84 1463#define FUNC_NAME s_scm_putenv
0f2d19dd 1464{
f158788f 1465 int rv;
f015614a 1466 char *c_str = scm_to_locale_string (str);
002409fe 1467
3731192f 1468 /* Leave C_STR in the environment. */
fcc5d734 1469
3731192f
LC
1470 /* Gnulib's `putenv' module honors the semantics described above. */
1471 rv = putenv (c_str);
1472 if (rv < 0)
1473 SCM_SYSERROR;
f015614a 1474
f93ddd39 1475 return SCM_UNSPECIFIED;
0f2d19dd 1476}
1bbd0b84 1477#undef FUNC_NAME
0f2d19dd 1478
b89c4943 1479/* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
a2f00b9b
LC
1480 systems (i.e., systems where a reentrant locale API is not available). It
1481 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1482 details. */
1483scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
b89c4943 1484
f25f761d 1485#ifdef HAVE_SETLOCALE
b89c4943 1486
a1ec6916 1487SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1bbd0b84 1488 (SCM category, SCM locale),
1e6808ea
MG
1489 "If @var{locale} is omitted, return the current value of the\n"
1490 "specified locale category as a system-dependent string.\n"
1491 "@var{category} should be specified using the values\n"
1492 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1493 "\n"
1494 "Otherwise the specified locale category is set to the string\n"
1495 "@var{locale} and the new value is returned as a\n"
1496 "system-dependent string. If @var{locale} is an empty string,\n"
889975e5
MG
1497 "the locale will be set using environment variables.\n"
1498 "\n"
1499 "When the locale is changed, the character encoding of the new\n"
1500 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1501 "input, output, and error ports\n")
1bbd0b84 1502#define FUNC_NAME s_scm_setlocale
0f2d19dd 1503{
a2f00b9b 1504 int c_category;
0f2d19dd
JB
1505 char *clocale;
1506 char *rv;
889975e5 1507 const char *enc;
0f2d19dd 1508
661ae7ab 1509 scm_dynwind_begin (0);
f015614a 1510
0f2d19dd
JB
1511 if (SCM_UNBNDP (locale))
1512 {
1513 clocale = NULL;
1514 }
1515 else
1516 {
f015614a 1517 clocale = scm_to_locale_string (locale);
661ae7ab 1518 scm_dynwind_free (clocale);
0f2d19dd
JB
1519 }
1520
a2f00b9b
LC
1521 c_category = scm_i_to_lc_category (category, 1);
1522
b89c4943 1523 scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
a2f00b9b 1524 rv = setlocale (c_category, clocale);
b89c4943 1525 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
b89c4943 1526
02b754d3 1527 if (rv == NULL)
41b74b7d
KR
1528 {
1529 /* POSIX and C99 don't say anything about setlocale setting errno, so
1530 force a sensible value here. glibc leaves ENOENT, which would be
1531 fine, but it's not a documented feature. */
1532 errno = EINVAL;
1533 SCM_SYSERROR;
1534 }
f015614a 1535
889975e5 1536 enc = locale_charset ();
9d9c66ba 1537
889975e5 1538 /* Set the default encoding for new ports. */
9d9c66ba
LC
1539 scm_i_set_default_port_encoding (enc);
1540
889975e5
MG
1541 /* Set the encoding for the stdio ports. */
1542 scm_i_set_port_encoding_x (scm_current_input_port (), enc);
1543 scm_i_set_port_encoding_x (scm_current_output_port (), enc);
1544 scm_i_set_port_encoding_x (scm_current_error_port (), enc);
a17d2654 1545
661ae7ab 1546 scm_dynwind_end ();
f015614a 1547 return scm_from_locale_string (rv);
0f2d19dd 1548}
1bbd0b84 1549#undef FUNC_NAME
f25f761d 1550#endif /* HAVE_SETLOCALE */
0f2d19dd 1551
f25f761d 1552#ifdef HAVE_MKNOD
a1ec6916 1553SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1bbd0b84 1554 (SCM path, SCM type, SCM perms, SCM dev),
b380b885
MD
1555 "Creates a new special file, such as a file corresponding to a device.\n"
1556 "@var{path} specifies the name of the file. @var{type} should\n"
1557 "be one of the following symbols:\n"
1558 "regular, directory, symlink, block-special, char-special,\n"
1559 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1560 "@var{dev} (an integer) specifies which device the special file refers\n"
1561 "to. Its exact interpretation depends on the kind of special file\n"
1562 "being created.\n\n"
1563 "E.g.,\n"
1e6808ea 1564 "@lisp\n"
09831f94 1565 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1e6808ea 1566 "@end lisp\n\n"
a3c8b9fc 1567 "The return value is unspecified.")
1bbd0b84 1568#define FUNC_NAME s_scm_mknod
0f2d19dd 1569{
0f2d19dd 1570 int val;
cc95e00a 1571 const char *p;
82a76bdf 1572 int ctype = 0;
19468eff 1573
a6d9e5ab 1574 SCM_VALIDATE_STRING (1, path);
34d19ef6 1575 SCM_VALIDATE_SYMBOL (2, type);
19468eff 1576
cc95e00a 1577 p = scm_i_symbol_chars (type);
19468eff
GH
1578 if (strcmp (p, "regular") == 0)
1579 ctype = S_IFREG;
1580 else if (strcmp (p, "directory") == 0)
1581 ctype = S_IFDIR;
23f2b9a3
KR
1582#ifdef S_IFLNK
1583 /* systems without symlinks probably don't have S_IFLNK defined */
19468eff
GH
1584 else if (strcmp (p, "symlink") == 0)
1585 ctype = S_IFLNK;
23f2b9a3 1586#endif
19468eff
GH
1587 else if (strcmp (p, "block-special") == 0)
1588 ctype = S_IFBLK;
1589 else if (strcmp (p, "char-special") == 0)
1590 ctype = S_IFCHR;
1591 else if (strcmp (p, "fifo") == 0)
1592 ctype = S_IFIFO;
e655d034 1593#ifdef S_IFSOCK
19468eff
GH
1594 else if (strcmp (p, "socket") == 0)
1595 ctype = S_IFSOCK;
e655d034 1596#endif
19468eff 1597 else
34d19ef6 1598 SCM_OUT_OF_RANGE (2, type);
19468eff 1599
f015614a
MV
1600 STRING_SYSCALL (path, c_path,
1601 val = mknod (c_path,
1602 ctype | scm_to_int (perms),
1603 scm_to_int (dev)));
02b754d3 1604 if (val != 0)
1bbd0b84 1605 SCM_SYSERROR;
02b754d3 1606 return SCM_UNSPECIFIED;
0f2d19dd 1607}
1bbd0b84 1608#undef FUNC_NAME
f25f761d 1609#endif /* HAVE_MKNOD */
0f2d19dd 1610
f25f761d 1611#ifdef HAVE_NICE
a1ec6916 1612SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1bbd0b84 1613 (SCM incr),
b380b885
MD
1614 "Increment the priority of the current process by @var{incr}. A higher\n"
1615 "priority value means that the process runs less often.\n"
1616 "The return value is unspecified.")
1bbd0b84 1617#define FUNC_NAME s_scm_nice
0f2d19dd 1618{
f1c82f55
KR
1619 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1620 from "prio-NZERO", so an error must be detected from errno changed */
1621 errno = 0;
03976fee 1622 nice (scm_to_int (incr));
f1c82f55 1623 if (errno != 0)
1bbd0b84 1624 SCM_SYSERROR;
634aa8de 1625
02b754d3 1626 return SCM_UNSPECIFIED;
0f2d19dd 1627}
1bbd0b84 1628#undef FUNC_NAME
f25f761d 1629#endif /* HAVE_NICE */
0f2d19dd 1630
f25f761d 1631#ifdef HAVE_SYNC
a1ec6916 1632SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1bbd0b84 1633 (),
b380b885
MD
1634 "Flush the operating system disk buffers.\n"
1635 "The return value is unspecified.")
1bbd0b84 1636#define FUNC_NAME s_scm_sync
0f2d19dd 1637{
0f2d19dd 1638 sync();
127ec750 1639 return SCM_UNSPECIFIED;
0f2d19dd 1640}
1bbd0b84 1641#undef FUNC_NAME
f25f761d 1642#endif /* HAVE_SYNC */
0f2d19dd 1643
33bea692
KR
1644
1645/* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1646 to avoid another thread overwriting it. A test program running crypt
1647 continuously in two threads can be quickly seen tripping this problem.
1648 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1649
1650 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1651 slower (about 5x) than plain crypt if you pass an uninitialized data
1652 block each time. Presumably there's some one-time setups. The best way
1653 to use crypt_r for parallel execution in multiple threads would probably
1654 be to maintain a little pool of initialized crypt_data structures, take
1655 one and use it, then return it to the pool. That pool could be garbage
1656 collected so it didn't add permanently to memory use if only a few crypt
1657 calls are made. But we expect crypt will be used rarely, and even more
1658 rarely will there be any desire for lots of parallel execution on
1659 multiple cpus. So for now we don't bother with anything fancy, just
1660 ensure it works. */
1661
534bbcc1 1662#ifdef HAVE_CRYPT
33bea692 1663SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
94e6d793
MG
1664 (SCM key, SCM salt),
1665 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
9401323e 1666 "crypt(3) library call.")
94e6d793
MG
1667#define FUNC_NAME s_scm_crypt
1668{
33bea692 1669 SCM ret;
23d72566 1670 char *c_key, *c_salt, *c_ret;
94e6d793 1671
661ae7ab
MV
1672 scm_dynwind_begin (0);
1673 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
33bea692 1674
f015614a 1675 c_key = scm_to_locale_string (key);
661ae7ab 1676 scm_dynwind_free (c_key);
f015614a 1677 c_salt = scm_to_locale_string (salt);
661ae7ab 1678 scm_dynwind_free (c_salt);
f015614a 1679
23d72566
KR
1680 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1681 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1682 implemented). */
1683 c_ret = crypt (c_key, c_salt);
1684 if (c_ret == NULL)
1685 SCM_SYSERROR;
33bea692 1686
23d72566 1687 ret = scm_from_locale_string (c_ret);
661ae7ab 1688 scm_dynwind_end ();
33bea692 1689 return ret;
94e6d793
MG
1690}
1691#undef FUNC_NAME
b9a7b725 1692#endif /* HAVE_CRYPT */
94e6d793
MG
1693
1694#if HAVE_CHROOT
1695SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1696 (SCM path),
1697 "Change the root directory to that specified in @var{path}.\n"
1698 "This directory will be used for path names beginning with\n"
1699 "@file{/}. The root directory is inherited by all children\n"
1700 "of the current process. Only the superuser may change the\n"
1701 "root directory.")
1702#define FUNC_NAME s_scm_chroot
1703{
f015614a 1704 int rv;
94e6d793 1705
f015614a
MV
1706 WITH_STRING (path, c_path,
1707 rv = chroot (c_path));
1708 if (rv == -1)
94e6d793
MG
1709 SCM_SYSERROR;
1710 return SCM_UNSPECIFIED;
1711}
1712#undef FUNC_NAME
1713#endif /* HAVE_CHROOT */
1714
7beabedb
MG
1715
1716#ifdef __MINGW32__
1717/* Wrapper function to supplying `getlogin()' under Windows. */
1718static char * getlogin (void)
1719{
1720 static char user[256];
1721 static unsigned long len = 256;
1722
1723 if (!GetUserName (user, &len))
1724 return NULL;
1725 return user;
1726}
1727#endif /* __MINGW32__ */
1728
1729
4f68365d 1730#if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
94e6d793
MG
1731SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1732 (void),
1733 "Return a string containing the name of the user logged in on\n"
1734 "the controlling terminal of the process, or @code{#f} if this\n"
1735 "information cannot be obtained.")
1736#define FUNC_NAME s_scm_getlogin
1737{
1738 char * p;
1739
1740 p = getlogin ();
1741 if (!p || !*p)
1742 return SCM_BOOL_F;
cc95e00a 1743 return scm_from_locale_string (p);
94e6d793
MG
1744}
1745#undef FUNC_NAME
1746#endif /* HAVE_GETLOGIN */
1747
94e6d793
MG
1748#if HAVE_GETPRIORITY
1749SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1750 (SCM which, SCM who),
1751 "Return the scheduling priority of the process, process group\n"
1752 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1753 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1754 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1755 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1756 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1757 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1758 "denotes the current process, process group, or user. Return\n"
1759 "the highest priority (lowest numerical value) of any of the\n"
1760 "specified processes.")
1761#define FUNC_NAME s_scm_getpriority
1762{
1763 int cwhich, cwho, ret;
1764
a55c2b68
MV
1765 cwhich = scm_to_int (which);
1766 cwho = scm_to_int (who);
94e6d793
MG
1767
1768 /* We have to clear errno and examine it later, because -1 is a
1769 legal return value for getpriority(). */
1770 errno = 0;
1771 ret = getpriority (cwhich, cwho);
1772 if (errno != 0)
1773 SCM_SYSERROR;
a55c2b68 1774 return scm_from_int (ret);
94e6d793
MG
1775}
1776#undef FUNC_NAME
1777#endif /* HAVE_GETPRIORITY */
1778
1779#if HAVE_SETPRIORITY
1780SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1781 (SCM which, SCM who, SCM prio),
1782 "Set the scheduling priority of the process, process group\n"
1783 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1784 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1785 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1786 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1787 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1788 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1789 "denotes the current process, process group, or user.\n"
1790 "@var{prio} is a value in the range -20 and 20, the default\n"
1791 "priority is 0; lower priorities cause more favorable\n"
1792 "scheduling. Sets the priority of all of the specified\n"
1793 "processes. Only the super-user may lower priorities.\n"
1794 "The return value is not specified.")
1795#define FUNC_NAME s_scm_setpriority
1796{
1797 int cwhich, cwho, cprio;
1798
a55c2b68
MV
1799 cwhich = scm_to_int (which);
1800 cwho = scm_to_int (who);
1801 cprio = scm_to_int (prio);
94e6d793
MG
1802
1803 if (setpriority (cwhich, cwho, cprio) == -1)
1804 SCM_SYSERROR;
1805 return SCM_UNSPECIFIED;
1806}
1807#undef FUNC_NAME
1808#endif /* HAVE_SETPRIORITY */
1809
fe613fe2
LC
1810#ifdef HAVE_SCHED_GETAFFINITY
1811
1812static SCM
1813cpu_set_to_bitvector (const cpu_set_t *cs)
1814{
1815 SCM bv;
1816 size_t cpu;
1817
1818 bv = scm_c_make_bitvector (sizeof (*cs), SCM_BOOL_F);
1819
1820 for (cpu = 0; cpu < sizeof (*cs); cpu++)
1821 {
1822 if (CPU_ISSET (cpu, cs))
1823 /* XXX: This is inefficient but avoids code duplication. */
1824 scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T);
1825 }
1826
1827 return bv;
1828}
1829
1830SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0,
1831 (SCM pid),
1832 "Return a bitvector representing the CPU affinity mask for\n"
1833 "process @var{pid}. Each CPU the process has affinity with\n"
1834 "has its corresponding bit set in the returned bitvector.\n"
1835 "The number of bits set is a good estimate of how many CPUs\n"
1836 "Guile can use without stepping on other processes' toes.\n\n"
3ae78cac
LC
1837 "Currently this procedure is only defined on GNU variants\n"
1838 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
1839 "GNU C Library Reference Manual}).\n")
fe613fe2
LC
1840#define FUNC_NAME s_scm_getaffinity
1841{
1842 int err;
1843 cpu_set_t cs;
1844
1845 CPU_ZERO (&cs);
1846 err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs);
1847 if (err)
1848 SCM_SYSERROR;
1849
1850 return cpu_set_to_bitvector (&cs);
1851}
1852#undef FUNC_NAME
1853
1854#endif /* HAVE_SCHED_GETAFFINITY */
1855
1856#ifdef HAVE_SCHED_SETAFFINITY
1857
1858SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0,
1859 (SCM pid, SCM mask),
1860 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
1861 "the process or thread with ID @var{pid}. The return value\n"
1862 "is unspecified.\n\n"
3ae78cac
LC
1863 "Currently this procedure is only defined on GNU variants\n"
1864 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
1865 "GNU C Library Reference Manual}).\n")
fe613fe2
LC
1866#define FUNC_NAME s_scm_setaffinity
1867{
1868 cpu_set_t cs;
1869 scm_t_array_handle handle;
1870 const scm_t_uint32 *c_mask;
1871 size_t len, off, cpu;
1872 ssize_t inc;
1873 int err;
1874
1875 c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc);
1876
1877 CPU_ZERO (&cs);
1878 for (cpu = 0; cpu < len; cpu++)
1879 {
1880 size_t idx;
1881
1882 idx = cpu * inc + off;
1883 if (c_mask[idx / 32] & (1UL << (idx % 32)))
1884 CPU_SET (cpu, &cs);
1885 }
1886
1887 err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs);
1888 if (err)
1889 SCM_SYSERROR;
1890
1891 return SCM_UNSPECIFIED;
1892}
1893#undef FUNC_NAME
1894
1895#endif /* HAVE_SCHED_SETAFFINITY */
1896
f0c0141f 1897\f
94e6d793
MG
1898#if HAVE_GETPASS
1899SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
1900 (SCM prompt),
1901 "Display @var{prompt} to the standard error output and read\n"
1902 "a password from @file{/dev/tty}. If this file is not\n"
1903 "accessible, it reads from standard input. The password may be\n"
1904 "up to 127 characters in length. Additional characters and the\n"
1905 "terminating newline character are discarded. While reading\n"
1906 "the password, echoing and the generation of signals by special\n"
1907 "characters is disabled.")
1908#define FUNC_NAME s_scm_getpass
1909{
1910 char * p;
1911 SCM passwd;
1912
1913 SCM_VALIDATE_STRING (1, prompt);
94e6d793 1914
f015614a
MV
1915 WITH_STRING (prompt, c_prompt,
1916 p = getpass(c_prompt));
1917 passwd = scm_from_locale_string (p);
94e6d793
MG
1918
1919 /* Clear out the password in the static buffer. */
1920 memset (p, 0, strlen (p));
1921
1922 return passwd;
1923}
1924#undef FUNC_NAME
1925#endif /* HAVE_GETPASS */
1926
94e6d793
MG
1927SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
1928 (SCM file, SCM operation),
1929 "Apply or remove an advisory lock on an open file.\n"
1930 "@var{operation} specifies the action to be done:\n"
0b91f1db
KR
1931 "\n"
1932 "@defvar LOCK_SH\n"
94e6d793
MG
1933 "Shared lock. More than one process may hold a shared lock\n"
1934 "for a given file at a given time.\n"
0b91f1db
KR
1935 "@end defvar\n"
1936 "@defvar LOCK_EX\n"
94e6d793
MG
1937 "Exclusive lock. Only one process may hold an exclusive lock\n"
1938 "for a given file at a given time.\n"
0b91f1db
KR
1939 "@end defvar\n"
1940 "@defvar LOCK_UN\n"
94e6d793 1941 "Unlock the file.\n"
0b91f1db
KR
1942 "@end defvar\n"
1943 "@defvar LOCK_NB\n"
1944 "Don't block when locking. This is combined with one of the\n"
1945 "other operations using @code{logior}. If @code{flock} would\n"
1946 "block an @code{EWOULDBLOCK} error is thrown.\n"
1947 "@end defvar\n"
1948 "\n"
94e6d793 1949 "The return value is not specified. @var{file} may be an open\n"
0b91f1db
KR
1950 "file descriptor or an open file descriptor port.\n"
1951 "\n"
1952 "Note that @code{flock} does not lock files across NFS.")
94e6d793
MG
1953#define FUNC_NAME s_scm_flock
1954{
a55c2b68 1955 int fdes;
94e6d793 1956
a55c2b68
MV
1957 if (scm_is_integer (file))
1958 fdes = scm_to_int (file);
94e6d793
MG
1959 else
1960 {
1961 SCM_VALIDATE_OPFPORT (2, file);
1962
1963 fdes = SCM_FPORT_FDES (file);
1964 }
a55c2b68 1965 if (flock (fdes, scm_to_int (operation)) == -1)
94e6d793
MG
1966 SCM_SYSERROR;
1967 return SCM_UNSPECIFIED;
1968}
1969#undef FUNC_NAME
94e6d793
MG
1970
1971#if HAVE_SETHOSTNAME
1972SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
1973 (SCM name),
1974 "Set the host name of the current processor to @var{name}. May\n"
1975 "only be used by the superuser. The return value is not\n"
1976 "specified.")
1977#define FUNC_NAME s_scm_sethostname
1978{
f015614a 1979 int rv;
94e6d793 1980
f015614a
MV
1981 WITH_STRING (name, c_name,
1982 rv = sethostname (c_name, strlen(c_name)));
1983 if (rv == -1)
94e6d793
MG
1984 SCM_SYSERROR;
1985 return SCM_UNSPECIFIED;
1986}
1987#undef FUNC_NAME
1988#endif /* HAVE_SETHOSTNAME */
1989
b28f5b3c 1990
94e6d793
MG
1991#if HAVE_GETHOSTNAME
1992SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
1993 (void),
1994 "Return the host name of the current processor.")
1995#define FUNC_NAME s_scm_gethostname
1996{
b28f5b3c
DH
1997#ifdef MAXHOSTNAMELEN
1998
1999 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2000 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2001 const int len = MAXHOSTNAMELEN + 1;
2002 char *const p = scm_malloc (len);
2003 const int res = gethostname (p, len);
2004
661ae7ab
MV
2005 scm_dynwind_begin (0);
2006 scm_dynwind_unwind_handler (free, p, 0);
b28f5b3c
DH
2007
2008#else
94e6d793 2009
0eaf1055 2010 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
b28f5b3c
DH
2011 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2012 int len = 256;
2013 int res;
2014 char *p;
0eaf1055 2015
b28f5b3c 2016# if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
0eaf1055
KR
2017
2018 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
b28f5b3c
DH
2019 * which may reflect a particular kernel configuration.
2020 * Must watch out for this existing but giving -1, as happens for instance
2021 * in gnu/linux glibc 2.3.2. */
0eaf1055 2022 {
b28f5b3c 2023 const long int n = sysconf (_SC_HOST_NAME_MAX);
0eaf1055
KR
2024 if (n != -1L)
2025 len = n;
2026 }
b28f5b3c
DH
2027
2028# endif
2029
2030 p = scm_malloc (len);
2031
661ae7ab
MV
2032 scm_dynwind_begin (0);
2033 scm_dynwind_unwind_handler (free, p, 0);
0eaf1055 2034
94e6d793
MG
2035 res = gethostname (p, len);
2036 while (res == -1 && errno == ENAMETOOLONG)
2037 {
94e6d793 2038 len *= 2;
b28f5b3c
DH
2039
2040 /* scm_realloc may throw an exception. */
2041 p = scm_realloc (p, len);
94e6d793
MG
2042 res = gethostname (p, len);
2043 }
b28f5b3c
DH
2044
2045#endif
2046
94e6d793
MG
2047 if (res == -1)
2048 {
b28f5b3c
DH
2049 const int save_errno = errno;
2050
2b829bbb 2051 /* No guile exceptions can occur before we have freed p's memory. */
661ae7ab 2052 scm_dynwind_end ();
4c9419ac 2053 free (p);
b28f5b3c 2054
22865124 2055 errno = save_errno;
94e6d793
MG
2056 SCM_SYSERROR;
2057 }
b28f5b3c
DH
2058 else
2059 {
cc95e00a
MV
2060 /* scm_from_locale_string may throw an exception. */
2061 const SCM name = scm_from_locale_string (p);
b28f5b3c 2062
2b829bbb 2063 /* No guile exceptions can occur before we have freed p's memory. */
661ae7ab 2064 scm_dynwind_end ();
b28f5b3c
DH
2065 free (p);
2066
2067 return name;
2068 }
94e6d793
MG
2069}
2070#undef FUNC_NAME
2071#endif /* HAVE_GETHOSTNAME */
2072
fe613fe2 2073\f
b89c4943 2074void
0f2d19dd 2075scm_init_posix ()
0f2d19dd
JB
2076{
2077 scm_add_feature ("posix");
2078#ifdef HAVE_GETEUID
2079 scm_add_feature ("EIDs");
2080#endif
2081#ifdef WAIT_ANY
e11e83f3 2082 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
0f2d19dd
JB
2083#endif
2084#ifdef WAIT_MYPGRP
e11e83f3 2085 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
0f2d19dd
JB
2086#endif
2087#ifdef WNOHANG
e11e83f3 2088 scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
0f2d19dd
JB
2089#endif
2090#ifdef WUNTRACED
e11e83f3 2091 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
0f2d19dd
JB
2092#endif
2093
0f2d19dd 2094#ifdef LC_COLLATE
e11e83f3 2095 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
0f2d19dd
JB
2096#endif
2097#ifdef LC_CTYPE
e11e83f3 2098 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
0f2d19dd
JB
2099#endif
2100#ifdef LC_MONETARY
e11e83f3 2101 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
0f2d19dd
JB
2102#endif
2103#ifdef LC_NUMERIC
e11e83f3 2104 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
0f2d19dd
JB
2105#endif
2106#ifdef LC_TIME
e11e83f3 2107 scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
0f2d19dd
JB
2108#endif
2109#ifdef LC_MESSAGES
e11e83f3 2110 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
0f2d19dd
JB
2111#endif
2112#ifdef LC_ALL
e11e83f3 2113 scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
0f2d19dd 2114#endif
9361f762
MV
2115#ifdef LC_PAPER
2116 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
2117#endif
2118#ifdef LC_NAME
2119 scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
2120#endif
2121#ifdef LC_ADDRESS
2122 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
2123#endif
2124#ifdef LC_TELEPHONE
2125 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
2126#endif
2127#ifdef LC_MEASUREMENT
2128 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
2129#endif
2130#ifdef LC_IDENTIFICATION
2131 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
2132#endif
bd9e24b3 2133#ifdef PIPE_BUF
b9bd8526 2134 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
bd9e24b3
GH
2135#endif
2136
94e6d793 2137#ifdef PRIO_PROCESS
e11e83f3 2138 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
94e6d793
MG
2139#endif
2140#ifdef PRIO_PGRP
e11e83f3 2141 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
94e6d793
MG
2142#endif
2143#ifdef PRIO_USER
e11e83f3 2144 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
94e6d793
MG
2145#endif
2146
2147#ifdef LOCK_SH
e11e83f3 2148 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
94e6d793
MG
2149#endif
2150#ifdef LOCK_EX
e11e83f3 2151 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
94e6d793
MG
2152#endif
2153#ifdef LOCK_UN
e11e83f3 2154 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
94e6d793
MG
2155#endif
2156#ifdef LOCK_NB
e11e83f3 2157 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
94e6d793
MG
2158#endif
2159
648da032 2160#include "libguile/cpp-SIG.c"
a0599745 2161#include "libguile/posix.x"
0f2d19dd 2162}
89e00824
ML
2163
2164/*
2165 Local Variables:
2166 c-file-style: "gnu"
2167 End:
2168*/