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