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