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