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