1.0.13.19: Odds and ends (OpenBSD NEWS, minor bug in PROBE-FILE, mkstemp())
[sbcl/simd.git] / src / runtime / wrap.c
blob8e17e3711176b6801593556bfd1ae74fbc7d7848
1 /*
2 * wrappers around low-level operations to provide a simpler interface
3 * to the operations that Lisp (and some contributed modules) needs.
5 * The functions in this file are typically called directly from Lisp.
6 * Thus, when their signature changes, they don't need updates in a .h
7 * file somewhere, but they do need updates in the Lisp code. FIXME:
8 * It would be nice to enforce this at compile time. It mighn't even
9 * be all that hard: make the cross-compiler versions of DEFINE-ALIEN-FOO
10 * macros accumulate strings in a list which then gets written out at
11 * the end of sbcl2.h at the end of cross-compilation, then rerun
12 * 'make' in src/runtime/ using the new sbcl2.h as sbcl.h (and make
13 * sure that all the files in src/runtime/ include sbcl.h). */
16 * This software is part of the SBCL system. See the README file for
17 * more information.
19 * This software is derived from the CMU CL system, which was
20 * written at Carnegie Mellon University and released into the
21 * public domain. The software is in the public domain and is
22 * provided with absolutely no warranty. See the COPYING and CREDITS
23 * files for more information.
26 #include "sbcl.h"
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <fcntl.h>
39 #ifndef LISP_FEATURE_WIN32
40 #include <pwd.h>
41 #include <sys/wait.h>
42 #include <netdb.h>
43 #endif
44 #include <stdio.h>
46 #if defined(LISP_FEATURE_WIN32)
47 #define WIN32_LEAN_AND_MEAN
48 #include <errno.h>
49 #endif
51 #include "runtime.h"
52 #include "util.h"
53 #include "wrap.h"
55 /* Although it might seem as though this should be in some standard
56 Unix header, according to Perry E. Metzger, in a message on
57 sbcl-devel dated 2004-03-29, this is the POSIXly-correct way of
58 using environ: by an explicit declaration. -- CSR, 2004-03-30 */
59 extern char **environ;
62 * stuff needed by CL:DIRECTORY and other Lisp directory operations
65 /* Unix directory operations think of "." and ".." as filenames, but
66 * Lisp directory operations do not. */
67 int
68 is_lispy_filename(const char *filename)
70 return strcmp(filename, ".") && strcmp(filename, "..");
73 /* Return a zero-terminated array of strings holding the Lispy filenames
74 * (i.e. excluding the Unix magic "." and "..") in the named directory. */
75 char**
76 alloc_directory_lispy_filenames(const char *directory_name)
78 DIR *dir_ptr = opendir(directory_name);
79 char **result = 0;
81 if (dir_ptr) { /* if opendir success */
83 struct voidacc va;
85 if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
86 struct dirent *dirent_ptr;
88 while ( (dirent_ptr = readdir(dir_ptr)) ) { /* until end of data */
89 char* original_name = dirent_ptr->d_name;
90 if (is_lispy_filename(original_name)) {
91 /* strdup(3) is in Linux and *BSD. If you port
92 * somewhere else that doesn't have it, it's easy
93 * to reimplement. */
94 char* dup_name = strdup(original_name);
95 if (!dup_name) { /* if strdup failure */
96 goto dtors;
98 if (voidacc_acc(&va, dup_name)) { /* if acc failure */
99 goto dtors;
103 result = (char**)voidacc_give_away_result(&va);
106 dtors:
107 voidacc_dtor(&va);
108 /* ignoring closedir(3) return code, since what could we do?
110 * "Never ask questions you don't want to know the answer to."
111 * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
112 closedir(dir_ptr);
115 return result;
118 /* Free a result returned by alloc_directory_lispy_filenames(). */
119 void
120 free_directory_lispy_filenames(char** directory_lispy_filenames)
122 char** p;
124 /* Free the strings. */
125 for (p = directory_lispy_filenames; *p; ++p) {
126 free(*p);
129 /* Free the table of strings. */
130 free(directory_lispy_filenames);
134 * readlink(2) stuff
137 #ifndef LISP_FEATURE_WIN32
138 /* a wrapped version of readlink(2):
139 * -- If path isn't a symlink, or is a broken symlink, return 0.
140 * -- If path is a symlink, return a newly allocated string holding
141 * the thing it's linked to. */
142 char *
143 wrapped_readlink(char *path)
145 int bufsiz = strlen(path) + 16;
146 while (1) {
147 char *result = malloc(bufsiz);
148 int n_read = readlink(path, result, bufsiz);
149 if (n_read < 0) {
150 free(result);
151 return 0;
152 } else if (n_read < bufsiz) {
153 result[n_read] = 0;
154 return result;
155 } else {
156 free(result);
157 bufsiz *= 2;
161 #endif
164 * realpath(3), including a wrapper for Windows.
166 char * sb_realpath (char *path)
168 #ifndef LISP_FEATURE_WIN32
169 char *ret;
170 int errnum;
172 if ((ret = calloc(PATH_MAX, sizeof(char))) == NULL)
173 return NULL;
174 if (realpath(path, ret) == NULL) {
175 errnum = errno;
176 free(ret);
177 errno = errnum;
178 return NULL;
180 return(ret);
181 #else
182 char *ret;
183 char *cp;
184 int errnum;
186 if ((ret = calloc(MAX_PATH, sizeof(char))) == NULL)
187 return NULL;
188 if (GetFullPathName(path, MAX_PATH, ret, cp) == 0) {
189 errnum = errno;
190 free(ret);
191 errno = errnum;
192 return NULL;
194 return(ret);
195 #endif
199 * stat(2) stuff
202 static void
203 copy_to_stat_wrapper(struct stat_wrapper *to, struct stat *from)
205 #define FROB(stem) to->wrapped_st_##stem = from->st_##stem
206 #ifndef LISP_FEATURE_WIN32
207 #define FROB2(stem) to->wrapped_st_##stem = from->st_##stem
208 #else
209 #define FROB2(stem) to->wrapped_st_##stem = 0;
210 #endif
211 FROB(dev);
212 FROB2(ino);
213 FROB(mode);
214 FROB(nlink);
215 FROB2(uid);
216 FROB2(gid);
217 FROB(rdev);
218 FROB(size);
219 FROB2(blksize);
220 FROB2(blocks);
221 FROB(atime);
222 FROB(mtime);
223 FROB(ctime);
224 #undef FROB
228 stat_wrapper(const char *file_name, struct stat_wrapper *buf)
230 struct stat real_buf;
231 int ret;
233 #ifdef LISP_FEATURE_WIN32
235 * Windows won't match the last component of a pathname if there
236 * is a trailing #\/ or #\\, except if it's <drive>:\ or <drive>:/
237 * in which case it behaves the other way around. So we remove the
238 * trailing directory separator unless we are being passed just a
239 * drive name (e.g. "c:\\"). Some, but not all, of this
240 * strangeness is documented at Microsoft's support site (as of
241 * 2006-01-08, at
242 * <http://support.microsoft.com/default.aspx?scid=kb;en-us;168439>)
244 char file_buf[MAX_PATH];
245 strcpy(file_buf, file_name);
246 int len = strlen(file_name);
247 if (len != 0 && (file_name[len-1] == '/' || file_name[len-1] == '\\') &&
248 !(len == 3 && file_name[1] == ':' && isalpha(file_name[0])))
249 file_buf[len-1] = '\0';
250 file_name = file_buf;
251 #endif
253 if ((ret = stat(file_name,&real_buf)) >= 0)
254 copy_to_stat_wrapper(buf, &real_buf);
255 return ret;
258 #ifndef LISP_FEATURE_WIN32
260 lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
262 struct stat real_buf;
263 int ret;
264 if ((ret = lstat(file_name,&real_buf)) >= 0)
265 copy_to_stat_wrapper(buf, &real_buf);
266 return ret;
268 #else
269 /* cleaner to do it here than in Lisp */
270 int lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
272 return stat_wrapper(file_name, buf);
274 #endif
277 fstat_wrapper(int filedes, struct stat_wrapper *buf)
279 struct stat real_buf;
280 int ret;
281 if ((ret = fstat(filedes,&real_buf)) >= 0)
282 copy_to_stat_wrapper(buf, &real_buf);
283 return ret;
286 /* A wrapper for mkstemp(3), for two reasons: (1) mkstemp does not
287 exist on Windows; (2) by passing down a mode_t, we don't need a
288 binding to chmod in SB-UNIX, and need not concern ourselves with
289 umask issues if we want to use mkstemp to make new files in
290 OPEN. */
291 int sb_mkstemp (char *template, mode_t mode) {
292 #ifdef LISP_FEATURE_WIN32
293 #define PATHNAME_BUFFER_SIZE MAX_PATH
294 #define MKTEMP _mktemp
295 #else
296 #define PATHNAME_BUFFER_SIZE PATH_MAX
297 #define MKTEMP mktemp
298 #endif
299 int fd;
300 char buf[PATHNAME_BUFFER_SIZE];
302 while (1) {
303 /* Fruit fallen from the tree: for people who like
304 microoptimizations, we might not need to copy the whole
305 template on every loop, but only the last several characters.
306 But I didn't feel like testing the boundary cases in Windows's
307 _mktemp. */
308 strcpy((char*)&buf, template);
309 if (MKTEMP((char*)&buf)) {
310 if ((fd=open((char*)&buf, O_CREAT|O_EXCL|O_RDWR, mode))!=-1) {
311 strcpy(template, (char*)&buf);
312 return (fd);
313 } else
314 if (errno != EEXIST)
315 return (-1);
316 } else
317 return (-1);
319 #undef MKTEMP
320 #undef PATHNAME_BUFFER_SIZE
325 * getpwuid() stuff
328 #ifndef LISP_FEATURE_WIN32
329 /* Return a newly-allocated string holding the username for "uid", or
330 * NULL if there's no such user.
332 * KLUDGE: We also return NULL if malloc() runs out of memory
333 * (returning strdup() result) since it's not clear how to handle that
334 * error better. -- WHN 2001-12-28 */
335 char *
336 uid_username(int uid)
338 struct passwd *p = getpwuid(uid);
339 if (p) {
340 /* The object *p is a static struct which'll be overwritten by
341 * the next call to getpwuid(), so it'd be unsafe to return
342 * p->pw_name without copying. */
343 return strdup(p->pw_name);
344 } else {
345 return 0;
349 char *
350 uid_homedir(uid_t uid)
352 struct passwd *p = getpwuid(uid);
353 if(p) {
354 /* Let's be careful about this, shall we? */
355 size_t len = strlen(p->pw_dir);
356 if (p->pw_dir[len-1] == '/') {
357 return strdup(p->pw_dir);
358 } else {
359 char *result = malloc(len + 2);
360 if (result) {
361 int nchars = sprintf(result,"%s/",p->pw_dir);
362 if (nchars == len + 1) {
363 return result;
364 } else {
365 return 0;
367 } else {
368 return 0;
371 } else {
372 return 0;
375 #endif /* !LISP_FEATURE_WIN32 */
378 * functions to get miscellaneous C-level variables
380 * (Doing this by calling functions lets us borrow the smarts of the C
381 * linker, so that things don't blow up when libc versions and thus
382 * variable locations change between compile time and run time.)
385 char **
386 wrapped_environ()
388 return environ;
391 #ifdef LISP_FEATURE_WIN32
392 #include <windows.h>
393 #include <time.h>
395 * faked-up implementation of select(). Right now just enough to get through
396 * second genesis.
398 int select(int top_fd, DWORD *read_set, DWORD *write_set, DWORD *except_set, time_t *timeout)
401 * FIXME: Going forward, we may want to use MsgWaitForMultipleObjects
402 * in order to support a windows message loop inside serve-event.
404 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
405 int fds[MAXIMUM_WAIT_OBJECTS];
406 int num_handles;
407 int i;
408 DWORD retval;
409 int polling_write;
410 DWORD win_timeout;
412 num_handles = 0;
413 polling_write = 0;
414 for (i = 0; i < top_fd; i++) {
415 if (except_set) except_set[i >> 5] = 0;
416 if (write_set && (write_set[i >> 5] & (1 << (i & 31)))) polling_write = 1;
417 if (read_set[i >> 5] & (1 << (i & 31))) {
418 read_set[i >> 5] &= ~(1 << (i & 31));
419 fds[num_handles] = i;
420 handles[num_handles++] = (HANDLE) _get_osfhandle(i);
424 win_timeout = INFINITE;
425 if (timeout) win_timeout = (timeout[0] * 1000) + timeout[1];
427 /* Last parameter here is timeout in milliseconds. */
428 /* retval = WaitForMultipleObjects(num_handles, handles, 0, INFINITE); */
429 retval = WaitForMultipleObjects(num_handles, handles, 0, win_timeout);
431 if (retval < WAIT_ABANDONED) {
432 /* retval, at this point, is the index of the single live HANDLE/fd. */
433 read_set[fds[retval] >> 5] |= (1 << (fds[retval] & 31));
434 return 1;
436 return polling_write;
440 * Windows doesn't have gettimeofday(), and we need it for the compiler,
441 * for serve-event, and for a couple other things. We don't need a timezone
442 * yet, however, and the closest we can easily get to a timeval is the
443 * seconds part. So that's what we do.
445 int gettimeofday(long *timeval, long *timezone)
447 timeval[0] = time(NULL);
448 timeval[1] = 0;
450 return 0;
452 #endif
455 /* We will need to define these things or their equivalents for Win32
456 eventually, but for now let's get it working for everyone else. */
457 #ifndef LISP_FEATURE_WIN32
458 /* From SB-BSD-SOCKETS, to get h_errno */
459 int get_h_errno()
461 return h_errno;
464 /* From SB-POSIX, wait-macros */
465 int wifexited(int status) {
466 return WIFEXITED(status);
468 int wexitstatus(int status) {
469 return WEXITSTATUS(status);
471 int wifsignaled(int status) {
472 return WIFSIGNALED(status);
474 int wtermsig(int status) {
475 return WTERMSIG(status);
477 int wifstopped(int status) {
478 return WIFSTOPPED(status);
480 int wstopsig(int status) {
481 return WSTOPSIG(status);
483 /* FIXME: POSIX also defines WIFCONTINUED, but that appears not to
484 exist on at least Linux... */
485 #endif /* !LISP_FEATURE_WIN32 */
487 /* From SB-POSIX, stat-macros */
488 int s_isreg(mode_t mode)
490 return S_ISREG(mode);
492 int s_isdir(mode_t mode)
494 return S_ISDIR(mode);
496 int s_ischr(mode_t mode)
498 return S_ISCHR(mode);
500 int s_isblk(mode_t mode)
502 return S_ISBLK(mode);
504 int s_isfifo(mode_t mode)
506 return S_ISFIFO(mode);
508 #ifndef LISP_FEATURE_WIN32
509 int s_islnk(mode_t mode)
511 #ifdef S_ISLNK
512 return S_ISLNK(mode);
513 #else
514 return ((mode & S_IFMT) == S_IFLNK);
515 #endif
517 int s_issock(mode_t mode)
519 #ifdef S_ISSOCK
520 return S_ISSOCK(mode);
521 #else
522 return ((mode & S_IFMT) == S_IFSOCK);
523 #endif
525 #endif /* !LISP_FEATURE_WIN32 */