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