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