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