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