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