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