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