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