2003-06-14 Stefan Jahn <stefan@lkcc.org>
[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 1177 ptr = scm_malloc (SCM_STRING_LENGTH (str) + 2);
fcc5d734
SJ
1178 if (ptr == NULL)
1179 SCM_MEMORY_ERROR;
1e498fbd
SJ
1180 strncpy (ptr, SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str));
1181 ptr[SCM_STRING_LENGTH (str)] = '=';
1182 ptr[SCM_STRING_LENGTH (str) + 1] = 0;
1183 rv = putenv (ptr);
dd65e90f 1184 e = errno; free (ptr); errno = e;
1e498fbd
SJ
1185 if (rv < 0)
1186 SCM_SYSERROR;
fcc5d734 1187#endif /* !HAVE_UNSETENV */
002409fe
MV
1188 }
1189 else
1190 {
1191 /* must make a new copy to be left in the environment, safe from gc. */
67329a9e 1192 ptr = scm_malloc (SCM_STRING_LENGTH (str) + 1);
002409fe
MV
1193 if (ptr == NULL)
1194 SCM_MEMORY_ERROR;
1195 strncpy (ptr, SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str));
fcc5d734
SJ
1196
1197#ifdef __MINGW32__
1198 /* If str is "FOO=", ie. attempting to set an empty string, then
1199 we need to see if it's been successful. On MINGW, "FOO="
1200 means remove FOO from the environment. As a workaround, we
1201 set "FOO= ", ie. a space, and then modify the string returned
1202 by getenv. It's not enough just to modify the string we set,
1203 because MINGW putenv copies it. */
1204 if (ptr[SCM_STRING_LENGTH (str) - 1] == '=')
1205 {
1206 char *alt;
1207 SCM name = scm_substring (str, SCM_MAKINUM (0),
1208 SCM_MAKINUM (SCM_STRING_LENGTH (str) - 1));
1209 if (getenv (SCM_STRING_CHARS (name)) == NULL)
1210 {
1211 alt = scm_malloc (SCM_STRING_LENGTH (str) + 2);
1212 if (alt == NULL)
1213 {
1214 free (ptr);
1215 SCM_MEMORY_ERROR;
1216 }
1217 memcpy (alt, SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str));
1218 alt[SCM_STRING_LENGTH (str)] = ' ';
1219 alt[SCM_STRING_LENGTH (str) + 1] = '\0';
1220 rv = putenv (alt);
1221 if (rv < 0)
1222 SCM_SYSERROR;
1223 free (ptr); /* don't need the old string we gave to putenv */
1224 }
1225 alt = getenv (SCM_STRING_CHARS (name));
1226 alt[0] = '\0';
1227 return SCM_UNSPECIFIED;
1228 }
1229#endif /* __MINGW32__ */
1230
002409fe
MV
1231 ptr[SCM_STRING_LENGTH (str)] = 0;
1232 rv = putenv (ptr);
1233 if (rv < 0)
1234 SCM_SYSERROR;
1235 }
f93ddd39 1236 return SCM_UNSPECIFIED;
0f2d19dd 1237}
1bbd0b84 1238#undef FUNC_NAME
0f2d19dd 1239
f25f761d 1240#ifdef HAVE_SETLOCALE
a1ec6916 1241SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1bbd0b84 1242 (SCM category, SCM locale),
1e6808ea
MG
1243 "If @var{locale} is omitted, return the current value of the\n"
1244 "specified locale category as a system-dependent string.\n"
1245 "@var{category} should be specified using the values\n"
1246 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1247 "\n"
1248 "Otherwise the specified locale category is set to the string\n"
1249 "@var{locale} and the new value is returned as a\n"
1250 "system-dependent string. If @var{locale} is an empty string,\n"
bb2c02f2 1251 "the locale will be set using environment variables.")
1bbd0b84 1252#define FUNC_NAME s_scm_setlocale
0f2d19dd 1253{
0f2d19dd
JB
1254 char *clocale;
1255 char *rv;
1256
34d19ef6 1257 SCM_VALIDATE_INUM (1, category);
0f2d19dd
JB
1258 if (SCM_UNBNDP (locale))
1259 {
1260 clocale = NULL;
1261 }
1262 else
1263 {
a6d9e5ab 1264 SCM_VALIDATE_STRING (2, locale);
a6d9e5ab 1265 clocale = SCM_STRING_CHARS (locale);
0f2d19dd
JB
1266 }
1267
1268 rv = setlocale (SCM_INUM (category), clocale);
02b754d3 1269 if (rv == NULL)
1bbd0b84 1270 SCM_SYSERROR;
02b754d3 1271 return scm_makfrom0str (rv);
0f2d19dd 1272}
1bbd0b84 1273#undef FUNC_NAME
f25f761d 1274#endif /* HAVE_SETLOCALE */
0f2d19dd 1275
f25f761d 1276#ifdef HAVE_MKNOD
a1ec6916 1277SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1bbd0b84 1278 (SCM path, SCM type, SCM perms, SCM dev),
b380b885
MD
1279 "Creates a new special file, such as a file corresponding to a device.\n"
1280 "@var{path} specifies the name of the file. @var{type} should\n"
1281 "be one of the following symbols:\n"
1282 "regular, directory, symlink, block-special, char-special,\n"
1283 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1284 "@var{dev} (an integer) specifies which device the special file refers\n"
1285 "to. Its exact interpretation depends on the kind of special file\n"
1286 "being created.\n\n"
1287 "E.g.,\n"
1e6808ea 1288 "@lisp\n"
09831f94 1289 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1e6808ea 1290 "@end lisp\n\n"
a3c8b9fc 1291 "The return value is unspecified.")
1bbd0b84 1292#define FUNC_NAME s_scm_mknod
0f2d19dd 1293{
0f2d19dd 1294 int val;
19468eff 1295 char *p;
82a76bdf 1296 int ctype = 0;
19468eff 1297
a6d9e5ab 1298 SCM_VALIDATE_STRING (1, path);
34d19ef6
HWN
1299 SCM_VALIDATE_SYMBOL (2, type);
1300 SCM_VALIDATE_INUM (3, perms);
1301 SCM_VALIDATE_INUM (4, dev);
19468eff 1302
86c991c2 1303 p = SCM_SYMBOL_CHARS (type);
19468eff
GH
1304 if (strcmp (p, "regular") == 0)
1305 ctype = S_IFREG;
1306 else if (strcmp (p, "directory") == 0)
1307 ctype = S_IFDIR;
1308 else if (strcmp (p, "symlink") == 0)
1309 ctype = S_IFLNK;
1310 else if (strcmp (p, "block-special") == 0)
1311 ctype = S_IFBLK;
1312 else if (strcmp (p, "char-special") == 0)
1313 ctype = S_IFCHR;
1314 else if (strcmp (p, "fifo") == 0)
1315 ctype = S_IFIFO;
e655d034 1316#ifdef S_IFSOCK
19468eff
GH
1317 else if (strcmp (p, "socket") == 0)
1318 ctype = S_IFSOCK;
e655d034 1319#endif
19468eff 1320 else
34d19ef6 1321 SCM_OUT_OF_RANGE (2, type);
19468eff 1322
a6d9e5ab
DH
1323 SCM_SYSCALL (val = mknod (SCM_STRING_CHARS (path), ctype | SCM_INUM (perms),
1324 SCM_INUM (dev)));
02b754d3 1325 if (val != 0)
1bbd0b84 1326 SCM_SYSERROR;
02b754d3 1327 return SCM_UNSPECIFIED;
0f2d19dd 1328}
1bbd0b84 1329#undef FUNC_NAME
f25f761d 1330#endif /* HAVE_MKNOD */
0f2d19dd 1331
f25f761d 1332#ifdef HAVE_NICE
a1ec6916 1333SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1bbd0b84 1334 (SCM incr),
b380b885
MD
1335 "Increment the priority of the current process by @var{incr}. A higher\n"
1336 "priority value means that the process runs less often.\n"
1337 "The return value is unspecified.")
1bbd0b84 1338#define FUNC_NAME s_scm_nice
0f2d19dd 1339{
34d19ef6 1340 SCM_VALIDATE_INUM (1, incr);
02b754d3 1341 if (nice(SCM_INUM(incr)) != 0)
1bbd0b84 1342 SCM_SYSERROR;
02b754d3 1343 return SCM_UNSPECIFIED;
0f2d19dd 1344}
1bbd0b84 1345#undef FUNC_NAME
f25f761d 1346#endif /* HAVE_NICE */
0f2d19dd 1347
f25f761d 1348#ifdef HAVE_SYNC
a1ec6916 1349SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1bbd0b84 1350 (),
b380b885
MD
1351 "Flush the operating system disk buffers.\n"
1352 "The return value is unspecified.")
1bbd0b84 1353#define FUNC_NAME s_scm_sync
0f2d19dd 1354{
0f2d19dd 1355 sync();
127ec750 1356 return SCM_UNSPECIFIED;
0f2d19dd 1357}
1bbd0b84 1358#undef FUNC_NAME
f25f761d 1359#endif /* HAVE_SYNC */
0f2d19dd 1360
94e6d793
MG
1361#if HAVE_LIBCRYPT && HAVE_CRYPT_H
1362SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
1363 (SCM key, SCM salt),
1364 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
9401323e 1365 "crypt(3) library call.")
94e6d793
MG
1366#define FUNC_NAME s_scm_crypt
1367{
1368 char * p;
1369
1370 SCM_VALIDATE_STRING (1, key);
1371 SCM_VALIDATE_STRING (2, salt);
94e6d793
MG
1372
1373 p = crypt (SCM_STRING_CHARS (key), SCM_STRING_CHARS (salt));
1374 return scm_makfrom0str (p);
1375}
1376#undef FUNC_NAME
1377#endif /* HAVE_LIBCRYPT && HAVE_CRYPT_H */
1378
1379#if HAVE_CHROOT
1380SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1381 (SCM path),
1382 "Change the root directory to that specified in @var{path}.\n"
1383 "This directory will be used for path names beginning with\n"
1384 "@file{/}. The root directory is inherited by all children\n"
1385 "of the current process. Only the superuser may change the\n"
1386 "root directory.")
1387#define FUNC_NAME s_scm_chroot
1388{
1389 SCM_VALIDATE_STRING (1, path);
94e6d793
MG
1390
1391 if (chroot (SCM_STRING_CHARS (path)) == -1)
1392 SCM_SYSERROR;
1393 return SCM_UNSPECIFIED;
1394}
1395#undef FUNC_NAME
1396#endif /* HAVE_CHROOT */
1397
7beabedb
MG
1398
1399#ifdef __MINGW32__
1400/* Wrapper function to supplying `getlogin()' under Windows. */
1401static char * getlogin (void)
1402{
1403 static char user[256];
1404 static unsigned long len = 256;
1405
1406 if (!GetUserName (user, &len))
1407 return NULL;
1408 return user;
1409}
1410#endif /* __MINGW32__ */
1411
1412
4f68365d 1413#if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
94e6d793
MG
1414SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1415 (void),
1416 "Return a string containing the name of the user logged in on\n"
1417 "the controlling terminal of the process, or @code{#f} if this\n"
1418 "information cannot be obtained.")
1419#define FUNC_NAME s_scm_getlogin
1420{
1421 char * p;
1422
1423 p = getlogin ();
1424 if (!p || !*p)
1425 return SCM_BOOL_F;
1426 return scm_makfrom0str (p);
1427}
1428#undef FUNC_NAME
1429#endif /* HAVE_GETLOGIN */
1430
1431#if HAVE_CUSERID
1432SCM_DEFINE (scm_cuserid, "cuserid", 0, 0, 0,
1433 (void),
1434 "Return a string containing a user name associated with the\n"
1435 "effective user id of the process. Return @code{#f} if this\n"
1436 "information cannot be obtained.")
1437#define FUNC_NAME s_scm_cuserid
1438{
1439 char * p;
1440
1441 p = cuserid (NULL);
1442 if (!p || !*p)
1443 return SCM_BOOL_F;
1444 return scm_makfrom0str (p);
1445}
1446#undef FUNC_NAME
1447#endif /* HAVE_CUSERID */
1448
1449#if HAVE_GETPRIORITY
1450SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1451 (SCM which, SCM who),
1452 "Return the scheduling priority of the process, process group\n"
1453 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1454 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1455 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1456 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1457 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1458 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1459 "denotes the current process, process group, or user. Return\n"
1460 "the highest priority (lowest numerical value) of any of the\n"
1461 "specified processes.")
1462#define FUNC_NAME s_scm_getpriority
1463{
1464 int cwhich, cwho, ret;
1465
1466 SCM_VALIDATE_INUM_COPY (1, which, cwhich);
1467 SCM_VALIDATE_INUM_COPY (2, who, cwho);
1468
1469 /* We have to clear errno and examine it later, because -1 is a
1470 legal return value for getpriority(). */
1471 errno = 0;
1472 ret = getpriority (cwhich, cwho);
1473 if (errno != 0)
1474 SCM_SYSERROR;
1475 return SCM_MAKINUM (ret);
1476}
1477#undef FUNC_NAME
1478#endif /* HAVE_GETPRIORITY */
1479
1480#if HAVE_SETPRIORITY
1481SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1482 (SCM which, SCM who, SCM prio),
1483 "Set the scheduling priority of the process, process group\n"
1484 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1485 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1486 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1487 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1488 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1489 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1490 "denotes the current process, process group, or user.\n"
1491 "@var{prio} is a value in the range -20 and 20, the default\n"
1492 "priority is 0; lower priorities cause more favorable\n"
1493 "scheduling. Sets the priority of all of the specified\n"
1494 "processes. Only the super-user may lower priorities.\n"
1495 "The return value is not specified.")
1496#define FUNC_NAME s_scm_setpriority
1497{
1498 int cwhich, cwho, cprio;
1499
1500 SCM_VALIDATE_INUM_COPY (1, which, cwhich);
1501 SCM_VALIDATE_INUM_COPY (2, who, cwho);
1502 SCM_VALIDATE_INUM_COPY (3, prio, cprio);
1503
1504 if (setpriority (cwhich, cwho, cprio) == -1)
1505 SCM_SYSERROR;
1506 return SCM_UNSPECIFIED;
1507}
1508#undef FUNC_NAME
1509#endif /* HAVE_SETPRIORITY */
1510
1511#if HAVE_GETPASS
1512SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
1513 (SCM prompt),
1514 "Display @var{prompt} to the standard error output and read\n"
1515 "a password from @file{/dev/tty}. If this file is not\n"
1516 "accessible, it reads from standard input. The password may be\n"
1517 "up to 127 characters in length. Additional characters and the\n"
1518 "terminating newline character are discarded. While reading\n"
1519 "the password, echoing and the generation of signals by special\n"
1520 "characters is disabled.")
1521#define FUNC_NAME s_scm_getpass
1522{
1523 char * p;
1524 SCM passwd;
1525
1526 SCM_VALIDATE_STRING (1, prompt);
94e6d793
MG
1527
1528 p = getpass(SCM_STRING_CHARS (prompt));
1529 passwd = scm_makfrom0str (p);
1530
1531 /* Clear out the password in the static buffer. */
1532 memset (p, 0, strlen (p));
1533
1534 return passwd;
1535}
1536#undef FUNC_NAME
1537#endif /* HAVE_GETPASS */
1538
8f99e3f3
SJ
1539/* Wrapper function for flock() support under M$-Windows. */
1540#ifdef __MINGW32__
1541# include <io.h>
1542# include <sys/locking.h>
1543# include <errno.h>
1544# ifndef _LK_UNLCK
1545 /* Current MinGW package fails to define this. *sigh* */
1546# define _LK_UNLCK 0
1547# endif
1548# define LOCK_EX 1
1549# define LOCK_UN 2
1550# define LOCK_SH 4
1551# define LOCK_NB 8
1552
1553static int flock (int fd, int operation)
1554{
1555 long pos, len;
1556 int ret, err;
1557
1558 /* Disable invalid arguments. */
1559 if (((operation & (LOCK_EX | LOCK_SH)) == (LOCK_EX | LOCK_SH)) ||
1560 ((operation & (LOCK_EX | LOCK_UN)) == (LOCK_EX | LOCK_UN)) ||
1561 ((operation & (LOCK_SH | LOCK_UN)) == (LOCK_SH | LOCK_UN)))
1562 {
1563 errno = EINVAL;
1564 return -1;
1565 }
1566
1567 /* Determine mode of operation and discard unsupported ones. */
1568 if (operation == (LOCK_NB | LOCK_EX))
1569 operation = _LK_NBLCK;
1570 else if (operation & LOCK_UN)
1571 operation = _LK_UNLCK;
1572 else if (operation == LOCK_EX)
1573 operation = _LK_LOCK;
1574 else
1575 {
1576 errno = EINVAL;
1577 return -1;
1578 }
1579
1580 /* Save current file pointer and seek to beginning. */
1581 if ((pos = lseek (fd, 0, SEEK_CUR)) == -1 || (len = filelength (fd)) == -1)
1582 return -1;
1583 lseek (fd, 0L, SEEK_SET);
1584
1585 /* Deadlock if necessary. */
1586 do
1587 {
1588 ret = _locking (fd, operation, len);
1589 }
1590 while (ret == -1 && errno == EDEADLOCK);
1591
1592 /* Produce meaningful error message. */
1593 if (errno == EACCES && operation == _LK_NBLCK)
1594 err = EDEADLOCK;
1595 else
1596 err = errno;
1597
1598 /* Return to saved file position pointer. */
1599 lseek (fd, pos, SEEK_SET);
1600 errno = err;
1601 return ret;
1602}
1603#endif /* __MINGW32__ */
1604
1605#if HAVE_FLOCK || defined (__MINGW32__)
94e6d793
MG
1606SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
1607 (SCM file, SCM operation),
1608 "Apply or remove an advisory lock on an open file.\n"
1609 "@var{operation} specifies the action to be done:\n"
1610 "@table @code\n"
1611 "@item LOCK_SH\n"
1612 "Shared lock. More than one process may hold a shared lock\n"
1613 "for a given file at a given time.\n"
1614 "@item LOCK_EX\n"
1615 "Exclusive lock. Only one process may hold an exclusive lock\n"
1616 "for a given file at a given time.\n"
1617 "@item LOCK_UN\n"
1618 "Unlock the file.\n"
1619 "@item LOCK_NB\n"
1620 "Don't block when locking. May be specified by bitwise OR'ing\n"
1621 "it to one of the other operations.\n"
1622 "@end table\n"
1623 "The return value is not specified. @var{file} may be an open\n"
bb2c02f2 1624 "file descriptor or an open file descriptor port.")
94e6d793
MG
1625#define FUNC_NAME s_scm_flock
1626{
1627 int coperation, fdes;
1628
1629 if (SCM_INUMP (file))
1630 fdes = SCM_INUM (file);
1631 else
1632 {
1633 SCM_VALIDATE_OPFPORT (2, file);
1634
1635 fdes = SCM_FPORT_FDES (file);
1636 }
1637 SCM_VALIDATE_INUM_COPY (2, operation, coperation);
1638 if (flock (fdes, coperation) == -1)
1639 SCM_SYSERROR;
1640 return SCM_UNSPECIFIED;
1641}
1642#undef FUNC_NAME
1643#endif /* HAVE_FLOCK */
1644
1645#if HAVE_SETHOSTNAME
1646SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
1647 (SCM name),
1648 "Set the host name of the current processor to @var{name}. May\n"
1649 "only be used by the superuser. The return value is not\n"
1650 "specified.")
1651#define FUNC_NAME s_scm_sethostname
1652{
1653 SCM_VALIDATE_STRING (1, name);
94e6d793
MG
1654
1655 if (sethostname (SCM_STRING_CHARS (name), SCM_STRING_LENGTH (name)) == -1)
1656 SCM_SYSERROR;
1657 return SCM_UNSPECIFIED;
1658}
1659#undef FUNC_NAME
1660#endif /* HAVE_SETHOSTNAME */
1661
1662#if HAVE_GETHOSTNAME
1663SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
1664 (void),
1665 "Return the host name of the current processor.")
1666#define FUNC_NAME s_scm_gethostname
1667{
a0f9c651
MG
1668 /* 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
1669 large enough. */
1670 int len = 256, res;
4c9419ac 1671 char *p = scm_malloc (len);
94e6d793
MG
1672 SCM name;
1673
1674 res = gethostname (p, len);
1675 while (res == -1 && errno == ENAMETOOLONG)
1676 {
4c9419ac 1677 p = scm_realloc (p, len * 2);
94e6d793
MG
1678 len *= 2;
1679 res = gethostname (p, len);
1680 }
1681 if (res == -1)
1682 {
4c9419ac 1683 free (p);
94e6d793
MG
1684 SCM_SYSERROR;
1685 }
1686 name = scm_makfrom0str (p);
4c9419ac 1687 free (p);
94e6d793
MG
1688 return name;
1689}
1690#undef FUNC_NAME
1691#endif /* HAVE_GETHOSTNAME */
1692
0f2d19dd
JB
1693void
1694scm_init_posix ()
0f2d19dd
JB
1695{
1696 scm_add_feature ("posix");
1697#ifdef HAVE_GETEUID
1698 scm_add_feature ("EIDs");
1699#endif
1700#ifdef WAIT_ANY
86d31dfe 1701 scm_c_define ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY));
0f2d19dd
JB
1702#endif
1703#ifdef WAIT_MYPGRP
86d31dfe 1704 scm_c_define ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP));
0f2d19dd
JB
1705#endif
1706#ifdef WNOHANG
86d31dfe 1707 scm_c_define ("WNOHANG", SCM_MAKINUM (WNOHANG));
0f2d19dd
JB
1708#endif
1709#ifdef WUNTRACED
86d31dfe 1710 scm_c_define ("WUNTRACED", SCM_MAKINUM (WUNTRACED));
0f2d19dd
JB
1711#endif
1712
0f2d19dd 1713 /* access() symbols. */
86d31dfe
MV
1714 scm_c_define ("R_OK", SCM_MAKINUM (R_OK));
1715 scm_c_define ("W_OK", SCM_MAKINUM (W_OK));
1716 scm_c_define ("X_OK", SCM_MAKINUM (X_OK));
1717 scm_c_define ("F_OK", SCM_MAKINUM (F_OK));
0f2d19dd
JB
1718
1719#ifdef LC_COLLATE
86d31dfe 1720 scm_c_define ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE));
0f2d19dd
JB
1721#endif
1722#ifdef LC_CTYPE
86d31dfe 1723 scm_c_define ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE));
0f2d19dd
JB
1724#endif
1725#ifdef LC_MONETARY
86d31dfe 1726 scm_c_define ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY));
0f2d19dd
JB
1727#endif
1728#ifdef LC_NUMERIC
86d31dfe 1729 scm_c_define ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC));
0f2d19dd
JB
1730#endif
1731#ifdef LC_TIME
86d31dfe 1732 scm_c_define ("LC_TIME", SCM_MAKINUM (LC_TIME));
0f2d19dd
JB
1733#endif
1734#ifdef LC_MESSAGES
86d31dfe 1735 scm_c_define ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES));
0f2d19dd
JB
1736#endif
1737#ifdef LC_ALL
86d31dfe 1738 scm_c_define ("LC_ALL", SCM_MAKINUM (LC_ALL));
0f2d19dd 1739#endif
bd9e24b3 1740#ifdef PIPE_BUF
86d31dfe 1741 scm_c_define ("PIPE_BUF", scm_long2num (PIPE_BUF));
bd9e24b3
GH
1742#endif
1743
94e6d793 1744#ifdef PRIO_PROCESS
86d31dfe 1745 scm_c_define ("PRIO_PROCESS", SCM_MAKINUM (PRIO_PROCESS));
94e6d793
MG
1746#endif
1747#ifdef PRIO_PGRP
86d31dfe 1748 scm_c_define ("PRIO_PGRP", SCM_MAKINUM (PRIO_PGRP));
94e6d793
MG
1749#endif
1750#ifdef PRIO_USER
86d31dfe 1751 scm_c_define ("PRIO_USER", SCM_MAKINUM (PRIO_USER));
94e6d793
MG
1752#endif
1753
1754#ifdef LOCK_SH
86d31dfe 1755 scm_c_define ("LOCK_SH", SCM_MAKINUM (LOCK_SH));
94e6d793
MG
1756#endif
1757#ifdef LOCK_EX
86d31dfe 1758 scm_c_define ("LOCK_EX", SCM_MAKINUM (LOCK_EX));
94e6d793
MG
1759#endif
1760#ifdef LOCK_UN
86d31dfe 1761 scm_c_define ("LOCK_UN", SCM_MAKINUM (LOCK_UN));
94e6d793
MG
1762#endif
1763#ifdef LOCK_NB
86d31dfe 1764 scm_c_define ("LOCK_NB", SCM_MAKINUM (LOCK_NB));
94e6d793
MG
1765#endif
1766
a0599745
MD
1767#include "libguile/cpp_sig_symbols.c"
1768#include "libguile/posix.x"
0f2d19dd 1769}
89e00824
ML
1770
1771/*
1772 Local Variables:
1773 c-file-style: "gnu"
1774 End:
1775*/