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