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