* Enhancements to online help presentation.
[bpt/guile.git] / libguile / posix.c
CommitLineData
e282f286 1/* Copyright (C) 1995, 96, 97, 98, 99, 2000 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
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
a0599745
MD
48#include "libguile/_scm.h"
49#include "libguile/fports.h"
50#include "libguile/scmsigs.h"
51#include "libguile/feature.h"
52#include "libguile/strings.h"
53#include "libguile/vectors.h"
54
55#include "libguile/validate.h"
56#include "libguile/posix.h"
0f2d19dd
JB
57\f
58
02b754d3
GH
59#ifdef HAVE_STRING_H
60#include <string.h>
61#endif
0f2d19dd
JB
62#ifdef TIME_WITH_SYS_TIME
63# include <sys/time.h>
64# include <time.h>
65#else
66# if HAVE_SYS_TIME_H
67# include <sys/time.h>
68# else
69# include <time.h>
70# endif
71#endif
72
73#ifdef HAVE_UNISTD_H
74#include <unistd.h>
95b88819
GH
75#else
76#ifndef ttyname
77extern char *ttyname();
78#endif
0f2d19dd
JB
79#endif
80
3594582b 81#ifdef LIBC_H_WITH_UNISTD_H
bab0f4e5
JB
82#include <libc.h>
83#endif
84
8cc71382 85#include <sys/types.h>
0f2d19dd
JB
86#include <sys/stat.h>
87#include <fcntl.h>
88
89#include <pwd.h>
90
91#if HAVE_SYS_WAIT_H
92# include <sys/wait.h>
93#endif
94#ifndef WEXITSTATUS
95# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
96#endif
97#ifndef WIFEXITED
98# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
99#endif
100
101#include <signal.h>
102
0f2d19dd
JB
103extern FILE *popen ();
104extern char ** environ;
105
106#include <grp.h>
107#include <sys/utsname.h>
108
109#if HAVE_DIRENT_H
110# include <dirent.h>
111# define NAMLEN(dirent) strlen((dirent)->d_name)
112#else
113# define dirent direct
114# define NAMLEN(dirent) (dirent)->d_namlen
115# if HAVE_SYS_NDIR_H
116# include <sys/ndir.h>
117# endif
118# if HAVE_SYS_DIR_H
119# include <sys/dir.h>
120# endif
121# if HAVE_NDIR_H
122# include <ndir.h>
123# endif
124#endif
125
0f2d19dd
JB
126#ifdef HAVE_SETLOCALE
127#include <locale.h>
128#endif
129
bab0f4e5
JB
130/* Some Unix systems don't define these. CPP hair is dangerous, but
131 this seems safe enough... */
132#ifndef R_OK
133#define R_OK 4
134#endif
135
136#ifndef W_OK
137#define W_OK 2
138#endif
139
140#ifndef X_OK
141#define X_OK 1
142#endif
143
144#ifndef F_OK
145#define F_OK 0
146#endif
398609a5
JB
147
148/* On NextStep, <utime.h> doesn't define struct utime, unless we
149 #define _POSIX_SOURCE before #including it. I think this is less
150 of a kludge than defining struct utimbuf ourselves. */
151#ifdef UTIMBUF_NEEDS_POSIX
152#define _POSIX_SOURCE
153#endif
154
155#ifdef HAVE_SYS_UTIME_H
156#include <sys/utime.h>
157#endif
158
159#ifdef HAVE_UTIME_H
160#include <utime.h>
161#endif
162
163/* Please don't add any more #includes or #defines here. The hack
164 above means that _POSIX_SOURCE may be #defined, which will
165 encourage header files to do strange things. */
166
0f2d19dd 167\f
bc45012d
JB
168SCM_SYMBOL (sym_read_pipe, "read pipe");
169SCM_SYMBOL (sym_write_pipe, "write pipe");
0f2d19dd 170
a1ec6916 171SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
1bbd0b84 172 (),
ae1b098b
GH
173 "Returns a newly created pipe: a pair of ports which are linked\n"
174 "together on the local machine. The CAR is the input port and\n"
175 "the CDR is the output port. Data written (and flushed) to the\n"
176 "output port can be read from the input port.\n"
177 "Pipes are commonly used for communication with a newly\n"
bd9e24b3
GH
178 "forked child process. The need to flush the output port\n"
179 "can be avoided by making it unbuffered using @code{setvbuf}.\n\n"
180 "Writes occur atomically provided the size of the data in\n"
181 "bytes is not greater than the value of @code{PIPE_BUF}\n"
182 "Note that the output port is likely to block if too much data\n"
183 "(typically equal to @code{PIPE_BUF}) has been written but not\n"
184 "yet read from the input port\n"
ae1b098b 185 )
1bbd0b84 186#define FUNC_NAME s_scm_pipe
0f2d19dd
JB
187{
188 int fd[2], rv;
0f2d19dd 189 SCM p_rd, p_wt;
02b754d3 190
0f2d19dd
JB
191 rv = pipe (fd);
192 if (rv)
1bbd0b84 193 SCM_SYSERROR;
ee149d03
JB
194
195 p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
196 p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
0f2d19dd
JB
197 return scm_cons (p_rd, p_wt);
198}
1bbd0b84 199#undef FUNC_NAME
0f2d19dd
JB
200
201
0e958795 202#ifdef HAVE_GETGROUPS
a1ec6916 203SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
1bbd0b84 204 (),
b380b885 205 "Returns a vector of integers representing the current supplimentary group IDs.")
1bbd0b84 206#define FUNC_NAME s_scm_getgroups
0f2d19dd
JB
207{
208 SCM grps, ans;
209 int ngroups = getgroups (0, NULL);
02b754d3 210 if (!ngroups)
1bbd0b84 211 SCM_SYSERROR;
0f2d19dd
JB
212 SCM_NEWCELL(grps);
213 SCM_DEFER_INTS;
214 {
215 GETGROUPS_T *groups;
216 int val;
217
1bbd0b84 218 groups = SCM_MUST_MALLOC_TYPE_NUM(GETGROUPS_T,ngroups);
0f2d19dd
JB
219 val = getgroups(ngroups, groups);
220 if (val < 0)
221 {
ee149d03 222 int en = errno;
0f2d19dd 223 scm_must_free((char *)groups);
ee149d03 224 errno = en;
1bbd0b84 225 SCM_SYSERROR;
0f2d19dd
JB
226 }
227 SCM_SETCHARS(grps, groups); /* set up grps as a GC protect */
228 SCM_SETLENGTH(grps, 0L + ngroups * sizeof(GETGROUPS_T), scm_tc7_string);
a8741caa 229 ans = scm_make_vector (SCM_MAKINUM(ngroups), SCM_UNDEFINED);
0f2d19dd
JB
230 while (--ngroups >= 0) SCM_VELTS(ans)[ngroups] = SCM_MAKINUM(groups[ngroups]);
231 SCM_SETCHARS(grps, groups); /* to make sure grps stays around. */
ee149d03 232 SCM_ALLOW_INTS;
0f2d19dd
JB
233 return ans;
234 }
1bbd0b84
GB
235}
236#undef FUNC_NAME
0e958795 237#endif
0f2d19dd
JB
238
239
a1ec6916 240SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
1bbd0b84 241 (SCM user),
b380b885
MD
242 "Look up an entry in the user database. @var{obj} can be an integer,\n"
243 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
244 "or getpwent respectively.")
1bbd0b84 245#define FUNC_NAME s_scm_getpwuid
0f2d19dd
JB
246{
247 SCM result;
248 struct passwd *entry;
249 SCM *ve;
250
a8741caa 251 result = scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED);
0f2d19dd
JB
252 ve = SCM_VELTS (result);
253 if (SCM_UNBNDP (user) || SCM_FALSEP (user))
254 {
0f2d19dd 255 SCM_SYSCALL (entry = getpwent ());
3d781d49
JB
256 if (! entry)
257 {
3d781d49
JB
258 return SCM_BOOL_F;
259 }
0f2d19dd
JB
260 }
261 else if (SCM_INUMP (user))
262 {
0f2d19dd
JB
263 entry = getpwuid (SCM_INUM (user));
264 }
265 else
266 {
3b3b36dd 267 SCM_VALIDATE_ROSTRING (1,user);
0f2d19dd
JB
268 if (SCM_SUBSTRP (user))
269 user = scm_makfromstr (SCM_ROCHARS (user), SCM_ROLENGTH (user), 0);
0f2d19dd
JB
270 entry = getpwnam (SCM_ROCHARS (user));
271 }
272 if (!entry)
1bbd0b84 273 SCM_MISC_ERROR ("entry not found", SCM_EOL);
02b754d3 274
0f2d19dd
JB
275 ve[0] = scm_makfrom0str (entry->pw_name);
276 ve[1] = scm_makfrom0str (entry->pw_passwd);
277 ve[2] = scm_ulong2num ((unsigned long) entry->pw_uid);
278 ve[3] = scm_ulong2num ((unsigned long) entry->pw_gid);
279 ve[4] = scm_makfrom0str (entry->pw_gecos);
280 if (!entry->pw_dir)
281 ve[5] = scm_makfrom0str ("");
282 else
283 ve[5] = scm_makfrom0str (entry->pw_dir);
284 if (!entry->pw_shell)
285 ve[6] = scm_makfrom0str ("");
286 else
287 ve[6] = scm_makfrom0str (entry->pw_shell);
0f2d19dd
JB
288 return result;
289}
1bbd0b84 290#undef FUNC_NAME
0f2d19dd
JB
291
292
0e958795 293#ifdef HAVE_SETPWENT
a1ec6916 294SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
1bbd0b84 295 (SCM arg),
b380b885
MD
296 "If called with a true argument, initialize or reset the password data\n"
297 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
298 "@code{endpwent} procedures are implemented on top of this.")
1bbd0b84 299#define FUNC_NAME s_scm_setpwent
0f2d19dd
JB
300{
301 if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
302 endpwent ();
303 else
304 setpwent ();
305 return SCM_UNSPECIFIED;
306}
1bbd0b84 307#undef FUNC_NAME
0e958795 308#endif
0f2d19dd
JB
309
310
311
312/* Combines getgrgid and getgrnam. */
a1ec6916 313SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
1bbd0b84 314 (SCM name),
b380b885
MD
315 "Look up an entry in the group database. @var{obj} can be an integer,\n"
316 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
317 "or getgrent respectively.")
1bbd0b84 318#define FUNC_NAME s_scm_getgrgid
0f2d19dd
JB
319{
320 SCM result;
321 struct group *entry;
322 SCM *ve;
a8741caa 323 result = scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED);
0f2d19dd 324 ve = SCM_VELTS (result);
4260a7fc 325 if (SCM_UNBNDP (name) || SCM_FALSEP (name))
3d781d49
JB
326 {
327 SCM_SYSCALL (entry = getgrent ());
328 if (! entry)
329 {
3d781d49
JB
330 return SCM_BOOL_F;
331 }
332 }
0f2d19dd
JB
333 else if (SCM_INUMP (name))
334 SCM_SYSCALL (entry = getgrgid (SCM_INUM (name)));
335 else
336 {
3b3b36dd 337 SCM_VALIDATE_ROSTRING (1,name);
89958ad0 338 SCM_COERCE_SUBSTR (name);
ae2fa5bc 339 SCM_SYSCALL (entry = getgrnam (SCM_ROCHARS (name)));
0f2d19dd
JB
340 }
341 if (!entry)
1bbd0b84 342 SCM_SYSERROR;
02b754d3 343
0f2d19dd
JB
344 ve[0] = scm_makfrom0str (entry->gr_name);
345 ve[1] = scm_makfrom0str (entry->gr_passwd);
346 ve[2] = scm_ulong2num ((unsigned long) entry->gr_gid);
347 ve[3] = scm_makfromstrs (-1, entry->gr_mem);
0f2d19dd
JB
348 return result;
349}
1bbd0b84 350#undef FUNC_NAME
0f2d19dd
JB
351
352
353
a1ec6916 354SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
1bbd0b84 355 (SCM arg),
b380b885
MD
356 "If called with a true argument, initialize or reset the group data\n"
357 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
358 "@code{endgrent} procedures are implemented on top of this.")
1bbd0b84 359#define FUNC_NAME s_scm_setgrent
0f2d19dd
JB
360{
361 if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
362 endgrent ();
363 else
364 setgrent ();
365 return SCM_UNSPECIFIED;
366}
1bbd0b84 367#undef FUNC_NAME
0f2d19dd
JB
368
369
370
a1ec6916 371SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
1bbd0b84 372 (SCM pid, SCM sig),
b380b885
MD
373 "Sends a signal to the specified process or group of processes.\n\n"
374 "@var{pid} specifies the processes to which the signal is sent:\n\n"
375 "@table @r\n"
376 "@item @var{pid} greater than 0\n"
377 "The process whose identifier is @var{pid}.\n"
378 "@item @var{pid} equal to 0\n"
379 "All processes in the current process group.\n"
380 "@item @var{pid} less than -1\n"
381 "The process group whose identifier is -@var{pid}\n"
382 "@item @var{pid} equal to -1\n"
383 "If the process is privileged, all processes except for some special\n"
384 "system processes. Otherwise, all processes with the current effective\n"
385 "user ID.\n"
386 "@end table\n\n"
387 "@var{sig} should be specified using a variable corresponding to\n"
388 "the Unix symbolic name, e.g.,\n\n"
389 "@defvar SIGHUP\n"
390 "Hang-up signal.\n"
391 "@end defvar\n\n"
392 "@defvar SIGINT\n"
393 "Interrupt signal.\n"
394 "@end defvar")
1bbd0b84 395#define FUNC_NAME s_scm_kill
0f2d19dd 396{
3b3b36dd
GB
397 SCM_VALIDATE_INUM (1,pid);
398 SCM_VALIDATE_INUM (2,sig);
0f2d19dd 399 /* Signal values are interned in scm_init_posix(). */
02b754d3 400 if (kill ((int) SCM_INUM (pid), (int) SCM_INUM (sig)) != 0)
1bbd0b84 401 SCM_SYSERROR;
02b754d3 402 return SCM_UNSPECIFIED;
0f2d19dd 403}
1bbd0b84 404#undef FUNC_NAME
0f2d19dd 405
d00ae47e 406#ifdef HAVE_WAITPID
a1ec6916 407SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
1bbd0b84 408 (SCM pid, SCM options),
b380b885
MD
409 "This procedure collects status information from a child process which\n"
410 "has terminated or (optionally) stopped. Normally it will\n"
411 "suspend the calling process until this can be done. If more than one\n"
412 "child process is eligible then one will be chosen by the operating system.\n\n"
413 "The value of @var{pid} determines the behaviour:\n\n"
414 "@table @r\n"
415 "@item @var{pid} greater than 0\n"
416 "Request status information from the specified child process.\n"
417 "@item @var{pid} equal to -1 or WAIT_ANY\n"
418 "Request status information for any child process.\n"
419 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
420 "Request status information for any child process in the current process\n"
421 "group.\n"
422 "@item @var{pid} less than -1\n"
423 "Request status information for any child process whose process group ID\n"
424 "is -@var{PID}.\n"
425 "@end table\n\n"
426 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
427 "values of zero or more of the following variables:\n\n"
428 "@defvar WNOHANG\n"
429 "Return immediately even if there are no child processes to be collected.\n"
430 "@end defvar\n\n"
431 "@defvar WUNTRACED\n"
432 "Report status information for stopped processes as well as terminated\n"
433 "processes.\n"
434 "@end defvar\n\n"
435 "The return value is a pair containing:\n\n"
436 "@enumerate\n"
437 "@item\n"
438 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
439 "specified and no process was collected.\n"
440 "@item\n"
441 "The integer status value.\n"
442 "@end enumerate")
1bbd0b84 443#define FUNC_NAME s_scm_waitpid
0f2d19dd
JB
444{
445 int i;
446 int status;
447 int ioptions;
3b3b36dd 448 SCM_VALIDATE_INUM (1,pid);
0f2d19dd
JB
449 if (SCM_UNBNDP (options))
450 ioptions = 0;
451 else
452 {
3b3b36dd 453 SCM_VALIDATE_INUM (2,options);
0f2d19dd
JB
454 /* Flags are interned in scm_init_posix. */
455 ioptions = SCM_INUM (options);
456 }
457 SCM_SYSCALL (i = waitpid (SCM_INUM (pid), &status, ioptions));
02b754d3 458 if (i == -1)
1bbd0b84 459 SCM_SYSERROR;
02b754d3 460 return scm_cons (SCM_MAKINUM (0L + i), SCM_MAKINUM (0L + status));
0f2d19dd 461}
1bbd0b84 462#undef FUNC_NAME
d00ae47e 463#endif /* HAVE_WAITPID */
0f2d19dd 464
a1ec6916 465SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
1bbd0b84 466 (SCM status),
b380b885
MD
467 "Returns the exit status value, as would be\n"
468 "set if a process ended normally through a\n"
469 "call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.")
1bbd0b84 470#define FUNC_NAME s_scm_status_exit_val
67ec3667 471{
e67dc2be
JB
472 int lstatus;
473
3b3b36dd 474 SCM_VALIDATE_INUM (1,status);
e67dc2be
JB
475
476 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
477 go figure. SCM_INUM does not yield an lvalue. */
478 lstatus = SCM_INUM (status);
479 if (WIFEXITED (lstatus))
480 return (SCM_MAKINUM (WEXITSTATUS (lstatus)));
67ec3667
GH
481 else
482 return SCM_BOOL_F;
483}
1bbd0b84 484#undef FUNC_NAME
e67dc2be 485
a1ec6916 486SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
1bbd0b84 487 (SCM status),
b380b885
MD
488 "Returns the signal number which terminated the\n"
489 "process, if any, otherwise @code{#f}.")
1bbd0b84 490#define FUNC_NAME s_scm_status_term_sig
67ec3667 491{
e67dc2be
JB
492 int lstatus;
493
3b3b36dd 494 SCM_VALIDATE_INUM (1,status);
e67dc2be
JB
495
496 lstatus = SCM_INUM (status);
497 if (WIFSIGNALED (lstatus))
498 return SCM_MAKINUM (WTERMSIG (lstatus));
67ec3667
GH
499 else
500 return SCM_BOOL_F;
501}
1bbd0b84 502#undef FUNC_NAME
0f2d19dd 503
a1ec6916 504SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
1bbd0b84 505 (SCM status),
b380b885
MD
506 "Returns the signal number which stopped the\n"
507 "process, if any, otherwise @code{#f}.")
1bbd0b84 508#define FUNC_NAME s_scm_status_stop_sig
67ec3667 509{
e67dc2be
JB
510 int lstatus;
511
3b3b36dd 512 SCM_VALIDATE_INUM (1,status);
e67dc2be
JB
513
514 lstatus = SCM_INUM (status);
515 if (WIFSTOPPED (lstatus))
516 return SCM_MAKINUM (WSTOPSIG (lstatus));
67ec3667
GH
517 else
518 return SCM_BOOL_F;
519}
1bbd0b84 520#undef FUNC_NAME
0f2d19dd 521
a1ec6916 522SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
1bbd0b84 523 (),
b380b885 524 "Returns an integer representing the process ID of the parent process.")
1bbd0b84 525#define FUNC_NAME s_scm_getppid
0f2d19dd
JB
526{
527 return SCM_MAKINUM (0L + getppid ());
528}
1bbd0b84 529#undef FUNC_NAME
0f2d19dd
JB
530
531
532
a1ec6916 533SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
1bbd0b84 534 (),
b380b885 535 "Returns an integer representing the current real user ID.")
1bbd0b84 536#define FUNC_NAME s_scm_getuid
0f2d19dd
JB
537{
538 return SCM_MAKINUM (0L + getuid ());
539}
1bbd0b84 540#undef FUNC_NAME
0f2d19dd
JB
541
542
543
a1ec6916 544SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
1bbd0b84 545 (),
b380b885 546 "Returns an integer representing the current real group ID.")
1bbd0b84 547#define FUNC_NAME s_scm_getgid
0f2d19dd
JB
548{
549 return SCM_MAKINUM (0L + getgid ());
550}
1bbd0b84 551#undef FUNC_NAME
0f2d19dd
JB
552
553
554
a1ec6916 555SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
1bbd0b84 556 (),
b380b885
MD
557 "Returns an integer representing the current effective user ID.\n"
558 "If the system does not support effective IDs, then the real ID\n"
559 "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
560 "supports effective IDs.")
1bbd0b84 561#define FUNC_NAME s_scm_geteuid
0f2d19dd
JB
562{
563#ifdef HAVE_GETEUID
564 return SCM_MAKINUM (0L + geteuid ());
565#else
566 return SCM_MAKINUM (0L + getuid ());
567#endif
568}
1bbd0b84 569#undef FUNC_NAME
0f2d19dd
JB
570
571
572
a1ec6916 573SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
1bbd0b84 574 (),
b380b885
MD
575 "Returns an integer representing the current effective group ID.\n"
576 "If the system does not support effective IDs, then the real ID\n"
577 "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
578 "supports effective IDs.")
1bbd0b84 579#define FUNC_NAME s_scm_getegid
0f2d19dd
JB
580{
581#ifdef HAVE_GETEUID
582 return SCM_MAKINUM (0L + getegid ());
583#else
584 return SCM_MAKINUM (0L + getgid ());
585#endif
586}
1bbd0b84 587#undef FUNC_NAME
0f2d19dd
JB
588
589
a1ec6916 590SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
1bbd0b84 591 (SCM id),
b380b885
MD
592 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
593 "the process has appropriate privileges.\n"
594 "The return value is unspecified.")
1bbd0b84 595#define FUNC_NAME s_scm_setuid
0f2d19dd 596{
3b3b36dd 597 SCM_VALIDATE_INUM (1,id);
02b754d3 598 if (setuid (SCM_INUM (id)) != 0)
1bbd0b84 599 SCM_SYSERROR;
02b754d3 600 return SCM_UNSPECIFIED;
0f2d19dd 601}
1bbd0b84 602#undef FUNC_NAME
0f2d19dd 603
a1ec6916 604SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
1bbd0b84 605 (SCM id),
b380b885
MD
606 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
607 "the process has appropriate privileges.\n"
608 "The return value is unspecified.")
1bbd0b84 609#define FUNC_NAME s_scm_setgid
0f2d19dd 610{
3b3b36dd 611 SCM_VALIDATE_INUM (1,id);
02b754d3 612 if (setgid (SCM_INUM (id)) != 0)
1bbd0b84 613 SCM_SYSERROR;
02b754d3 614 return SCM_UNSPECIFIED;
0f2d19dd 615}
1bbd0b84 616#undef FUNC_NAME
0f2d19dd 617
a1ec6916 618SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
1bbd0b84 619 (SCM id),
b380b885
MD
620 "Sets the effective user ID to the integer @var{id}, provided the process\n"
621 "has appropriate privileges. If effective IDs are not supported, the\n"
622 "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
623 "system supports effective IDs.\n"
624 "The return value is unspecified.")
1bbd0b84 625#define FUNC_NAME s_scm_seteuid
0f2d19dd 626{
02b754d3
GH
627 int rv;
628
3b3b36dd 629 SCM_VALIDATE_INUM (1,id);
0f2d19dd 630#ifdef HAVE_SETEUID
02b754d3 631 rv = seteuid (SCM_INUM (id));
0f2d19dd 632#else
02b754d3 633 rv = setuid (SCM_INUM (id));
0f2d19dd 634#endif
02b754d3 635 if (rv != 0)
1bbd0b84 636 SCM_SYSERROR;
02b754d3 637 return SCM_UNSPECIFIED;
0f2d19dd 638}
1bbd0b84 639#undef FUNC_NAME
0f2d19dd 640
0e958795 641#ifdef HAVE_SETEGID
a1ec6916 642SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
1bbd0b84 643 (SCM id),
b380b885
MD
644 "Sets the effective group ID to the integer @var{id}, provided the process\n"
645 "has appropriate privileges. If effective IDs are not supported, the\n"
646 "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
647 "system supports effective IDs.\n"
648 "The return value is unspecified.")
1bbd0b84 649#define FUNC_NAME s_scm_setegid
0f2d19dd 650{
02b754d3
GH
651 int rv;
652
3b3b36dd 653 SCM_VALIDATE_INUM (1,id);
0f2d19dd 654#ifdef HAVE_SETEUID
02b754d3 655 rv = setegid (SCM_INUM (id));
0f2d19dd 656#else
02b754d3 657 rv = setgid (SCM_INUM (id));
0f2d19dd 658#endif
02b754d3 659 if (rv != 0)
1bbd0b84 660 SCM_SYSERROR;
02b754d3
GH
661 return SCM_UNSPECIFIED;
662
0f2d19dd 663}
1bbd0b84 664#undef FUNC_NAME
0e958795 665#endif
0f2d19dd 666
a1ec6916 667SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
1bbd0b84 668 (),
b380b885
MD
669 "Returns an integer representing the current process group ID.\n"
670 "This is the POSIX definition, not BSD.")
1bbd0b84 671#define FUNC_NAME s_scm_getpgrp
0f2d19dd
JB
672{
673 int (*fn)();
4625e44f 674 fn = (int (*) ()) getpgrp;
0f2d19dd
JB
675 return SCM_MAKINUM (fn (0));
676}
1bbd0b84 677#undef FUNC_NAME
0f2d19dd 678
f25f761d 679#ifdef HAVE_SETPGID
a1ec6916 680SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
1bbd0b84 681 (SCM pid, SCM pgid),
b380b885
MD
682 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
683 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
684 "current process.\n"
685 "Fails on systems that do not support job control.\n"
686 "The return value is unspecified.")
1bbd0b84 687#define FUNC_NAME s_scm_setpgid
0f2d19dd 688{
3b3b36dd
GB
689 SCM_VALIDATE_INUM (1,pid);
690 SCM_VALIDATE_INUM (2,pgid);
02b754d3
GH
691 /* FIXME(?): may be known as setpgrp. */
692 if (setpgid (SCM_INUM (pid), SCM_INUM (pgid)) != 0)
1bbd0b84 693 SCM_SYSERROR;
02b754d3 694 return SCM_UNSPECIFIED;
0f2d19dd 695}
1bbd0b84 696#undef FUNC_NAME
f25f761d 697#endif /* HAVE_SETPGID */
0f2d19dd 698
f25f761d 699#ifdef HAVE_SETSID
a1ec6916 700SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
1bbd0b84 701 (),
b380b885
MD
702 "Creates a new session. The current process becomes the session leader\n"
703 "and is put in a new process group. The process will be detached\n"
704 "from its controlling terminal if it has one.\n"
705 "The return value is an integer representing the new process group ID.")
1bbd0b84 706#define FUNC_NAME s_scm_setsid
0f2d19dd
JB
707{
708 pid_t sid = setsid ();
02b754d3 709 if (sid == -1)
1bbd0b84 710 SCM_SYSERROR;
02b754d3 711 return SCM_UNSPECIFIED;
0f2d19dd 712}
1bbd0b84 713#undef FUNC_NAME
f25f761d 714#endif /* HAVE_SETSID */
0f2d19dd 715
a1ec6916 716SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
1bbd0b84 717 (SCM port),
b380b885
MD
718 "Returns a string with the name of the serial terminal device underlying\n"
719 "@var{port}.")
1bbd0b84 720#define FUNC_NAME s_scm_ttyname
0f2d19dd
JB
721{
722 char *ans;
723 int fd;
78446828
MV
724
725 port = SCM_COERCE_OUTPORT (port);
3b3b36dd 726 SCM_VALIDATE_OPPORT (1,port);
0f2d19dd
JB
727 if (scm_tc16_fport != SCM_TYP16 (port))
728 return SCM_BOOL_F;
ee149d03 729 fd = SCM_FPORT_FDES (port);
02b754d3
GH
730 SCM_SYSCALL (ans = ttyname (fd));
731 if (!ans)
1bbd0b84 732 SCM_SYSERROR;
0f2d19dd 733 /* ans could be overwritten by another call to ttyname */
02b754d3 734 return (scm_makfrom0str (ans));
0f2d19dd 735}
1bbd0b84 736#undef FUNC_NAME
0f2d19dd 737
f25f761d 738#ifdef HAVE_CTERMID
a1ec6916 739SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
1bbd0b84 740 (),
b380b885
MD
741 "Returns a string containing the file name of the controlling terminal\n"
742 "for the current process.")
1bbd0b84 743#define FUNC_NAME s_scm_ctermid
0f2d19dd
JB
744{
745 char *result = ctermid (NULL);
02b754d3 746 if (*result == '\0')
1bbd0b84 747 SCM_SYSERROR;
02b754d3 748 return scm_makfrom0str (result);
0f2d19dd 749}
1bbd0b84 750#undef FUNC_NAME
f25f761d 751#endif /* HAVE_CTERMID */
0f2d19dd 752
f25f761d 753#ifdef HAVE_TCGETPGRP
a1ec6916 754SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
1bbd0b84 755 (SCM port),
b380b885
MD
756 "Returns the process group ID of the foreground\n"
757 "process group associated with the terminal open on the file descriptor\n"
758 "underlying @var{port}.\n\n"
759 "If there is no foreground process group, the return value is a\n"
760 "number greater than 1 that does not match the process group ID\n"
761 "of any existing process group. This can happen if all of the\n"
762 "processes in the job that was formerly the foreground job have\n"
763 "terminated, and no other job has yet been moved into the\n"
764 "foreground.")
1bbd0b84 765#define FUNC_NAME s_scm_tcgetpgrp
0f2d19dd
JB
766{
767 int fd;
768 pid_t pgid;
78446828
MV
769
770 port = SCM_COERCE_OUTPORT (port);
771
3b3b36dd 772 SCM_VALIDATE_OPFPORT (1,port);
ee149d03
JB
773 fd = SCM_FPORT_FDES (port);
774 if ((pgid = tcgetpgrp (fd)) == -1)
1bbd0b84 775 SCM_SYSERROR;
02b754d3 776 return SCM_MAKINUM (pgid);
1bbd0b84
GB
777}
778#undef FUNC_NAME
f25f761d 779#endif /* HAVE_TCGETPGRP */
0f2d19dd 780
f25f761d 781#ifdef HAVE_TCSETPGRP
a1ec6916 782SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
1bbd0b84 783 (SCM port, SCM pgid),
b380b885
MD
784 "Set the foreground process group ID for the terminal used by the file\n"
785 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
786 "The calling process\n"
787 "must be a member of the same session as @var{pgid} and must have the same\n"
788 "controlling terminal. The return value is unspecified.")
1bbd0b84 789#define FUNC_NAME s_scm_tcsetpgrp
0f2d19dd
JB
790{
791 int fd;
78446828
MV
792
793 port = SCM_COERCE_OUTPORT (port);
794
3b3b36dd
GB
795 SCM_VALIDATE_OPFPORT (1,port);
796 SCM_VALIDATE_INUM (2,pgid);
ee149d03
JB
797 fd = SCM_FPORT_FDES (port);
798 if (tcsetpgrp (fd, SCM_INUM (pgid)) == -1)
1bbd0b84 799 SCM_SYSERROR;
02b754d3 800 return SCM_UNSPECIFIED;
1bbd0b84
GB
801}
802#undef FUNC_NAME
f25f761d 803#endif /* HAVE_TCSETPGRP */
0f2d19dd
JB
804
805/* Copy exec args from an SCM vector into a new C array. */
1cc91f1b 806
0f2d19dd 807static char **
3eeba8d4 808scm_convert_exec_args (SCM args, int pos, const char *subr)
0f2d19dd
JB
809{
810 char **execargv;
811 int num_args;
812 int i;
6afcd3b2 813
0f2d19dd 814 num_args = scm_ilength (args);
af45e3b0 815 SCM_ASSERT (num_args >= 0, args, pos, subr);
0f2d19dd 816 execargv = (char **)
6afcd3b2 817 scm_must_malloc ((num_args + 1) * sizeof (char *), subr);
af45e3b0 818 for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i)
0f2d19dd
JB
819 {
820 scm_sizet len;
821 char *dst;
822 char *src;
0c95b57d 823 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (args)),
6afcd3b2 824 SCM_CAR (args), SCM_ARGn, subr);
0f2d19dd 825 len = 1 + SCM_ROLENGTH (SCM_CAR (args));
6afcd3b2 826 dst = (char *) scm_must_malloc ((long) len, subr);
0f2d19dd
JB
827 src = SCM_ROCHARS (SCM_CAR (args));
828 while (len--)
829 dst[len] = src[len];
830 execargv[i] = dst;
831 }
832 execargv[i] = 0;
0f2d19dd
JB
833 return execargv;
834}
835
a1ec6916 836SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
1bbd0b84 837 (SCM filename, SCM args),
b380b885
MD
838 "Executes the file named by @var{path} as a new process image.\n"
839 "The remaining arguments are supplied to the process; from a C program\n"
840 "they are accessable as the @code{argv} argument to @code{main}.\n"
841 "Conventionally the first @var{arg} is the same as @var{path}.\n"
842 "All arguments must be strings. \n\n"
843 "If @var{arg} is missing, @var{path} is executed with a null\n"
844 "argument list, which may have system-dependent side-effects.\n\n"
845 "This procedure is currently implemented using the @code{execv} system\n"
846 "call, but we call it @code{execl} because of its Scheme calling interface.")
1bbd0b84 847#define FUNC_NAME s_scm_execl
0f2d19dd
JB
848{
849 char **execargv;
3b3b36dd 850 SCM_VALIDATE_ROSTRING (1,filename);
6afcd3b2 851 SCM_COERCE_SUBSTR (filename);
1bbd0b84 852 execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
0f2d19dd 853 execv (SCM_ROCHARS (filename), execargv);
1bbd0b84 854 SCM_SYSERROR;
02b754d3
GH
855 /* not reached. */
856 return SCM_BOOL_F;
0f2d19dd 857}
1bbd0b84 858#undef FUNC_NAME
0f2d19dd 859
a1ec6916 860SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
1bbd0b84 861 (SCM filename, SCM args),
b380b885
MD
862 "Similar to @code{execl}, however if\n"
863 "@var{filename} does not contain a slash\n"
864 "then the file to execute will be located by searching the\n"
865 "directories listed in the @code{PATH} environment variable.\n\n"
6bcd01fc 866 "This procedure is currently implemented using the @code{execvp} system\n"
b380b885 867 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1bbd0b84 868#define FUNC_NAME s_scm_execlp
0f2d19dd
JB
869{
870 char **execargv;
3b3b36dd 871 SCM_VALIDATE_ROSTRING (1,filename);
6afcd3b2 872 SCM_COERCE_SUBSTR (filename);
1bbd0b84 873 execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
0f2d19dd 874 execvp (SCM_ROCHARS (filename), execargv);
1bbd0b84 875 SCM_SYSERROR;
02b754d3
GH
876 /* not reached. */
877 return SCM_BOOL_F;
0f2d19dd 878}
1bbd0b84 879#undef FUNC_NAME
0f2d19dd 880
6afcd3b2 881static char **
3eeba8d4 882environ_list_to_c (SCM envlist, int arg, const char *proc)
6afcd3b2
GH
883{
884 int num_strings;
885 char **result;
886 int i = 0;
887
0c95b57d 888 SCM_ASSERT (SCM_NULLP (envlist) || SCM_CONSP (envlist),
6afcd3b2
GH
889 envlist, arg, proc);
890 num_strings = scm_ilength (envlist);
891 result = (char **) malloc ((num_strings + 1) * sizeof (char *));
892 if (result == NULL)
893 scm_memory_error (proc);
894 while (SCM_NNULLP (envlist))
895 {
896 int len;
897 char *src;
898
368cf54d 899 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (envlist)),
6afcd3b2
GH
900 envlist, arg, proc);
901 len = 1 + SCM_ROLENGTH (SCM_CAR (envlist));
902 result[i] = malloc ((long) len);
903 if (result[i] == NULL)
904 scm_memory_error (proc);
905 src = SCM_ROCHARS (SCM_CAR (envlist));
906 while (len--)
907 result[i][len] = src[len];
908 envlist = SCM_CDR (envlist);
909 i++;
910 }
911 result[i] = 0;
6afcd3b2
GH
912 return result;
913}
914
a1ec6916 915SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
1bbd0b84 916 (SCM filename, SCM env, SCM args),
b380b885
MD
917 "Similar to @code{execl}, but the environment of the new process is\n"
918 "specified by @var{env}, which must be a list of strings as returned by the\n"
919 "@code{environ} procedure.\n\n"
920 "This procedure is currently implemented using the @code{execve} system\n"
921 "call, but we call it @code{execle} because of its Scheme calling interface.")
1bbd0b84 922#define FUNC_NAME s_scm_execle
6afcd3b2
GH
923{
924 char **execargv;
925 char **exec_env;
926
3b3b36dd 927 SCM_VALIDATE_ROSTRING (1,filename);
6afcd3b2
GH
928 SCM_COERCE_SUBSTR (filename);
929
1bbd0b84
GB
930 execargv = scm_convert_exec_args (args, SCM_ARG1, FUNC_NAME);
931 exec_env = environ_list_to_c (env, SCM_ARG2, FUNC_NAME);
6afcd3b2 932 execve (SCM_ROCHARS (filename), execargv, exec_env);
1bbd0b84 933 SCM_SYSERROR;
6afcd3b2
GH
934 /* not reached. */
935 return SCM_BOOL_F;
936}
1bbd0b84 937#undef FUNC_NAME
6afcd3b2 938
a1ec6916 939SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
1bbd0b84 940 (),
b380b885
MD
941 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
942 "In the child the return value is 0. In the parent the return value is\n"
943 "the integer process ID of the child.\n\n"
944 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
945 "with the scsh fork.")
1bbd0b84 946#define FUNC_NAME s_scm_fork
0f2d19dd 947{
bab0f4e5 948 int pid;
0f2d19dd
JB
949 pid = fork ();
950 if (pid == -1)
1bbd0b84 951 SCM_SYSERROR;
02b754d3 952 return SCM_MAKINUM (0L+pid);
0f2d19dd 953}
1bbd0b84 954#undef FUNC_NAME
0f2d19dd 955
f25f761d 956#ifdef HAVE_UNAME
a1ec6916 957SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
1bbd0b84 958 (),
b380b885
MD
959 "Returns an object with some information about the computer system the\n"
960 "program is running on.")
1bbd0b84 961#define FUNC_NAME s_scm_uname
0f2d19dd 962{
0f2d19dd 963 struct utsname buf;
a8741caa 964 SCM ans = scm_make_vector (SCM_MAKINUM(5), SCM_UNSPECIFIED);
0f2d19dd 965 SCM *ve = SCM_VELTS (ans);
e1a191a8 966 if (uname (&buf) < 0)
1bbd0b84 967 SCM_SYSERROR;
0f2d19dd
JB
968 ve[0] = scm_makfrom0str (buf.sysname);
969 ve[1] = scm_makfrom0str (buf.nodename);
970 ve[2] = scm_makfrom0str (buf.release);
971 ve[3] = scm_makfrom0str (buf.version);
972 ve[4] = scm_makfrom0str (buf.machine);
973/*
02b754d3 974 a linux special?
0f2d19dd
JB
975 ve[5] = scm_makfrom0str (buf.domainname);
976*/
977 return ans;
0f2d19dd 978}
1bbd0b84 979#undef FUNC_NAME
f25f761d 980#endif /* HAVE_UNAME */
0f2d19dd 981
a1ec6916 982SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1bbd0b84 983 (SCM env),
b380b885
MD
984 "If @var{env} is omitted, returns the current environment as a list of strings.\n"
985 "Otherwise it sets the current environment, which is also the\n"
986 "default environment for child processes, to the supplied list of strings.\n"
987 "Each member of @var{env} should be of the form\n"
988 "@code{NAME=VALUE} and values of @code{NAME} should not be duplicated.\n"
989 "If @var{env} is supplied then the return value is unspecified.")
1bbd0b84 990#define FUNC_NAME s_scm_environ
0f2d19dd
JB
991{
992 if (SCM_UNBNDP (env))
993 return scm_makfromstrs (-1, environ);
994 else
995 {
0f2d19dd 996 char **new_environ;
6afcd3b2 997
1bbd0b84 998 new_environ = environ_list_to_c (env, SCM_ARG1, FUNC_NAME);
0f2d19dd
JB
999 /* Free the old environment, except when called for the first
1000 * time.
1001 */
1002 {
1003 char **ep;
1004 static int first = 1;
1005 if (!first)
1006 {
1007 for (ep = environ; *ep != NULL; ep++)
19468eff
GH
1008 free (*ep);
1009 free ((char *) environ);
0f2d19dd
JB
1010 }
1011 first = 0;
1012 }
1013 environ = new_environ;
1014 return SCM_UNSPECIFIED;
1015 }
1016}
1bbd0b84 1017#undef FUNC_NAME
0f2d19dd 1018
9ee5fce4
MD
1019#ifdef L_tmpnam
1020
a1ec6916 1021SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1bbd0b84 1022 (),
b380b885
MD
1023 "Create a new file in the file system with a unique name. The return\n"
1024 "value is the name of the new file. This function is implemented with\n"
1025 "the @code{tmpnam} function in the system libraries.")
1bbd0b84 1026#define FUNC_NAME s_scm_tmpnam
9ee5fce4
MD
1027{
1028 char name[L_tmpnam];
1029 SCM_SYSCALL (tmpnam (name););
1030 return scm_makfrom0str (name);
1031}
0f981281 1032#undef FUNC_NAME
0f2d19dd 1033
1bbd0b84 1034#endif
1cc91f1b 1035
a1ec6916 1036SCM_DEFINE (scm_utime, "utime", 1, 2, 0,
1bbd0b84 1037 (SCM pathname, SCM actime, SCM modtime),
b380b885
MD
1038 "@code{utime} sets the access and modification times for\n"
1039 "the file named by @var{path}. If @var{actime} or @var{modtime}\n"
1040 "is not supplied, then the current time is used.\n"
1041 "@var{actime} and @var{modtime}\n"
1042 "must be integer time values as returned by the @code{current-time}\n"
1043 "procedure.\n\n"
1044 "E.g.,\n\n"
1045 "@smalllisp\n"
1046 "(utime \"foo\" (- (current-time) 3600))\n"
1047 "@end smalllisp\n\n"
1048 "will set the access time to one hour in the past and the modification\n"
1049 "time to the current time.")
1bbd0b84 1050#define FUNC_NAME s_scm_utime
0f2d19dd
JB
1051{
1052 int rv;
1053 struct utimbuf utm_tmp;
1054
3b3b36dd 1055 SCM_VALIDATE_ROSTRING (1,pathname);
89958ad0 1056 SCM_COERCE_SUBSTR (pathname);
0f2d19dd
JB
1057 if (SCM_UNBNDP (actime))
1058 SCM_SYSCALL (time (&utm_tmp.actime));
1059 else
1bbd0b84 1060 utm_tmp.actime = SCM_NUM2ULONG (2,actime);
0f2d19dd
JB
1061
1062 if (SCM_UNBNDP (modtime))
1063 SCM_SYSCALL (time (&utm_tmp.modtime));
1064 else
1bbd0b84 1065 utm_tmp.modtime = SCM_NUM2ULONG (3,modtime);
0f2d19dd 1066
89958ad0 1067 SCM_SYSCALL (rv = utime (SCM_ROCHARS (pathname), &utm_tmp));
02b754d3 1068 if (rv != 0)
1bbd0b84 1069 SCM_SYSERROR;
02b754d3 1070 return SCM_UNSPECIFIED;
0f2d19dd 1071}
1bbd0b84 1072#undef FUNC_NAME
0f2d19dd 1073
a1ec6916 1074SCM_DEFINE (scm_access, "access?", 2, 0, 0,
1bbd0b84 1075 (SCM path, SCM how),
b380b885
MD
1076 "Returns @code{#t} if @var{path} corresponds to an existing\n"
1077 "file and the current process\n"
1078 "has the type of access specified by @var{how}, otherwise \n"
1079 "@code{#f}.\n"
1080 "@var{how} should be specified\n"
1081 "using the values of the variables listed below. Multiple values can\n"
1082 "be combined using a bitwise or, in which case @code{#t} will only\n"
1083 "be returned if all accesses are granted.\n\n"
1084 "Permissions are checked using the real id of the current process,\n"
1085 "not the effective id, although it's the effective id which determines\n"
1086 "whether the access would actually be granted.\n\n"
1087 "@defvar R_OK\n"
1088 "test for read permission.\n"
1089 "@end defvar\n"
1090 "@defvar W_OK\n"
1091 "test for write permission.\n"
1092 "@end defvar\n"
1093 "@defvar X_OK\n"
1094 "test for execute permission.\n"
1095 "@end defvar\n"
1096 "@defvar F_OK\n"
1097 "test for existence of the file.\n"
1098 "@end defvar")
1bbd0b84 1099#define FUNC_NAME s_scm_access
0f2d19dd
JB
1100{
1101 int rv;
1102
3b3b36dd 1103 SCM_VALIDATE_ROSTRING (1,path);
0f2d19dd
JB
1104 if (SCM_SUBSTRP (path))
1105 path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0);
3b3b36dd 1106 SCM_VALIDATE_INUM (2,how);
0f2d19dd 1107 rv = access (SCM_ROCHARS (path), SCM_INUM (how));
156dcb09 1108 return SCM_NEGATE_BOOL(rv);
0f2d19dd 1109}
1bbd0b84 1110#undef FUNC_NAME
0f2d19dd 1111
a1ec6916 1112SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1bbd0b84 1113 (),
b380b885 1114 "Returns an integer representing the current process ID.")
1bbd0b84 1115#define FUNC_NAME s_scm_getpid
0f2d19dd
JB
1116{
1117 return SCM_MAKINUM ((unsigned long) getpid ());
1118}
1bbd0b84 1119#undef FUNC_NAME
0f2d19dd 1120
a1ec6916 1121SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1bbd0b84 1122 (SCM str),
b380b885
MD
1123 "Modifies the environment of the current process, which is\n"
1124 "also the default environment inherited by child processes.\n\n"
1125 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1126 "directly into the environment, replacing any existing environment string\n"
1127 "with\n"
1128 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1129 "sign, then any existing string with name matching @var{string} will\n"
1130 "be removed.\n\n"
1131 "The return value is unspecified.")
1bbd0b84 1132#define FUNC_NAME s_scm_putenv
0f2d19dd 1133{
f93ddd39 1134 int rv;
19468eff 1135 char *ptr;
f93ddd39 1136
3b3b36dd 1137 SCM_VALIDATE_ROSTRING (1,str);
19468eff
GH
1138 /* must make a new copy to be left in the environment, safe from gc. */
1139 ptr = malloc (SCM_LENGTH (str) + 1);
1140 if (ptr == NULL)
1bbd0b84 1141 SCM_MEMORY_ERROR;
89958ad0
JB
1142 strncpy (ptr, SCM_ROCHARS (str), SCM_LENGTH (str));
1143 ptr[SCM_LENGTH(str)] = 0;
19468eff 1144 rv = putenv (ptr);
f93ddd39 1145 if (rv < 0)
1bbd0b84 1146 SCM_SYSERROR;
f93ddd39 1147 return SCM_UNSPECIFIED;
0f2d19dd 1148}
1bbd0b84 1149#undef FUNC_NAME
0f2d19dd 1150
f25f761d 1151#ifdef HAVE_SETLOCALE
a1ec6916 1152SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1bbd0b84 1153 (SCM category, SCM locale),
b380b885
MD
1154 "If @var{locale} is omitted, returns the current value of the specified\n"
1155 "locale category \n"
1156 "as a system-dependent string.\n"
1157 "@var{category} should be specified using the values @code{LC_COLLATE},\n"
1158 "@code{LC_ALL} etc.\n\n"
1159 "Otherwise the specified locale category is set to\n"
1160 "the string @var{locale}\n"
1161 "and the new value is returned as a system-dependent string. If @var{locale}\n"
1162 "is an empty string, the locale will be set using envirionment variables.")
1bbd0b84 1163#define FUNC_NAME s_scm_setlocale
0f2d19dd 1164{
0f2d19dd
JB
1165 char *clocale;
1166 char *rv;
1167
3b3b36dd 1168 SCM_VALIDATE_INUM (1,category);
0f2d19dd
JB
1169 if (SCM_UNBNDP (locale))
1170 {
1171 clocale = NULL;
1172 }
1173 else
1174 {
3b3b36dd 1175 SCM_VALIDATE_ROSTRING (2,locale);
89958ad0
JB
1176 SCM_COERCE_SUBSTR (locale);
1177 clocale = SCM_ROCHARS (locale);
0f2d19dd
JB
1178 }
1179
1180 rv = setlocale (SCM_INUM (category), clocale);
02b754d3 1181 if (rv == NULL)
1bbd0b84 1182 SCM_SYSERROR;
02b754d3 1183 return scm_makfrom0str (rv);
0f2d19dd 1184}
1bbd0b84 1185#undef FUNC_NAME
f25f761d 1186#endif /* HAVE_SETLOCALE */
0f2d19dd 1187
f25f761d 1188#ifdef HAVE_MKNOD
a1ec6916 1189SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1bbd0b84 1190 (SCM path, SCM type, SCM perms, SCM dev),
b380b885
MD
1191 "Creates a new special file, such as a file corresponding to a device.\n"
1192 "@var{path} specifies the name of the file. @var{type} should\n"
1193 "be one of the following symbols:\n"
1194 "regular, directory, symlink, block-special, char-special,\n"
1195 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1196 "@var{dev} (an integer) specifies which device the special file refers\n"
1197 "to. Its exact interpretation depends on the kind of special file\n"
1198 "being created.\n\n"
1199 "E.g.,\n"
1200 "@example\n"
09831f94
NJ
1201 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1202 "@end example\n\n"
a3c8b9fc 1203 "The return value is unspecified.")
1bbd0b84 1204#define FUNC_NAME s_scm_mknod
0f2d19dd 1205{
0f2d19dd 1206 int val;
19468eff 1207 char *p;
82a76bdf 1208 int ctype = 0;
19468eff 1209
3b3b36dd
GB
1210 SCM_VALIDATE_ROSTRING (1,path);
1211 SCM_VALIDATE_SYMBOL (2,type);
1212 SCM_VALIDATE_INUM (3,perms);
1213 SCM_VALIDATE_INUM (4,dev);
89958ad0 1214 SCM_COERCE_SUBSTR (path);
19468eff 1215
86c991c2 1216 p = SCM_SYMBOL_CHARS (type);
19468eff
GH
1217 if (strcmp (p, "regular") == 0)
1218 ctype = S_IFREG;
1219 else if (strcmp (p, "directory") == 0)
1220 ctype = S_IFDIR;
1221 else if (strcmp (p, "symlink") == 0)
1222 ctype = S_IFLNK;
1223 else if (strcmp (p, "block-special") == 0)
1224 ctype = S_IFBLK;
1225 else if (strcmp (p, "char-special") == 0)
1226 ctype = S_IFCHR;
1227 else if (strcmp (p, "fifo") == 0)
1228 ctype = S_IFIFO;
1229 else if (strcmp (p, "socket") == 0)
1230 ctype = S_IFSOCK;
1231 else
1bbd0b84 1232 SCM_OUT_OF_RANGE (2,type);
19468eff 1233
19468eff
GH
1234 SCM_SYSCALL (val = mknod(SCM_ROCHARS(path), ctype | SCM_INUM (perms),
1235 SCM_INUM (dev)));
02b754d3 1236 if (val != 0)
1bbd0b84 1237 SCM_SYSERROR;
02b754d3 1238 return SCM_UNSPECIFIED;
0f2d19dd 1239}
1bbd0b84 1240#undef FUNC_NAME
f25f761d 1241#endif /* HAVE_MKNOD */
0f2d19dd 1242
f25f761d 1243#ifdef HAVE_NICE
a1ec6916 1244SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1bbd0b84 1245 (SCM incr),
b380b885
MD
1246 "Increment the priority of the current process by @var{incr}. A higher\n"
1247 "priority value means that the process runs less often.\n"
1248 "The return value is unspecified.")
1bbd0b84 1249#define FUNC_NAME s_scm_nice
0f2d19dd 1250{
3b3b36dd 1251 SCM_VALIDATE_INUM (1,incr);
02b754d3 1252 if (nice(SCM_INUM(incr)) != 0)
1bbd0b84 1253 SCM_SYSERROR;
02b754d3 1254 return SCM_UNSPECIFIED;
0f2d19dd 1255}
1bbd0b84 1256#undef FUNC_NAME
f25f761d 1257#endif /* HAVE_NICE */
0f2d19dd 1258
f25f761d 1259#ifdef HAVE_SYNC
a1ec6916 1260SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1bbd0b84 1261 (),
b380b885
MD
1262 "Flush the operating system disk buffers.\n"
1263 "The return value is unspecified.")
1bbd0b84 1264#define FUNC_NAME s_scm_sync
0f2d19dd 1265{
0f2d19dd 1266 sync();
127ec750 1267 return SCM_UNSPECIFIED;
0f2d19dd 1268}
1bbd0b84 1269#undef FUNC_NAME
f25f761d 1270#endif /* HAVE_SYNC */
0f2d19dd 1271
0f2d19dd
JB
1272void
1273scm_init_posix ()
0f2d19dd
JB
1274{
1275 scm_add_feature ("posix");
1276#ifdef HAVE_GETEUID
1277 scm_add_feature ("EIDs");
1278#endif
1279#ifdef WAIT_ANY
1280 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY));
1281#endif
1282#ifdef WAIT_MYPGRP
1283 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP));
1284#endif
1285#ifdef WNOHANG
1286 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG));
1287#endif
1288#ifdef WUNTRACED
1289 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED));
1290#endif
1291
0f2d19dd 1292 /* access() symbols. */
bab0f4e5
JB
1293 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK));
1294 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK));
1295 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK));
1296 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK));
0f2d19dd
JB
1297
1298#ifdef LC_COLLATE
1299 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE));
1300#endif
1301#ifdef LC_CTYPE
1302 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE));
1303#endif
1304#ifdef LC_MONETARY
1305 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY));
1306#endif
1307#ifdef LC_NUMERIC
1308 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC));
1309#endif
1310#ifdef LC_TIME
1311 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME));
1312#endif
1313#ifdef LC_MESSAGES
1314 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES));
1315#endif
1316#ifdef LC_ALL
1317 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL));
1318#endif
bd9e24b3
GH
1319#ifdef PIPE_BUF
1320scm_sysintern ("PIPE_BUF", scm_long2num (PIPE_BUF));
1321#endif
1322
a0599745
MD
1323#include "libguile/cpp_sig_symbols.c"
1324#include "libguile/posix.x"
0f2d19dd 1325}
89e00824
ML
1326
1327/*
1328 Local Variables:
1329 c-file-style: "gnu"
1330 End:
1331*/