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