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