add pointer->scm, scm->pointer
[bpt/guile.git] / libguile / posix.c
CommitLineData
b56c252b 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5 21#ifdef HAVE_CONFIG_H
2546b5c3
RB
22# include <config.h>
23#endif
0f2d19dd 24
3731192f 25#include <stdlib.h>
0f2d19dd 26#include <stdio.h>
e6e2e95a 27#include <errno.h>
889975e5 28#include <uniconv.h>
e6e2e95a 29
a0599745 30#include "libguile/_scm.h"
b28f5b3c 31#include "libguile/dynwind.h"
a0599745
MD
32#include "libguile/fports.h"
33#include "libguile/scmsigs.h"
34#include "libguile/feature.h"
35#include "libguile/strings.h"
c44ca4fe 36#include "libguile/srfi-13.h"
a17d2654 37#include "libguile/srfi-14.h"
a0599745 38#include "libguile/vectors.h"
4ea9429e 39#include "libguile/values.h"
a0599745
MD
40
41#include "libguile/validate.h"
42#include "libguile/posix.h"
b89c4943 43#include "libguile/gettext.h"
9de87eea 44#include "libguile/threads.h"
0f2d19dd
JB
45\f
46
02b754d3
GH
47#ifdef HAVE_STRING_H
48#include <string.h>
49#endif
0f2d19dd
JB
50#ifdef TIME_WITH_SYS_TIME
51# include <sys/time.h>
52# include <time.h>
53#else
54# if HAVE_SYS_TIME_H
55# include <sys/time.h>
56# else
57# include <time.h>
58# endif
59#endif
60
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
95b88819
GH
63#else
64#ifndef ttyname
65extern char *ttyname();
66#endif
0f2d19dd
JB
67#endif
68
3594582b 69#ifdef LIBC_H_WITH_UNISTD_H
bab0f4e5
JB
70#include <libc.h>
71#endif
72
8cc71382 73#include <sys/types.h>
0f2d19dd
JB
74#include <sys/stat.h>
75#include <fcntl.h>
76
82893676 77#ifdef HAVE_PWD_H
0f2d19dd 78#include <pwd.h>
82893676
MG
79#endif
80#ifdef HAVE_IO_H
81#include <io.h>
82#endif
f87c105a 83#ifdef HAVE_WINSOCK2_H
82893676
MG
84#include <winsock2.h>
85#endif
86
87#ifdef __MINGW32__
88/* Some defines for Windows here. */
7beabedb 89# include <process.h>
82893676
MG
90# define pipe(fd) _pipe (fd, 256, O_BINARY)
91#endif /* __MINGW32__ */
0f2d19dd
JB
92
93#if HAVE_SYS_WAIT_H
94# include <sys/wait.h>
95#endif
96#ifndef WEXITSTATUS
97# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
98#endif
99#ifndef WIFEXITED
100# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
101#endif
102
103#include <signal.h>
104
82893676 105#ifdef HAVE_GRP_H
0f2d19dd 106#include <grp.h>
82893676
MG
107#endif
108#ifdef HAVE_SYS_UTSNAME_H
0f2d19dd 109#include <sys/utsname.h>
82893676 110#endif
0f2d19dd 111
0f2d19dd
JB
112#ifdef HAVE_SETLOCALE
113#include <locale.h>
114#endif
115
b89c4943
LC
116#if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
117# define USE_GNU_LOCALE_API
118#endif
119
a2f00b9b
LC
120#if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
121# include <xlocale.h>
122#endif
123
534bbcc1 124#ifdef HAVE_CRYPT_H
94e6d793
MG
125# include <crypt.h>
126#endif
127
0eaf1055
KR
128#ifdef HAVE_NETDB_H
129#include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
130#endif
131
132#ifdef HAVE_SYS_PARAM_H
133#include <sys/param.h> /* for MAXHOSTNAMELEN */
134#endif
135
94e6d793
MG
136#if HAVE_SYS_RESOURCE_H
137# include <sys/resource.h>
138#endif
139
837b0ae0 140#include <sys/file.h> /* from Gnulib */
f0c0141f 141#include <nproc.h>
eb7e1603 142
bab0f4e5
JB
143/* Some Unix systems don't define these. CPP hair is dangerous, but
144 this seems safe enough... */
145#ifndef R_OK
146#define R_OK 4
147#endif
148
149#ifndef W_OK
150#define W_OK 2
151#endif
152
153#ifndef X_OK
154#define X_OK 1
155#endif
156
157#ifndef F_OK
158#define F_OK 0
159#endif
398609a5 160
8ab3d8a0
KR
161/* No prototype for this on Solaris 10. The man page says it's in
162 <unistd.h> ... but it lies. */
163#if ! HAVE_DECL_SETHOSTNAME
164int sethostname (char *name, size_t namelen);
165#endif
166
398609a5
JB
167/* On NextStep, <utime.h> doesn't define struct utime, unless we
168 #define _POSIX_SOURCE before #including it. I think this is less
169 of a kludge than defining struct utimbuf ourselves. */
170#ifdef UTIMBUF_NEEDS_POSIX
171#define _POSIX_SOURCE
172#endif
173
174#ifdef HAVE_SYS_UTIME_H
175#include <sys/utime.h>
176#endif
177
178#ifdef HAVE_UTIME_H
179#include <utime.h>
180#endif
181
182/* Please don't add any more #includes or #defines here. The hack
183 above means that _POSIX_SOURCE may be #defined, which will
eb7e1603
KR
184 encourage header files to do strange things.
185
186 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
187
188 FIXME: Probably should do all the includes first, then all the fallback
189 declarations and defines, in case things are not in the header we
190 imagine. */
191
192
193
194
0f2d19dd 195\f
f015614a
MV
196
197/* Two often used patterns
198 */
199
200#define WITH_STRING(str,cstr,code) \
201 do { \
202 char *cstr = scm_to_locale_string (str); \
203 code; \
204 free (cstr); \
205 } while (0)
206
207#define STRING_SYSCALL(str,cstr,code) \
208 do { \
209 int eno; \
210 char *cstr = scm_to_locale_string (str); \
211 SCM_SYSCALL (code); \
212 eno = errno; free (cstr); errno = eno; \
213 } while (0)
214
215
216\f
bc45012d
JB
217SCM_SYMBOL (sym_read_pipe, "read pipe");
218SCM_SYMBOL (sym_write_pipe, "write pipe");
0f2d19dd 219
a1ec6916 220SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
1bbd0b84 221 (),
1e6808ea
MG
222 "Return a newly created pipe: a pair of ports which are linked\n"
223 "together on the local machine. The @emph{car} is the input\n"
224 "port and the @emph{cdr} is the output port. Data written (and\n"
225 "flushed) to the output port can be read from the input port.\n"
226 "Pipes are commonly used for communication with a newly forked\n"
227 "child process. The need to flush the output port can be\n"
228 "avoided by making it unbuffered using @code{setvbuf}.\n"
229 "\n"
230 "Writes occur atomically provided the size of the data in bytes\n"
231 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
232 "the output port is likely to block if too much data (typically\n"
233 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
234 "from the input port.")
1bbd0b84 235#define FUNC_NAME s_scm_pipe
0f2d19dd
JB
236{
237 int fd[2], rv;
0f2d19dd 238 SCM p_rd, p_wt;
02b754d3 239
0f2d19dd
JB
240 rv = pipe (fd);
241 if (rv)
1bbd0b84 242 SCM_SYSERROR;
ee149d03
JB
243
244 p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
245 p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
0f2d19dd
JB
246 return scm_cons (p_rd, p_wt);
247}
1bbd0b84 248#undef FUNC_NAME
0f2d19dd
JB
249
250
0e958795 251#ifdef HAVE_GETGROUPS
a1ec6916 252SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
1bbd0b84 253 (),
1e6808ea 254 "Return a vector of integers representing the current\n"
bb2c02f2 255 "supplementary group IDs.")
1bbd0b84 256#define FUNC_NAME s_scm_getgroups
0f2d19dd 257{
1d1559ce 258 SCM result;
66460dfb 259 int ngroups;
1be6b49c 260 size_t size;
66460dfb
DH
261 GETGROUPS_T *groups;
262
263 ngroups = getgroups (0, NULL);
264 if (ngroups <= 0)
1bbd0b84 265 SCM_SYSERROR;
66460dfb
DH
266
267 size = ngroups * sizeof (GETGROUPS_T);
4c9419ac 268 groups = scm_malloc (size);
634aa8de 269 ngroups = getgroups (ngroups, groups);
66460dfb 270
f360a962
MV
271 result = scm_c_make_vector (ngroups, SCM_BOOL_F);
272 while (--ngroups >= 0)
4057a3e0 273 SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups]));
66460dfb 274
4c9419ac 275 free (groups);
1d1559ce 276 return result;
1bbd0b84
GB
277}
278#undef FUNC_NAME
0e958795 279#endif
0f2d19dd 280
f360a962
MV
281#ifdef HAVE_SETGROUPS
282SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
283 (SCM group_vec),
fcc3adc2
KR
284 "Set the current set of supplementary group IDs to the integers\n"
285 "in the given vector @var{vec}. The return value is\n"
286 "unspecified.\n"
287 "\n"
288 "Generally only the superuser can set the process group IDs.")
f360a962
MV
289#define FUNC_NAME s_scm_setgroups
290{
291 size_t ngroups;
292 size_t size;
293 size_t i;
294 int result;
295 int save_errno;
296 GETGROUPS_T *groups;
297
298 SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
299
4057a3e0 300 ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
f360a962
MV
301
302 /* validate before allocating, so we don't have to worry about leaks */
303 for (i = 0; i < ngroups; i++)
304 {
305 unsigned long ulong_gid;
306 GETGROUPS_T gid;
4057a3e0
MV
307 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
308 ulong_gid);
f360a962
MV
309 gid = ulong_gid;
310 if (gid != ulong_gid)
4057a3e0 311 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
f360a962
MV
312 }
313
314 size = ngroups * sizeof (GETGROUPS_T);
2eb78d06 315 if (size / sizeof (GETGROUPS_T) != ngroups)
e11e83f3 316 SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups));
f360a962
MV
317 groups = scm_malloc (size);
318 for(i = 0; i < ngroups; i++)
4057a3e0 319 groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
f360a962
MV
320
321 result = setgroups (ngroups, groups);
322 save_errno = errno; /* don't let free() touch errno */
323 free (groups);
324 errno = save_errno;
325 if (result < 0)
326 SCM_SYSERROR;
327 return SCM_UNSPECIFIED;
328}
329#undef FUNC_NAME
330#endif
331
82893676 332#ifdef HAVE_GETPWENT
a1ec6916 333SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
1bbd0b84 334 (SCM user),
b380b885
MD
335 "Look up an entry in the user database. @var{obj} can be an integer,\n"
336 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
337 "or getpwent respectively.")
1bbd0b84 338#define FUNC_NAME s_scm_getpwuid
0f2d19dd 339{
0f2d19dd 340 struct passwd *entry;
0f2d19dd 341
1d1559ce 342 SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
7888309b 343 if (SCM_UNBNDP (user) || scm_is_false (user))
0f2d19dd 344 {
0f2d19dd 345 SCM_SYSCALL (entry = getpwent ());
3d781d49
JB
346 if (! entry)
347 {
3d781d49
JB
348 return SCM_BOOL_F;
349 }
0f2d19dd 350 }
e11e83f3 351 else if (scm_is_integer (user))
0f2d19dd 352 {
e11e83f3 353 entry = getpwuid (scm_to_int (user));
0f2d19dd
JB
354 }
355 else
356 {
f015614a
MV
357 WITH_STRING (user, c_user,
358 entry = getpwnam (c_user));
0f2d19dd
JB
359 }
360 if (!entry)
1bbd0b84 361 SCM_MISC_ERROR ("entry not found", SCM_EOL);
02b754d3 362
4057a3e0
MV
363 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name));
364 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd));
365 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid));
366 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid));
367 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos));
0f2d19dd 368 if (!entry->pw_dir)
4057a3e0 369 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
0f2d19dd 370 else
4057a3e0 371 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir));
0f2d19dd 372 if (!entry->pw_shell)
4057a3e0 373 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (""));
0f2d19dd 374 else
4057a3e0 375 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
1d1559ce 376 return result;
0f2d19dd 377}
1bbd0b84 378#undef FUNC_NAME
82893676 379#endif /* HAVE_GETPWENT */
0f2d19dd
JB
380
381
0e958795 382#ifdef HAVE_SETPWENT
a1ec6916 383SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
1bbd0b84 384 (SCM arg),
b380b885
MD
385 "If called with a true argument, initialize or reset the password data\n"
386 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
387 "@code{endpwent} procedures are implemented on top of this.")
1bbd0b84 388#define FUNC_NAME s_scm_setpwent
0f2d19dd 389{
7888309b 390 if (SCM_UNBNDP (arg) || scm_is_false (arg))
0f2d19dd
JB
391 endpwent ();
392 else
393 setpwent ();
394 return SCM_UNSPECIFIED;
395}
1bbd0b84 396#undef FUNC_NAME
0e958795 397#endif
0f2d19dd
JB
398
399
82893676 400#ifdef HAVE_GETGRENT
0f2d19dd 401/* Combines getgrgid and getgrnam. */
a1ec6916 402SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
1bbd0b84 403 (SCM name),
b380b885
MD
404 "Look up an entry in the group database. @var{obj} can be an integer,\n"
405 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
406 "or getgrent respectively.")
1bbd0b84 407#define FUNC_NAME s_scm_getgrgid
0f2d19dd 408{
0f2d19dd 409 struct group *entry;
1d1559ce 410 SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
34d19ef6 411
7888309b 412 if (SCM_UNBNDP (name) || scm_is_false (name))
3d781d49
JB
413 {
414 SCM_SYSCALL (entry = getgrent ());
415 if (! entry)
416 {
3d781d49
JB
417 return SCM_BOOL_F;
418 }
419 }
e11e83f3
MV
420 else if (scm_is_integer (name))
421 SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
0f2d19dd 422 else
f015614a
MV
423 STRING_SYSCALL (name, c_name,
424 entry = getgrnam (c_name));
0f2d19dd 425 if (!entry)
1bbd0b84 426 SCM_SYSERROR;
02b754d3 427
4057a3e0
MV
428 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name));
429 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd));
430 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid));
431 SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem));
1d1559ce 432 return result;
0f2d19dd 433}
1bbd0b84 434#undef FUNC_NAME
0f2d19dd
JB
435
436
437
a1ec6916 438SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
1bbd0b84 439 (SCM arg),
b380b885
MD
440 "If called with a true argument, initialize or reset the group data\n"
441 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
442 "@code{endgrent} procedures are implemented on top of this.")
1bbd0b84 443#define FUNC_NAME s_scm_setgrent
0f2d19dd 444{
7888309b 445 if (SCM_UNBNDP (arg) || scm_is_false (arg))
0f2d19dd
JB
446 endgrent ();
447 else
448 setgrent ();
449 return SCM_UNSPECIFIED;
450}
1bbd0b84 451#undef FUNC_NAME
82893676 452#endif /* HAVE_GETGRENT */
0f2d19dd
JB
453
454
4ea9429e
AW
455#ifdef HAVE_GETRLIMIT
456#ifdef RLIMIT_AS
457SCM_SYMBOL (sym_as, "as");
458#endif
459#ifdef RLIMIT_CORE
460SCM_SYMBOL (sym_core, "core");
461#endif
462#ifdef RLIMIT_CPU
463SCM_SYMBOL (sym_cpu, "cpu");
464#endif
465#ifdef RLIMIT_DATA
466SCM_SYMBOL (sym_data, "data");
467#endif
468#ifdef RLIMIT_FSIZE
469SCM_SYMBOL (sym_fsize, "fsize");
470#endif
471#ifdef RLIMIT_MEMLOCK
472SCM_SYMBOL (sym_memlock, "memlock");
473#endif
474#ifdef RLIMIT_MSGQUEUE
475SCM_SYMBOL (sym_msgqueue, "msgqueue");
476#endif
477#ifdef RLIMIT_NICE
478SCM_SYMBOL (sym_nice, "nice");
479#endif
480#ifdef RLIMIT_NOFILE
481SCM_SYMBOL (sym_nofile, "nofile");
482#endif
483#ifdef RLIMIT_NPROC
484SCM_SYMBOL (sym_nproc, "nproc");
485#endif
486#ifdef RLIMIT_RSS
487SCM_SYMBOL (sym_rss, "rss");
488#endif
489#ifdef RLIMIT_RTPRIO
490SCM_SYMBOL (sym_rtprio, "rtprio");
491#endif
492#ifdef RLIMIT_RTPRIO
493SCM_SYMBOL (sym_rttime, "rttime");
494#endif
495#ifdef RLIMIT_SIGPENDING
496SCM_SYMBOL (sym_sigpending, "sigpending");
497#endif
498#ifdef RLIMIT_STACK
499SCM_SYMBOL (sym_stack, "stack");
500#endif
501
502static int
503scm_to_resource (SCM s, const char *func, int pos)
504{
505 if (scm_is_number (s))
506 return scm_to_int (s);
507
508 SCM_ASSERT_TYPE (scm_is_symbol (s), s, pos, func, "symbol");
509
510#ifdef RLIMIT_AS
511 if (s == sym_as)
512 return RLIMIT_AS;
513#endif
514#ifdef RLIMIT_CORE
515 if (s == sym_core)
516 return RLIMIT_CORE;
517#endif
518#ifdef RLIMIT_CPU
519 if (s == sym_cpu)
520 return RLIMIT_CPU;
521#endif
522#ifdef RLIMIT_DATA
523 if (s == sym_data)
524 return RLIMIT_DATA;
525#endif
526#ifdef RLIMIT_FSIZE
527 if (s == sym_fsize)
528 return RLIMIT_FSIZE;
529#endif
530#ifdef RLIMIT_MEMLOCK
531 if (s == sym_memlock)
532 return RLIMIT_MEMLOCK;
533#endif
534#ifdef RLIMIT_MSGQUEUE
535 if (s == sym_msgqueue)
536 return RLIMIT_MSGQUEUE;
537#endif
538#ifdef RLIMIT_NICE
539 if (s == sym_nice)
540 return RLIMIT_NICE;
541#endif
542#ifdef RLIMIT_NOFILE
543 if (s == sym_nofile)
544 return RLIMIT_NOFILE;
545#endif
546#ifdef RLIMIT_NPROC
547 if (s == sym_nproc)
548 return RLIMIT_NPROC;
549#endif
550#ifdef RLIMIT_RSS
551 if (s == sym_rss)
552 return RLIMIT_RSS;
553#endif
554#ifdef RLIMIT_RTPRIO
555 if (s == sym_rtprio)
556 return RLIMIT_RTPRIO;
557#endif
558#ifdef RLIMIT_RTPRIO
559 if (s == sym_rttime)
560 return RLIMIT_RTPRIO;
561#endif
562#ifdef RLIMIT_SIGPENDING
563 if (s == sym_sigpending)
564 return RLIMIT_SIGPENDING;
565#endif
566#ifdef RLIMIT_STACK
567 if (s == sym_stack)
568 return RLIMIT_STACK;
569#endif
570
571 scm_misc_error (func, "invalid rlimit resource ~A", scm_list_1 (s));
572 return 0;
573}
574
575SCM_DEFINE (scm_getrlimit, "getrlimit", 1, 0, 0,
576 (SCM resource),
577 "Get a resource limit for this process. @var{resource} identifies the resource,\n"
578 "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
579 "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
580 "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
581 "limit is set for the resource in question, the returned limit will be @code{#f}.")
582#define FUNC_NAME s_scm_getrlimit
583{
584 int iresource;
585 struct rlimit lim = { 0, 0 };
586
587 iresource = scm_to_resource (resource, FUNC_NAME, 1);
588
589 if (getrlimit (iresource, &lim) != 0)
590 scm_syserror (FUNC_NAME);
591
592 return scm_values (scm_list_2 ((lim.rlim_cur == RLIM_INFINITY) ? SCM_BOOL_F
593 : scm_from_long (lim.rlim_cur),
594 (lim.rlim_max == RLIM_INFINITY) ? SCM_BOOL_F
595 : scm_from_long (lim.rlim_max)));
596}
597#undef FUNC_NAME
598
599
600#ifdef HAVE_SETRLIMIT
601SCM_DEFINE (scm_setrlimit, "setrlimit", 3, 0, 0,
602 (SCM resource, SCM soft, SCM hard),
603 "Set a resource limit for this process. @var{resource} identifies the resource,\n"
604 "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
605 "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
606 "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
607 "limit to 150 kilobytes, with a hard limit of 300 kB.")
608#define FUNC_NAME s_scm_setrlimit
609{
610 int iresource;
611 struct rlimit lim = { 0, 0 };
612
613 iresource = scm_to_resource (resource, FUNC_NAME, 1);
614
615 lim.rlim_cur = (soft == SCM_BOOL_F) ? RLIM_INFINITY : scm_to_long (soft);
616 lim.rlim_max = (hard == SCM_BOOL_F) ? RLIM_INFINITY : scm_to_long (hard);
617
618 if (setrlimit (iresource, &lim) != 0)
619 scm_syserror (FUNC_NAME);
620
621 return SCM_UNSPECIFIED;
622}
623#undef FUNC_NAME
624#endif /* HAVE_SETRLIMIT */
625#endif /* HAVE_GETRLIMIT */
626
627
a1ec6916 628SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
1bbd0b84 629 (SCM pid, SCM sig),
b380b885
MD
630 "Sends a signal to the specified process or group of processes.\n\n"
631 "@var{pid} specifies the processes to which the signal is sent:\n\n"
632 "@table @r\n"
633 "@item @var{pid} greater than 0\n"
634 "The process whose identifier is @var{pid}.\n"
635 "@item @var{pid} equal to 0\n"
636 "All processes in the current process group.\n"
637 "@item @var{pid} less than -1\n"
638 "The process group whose identifier is -@var{pid}\n"
639 "@item @var{pid} equal to -1\n"
640 "If the process is privileged, all processes except for some special\n"
641 "system processes. Otherwise, all processes with the current effective\n"
642 "user ID.\n"
643 "@end table\n\n"
644 "@var{sig} should be specified using a variable corresponding to\n"
645 "the Unix symbolic name, e.g.,\n\n"
646 "@defvar SIGHUP\n"
647 "Hang-up signal.\n"
648 "@end defvar\n\n"
649 "@defvar SIGINT\n"
650 "Interrupt signal.\n"
651 "@end defvar")
1bbd0b84 652#define FUNC_NAME s_scm_kill
0f2d19dd 653{
0f2d19dd 654 /* Signal values are interned in scm_init_posix(). */
82893676 655#ifdef HAVE_KILL
a55c2b68 656 if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
23d72566 657 SCM_SYSERROR;
82893676 658#else
23d72566
KR
659 /* Mingw has raise(), but not kill(). (Other raw DOS environments might
660 be similar.) Use raise() when the requested pid is our own process,
661 otherwise bomb. */
a55c2b68 662 if (scm_to_int (pid) == getpid ())
23d72566
KR
663 {
664 if (raise (scm_to_int (sig)) != 0)
665 {
666 err:
667 SCM_SYSERROR;
668 }
669 else
670 {
671 errno = ENOSYS;
672 goto err;
673 }
674 }
82893676 675#endif
02b754d3 676 return SCM_UNSPECIFIED;
0f2d19dd 677}
1bbd0b84 678#undef FUNC_NAME
0f2d19dd 679
d00ae47e 680#ifdef HAVE_WAITPID
a1ec6916 681SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
1bbd0b84 682 (SCM pid, SCM options),
b380b885
MD
683 "This procedure collects status information from a child process which\n"
684 "has terminated or (optionally) stopped. Normally it will\n"
685 "suspend the calling process until this can be done. If more than one\n"
686 "child process is eligible then one will be chosen by the operating system.\n\n"
687 "The value of @var{pid} determines the behaviour:\n\n"
688 "@table @r\n"
689 "@item @var{pid} greater than 0\n"
690 "Request status information from the specified child process.\n"
691 "@item @var{pid} equal to -1 or WAIT_ANY\n"
692 "Request status information for any child process.\n"
693 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
694 "Request status information for any child process in the current process\n"
695 "group.\n"
696 "@item @var{pid} less than -1\n"
697 "Request status information for any child process whose process group ID\n"
698 "is -@var{PID}.\n"
699 "@end table\n\n"
700 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
701 "values of zero or more of the following variables:\n\n"
702 "@defvar WNOHANG\n"
703 "Return immediately even if there are no child processes to be collected.\n"
704 "@end defvar\n\n"
705 "@defvar WUNTRACED\n"
706 "Report status information for stopped processes as well as terminated\n"
707 "processes.\n"
708 "@end defvar\n\n"
709 "The return value is a pair containing:\n\n"
710 "@enumerate\n"
711 "@item\n"
712 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
713 "specified and no process was collected.\n"
714 "@item\n"
715 "The integer status value.\n"
716 "@end enumerate")
1bbd0b84 717#define FUNC_NAME s_scm_waitpid
0f2d19dd
JB
718{
719 int i;
720 int status;
721 int ioptions;
0f2d19dd
JB
722 if (SCM_UNBNDP (options))
723 ioptions = 0;
724 else
725 {
0f2d19dd 726 /* Flags are interned in scm_init_posix. */
a55c2b68 727 ioptions = scm_to_int (options);
0f2d19dd 728 }
a55c2b68 729 SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
02b754d3 730 if (i == -1)
1bbd0b84 731 SCM_SYSERROR;
a55c2b68 732 return scm_cons (scm_from_int (i), scm_from_int (status));
0f2d19dd 733}
1bbd0b84 734#undef FUNC_NAME
d00ae47e 735#endif /* HAVE_WAITPID */
0f2d19dd 736
82893676 737#ifndef __MINGW32__
a1ec6916 738SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
1bbd0b84 739 (SCM status),
1e6808ea
MG
740 "Return the exit status value, as would be set if a process\n"
741 "ended normally through a call to @code{exit} or @code{_exit},\n"
742 "if any, otherwise @code{#f}.")
1bbd0b84 743#define FUNC_NAME s_scm_status_exit_val
67ec3667 744{
e67dc2be
JB
745 int lstatus;
746
e67dc2be 747 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
e11e83f3 748 go figure. */
a55c2b68 749 lstatus = scm_to_int (status);
e67dc2be 750 if (WIFEXITED (lstatus))
a55c2b68 751 return (scm_from_int (WEXITSTATUS (lstatus)));
67ec3667
GH
752 else
753 return SCM_BOOL_F;
754}
1bbd0b84 755#undef FUNC_NAME
e67dc2be 756
a1ec6916 757SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
1bbd0b84 758 (SCM status),
1e6808ea
MG
759 "Return the signal number which terminated the process, if any,\n"
760 "otherwise @code{#f}.")
1bbd0b84 761#define FUNC_NAME s_scm_status_term_sig
67ec3667 762{
e67dc2be
JB
763 int lstatus;
764
a55c2b68 765 lstatus = scm_to_int (status);
e67dc2be 766 if (WIFSIGNALED (lstatus))
a55c2b68 767 return scm_from_int (WTERMSIG (lstatus));
67ec3667
GH
768 else
769 return SCM_BOOL_F;
770}
1bbd0b84 771#undef FUNC_NAME
0f2d19dd 772
a1ec6916 773SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
1bbd0b84 774 (SCM status),
1e6808ea
MG
775 "Return the signal number which stopped the process, if any,\n"
776 "otherwise @code{#f}.")
1bbd0b84 777#define FUNC_NAME s_scm_status_stop_sig
67ec3667 778{
e67dc2be
JB
779 int lstatus;
780
a55c2b68 781 lstatus = scm_to_int (status);
e67dc2be 782 if (WIFSTOPPED (lstatus))
a55c2b68 783 return scm_from_int (WSTOPSIG (lstatus));
67ec3667
GH
784 else
785 return SCM_BOOL_F;
786}
1bbd0b84 787#undef FUNC_NAME
82893676 788#endif /* __MINGW32__ */
0f2d19dd 789
82893676 790#ifdef HAVE_GETPPID
a1ec6916 791SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
1bbd0b84 792 (),
1e6808ea
MG
793 "Return an integer representing the process ID of the parent\n"
794 "process.")
1bbd0b84 795#define FUNC_NAME s_scm_getppid
0f2d19dd 796{
e11e83f3 797 return scm_from_int (getppid ());
0f2d19dd 798}
1bbd0b84 799#undef FUNC_NAME
82893676 800#endif /* HAVE_GETPPID */
0f2d19dd
JB
801
802
82893676 803#ifndef __MINGW32__
a1ec6916 804SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
1bbd0b84 805 (),
1e6808ea 806 "Return an integer representing the current real user ID.")
1bbd0b84 807#define FUNC_NAME s_scm_getuid
0f2d19dd 808{
e11e83f3 809 return scm_from_int (getuid ());
0f2d19dd 810}
1bbd0b84 811#undef FUNC_NAME
0f2d19dd
JB
812
813
814
a1ec6916 815SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
1bbd0b84 816 (),
1e6808ea 817 "Return an integer representing the current real group ID.")
1bbd0b84 818#define FUNC_NAME s_scm_getgid
0f2d19dd 819{
e11e83f3 820 return scm_from_int (getgid ());
0f2d19dd 821}
1bbd0b84 822#undef FUNC_NAME
0f2d19dd
JB
823
824
825
a1ec6916 826SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
1bbd0b84 827 (),
1e6808ea 828 "Return an integer representing the current effective user ID.\n"
b380b885 829 "If the system does not support effective IDs, then the real ID\n"
480fa28d 830 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
1e6808ea 831 "system supports effective IDs.")
1bbd0b84 832#define FUNC_NAME s_scm_geteuid
0f2d19dd
JB
833{
834#ifdef HAVE_GETEUID
e11e83f3 835 return scm_from_int (geteuid ());
0f2d19dd 836#else
e11e83f3 837 return scm_from_int (getuid ());
0f2d19dd
JB
838#endif
839}
1bbd0b84 840#undef FUNC_NAME
0f2d19dd
JB
841
842
a1ec6916 843SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
1bbd0b84 844 (),
1e6808ea 845 "Return an integer representing the current effective group ID.\n"
b380b885 846 "If the system does not support effective IDs, then the real ID\n"
480fa28d 847 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
1e6808ea 848 "system supports effective IDs.")
1bbd0b84 849#define FUNC_NAME s_scm_getegid
0f2d19dd
JB
850{
851#ifdef HAVE_GETEUID
e11e83f3 852 return scm_from_int (getegid ());
0f2d19dd 853#else
e11e83f3 854 return scm_from_int (getgid ());
0f2d19dd
JB
855#endif
856}
1bbd0b84 857#undef FUNC_NAME
0f2d19dd
JB
858
859
a1ec6916 860SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
1bbd0b84 861 (SCM id),
b380b885
MD
862 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
863 "the process has appropriate privileges.\n"
864 "The return value is unspecified.")
1bbd0b84 865#define FUNC_NAME s_scm_setuid
0f2d19dd 866{
a55c2b68 867 if (setuid (scm_to_int (id)) != 0)
1bbd0b84 868 SCM_SYSERROR;
02b754d3 869 return SCM_UNSPECIFIED;
0f2d19dd 870}
1bbd0b84 871#undef FUNC_NAME
0f2d19dd 872
a1ec6916 873SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
1bbd0b84 874 (SCM id),
b380b885
MD
875 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
876 "the process has appropriate privileges.\n"
877 "The return value is unspecified.")
1bbd0b84 878#define FUNC_NAME s_scm_setgid
0f2d19dd 879{
a55c2b68 880 if (setgid (scm_to_int (id)) != 0)
1bbd0b84 881 SCM_SYSERROR;
02b754d3 882 return SCM_UNSPECIFIED;
0f2d19dd 883}
1bbd0b84 884#undef FUNC_NAME
0f2d19dd 885
a1ec6916 886SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
1bbd0b84 887 (SCM id),
b380b885
MD
888 "Sets the effective user ID to the integer @var{id}, provided the process\n"
889 "has appropriate privileges. If effective IDs are not supported, the\n"
480fa28d 890 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
b380b885
MD
891 "system supports effective IDs.\n"
892 "The return value is unspecified.")
1bbd0b84 893#define FUNC_NAME s_scm_seteuid
0f2d19dd 894{
02b754d3
GH
895 int rv;
896
0f2d19dd 897#ifdef HAVE_SETEUID
a55c2b68 898 rv = seteuid (scm_to_int (id));
0f2d19dd 899#else
a55c2b68 900 rv = setuid (scm_to_int (id));
0f2d19dd 901#endif
02b754d3 902 if (rv != 0)
1bbd0b84 903 SCM_SYSERROR;
02b754d3 904 return SCM_UNSPECIFIED;
0f2d19dd 905}
1bbd0b84 906#undef FUNC_NAME
7beabedb
MG
907#endif /* __MINGW32__ */
908
0f2d19dd 909
0e958795 910#ifdef HAVE_SETEGID
a1ec6916 911SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
1bbd0b84 912 (SCM id),
b380b885
MD
913 "Sets the effective group ID to the integer @var{id}, provided the process\n"
914 "has appropriate privileges. If effective IDs are not supported, the\n"
480fa28d 915 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
b380b885
MD
916 "system supports effective IDs.\n"
917 "The return value is unspecified.")
1bbd0b84 918#define FUNC_NAME s_scm_setegid
0f2d19dd 919{
02b754d3
GH
920 int rv;
921
0f2d19dd 922#ifdef HAVE_SETEUID
a55c2b68 923 rv = setegid (scm_to_int (id));
0f2d19dd 924#else
a55c2b68 925 rv = setgid (scm_to_int (id));
0f2d19dd 926#endif
02b754d3 927 if (rv != 0)
1bbd0b84 928 SCM_SYSERROR;
02b754d3
GH
929 return SCM_UNSPECIFIED;
930
0f2d19dd 931}
1bbd0b84 932#undef FUNC_NAME
0e958795 933#endif
0f2d19dd 934
82893676
MG
935
936#ifdef HAVE_GETPGRP
a1ec6916 937SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
1bbd0b84 938 (),
1e6808ea 939 "Return an integer representing the current process group ID.\n"
b380b885 940 "This is the POSIX definition, not BSD.")
1bbd0b84 941#define FUNC_NAME s_scm_getpgrp
0f2d19dd
JB
942{
943 int (*fn)();
4625e44f 944 fn = (int (*) ()) getpgrp;
e11e83f3 945 return scm_from_int (fn (0));
0f2d19dd 946}
1bbd0b84 947#undef FUNC_NAME
82893676
MG
948#endif /* HAVE_GETPGRP */
949
0f2d19dd 950
f25f761d 951#ifdef HAVE_SETPGID
a1ec6916 952SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
1bbd0b84 953 (SCM pid, SCM pgid),
b380b885
MD
954 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
955 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
956 "current process.\n"
957 "Fails on systems that do not support job control.\n"
958 "The return value is unspecified.")
1bbd0b84 959#define FUNC_NAME s_scm_setpgid
0f2d19dd 960{
02b754d3 961 /* FIXME(?): may be known as setpgrp. */
a55c2b68 962 if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
1bbd0b84 963 SCM_SYSERROR;
02b754d3 964 return SCM_UNSPECIFIED;
0f2d19dd 965}
1bbd0b84 966#undef FUNC_NAME
f25f761d 967#endif /* HAVE_SETPGID */
0f2d19dd 968
f25f761d 969#ifdef HAVE_SETSID
a1ec6916 970SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
1bbd0b84 971 (),
b380b885
MD
972 "Creates a new session. The current process becomes the session leader\n"
973 "and is put in a new process group. The process will be detached\n"
974 "from its controlling terminal if it has one.\n"
975 "The return value is an integer representing the new process group ID.")
1bbd0b84 976#define FUNC_NAME s_scm_setsid
0f2d19dd
JB
977{
978 pid_t sid = setsid ();
02b754d3 979 if (sid == -1)
1bbd0b84 980 SCM_SYSERROR;
02b754d3 981 return SCM_UNSPECIFIED;
0f2d19dd 982}
1bbd0b84 983#undef FUNC_NAME
f25f761d 984#endif /* HAVE_SETSID */
0f2d19dd 985
3e5aed1c
NJ
986#ifdef HAVE_GETSID
987SCM_DEFINE (scm_getsid, "getsid", 1, 0, 0,
988 (SCM pid),
989 "Returns the session ID of process @var{pid}. (The session\n"
990 "ID of a process is the process group ID of its session leader.)")
991#define FUNC_NAME s_scm_getsid
992{
993 return scm_from_int (getsid (scm_to_int (pid)));
994}
995#undef FUNC_NAME
996#endif /* HAVE_GETSID */
997
e8a59063
KR
998
999/* ttyname returns its result in a single static buffer, hence
1000 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
1001 continuously calling ttyname will otherwise get an overwrite quite
1002 easily.
1003
1004 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
1005 there's probably little to be gained in either speed or parallelism. */
1006
82893676 1007#ifdef HAVE_TTYNAME
a1ec6916 1008SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
1bbd0b84 1009 (SCM port),
1e6808ea
MG
1010 "Return a string with the name of the serial terminal device\n"
1011 "underlying @var{port}.")
1bbd0b84 1012#define FUNC_NAME s_scm_ttyname
0f2d19dd 1013{
1d1559ce 1014 char *result;
e8a59063 1015 int fd, err;
a1ef7406 1016 SCM ret = SCM_BOOL_F;
78446828
MV
1017
1018 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1019 SCM_VALIDATE_OPPORT (1, port);
a98bddfd 1020 if (!SCM_FPORTP (port))
0f2d19dd 1021 return SCM_BOOL_F;
ee149d03 1022 fd = SCM_FPORT_FDES (port);
e8a59063 1023
9de87eea 1024 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
a1ef7406 1025
1d1559ce 1026 SCM_SYSCALL (result = ttyname (fd));
e8a59063 1027 err = errno;
a1ef7406
LC
1028 if (result != NULL)
1029 result = strdup (result);
1030
9de87eea 1031 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
e8a59063 1032
1d1559ce 1033 if (!result)
e8a59063
KR
1034 {
1035 errno = err;
1036 SCM_SYSERROR;
1037 }
a1ef7406
LC
1038 else
1039 ret = scm_take_locale_string (result);
1040
e8a59063 1041 return ret;
0f2d19dd 1042}
1bbd0b84 1043#undef FUNC_NAME
82893676 1044#endif /* HAVE_TTYNAME */
0f2d19dd 1045
e8a59063 1046
0a9d83b0
KR
1047/* For thread safety "buf" is used instead of NULL for the ctermid static
1048 buffer. Actually it's unlikely the controlling terminal will change
1049 during program execution, and indeed on glibc (2.3.2) it's always just
1050 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
1051 safety everywhere. */
f25f761d 1052#ifdef HAVE_CTERMID
a1ec6916 1053SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
1bbd0b84 1054 (),
1e6808ea
MG
1055 "Return a string containing the file name of the controlling\n"
1056 "terminal for the current process.")
1bbd0b84 1057#define FUNC_NAME s_scm_ctermid
0f2d19dd 1058{
0a9d83b0
KR
1059 char buf[L_ctermid];
1060 char *result = ctermid (buf);
02b754d3 1061 if (*result == '\0')
1bbd0b84 1062 SCM_SYSERROR;
cc95e00a 1063 return scm_from_locale_string (result);
0f2d19dd 1064}
1bbd0b84 1065#undef FUNC_NAME
f25f761d 1066#endif /* HAVE_CTERMID */
0f2d19dd 1067
f25f761d 1068#ifdef HAVE_TCGETPGRP
a1ec6916 1069SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
1bbd0b84 1070 (SCM port),
1e6808ea
MG
1071 "Return the process group ID of the foreground process group\n"
1072 "associated with the terminal open on the file descriptor\n"
1073 "underlying @var{port}.\n"
1074 "\n"
b380b885
MD
1075 "If there is no foreground process group, the return value is a\n"
1076 "number greater than 1 that does not match the process group ID\n"
1077 "of any existing process group. This can happen if all of the\n"
1078 "processes in the job that was formerly the foreground job have\n"
1079 "terminated, and no other job has yet been moved into the\n"
1080 "foreground.")
1bbd0b84 1081#define FUNC_NAME s_scm_tcgetpgrp
0f2d19dd
JB
1082{
1083 int fd;
1084 pid_t pgid;
78446828
MV
1085
1086 port = SCM_COERCE_OUTPORT (port);
1087
34d19ef6 1088 SCM_VALIDATE_OPFPORT (1, port);
ee149d03
JB
1089 fd = SCM_FPORT_FDES (port);
1090 if ((pgid = tcgetpgrp (fd)) == -1)
1bbd0b84 1091 SCM_SYSERROR;
e11e83f3 1092 return scm_from_int (pgid);
1bbd0b84
GB
1093}
1094#undef FUNC_NAME
f25f761d 1095#endif /* HAVE_TCGETPGRP */
0f2d19dd 1096
f25f761d 1097#ifdef HAVE_TCSETPGRP
a1ec6916 1098SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
1bbd0b84 1099 (SCM port, SCM pgid),
b380b885
MD
1100 "Set the foreground process group ID for the terminal used by the file\n"
1101 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
1102 "The calling process\n"
1103 "must be a member of the same session as @var{pgid} and must have the same\n"
1104 "controlling terminal. The return value is unspecified.")
1bbd0b84 1105#define FUNC_NAME s_scm_tcsetpgrp
0f2d19dd
JB
1106{
1107 int fd;
78446828
MV
1108
1109 port = SCM_COERCE_OUTPORT (port);
1110
34d19ef6 1111 SCM_VALIDATE_OPFPORT (1, port);
ee149d03 1112 fd = SCM_FPORT_FDES (port);
a55c2b68 1113 if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
1bbd0b84 1114 SCM_SYSERROR;
02b754d3 1115 return SCM_UNSPECIFIED;
1bbd0b84
GB
1116}
1117#undef FUNC_NAME
f25f761d 1118#endif /* HAVE_TCSETPGRP */
0f2d19dd 1119
a1ec6916 1120SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
1bbd0b84 1121 (SCM filename, SCM args),
b380b885
MD
1122 "Executes the file named by @var{path} as a new process image.\n"
1123 "The remaining arguments are supplied to the process; from a C program\n"
bb2c02f2 1124 "they are accessible as the @code{argv} argument to @code{main}.\n"
b380b885 1125 "Conventionally the first @var{arg} is the same as @var{path}.\n"
8f85c0c6 1126 "All arguments must be strings.\n\n"
b380b885
MD
1127 "If @var{arg} is missing, @var{path} is executed with a null\n"
1128 "argument list, which may have system-dependent side-effects.\n\n"
1129 "This procedure is currently implemented using the @code{execv} system\n"
1130 "call, but we call it @code{execl} because of its Scheme calling interface.")
1bbd0b84 1131#define FUNC_NAME s_scm_execl
0f2d19dd 1132{
7f03f970
MV
1133 char *exec_file;
1134 char **exec_argv;
1135
661ae7ab 1136 scm_dynwind_begin (0);
7f03f970
MV
1137
1138 exec_file = scm_to_locale_string (filename);
661ae7ab 1139 scm_dynwind_free (exec_file);
7f03f970
MV
1140
1141 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1142
8ab3d8a0
KR
1143 execv (exec_file,
1144#ifdef __MINGW32__
1145 /* extra "const" in mingw formals, provokes warning from gcc */
1146 (const char * const *)
1147#endif
1148 exec_argv);
1bbd0b84 1149 SCM_SYSERROR;
7f03f970 1150
02b754d3 1151 /* not reached. */
661ae7ab 1152 scm_dynwind_end ();
02b754d3 1153 return SCM_BOOL_F;
0f2d19dd 1154}
1bbd0b84 1155#undef FUNC_NAME
0f2d19dd 1156
a1ec6916 1157SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
1bbd0b84 1158 (SCM filename, SCM args),
b380b885
MD
1159 "Similar to @code{execl}, however if\n"
1160 "@var{filename} does not contain a slash\n"
1161 "then the file to execute will be located by searching the\n"
1162 "directories listed in the @code{PATH} environment variable.\n\n"
6bcd01fc 1163 "This procedure is currently implemented using the @code{execvp} system\n"
b380b885 1164 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1bbd0b84 1165#define FUNC_NAME s_scm_execlp
0f2d19dd 1166{
7f03f970
MV
1167 char *exec_file;
1168 char **exec_argv;
1169
661ae7ab 1170 scm_dynwind_begin (0);
7f03f970
MV
1171
1172 exec_file = scm_to_locale_string (filename);
661ae7ab 1173 scm_dynwind_free (exec_file);
7f03f970
MV
1174
1175 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1176
8ab3d8a0
KR
1177 execvp (exec_file,
1178#ifdef __MINGW32__
1179 /* extra "const" in mingw formals, provokes warning from gcc */
1180 (const char * const *)
1181#endif
1182 exec_argv);
1bbd0b84 1183 SCM_SYSERROR;
7f03f970 1184
02b754d3 1185 /* not reached. */
661ae7ab 1186 scm_dynwind_end ();
02b754d3 1187 return SCM_BOOL_F;
0f2d19dd 1188}
1bbd0b84 1189#undef FUNC_NAME
0f2d19dd 1190
6afcd3b2 1191
807f353f
KR
1192/* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1193 list strings the way environ_list_to_c gives. */
1194
a1ec6916 1195SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
1bbd0b84 1196 (SCM filename, SCM env, SCM args),
b380b885
MD
1197 "Similar to @code{execl}, but the environment of the new process is\n"
1198 "specified by @var{env}, which must be a list of strings as returned by the\n"
1199 "@code{environ} procedure.\n\n"
1200 "This procedure is currently implemented using the @code{execve} system\n"
1201 "call, but we call it @code{execle} because of its Scheme calling interface.")
1bbd0b84 1202#define FUNC_NAME s_scm_execle
6afcd3b2 1203{
7f03f970 1204 char **exec_argv;
6afcd3b2 1205 char **exec_env;
7f03f970 1206 char *exec_file;
6afcd3b2 1207
661ae7ab 1208 scm_dynwind_begin (0);
7f03f970
MV
1209
1210 exec_file = scm_to_locale_string (filename);
661ae7ab 1211 scm_dynwind_free (exec_file);
7f03f970
MV
1212
1213 exec_argv = scm_i_allocate_string_pointers (args);
7f03f970 1214 exec_env = scm_i_allocate_string_pointers (env);
7f03f970 1215
8ab3d8a0
KR
1216 execve (exec_file,
1217#ifdef __MINGW32__
1218 /* extra "const" in mingw formals, provokes warning from gcc */
1219 (const char * const *)
1220#endif
1221 exec_argv,
1222#ifdef __MINGW32__
1223 /* extra "const" in mingw formals, provokes warning from gcc */
1224 (const char * const *)
1225#endif
1226 exec_env);
1bbd0b84 1227 SCM_SYSERROR;
7f03f970 1228
6afcd3b2 1229 /* not reached. */
661ae7ab 1230 scm_dynwind_end ();
6afcd3b2
GH
1231 return SCM_BOOL_F;
1232}
1bbd0b84 1233#undef FUNC_NAME
6afcd3b2 1234
82893676 1235#ifdef HAVE_FORK
a1ec6916 1236SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
1bbd0b84 1237 (),
b380b885
MD
1238 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1239 "In the child the return value is 0. In the parent the return value is\n"
1240 "the integer process ID of the child.\n\n"
1241 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1242 "with the scsh fork.")
1bbd0b84 1243#define FUNC_NAME s_scm_fork
0f2d19dd 1244{
bab0f4e5 1245 int pid;
0f2d19dd
JB
1246 pid = fork ();
1247 if (pid == -1)
1bbd0b84 1248 SCM_SYSERROR;
e11e83f3 1249 return scm_from_int (pid);
0f2d19dd 1250}
1bbd0b84 1251#undef FUNC_NAME
82893676 1252#endif /* HAVE_FORK */
0f2d19dd 1253
4f68365d
MV
1254#ifdef __MINGW32__
1255# include "win32-uname.h"
1256#endif
1257
1258#if defined (HAVE_UNAME) || defined (__MINGW32__)
a1ec6916 1259SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
1bbd0b84 1260 (),
1e6808ea
MG
1261 "Return an object with some information about the computer\n"
1262 "system the program is running on.")
1bbd0b84 1263#define FUNC_NAME s_scm_uname
0f2d19dd 1264{
0f2d19dd 1265 struct utsname buf;
1d1559ce 1266 SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
e1a191a8 1267 if (uname (&buf) < 0)
1bbd0b84 1268 SCM_SYSERROR;
4057a3e0
MV
1269 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname));
1270 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename));
1271 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release));
1272 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version));
1273 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine));
0f2d19dd 1274/*
02b754d3 1275 a linux special?
4057a3e0 1276 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
0f2d19dd 1277*/
1d1559ce 1278 return result;
0f2d19dd 1279}
1bbd0b84 1280#undef FUNC_NAME
f25f761d 1281#endif /* HAVE_UNAME */
0f2d19dd 1282
a1ec6916 1283SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1bbd0b84 1284 (SCM env),
1e6808ea
MG
1285 "If @var{env} is omitted, return the current environment (in the\n"
1286 "Unix sense) as a list of strings. Otherwise set the current\n"
1287 "environment, which is also the default environment for child\n"
1288 "processes, to the supplied list of strings. Each member of\n"
1289 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1290 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1291 "then the return value is unspecified.")
1bbd0b84 1292#define FUNC_NAME s_scm_environ
0f2d19dd
JB
1293{
1294 if (SCM_UNBNDP (env))
1295 return scm_makfromstrs (-1, environ);
1296 else
1297 {
2a776823 1298 environ = scm_i_allocate_string_pointers (env);
0f2d19dd
JB
1299 return SCM_UNSPECIFIED;
1300 }
1301}
1bbd0b84 1302#undef FUNC_NAME
0f2d19dd 1303
9ee5fce4
MD
1304#ifdef L_tmpnam
1305
a1ec6916 1306SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1bbd0b84 1307 (),
1e6808ea
MG
1308 "Return a name in the file system that does not match any\n"
1309 "existing file. However there is no guarantee that another\n"
1310 "process will not create the file after @code{tmpnam} is called.\n"
1311 "Care should be taken if opening the file, e.g., use the\n"
1312 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1bbd0b84 1313#define FUNC_NAME s_scm_tmpnam
9ee5fce4
MD
1314{
1315 char name[L_tmpnam];
6d163216
GH
1316 char *rv;
1317
1318 SCM_SYSCALL (rv = tmpnam (name));
1319 if (rv == NULL)
1320 /* not SCM_SYSERROR since errno probably not set. */
1321 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
cc95e00a 1322 return scm_from_locale_string (name);
9ee5fce4 1323}
0f981281 1324#undef FUNC_NAME
0f2d19dd 1325
1bbd0b84 1326#endif
1cc91f1b 1327
4f68365d
MV
1328#ifndef HAVE_MKSTEMP
1329extern int mkstemp (char *);
1330#endif
1331
6d163216
GH
1332SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
1333 (SCM tmpl),
23f2b9a3 1334 "Create a new unique file in the file system and return a new\n"
1e6808ea 1335 "buffered port open for reading and writing to the file.\n"
2c4cdcb0 1336 "\n"
1e6808ea 1337 "@var{tmpl} is a string specifying where the file should be\n"
23f2b9a3
KR
1338 "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
1339 "will be changed in the string to return the name of the file.\n"
1340 "(@code{port-filename} on the port also gives the name.)\n"
2c4cdcb0 1341 "\n"
23f2b9a3
KR
1342 "POSIX doesn't specify the permissions mode of the file, on GNU\n"
1343 "and most systems it's @code{#o600}. An application can use\n"
1344 "@code{chmod} to relax that if desired. For example\n"
1345 "@code{#o666} less @code{umask}, which is usual for ordinary\n"
1346 "file creation,\n"
1347 "\n"
1348 "@example\n"
1349 "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
1350 " (chmod port (logand #o666 (lognot (umask))))\n"
1351 " ...)\n"
1352 "@end example")
6d163216
GH
1353#define FUNC_NAME s_scm_mkstemp
1354{
1355 char *c_tmpl;
71978ac9 1356 int rv;
6d163216 1357
661ae7ab 1358 scm_dynwind_begin (0);
892065da
MV
1359
1360 c_tmpl = scm_to_locale_string (tmpl);
661ae7ab 1361 scm_dynwind_free (c_tmpl);
892065da 1362
6d163216
GH
1363 SCM_SYSCALL (rv = mkstemp (c_tmpl));
1364 if (rv == -1)
1365 SCM_SYSERROR;
892065da
MV
1366
1367 scm_substring_move_x (scm_from_locale_string (c_tmpl),
1368 SCM_INUM0, scm_string_length (tmpl),
1369 tmpl, SCM_INUM0);
1370
661ae7ab 1371 scm_dynwind_end ();
6d163216
GH
1372 return scm_fdes_to_port (rv, "w+", tmpl);
1373}
1374#undef FUNC_NAME
1375
cd53bd36
TTN
1376SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0,
1377 (void),
1378 "Return an input/output port to a unique temporary file\n"
1379 "named using the path prefix @code{P_tmpdir} defined in\n"
1380 "@file{stdio.h}.\n"
1381 "The file is automatically deleted when the port is closed\n"
1382 "or the program terminates.")
1383#define FUNC_NAME s_scm_tmpfile
1384{
1385 FILE *rv;
1386
1387 if (! (rv = tmpfile ()))
1388 SCM_SYSERROR;
1389 return scm_fdes_to_port (fileno (rv), "w+", SCM_BOOL_F);
1390}
1391#undef FUNC_NAME
1392
06bfe276
AW
1393SCM_DEFINE (scm_utime, "utime", 1, 5, 0,
1394 (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens,
1395 SCM flags),
1e6808ea
MG
1396 "@code{utime} sets the access and modification times for the\n"
1397 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1398 "not supplied, then the current time is used. @var{actime} and\n"
1399 "@var{modtime} must be integer time values as returned by the\n"
06bfe276
AW
1400 "@code{current-time} procedure.\n\n"
1401 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1402 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
477e4219 1403 "only supported on some combinations of file systems and operating\n"
06bfe276 1404 "systems.\n"
1e6808ea 1405 "@lisp\n"
b380b885 1406 "(utime \"foo\" (- (current-time) 3600))\n"
1e6808ea
MG
1407 "@end lisp\n"
1408 "will set the access time to one hour in the past and the\n"
1409 "modification time to the current time.")
1bbd0b84 1410#define FUNC_NAME s_scm_utime
0f2d19dd
JB
1411{
1412 int rv;
06bfe276
AW
1413 time_t atim_sec, mtim_sec;
1414 long atim_nsec, mtim_nsec;
1415 int f;
1416
0f2d19dd 1417 if (SCM_UNBNDP (actime))
06bfe276 1418 {
adfb4284 1419#ifdef HAVE_UTIMENSAT
06bfe276
AW
1420 atim_sec = 0;
1421 atim_nsec = UTIME_NOW;
1422#else
1423 SCM_SYSCALL (time (&atim_sec));
1424 atim_nsec = 0;
1425#endif
1426 }
0f2d19dd 1427 else
06bfe276
AW
1428 {
1429 atim_sec = SCM_NUM2ULONG (2, actime);
1430 if (SCM_UNBNDP (actimens))
1431 atim_nsec = 0;
1432 else
1433 atim_nsec = SCM_NUM2LONG (4, actimens);
1434 }
1435
0f2d19dd 1436 if (SCM_UNBNDP (modtime))
06bfe276 1437 {
adfb4284 1438#ifdef HAVE_UTIMENSAT
06bfe276
AW
1439 mtim_sec = 0;
1440 mtim_nsec = UTIME_NOW;
1441#else
1442 SCM_SYSCALL (time (&mtim_sec));
1443 mtim_nsec = 0;
1444#endif
1445 }
0f2d19dd 1446 else
06bfe276
AW
1447 {
1448 mtim_sec = SCM_NUM2ULONG (3, modtime);
1449 if (SCM_UNBNDP (modtimens))
1450 mtim_nsec = 0;
1451 else
1452 mtim_nsec = SCM_NUM2LONG (5, modtimens);
1453 }
1454
1455 if (SCM_UNBNDP (flags))
1456 f = 0;
1457 else
1458 f = SCM_NUM2INT (6, flags);
1459
adfb4284 1460#ifdef HAVE_UTIMENSAT
06bfe276
AW
1461 {
1462 struct timespec times[2];
1463 times[0].tv_sec = atim_sec;
1464 times[0].tv_nsec = atim_nsec;
1465 times[1].tv_sec = mtim_sec;
1466 times[1].tv_nsec = mtim_nsec;
1467
1468 STRING_SYSCALL (pathname, c_pathname,
adfb4284 1469 rv = utimensat (AT_FDCWD, c_pathname, times, f));
06bfe276
AW
1470 }
1471#else
1472 {
1473 struct utimbuf utm;
1474 utm.actime = atim_sec;
1475 utm.modtime = mtim_sec;
1476
1477 STRING_SYSCALL (pathname, c_pathname,
1478 rv = utime (c_pathname, &utm));
1479 }
1480#endif
0f2d19dd 1481
02b754d3 1482 if (rv != 0)
1bbd0b84 1483 SCM_SYSERROR;
02b754d3 1484 return SCM_UNSPECIFIED;
0f2d19dd 1485}
1bbd0b84 1486#undef FUNC_NAME
0f2d19dd 1487
a1ec6916 1488SCM_DEFINE (scm_access, "access?", 2, 0, 0,
1bbd0b84 1489 (SCM path, SCM how),
20e0b166
KR
1490 "Test accessibility of a file under the real UID and GID of the\n"
1491 "calling process. The return is @code{#t} if @var{path} exists\n"
1492 "and the permissions requested by @var{how} are all allowed, or\n"
1493 "@code{#f} if not.\n"
1e6808ea 1494 "\n"
20e0b166
KR
1495 "@var{how} is an integer which is one of the following values,\n"
1496 "or a bitwise-OR (@code{logior}) of multiple values.\n"
1e6808ea 1497 "\n"
b380b885 1498 "@defvar R_OK\n"
20e0b166 1499 "Test for read permission.\n"
b380b885
MD
1500 "@end defvar\n"
1501 "@defvar W_OK\n"
20e0b166 1502 "Test for write permission.\n"
b380b885
MD
1503 "@end defvar\n"
1504 "@defvar X_OK\n"
20e0b166 1505 "Test for execute permission.\n"
b380b885
MD
1506 "@end defvar\n"
1507 "@defvar F_OK\n"
20e0b166
KR
1508 "Test for existence of the file. This is implied by each of the\n"
1509 "other tests, so there's no need to combine it with them.\n"
1510 "@end defvar\n"
1511 "\n"
1512 "It's important to note that @code{access?} does not simply\n"
1513 "indicate what will happen on attempting to read or write a\n"
1514 "file. In normal circumstances it does, but in a set-UID or\n"
1515 "set-GID program it doesn't because @code{access?} tests the\n"
1516 "real ID, whereas an open or execute attempt uses the effective\n"
1517 "ID.\n"
1518 "\n"
1519 "A program which will never run set-UID/GID can ignore the\n"
1520 "difference between real and effective IDs, but for maximum\n"
1521 "generality, especially in library functions, it's best not to\n"
1522 "use @code{access?} to predict the result of an open or execute,\n"
1523 "instead simply attempt that and catch any exception.\n"
1524 "\n"
1525 "The main use for @code{access?} is to let a set-UID/GID program\n"
1526 "determine what the invoking user would have been allowed to do,\n"
1527 "without the greater (or perhaps lesser) privileges afforded by\n"
1528 "the effective ID. For more on this, see ``Testing File\n"
1529 "Access'' in The GNU C Library Reference Manual.")
1bbd0b84 1530#define FUNC_NAME s_scm_access
0f2d19dd
JB
1531{
1532 int rv;
1533
f015614a
MV
1534 WITH_STRING (path, c_path,
1535 rv = access (c_path, scm_to_int (how)));
7888309b 1536 return scm_from_bool (!rv);
0f2d19dd 1537}
1bbd0b84 1538#undef FUNC_NAME
0f2d19dd 1539
a1ec6916 1540SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1bbd0b84 1541 (),
1e6808ea 1542 "Return an integer representing the current process ID.")
1bbd0b84 1543#define FUNC_NAME s_scm_getpid
0f2d19dd 1544{
e11e83f3 1545 return scm_from_ulong (getpid ());
0f2d19dd 1546}
1bbd0b84 1547#undef FUNC_NAME
0f2d19dd 1548
a1ec6916 1549SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1bbd0b84 1550 (SCM str),
b380b885
MD
1551 "Modifies the environment of the current process, which is\n"
1552 "also the default environment inherited by child processes.\n\n"
1553 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1554 "directly into the environment, replacing any existing environment string\n"
1555 "with\n"
1556 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1557 "sign, then any existing string with name matching @var{string} will\n"
1558 "be removed.\n\n"
1559 "The return value is unspecified.")
1bbd0b84 1560#define FUNC_NAME s_scm_putenv
0f2d19dd 1561{
f158788f 1562 int rv;
f015614a 1563 char *c_str = scm_to_locale_string (str);
002409fe 1564
3731192f 1565 /* Leave C_STR in the environment. */
fcc5d734 1566
3731192f
LC
1567 /* Gnulib's `putenv' module honors the semantics described above. */
1568 rv = putenv (c_str);
1569 if (rv < 0)
1570 SCM_SYSERROR;
f015614a 1571
f93ddd39 1572 return SCM_UNSPECIFIED;
0f2d19dd 1573}
1bbd0b84 1574#undef FUNC_NAME
0f2d19dd 1575
b89c4943 1576/* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
a2f00b9b
LC
1577 systems (i.e., systems where a reentrant locale API is not available). It
1578 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1579 details. */
1580scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
b89c4943 1581
f25f761d 1582#ifdef HAVE_SETLOCALE
b89c4943 1583
a1ec6916 1584SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1bbd0b84 1585 (SCM category, SCM locale),
1e6808ea
MG
1586 "If @var{locale} is omitted, return the current value of the\n"
1587 "specified locale category as a system-dependent string.\n"
1588 "@var{category} should be specified using the values\n"
1589 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1590 "\n"
1591 "Otherwise the specified locale category is set to the string\n"
1592 "@var{locale} and the new value is returned as a\n"
1593 "system-dependent string. If @var{locale} is an empty string,\n"
889975e5
MG
1594 "the locale will be set using environment variables.\n"
1595 "\n"
1596 "When the locale is changed, the character encoding of the new\n"
1597 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1598 "input, output, and error ports\n")
1bbd0b84 1599#define FUNC_NAME s_scm_setlocale
0f2d19dd 1600{
a2f00b9b 1601 int c_category;
0f2d19dd
JB
1602 char *clocale;
1603 char *rv;
889975e5 1604 const char *enc;
0f2d19dd 1605
661ae7ab 1606 scm_dynwind_begin (0);
f015614a 1607
0f2d19dd
JB
1608 if (SCM_UNBNDP (locale))
1609 {
1610 clocale = NULL;
1611 }
1612 else
1613 {
f015614a 1614 clocale = scm_to_locale_string (locale);
661ae7ab 1615 scm_dynwind_free (clocale);
0f2d19dd
JB
1616 }
1617
a2f00b9b
LC
1618 c_category = scm_i_to_lc_category (category, 1);
1619
b89c4943 1620 scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
a2f00b9b 1621 rv = setlocale (c_category, clocale);
b89c4943 1622 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
b89c4943 1623
02b754d3 1624 if (rv == NULL)
41b74b7d
KR
1625 {
1626 /* POSIX and C99 don't say anything about setlocale setting errno, so
1627 force a sensible value here. glibc leaves ENOENT, which would be
1628 fine, but it's not a documented feature. */
1629 errno = EINVAL;
1630 SCM_SYSERROR;
1631 }
f015614a 1632
889975e5 1633 enc = locale_charset ();
9d9c66ba 1634
889975e5 1635 /* Set the default encoding for new ports. */
9d9c66ba
LC
1636 scm_i_set_default_port_encoding (enc);
1637
889975e5
MG
1638 /* Set the encoding for the stdio ports. */
1639 scm_i_set_port_encoding_x (scm_current_input_port (), enc);
1640 scm_i_set_port_encoding_x (scm_current_output_port (), enc);
1641 scm_i_set_port_encoding_x (scm_current_error_port (), enc);
a17d2654 1642
661ae7ab 1643 scm_dynwind_end ();
f015614a 1644 return scm_from_locale_string (rv);
0f2d19dd 1645}
1bbd0b84 1646#undef FUNC_NAME
f25f761d 1647#endif /* HAVE_SETLOCALE */
0f2d19dd 1648
f25f761d 1649#ifdef HAVE_MKNOD
a1ec6916 1650SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1bbd0b84 1651 (SCM path, SCM type, SCM perms, SCM dev),
b380b885
MD
1652 "Creates a new special file, such as a file corresponding to a device.\n"
1653 "@var{path} specifies the name of the file. @var{type} should\n"
1654 "be one of the following symbols:\n"
1655 "regular, directory, symlink, block-special, char-special,\n"
1656 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1657 "@var{dev} (an integer) specifies which device the special file refers\n"
1658 "to. Its exact interpretation depends on the kind of special file\n"
1659 "being created.\n\n"
1660 "E.g.,\n"
1e6808ea 1661 "@lisp\n"
09831f94 1662 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1e6808ea 1663 "@end lisp\n\n"
a3c8b9fc 1664 "The return value is unspecified.")
1bbd0b84 1665#define FUNC_NAME s_scm_mknod
0f2d19dd 1666{
0f2d19dd 1667 int val;
cc95e00a 1668 const char *p;
82a76bdf 1669 int ctype = 0;
19468eff 1670
a6d9e5ab 1671 SCM_VALIDATE_STRING (1, path);
34d19ef6 1672 SCM_VALIDATE_SYMBOL (2, type);
19468eff 1673
cc95e00a 1674 p = scm_i_symbol_chars (type);
19468eff
GH
1675 if (strcmp (p, "regular") == 0)
1676 ctype = S_IFREG;
1677 else if (strcmp (p, "directory") == 0)
1678 ctype = S_IFDIR;
23f2b9a3
KR
1679#ifdef S_IFLNK
1680 /* systems without symlinks probably don't have S_IFLNK defined */
19468eff
GH
1681 else if (strcmp (p, "symlink") == 0)
1682 ctype = S_IFLNK;
23f2b9a3 1683#endif
19468eff
GH
1684 else if (strcmp (p, "block-special") == 0)
1685 ctype = S_IFBLK;
1686 else if (strcmp (p, "char-special") == 0)
1687 ctype = S_IFCHR;
1688 else if (strcmp (p, "fifo") == 0)
1689 ctype = S_IFIFO;
e655d034 1690#ifdef S_IFSOCK
19468eff
GH
1691 else if (strcmp (p, "socket") == 0)
1692 ctype = S_IFSOCK;
e655d034 1693#endif
19468eff 1694 else
34d19ef6 1695 SCM_OUT_OF_RANGE (2, type);
19468eff 1696
f015614a
MV
1697 STRING_SYSCALL (path, c_path,
1698 val = mknod (c_path,
1699 ctype | scm_to_int (perms),
1700 scm_to_int (dev)));
02b754d3 1701 if (val != 0)
1bbd0b84 1702 SCM_SYSERROR;
02b754d3 1703 return SCM_UNSPECIFIED;
0f2d19dd 1704}
1bbd0b84 1705#undef FUNC_NAME
f25f761d 1706#endif /* HAVE_MKNOD */
0f2d19dd 1707
f25f761d 1708#ifdef HAVE_NICE
a1ec6916 1709SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1bbd0b84 1710 (SCM incr),
b380b885
MD
1711 "Increment the priority of the current process by @var{incr}. A higher\n"
1712 "priority value means that the process runs less often.\n"
1713 "The return value is unspecified.")
1bbd0b84 1714#define FUNC_NAME s_scm_nice
0f2d19dd 1715{
634aa8de
LC
1716 int nice_value;
1717
f1c82f55
KR
1718 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1719 from "prio-NZERO", so an error must be detected from errno changed */
1720 errno = 0;
634aa8de 1721 nice_value = nice (scm_to_int (incr));
f1c82f55 1722 if (errno != 0)
1bbd0b84 1723 SCM_SYSERROR;
634aa8de 1724
02b754d3 1725 return SCM_UNSPECIFIED;
0f2d19dd 1726}
1bbd0b84 1727#undef FUNC_NAME
f25f761d 1728#endif /* HAVE_NICE */
0f2d19dd 1729
f25f761d 1730#ifdef HAVE_SYNC
a1ec6916 1731SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1bbd0b84 1732 (),
b380b885
MD
1733 "Flush the operating system disk buffers.\n"
1734 "The return value is unspecified.")
1bbd0b84 1735#define FUNC_NAME s_scm_sync
0f2d19dd 1736{
0f2d19dd 1737 sync();
127ec750 1738 return SCM_UNSPECIFIED;
0f2d19dd 1739}
1bbd0b84 1740#undef FUNC_NAME
f25f761d 1741#endif /* HAVE_SYNC */
0f2d19dd 1742
33bea692
KR
1743
1744/* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1745 to avoid another thread overwriting it. A test program running crypt
1746 continuously in two threads can be quickly seen tripping this problem.
1747 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1748
1749 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1750 slower (about 5x) than plain crypt if you pass an uninitialized data
1751 block each time. Presumably there's some one-time setups. The best way
1752 to use crypt_r for parallel execution in multiple threads would probably
1753 be to maintain a little pool of initialized crypt_data structures, take
1754 one and use it, then return it to the pool. That pool could be garbage
1755 collected so it didn't add permanently to memory use if only a few crypt
1756 calls are made. But we expect crypt will be used rarely, and even more
1757 rarely will there be any desire for lots of parallel execution on
1758 multiple cpus. So for now we don't bother with anything fancy, just
1759 ensure it works. */
1760
534bbcc1 1761#ifdef HAVE_CRYPT
33bea692 1762SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
94e6d793
MG
1763 (SCM key, SCM salt),
1764 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
9401323e 1765 "crypt(3) library call.")
94e6d793
MG
1766#define FUNC_NAME s_scm_crypt
1767{
33bea692 1768 SCM ret;
23d72566 1769 char *c_key, *c_salt, *c_ret;
94e6d793 1770
661ae7ab
MV
1771 scm_dynwind_begin (0);
1772 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
33bea692 1773
f015614a 1774 c_key = scm_to_locale_string (key);
661ae7ab 1775 scm_dynwind_free (c_key);
f015614a 1776 c_salt = scm_to_locale_string (salt);
661ae7ab 1777 scm_dynwind_free (c_salt);
f015614a 1778
23d72566
KR
1779 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1780 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1781 implemented). */
1782 c_ret = crypt (c_key, c_salt);
1783 if (c_ret == NULL)
1784 SCM_SYSERROR;
33bea692 1785
23d72566 1786 ret = scm_from_locale_string (c_ret);
661ae7ab 1787 scm_dynwind_end ();
33bea692 1788 return ret;
94e6d793
MG
1789}
1790#undef FUNC_NAME
b9a7b725 1791#endif /* HAVE_CRYPT */
94e6d793
MG
1792
1793#if HAVE_CHROOT
1794SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1795 (SCM path),
1796 "Change the root directory to that specified in @var{path}.\n"
1797 "This directory will be used for path names beginning with\n"
1798 "@file{/}. The root directory is inherited by all children\n"
1799 "of the current process. Only the superuser may change the\n"
1800 "root directory.")
1801#define FUNC_NAME s_scm_chroot
1802{
f015614a 1803 int rv;
94e6d793 1804
f015614a
MV
1805 WITH_STRING (path, c_path,
1806 rv = chroot (c_path));
1807 if (rv == -1)
94e6d793
MG
1808 SCM_SYSERROR;
1809 return SCM_UNSPECIFIED;
1810}
1811#undef FUNC_NAME
1812#endif /* HAVE_CHROOT */
1813
7beabedb
MG
1814
1815#ifdef __MINGW32__
1816/* Wrapper function to supplying `getlogin()' under Windows. */
1817static char * getlogin (void)
1818{
1819 static char user[256];
1820 static unsigned long len = 256;
1821
1822 if (!GetUserName (user, &len))
1823 return NULL;
1824 return user;
1825}
1826#endif /* __MINGW32__ */
1827
1828
4f68365d 1829#if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
94e6d793
MG
1830SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1831 (void),
1832 "Return a string containing the name of the user logged in on\n"
1833 "the controlling terminal of the process, or @code{#f} if this\n"
1834 "information cannot be obtained.")
1835#define FUNC_NAME s_scm_getlogin
1836{
1837 char * p;
1838
1839 p = getlogin ();
1840 if (!p || !*p)
1841 return SCM_BOOL_F;
cc95e00a 1842 return scm_from_locale_string (p);
94e6d793
MG
1843}
1844#undef FUNC_NAME
1845#endif /* HAVE_GETLOGIN */
1846
94e6d793
MG
1847#if HAVE_GETPRIORITY
1848SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1849 (SCM which, SCM who),
1850 "Return the scheduling priority of the process, process group\n"
1851 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1852 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1853 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1854 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1855 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1856 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1857 "denotes the current process, process group, or user. Return\n"
1858 "the highest priority (lowest numerical value) of any of the\n"
1859 "specified processes.")
1860#define FUNC_NAME s_scm_getpriority
1861{
1862 int cwhich, cwho, ret;
1863
a55c2b68
MV
1864 cwhich = scm_to_int (which);
1865 cwho = scm_to_int (who);
94e6d793
MG
1866
1867 /* We have to clear errno and examine it later, because -1 is a
1868 legal return value for getpriority(). */
1869 errno = 0;
1870 ret = getpriority (cwhich, cwho);
1871 if (errno != 0)
1872 SCM_SYSERROR;
a55c2b68 1873 return scm_from_int (ret);
94e6d793
MG
1874}
1875#undef FUNC_NAME
1876#endif /* HAVE_GETPRIORITY */
1877
1878#if HAVE_SETPRIORITY
1879SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1880 (SCM which, SCM who, SCM prio),
1881 "Set the scheduling priority of the process, process group\n"
1882 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1883 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1884 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1885 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1886 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1887 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1888 "denotes the current process, process group, or user.\n"
1889 "@var{prio} is a value in the range -20 and 20, the default\n"
1890 "priority is 0; lower priorities cause more favorable\n"
1891 "scheduling. Sets the priority of all of the specified\n"
1892 "processes. Only the super-user may lower priorities.\n"
1893 "The return value is not specified.")
1894#define FUNC_NAME s_scm_setpriority
1895{
1896 int cwhich, cwho, cprio;
1897
a55c2b68
MV
1898 cwhich = scm_to_int (which);
1899 cwho = scm_to_int (who);
1900 cprio = scm_to_int (prio);
94e6d793
MG
1901
1902 if (setpriority (cwhich, cwho, cprio) == -1)
1903 SCM_SYSERROR;
1904 return SCM_UNSPECIFIED;
1905}
1906#undef FUNC_NAME
1907#endif /* HAVE_SETPRIORITY */
1908
fe613fe2
LC
1909#ifdef HAVE_SCHED_GETAFFINITY
1910
1911static SCM
1912cpu_set_to_bitvector (const cpu_set_t *cs)
1913{
1914 SCM bv;
1915 size_t cpu;
1916
1917 bv = scm_c_make_bitvector (sizeof (*cs), SCM_BOOL_F);
1918
1919 for (cpu = 0; cpu < sizeof (*cs); cpu++)
1920 {
1921 if (CPU_ISSET (cpu, cs))
1922 /* XXX: This is inefficient but avoids code duplication. */
1923 scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T);
1924 }
1925
1926 return bv;
1927}
1928
1929SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0,
1930 (SCM pid),
1931 "Return a bitvector representing the CPU affinity mask for\n"
1932 "process @var{pid}. Each CPU the process has affinity with\n"
1933 "has its corresponding bit set in the returned bitvector.\n"
1934 "The number of bits set is a good estimate of how many CPUs\n"
1935 "Guile can use without stepping on other processes' toes.\n\n"
3ae78cac
LC
1936 "Currently this procedure is only defined on GNU variants\n"
1937 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
1938 "GNU C Library Reference Manual}).\n")
fe613fe2
LC
1939#define FUNC_NAME s_scm_getaffinity
1940{
1941 int err;
1942 cpu_set_t cs;
1943
1944 CPU_ZERO (&cs);
1945 err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs);
1946 if (err)
1947 SCM_SYSERROR;
1948
1949 return cpu_set_to_bitvector (&cs);
1950}
1951#undef FUNC_NAME
1952
1953#endif /* HAVE_SCHED_GETAFFINITY */
1954
1955#ifdef HAVE_SCHED_SETAFFINITY
1956
1957SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0,
1958 (SCM pid, SCM mask),
1959 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
1960 "the process or thread with ID @var{pid}. The return value\n"
1961 "is unspecified.\n\n"
3ae78cac
LC
1962 "Currently this procedure is only defined on GNU variants\n"
1963 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
1964 "GNU C Library Reference Manual}).\n")
fe613fe2
LC
1965#define FUNC_NAME s_scm_setaffinity
1966{
1967 cpu_set_t cs;
1968 scm_t_array_handle handle;
1969 const scm_t_uint32 *c_mask;
1970 size_t len, off, cpu;
1971 ssize_t inc;
1972 int err;
1973
1974 c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc);
1975
1976 CPU_ZERO (&cs);
1977 for (cpu = 0; cpu < len; cpu++)
1978 {
1979 size_t idx;
1980
1981 idx = cpu * inc + off;
1982 if (c_mask[idx / 32] & (1UL << (idx % 32)))
1983 CPU_SET (cpu, &cs);
1984 }
1985
1986 err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs);
1987 if (err)
1988 SCM_SYSERROR;
1989
1990 return SCM_UNSPECIFIED;
1991}
1992#undef FUNC_NAME
1993
1994#endif /* HAVE_SCHED_SETAFFINITY */
1995
f0c0141f
LC
1996SCM_DEFINE (scm_total_processor_count, "total-processor-count", 0, 0, 0,
1997 (void),
1998 "Return the total number of processors of the machine, which\n"
1999 "is guaranteed to be at least 1. A ``processor'' here is a\n"
2000 "thread execution unit, which can be either:\n\n"
2001 "@itemize\n"
2002 "@item an execution core in a (possibly multi-core) chip, in a\n"
2003 " (possibly multi- chip) module, in a single computer, or\n"
2004 "@item a thread execution unit inside a core in the case of\n"
2005 " @dfn{hyper-threaded} CPUs.\n"
2006 "@end itemize\n\n"
2007 "Which of the two definitions is used, is unspecified.\n")
2008#define FUNC_NAME s_scm_total_processor_count
2009{
2010 return scm_from_ulong (num_processors (NPROC_ALL));
2011}
2012#undef FUNC_NAME
2013
2014SCM_DEFINE (scm_current_processor_count, "current-processor-count", 0, 0, 0,
2015 (void),
2016 "Like @code{total-processor-count}, but return the number of\n"
2017 "processors available to the current process. See\n"
2018 "@code{setaffinity} and @code{getaffinity} for more\n"
2019 "information.\n")
b56c252b 2020#define FUNC_NAME s_scm_current_processor_count
f0c0141f
LC
2021{
2022 return scm_from_ulong (num_processors (NPROC_CURRENT));
2023}
2024#undef FUNC_NAME
2025
2026\f
94e6d793
MG
2027#if HAVE_GETPASS
2028SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
2029 (SCM prompt),
2030 "Display @var{prompt} to the standard error output and read\n"
2031 "a password from @file{/dev/tty}. If this file is not\n"
2032 "accessible, it reads from standard input. The password may be\n"
2033 "up to 127 characters in length. Additional characters and the\n"
2034 "terminating newline character are discarded. While reading\n"
2035 "the password, echoing and the generation of signals by special\n"
2036 "characters is disabled.")
2037#define FUNC_NAME s_scm_getpass
2038{
2039 char * p;
2040 SCM passwd;
2041
2042 SCM_VALIDATE_STRING (1, prompt);
94e6d793 2043
f015614a
MV
2044 WITH_STRING (prompt, c_prompt,
2045 p = getpass(c_prompt));
2046 passwd = scm_from_locale_string (p);
94e6d793
MG
2047
2048 /* Clear out the password in the static buffer. */
2049 memset (p, 0, strlen (p));
2050
2051 return passwd;
2052}
2053#undef FUNC_NAME
2054#endif /* HAVE_GETPASS */
2055
94e6d793
MG
2056SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
2057 (SCM file, SCM operation),
2058 "Apply or remove an advisory lock on an open file.\n"
2059 "@var{operation} specifies the action to be done:\n"
0b91f1db
KR
2060 "\n"
2061 "@defvar LOCK_SH\n"
94e6d793
MG
2062 "Shared lock. More than one process may hold a shared lock\n"
2063 "for a given file at a given time.\n"
0b91f1db
KR
2064 "@end defvar\n"
2065 "@defvar LOCK_EX\n"
94e6d793
MG
2066 "Exclusive lock. Only one process may hold an exclusive lock\n"
2067 "for a given file at a given time.\n"
0b91f1db
KR
2068 "@end defvar\n"
2069 "@defvar LOCK_UN\n"
94e6d793 2070 "Unlock the file.\n"
0b91f1db
KR
2071 "@end defvar\n"
2072 "@defvar LOCK_NB\n"
2073 "Don't block when locking. This is combined with one of the\n"
2074 "other operations using @code{logior}. If @code{flock} would\n"
2075 "block an @code{EWOULDBLOCK} error is thrown.\n"
2076 "@end defvar\n"
2077 "\n"
94e6d793 2078 "The return value is not specified. @var{file} may be an open\n"
0b91f1db
KR
2079 "file descriptor or an open file descriptor port.\n"
2080 "\n"
2081 "Note that @code{flock} does not lock files across NFS.")
94e6d793
MG
2082#define FUNC_NAME s_scm_flock
2083{
a55c2b68 2084 int fdes;
94e6d793 2085
a55c2b68
MV
2086 if (scm_is_integer (file))
2087 fdes = scm_to_int (file);
94e6d793
MG
2088 else
2089 {
2090 SCM_VALIDATE_OPFPORT (2, file);
2091
2092 fdes = SCM_FPORT_FDES (file);
2093 }
a55c2b68 2094 if (flock (fdes, scm_to_int (operation)) == -1)
94e6d793
MG
2095 SCM_SYSERROR;
2096 return SCM_UNSPECIFIED;
2097}
2098#undef FUNC_NAME
94e6d793
MG
2099
2100#if HAVE_SETHOSTNAME
2101SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
2102 (SCM name),
2103 "Set the host name of the current processor to @var{name}. May\n"
2104 "only be used by the superuser. The return value is not\n"
2105 "specified.")
2106#define FUNC_NAME s_scm_sethostname
2107{
f015614a 2108 int rv;
94e6d793 2109
f015614a
MV
2110 WITH_STRING (name, c_name,
2111 rv = sethostname (c_name, strlen(c_name)));
2112 if (rv == -1)
94e6d793
MG
2113 SCM_SYSERROR;
2114 return SCM_UNSPECIFIED;
2115}
2116#undef FUNC_NAME
2117#endif /* HAVE_SETHOSTNAME */
2118
b28f5b3c 2119
94e6d793
MG
2120#if HAVE_GETHOSTNAME
2121SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
2122 (void),
2123 "Return the host name of the current processor.")
2124#define FUNC_NAME s_scm_gethostname
2125{
b28f5b3c
DH
2126#ifdef MAXHOSTNAMELEN
2127
2128 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2129 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2130 const int len = MAXHOSTNAMELEN + 1;
2131 char *const p = scm_malloc (len);
2132 const int res = gethostname (p, len);
2133
661ae7ab
MV
2134 scm_dynwind_begin (0);
2135 scm_dynwind_unwind_handler (free, p, 0);
b28f5b3c
DH
2136
2137#else
94e6d793 2138
0eaf1055 2139 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
b28f5b3c
DH
2140 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2141 int len = 256;
2142 int res;
2143 char *p;
0eaf1055 2144
b28f5b3c 2145# if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
0eaf1055
KR
2146
2147 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
b28f5b3c
DH
2148 * which may reflect a particular kernel configuration.
2149 * Must watch out for this existing but giving -1, as happens for instance
2150 * in gnu/linux glibc 2.3.2. */
0eaf1055 2151 {
b28f5b3c 2152 const long int n = sysconf (_SC_HOST_NAME_MAX);
0eaf1055
KR
2153 if (n != -1L)
2154 len = n;
2155 }
b28f5b3c
DH
2156
2157# endif
2158
2159 p = scm_malloc (len);
2160
661ae7ab
MV
2161 scm_dynwind_begin (0);
2162 scm_dynwind_unwind_handler (free, p, 0);
0eaf1055 2163
94e6d793
MG
2164 res = gethostname (p, len);
2165 while (res == -1 && errno == ENAMETOOLONG)
2166 {
94e6d793 2167 len *= 2;
b28f5b3c
DH
2168
2169 /* scm_realloc may throw an exception. */
2170 p = scm_realloc (p, len);
94e6d793
MG
2171 res = gethostname (p, len);
2172 }
b28f5b3c
DH
2173
2174#endif
2175
94e6d793
MG
2176 if (res == -1)
2177 {
b28f5b3c
DH
2178 const int save_errno = errno;
2179
2b829bbb 2180 /* No guile exceptions can occur before we have freed p's memory. */
661ae7ab 2181 scm_dynwind_end ();
4c9419ac 2182 free (p);
b28f5b3c 2183
22865124 2184 errno = save_errno;
94e6d793
MG
2185 SCM_SYSERROR;
2186 }
b28f5b3c
DH
2187 else
2188 {
cc95e00a
MV
2189 /* scm_from_locale_string may throw an exception. */
2190 const SCM name = scm_from_locale_string (p);
b28f5b3c 2191
2b829bbb 2192 /* No guile exceptions can occur before we have freed p's memory. */
661ae7ab 2193 scm_dynwind_end ();
b28f5b3c
DH
2194 free (p);
2195
2196 return name;
2197 }
94e6d793
MG
2198}
2199#undef FUNC_NAME
2200#endif /* HAVE_GETHOSTNAME */
2201
fe613fe2 2202\f
b89c4943 2203void
0f2d19dd 2204scm_init_posix ()
0f2d19dd
JB
2205{
2206 scm_add_feature ("posix");
2207#ifdef HAVE_GETEUID
2208 scm_add_feature ("EIDs");
2209#endif
2210#ifdef WAIT_ANY
e11e83f3 2211 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
0f2d19dd
JB
2212#endif
2213#ifdef WAIT_MYPGRP
e11e83f3 2214 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
0f2d19dd
JB
2215#endif
2216#ifdef WNOHANG
e11e83f3 2217 scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
0f2d19dd
JB
2218#endif
2219#ifdef WUNTRACED
e11e83f3 2220 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
0f2d19dd
JB
2221#endif
2222
0f2d19dd 2223 /* access() symbols. */
e11e83f3
MV
2224 scm_c_define ("R_OK", scm_from_int (R_OK));
2225 scm_c_define ("W_OK", scm_from_int (W_OK));
2226 scm_c_define ("X_OK", scm_from_int (X_OK));
2227 scm_c_define ("F_OK", scm_from_int (F_OK));
0f2d19dd
JB
2228
2229#ifdef LC_COLLATE
e11e83f3 2230 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
0f2d19dd
JB
2231#endif
2232#ifdef LC_CTYPE
e11e83f3 2233 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
0f2d19dd
JB
2234#endif
2235#ifdef LC_MONETARY
e11e83f3 2236 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
0f2d19dd
JB
2237#endif
2238#ifdef LC_NUMERIC
e11e83f3 2239 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
0f2d19dd
JB
2240#endif
2241#ifdef LC_TIME
e11e83f3 2242 scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
0f2d19dd
JB
2243#endif
2244#ifdef LC_MESSAGES
e11e83f3 2245 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
0f2d19dd
JB
2246#endif
2247#ifdef LC_ALL
e11e83f3 2248 scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
0f2d19dd 2249#endif
9361f762
MV
2250#ifdef LC_PAPER
2251 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
2252#endif
2253#ifdef LC_NAME
2254 scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
2255#endif
2256#ifdef LC_ADDRESS
2257 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
2258#endif
2259#ifdef LC_TELEPHONE
2260 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
2261#endif
2262#ifdef LC_MEASUREMENT
2263 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
2264#endif
2265#ifdef LC_IDENTIFICATION
2266 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
2267#endif
bd9e24b3 2268#ifdef PIPE_BUF
b9bd8526 2269 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
bd9e24b3
GH
2270#endif
2271
94e6d793 2272#ifdef PRIO_PROCESS
e11e83f3 2273 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
94e6d793
MG
2274#endif
2275#ifdef PRIO_PGRP
e11e83f3 2276 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
94e6d793
MG
2277#endif
2278#ifdef PRIO_USER
e11e83f3 2279 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
94e6d793
MG
2280#endif
2281
2282#ifdef LOCK_SH
e11e83f3 2283 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
94e6d793
MG
2284#endif
2285#ifdef LOCK_EX
e11e83f3 2286 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
94e6d793
MG
2287#endif
2288#ifdef LOCK_UN
e11e83f3 2289 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
94e6d793
MG
2290#endif
2291#ifdef LOCK_NB
e11e83f3 2292 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
94e6d793
MG
2293#endif
2294
648da032 2295#include "libguile/cpp-SIG.c"
a0599745 2296#include "libguile/posix.x"
0f2d19dd 2297}
89e00824
ML
2298
2299/*
2300 Local Variables:
2301 c-file-style: "gnu"
2302 End:
2303*/