1.0.9.27: Fix large file support for MIPS.
[sbcl/tcr.git] / src / runtime / wrap.c
blob376beaf649c39226c233c53b5ed5b70711ac196b
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 #ifndef LISP_FEATURE_WIN32
36 #include <pwd.h>
37 #include <sys/wait.h>
38 #include <netdb.h>
39 #endif
40 #include <stdio.h>
42 #include "runtime.h"
43 #include "util.h"
45 /* Although it might seem as though this should be in some standard
46 Unix header, according to Perry E. Metzger, in a message on
47 sbcl-devel dated 2004-03-29, this is the POSIXly-correct way of
48 using environ: by an explicit declaration. -- CSR, 2004-03-30 */
49 extern char **environ;
52 * stuff needed by CL:DIRECTORY and other Lisp directory operations
55 /* Unix directory operations think of "." and ".." as filenames, but
56 * Lisp directory operations do not. */
57 int
58 is_lispy_filename(const char *filename)
60 return strcmp(filename, ".") && strcmp(filename, "..");
63 /* Return a zero-terminated array of strings holding the Lispy filenames
64 * (i.e. excluding the Unix magic "." and "..") in the named directory. */
65 char**
66 alloc_directory_lispy_filenames(const char *directory_name)
68 DIR *dir_ptr = opendir(directory_name);
69 char **result = 0;
71 if (dir_ptr) { /* if opendir success */
73 struct voidacc va;
75 if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
76 struct dirent *dirent_ptr;
78 while ( (dirent_ptr = readdir(dir_ptr)) ) { /* until end of data */
79 char* original_name = dirent_ptr->d_name;
80 if (is_lispy_filename(original_name)) {
81 /* strdup(3) is in Linux and *BSD. If you port
82 * somewhere else that doesn't have it, it's easy
83 * to reimplement. */
84 char* dup_name = strdup(original_name);
85 if (!dup_name) { /* if strdup failure */
86 goto dtors;
88 if (voidacc_acc(&va, dup_name)) { /* if acc failure */
89 goto dtors;
93 result = (char**)voidacc_give_away_result(&va);
96 dtors:
97 voidacc_dtor(&va);
98 /* ignoring closedir(3) return code, since what could we do?
100 * "Never ask questions you don't want to know the answer to."
101 * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
102 closedir(dir_ptr);
105 return result;
108 /* Free a result returned by alloc_directory_lispy_filenames(). */
109 void
110 free_directory_lispy_filenames(char** directory_lispy_filenames)
112 char** p;
114 /* Free the strings. */
115 for (p = directory_lispy_filenames; *p; ++p) {
116 free(*p);
119 /* Free the table of strings. */
120 free(directory_lispy_filenames);
124 * readlink(2) stuff
127 #ifndef LISP_FEATURE_WIN32
128 /* a wrapped version of readlink(2):
129 * -- If path isn't a symlink, or is a broken symlink, return 0.
130 * -- If path is a symlink, return a newly allocated string holding
131 * the thing it's linked to. */
132 char *
133 wrapped_readlink(char *path)
135 int bufsiz = strlen(path) + 16;
136 while (1) {
137 char *result = malloc(bufsiz);
138 int n_read = readlink(path, result, bufsiz);
139 if (n_read < 0) {
140 free(result);
141 return 0;
142 } else if (n_read < bufsiz) {
143 result[n_read] = 0;
144 return result;
145 } else {
146 free(result);
147 bufsiz *= 2;
151 #endif
154 * stat(2) stuff
157 /* As of 0.6.12, the FFI can't handle 64-bit values. For now, we use
158 * these munged-to-32-bits values for might-be-64-bit slots of
159 * stat_wrapper as a workaround, so that at least we can still work
160 * when values are small.
162 * FIXME: But of course we should fix the FFI so that we can use the
163 * actual 64-bit values instead. In fact, we probably have by now
164 * (2003-10-03) on all working platforms except MIPS and HPPA; if some
165 * motivated spark would simply fix those, this hack could go away.
166 * -- CSR, 2003-10-03
168 * Some motivated spark fixed MIPS. -- ths, 2005-10-06 */
170 #if defined(LISP_FEATURE_LARGEFILE) && !defined(LISP_FEATURE_MIPS)
171 typedef dev_t ffi_dev_t;
172 typedef off_t ffi_off_t;
173 #elif defined(LISP_FEATURE_MIPS)
174 typedef unsigned long ffi_dev_t; /* Linux/MIPS struct stat doesn't use dev_t */
175 typedef off_t ffi_off_t;
176 #elif defined(LISP_FEATURE_DARWIN)
177 typedef dev_t ffi_dev_t;
178 typedef off_t ffi_off_t;
179 #else
180 typedef u32 ffi_dev_t; /* since Linux dev_t can be 64 bits */
181 typedef u32 ffi_off_t; /* since OpenBSD 2.8 st_size is 64 bits */
182 #endif
184 #ifdef LISP_FEATURE_OS_PROVIDES_BLKSIZE_T
185 typedef blksize_t ffi_blksize_t;
186 #else
187 typedef unsigned long ffi_blksize_t;
188 #endif
190 /* a representation of stat(2) results which doesn't depend on CPU or OS */
191 struct stat_wrapper {
192 /* KLUDGE: The verbose wrapped_st_ prefixes are to protect us from
193 * the C preprocessor as wielded by the fiends of OpenBSD, who do
194 * things like
195 * #define st_atime st_atimespec.tv_sec
196 * I remember when I was young and innocent, I read about how the
197 * C preprocessor isn't to be used to globally munge random
198 * lowercase symbols like this, because things like this could
199 * happen, and I nodded sagely. But now I know better.:-| This is
200 * another entry for Dan Barlow's ongoing episodic rant about C
201 * header files, I guess.. -- WHN 2001-05-10 */
202 ffi_dev_t wrapped_st_dev; /* device */
203 ino_t wrapped_st_ino; /* inode */
204 mode_t wrapped_st_mode; /* protection */
205 #ifndef LISP_FEATURE_WIN32
206 nlink_t wrapped_st_nlink; /* number of hard links */
207 uid_t wrapped_st_uid; /* user ID of owner */
208 gid_t wrapped_st_gid; /* group ID of owner */
209 #else
210 short wrapped_st_nlink; /* Win32 doesn't have nlink_t */
211 short wrapped_st_uid; /* Win32 doesn't have st_uid */
212 short wrapped_st_gid; /* Win32 doesn't have st_gid */
213 #endif
214 ffi_dev_t wrapped_st_rdev; /* device type (if inode device) */
215 ffi_off_t wrapped_st_size; /* total size, in bytes */
216 ffi_blksize_t wrapped_st_blksize; /* blocksize for filesystem I/O */
217 unsigned long wrapped_st_blocks; /* number of blocks allocated */
218 time_t wrapped_st_atime; /* time_t of last access */
219 time_t wrapped_st_mtime; /* time_t of last modification */
220 time_t wrapped_st_ctime; /* time_t of last change */
223 static void
224 copy_to_stat_wrapper(struct stat_wrapper *to, struct stat *from)
226 #define FROB(stem) to->wrapped_st_##stem = from->st_##stem
227 #ifndef LISP_FEATURE_WIN32
228 #define FROB2(stem) to->wrapped_st_##stem = from->st_##stem
229 #else
230 #define FROB2(stem) to->wrapped_st_##stem = 0;
231 #endif
232 FROB(dev);
233 FROB2(ino);
234 FROB(mode);
235 FROB(nlink);
236 FROB2(uid);
237 FROB2(gid);
238 FROB(rdev);
239 FROB(size);
240 FROB2(blksize);
241 FROB2(blocks);
242 FROB(atime);
243 FROB(mtime);
244 FROB(ctime);
245 #undef FROB
249 stat_wrapper(const char *file_name, struct stat_wrapper *buf)
251 struct stat real_buf;
252 int ret;
254 #ifdef LISP_FEATURE_WIN32
256 * Windows won't match the last component of a pathname if there
257 * is a trailing #\/ or #\\, except if it's <drive>:\ or <drive>:/
258 * in which case it behaves the other way around. So we remove the
259 * trailing directory separator unless we are being passed just a
260 * drive name (e.g. "c:\\"). Some, but not all, of this
261 * strangeness is documented at Microsoft's support site (as of
262 * 2006-01-08, at
263 * <http://support.microsoft.com/default.aspx?scid=kb;en-us;168439>)
265 char file_buf[MAX_PATH];
266 strcpy(file_buf, file_name);
267 int len = strlen(file_name);
268 if (len != 0 && (file_name[len-1] == '/' || file_name[len-1] == '\\') &&
269 !(len == 3 && file_name[1] == ':' && isalpha(file_name[0])))
270 file_buf[len-1] = '\0';
271 file_name = file_buf;
272 #endif
274 if ((ret = stat(file_name,&real_buf)) >= 0)
275 copy_to_stat_wrapper(buf, &real_buf);
276 return ret;
279 #ifndef LISP_FEATURE_WIN32
281 lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
283 struct stat real_buf;
284 int ret;
285 if ((ret = lstat(file_name,&real_buf)) >= 0)
286 copy_to_stat_wrapper(buf, &real_buf);
287 return ret;
289 #else
290 /* cleaner to do it here than in Lisp */
291 int lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
293 return stat_wrapper(file_name, buf);
295 #endif
298 fstat_wrapper(int filedes, struct stat_wrapper *buf)
300 struct stat real_buf;
301 int ret;
302 if ((ret = fstat(filedes,&real_buf)) >= 0)
303 copy_to_stat_wrapper(buf, &real_buf);
304 return ret;
308 * getpwuid() stuff
311 #ifndef LISP_FEATURE_WIN32
312 /* Return a newly-allocated string holding the username for "uid", or
313 * NULL if there's no such user.
315 * KLUDGE: We also return NULL if malloc() runs out of memory
316 * (returning strdup() result) since it's not clear how to handle that
317 * error better. -- WHN 2001-12-28 */
318 char *
319 uid_username(int uid)
321 struct passwd *p = getpwuid(uid);
322 if (p) {
323 /* The object *p is a static struct which'll be overwritten by
324 * the next call to getpwuid(), so it'd be unsafe to return
325 * p->pw_name without copying. */
326 return strdup(p->pw_name);
327 } else {
328 return 0;
332 char *
333 uid_homedir(uid_t uid)
335 struct passwd *p = getpwuid(uid);
336 if(p) {
337 /* Let's be careful about this, shall we? */
338 size_t len = strlen(p->pw_dir);
339 if (p->pw_dir[len-1] == '/') {
340 return strdup(p->pw_dir);
341 } else {
342 char *result = malloc(len + 2);
343 if (result) {
344 int nchars = sprintf(result,"%s/",p->pw_dir);
345 if (nchars == len + 1) {
346 return result;
347 } else {
348 return 0;
350 } else {
351 return 0;
354 } else {
355 return 0;
358 #endif /* !LISP_FEATURE_WIN32 */
361 * functions to get miscellaneous C-level variables
363 * (Doing this by calling functions lets us borrow the smarts of the C
364 * linker, so that things don't blow up when libc versions and thus
365 * variable locations change between compile time and run time.)
368 char **
369 wrapped_environ()
371 return environ;
374 #ifdef LISP_FEATURE_WIN32
375 #define WIN32_LEAN_AND_MEAN
376 #include <windows.h>
377 #include <time.h>
379 * faked-up implementation of select(). Right now just enough to get through
380 * second genesis.
382 int select(int top_fd, DWORD *read_set, DWORD *write_set, DWORD *except_set, time_t *timeout)
385 * FIXME: Going forward, we may want to use MsgWaitForMultipleObjects
386 * in order to support a windows message loop inside serve-event.
388 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
389 int fds[MAXIMUM_WAIT_OBJECTS];
390 int num_handles;
391 int i;
392 DWORD retval;
393 int polling_write;
394 DWORD win_timeout;
396 num_handles = 0;
397 polling_write = 0;
398 for (i = 0; i < top_fd; i++) {
399 if (except_set) except_set[i >> 5] = 0;
400 if (write_set && (write_set[i >> 5] & (1 << (i & 31)))) polling_write = 1;
401 if (read_set[i >> 5] & (1 << (i & 31))) {
402 read_set[i >> 5] &= ~(1 << (i & 31));
403 fds[num_handles] = i;
404 handles[num_handles++] = (HANDLE) _get_osfhandle(i);
408 win_timeout = INFINITE;
409 if (timeout) win_timeout = (timeout[0] * 1000) + timeout[1];
411 /* Last parameter here is timeout in milliseconds. */
412 /* retval = WaitForMultipleObjects(num_handles, handles, 0, INFINITE); */
413 retval = WaitForMultipleObjects(num_handles, handles, 0, win_timeout);
415 if (retval < WAIT_ABANDONED) {
416 /* retval, at this point, is the index of the single live HANDLE/fd. */
417 read_set[fds[retval] >> 5] |= (1 << (fds[retval] & 31));
418 return 1;
420 return polling_write;
424 * Windows doesn't have gettimeofday(), and we need it for the compiler,
425 * for serve-event, and for a couple other things. We don't need a timezone
426 * yet, however, and the closest we can easily get to a timeval is the
427 * seconds part. So that's what we do.
429 int gettimeofday(long *timeval, long *timezone)
431 timeval[0] = time(NULL);
432 timeval[1] = 0;
434 return 0;
436 #endif
439 /* We will need to define these things or their equivalents for Win32
440 eventually, but for now let's get it working for everyone else. */
441 #ifndef LISP_FEATURE_WIN32
442 /* From SB-BSD-SOCKETS, to get h_errno */
443 int get_h_errno()
445 return h_errno;
448 /* From SB-POSIX, wait-macros */
449 int wifexited(int status) {
450 return WIFEXITED(status);
452 int wexitstatus(int status) {
453 return WEXITSTATUS(status);
455 int wifsignaled(int status) {
456 return WIFSIGNALED(status);
458 int wtermsig(int status) {
459 return WTERMSIG(status);
461 int wifstopped(int status) {
462 return WIFSTOPPED(status);
464 int wstopsig(int status) {
465 return WSTOPSIG(status);
467 /* FIXME: POSIX also defines WIFCONTINUED, but that appears not to
468 exist on at least Linux... */
469 #endif /* !LISP_FEATURE_WIN32 */
471 /* From SB-POSIX, stat-macros */
472 int s_isreg(mode_t mode)
474 return S_ISREG(mode);
476 int s_isdir(mode_t mode)
478 return S_ISDIR(mode);
480 int s_ischr(mode_t mode)
482 return S_ISCHR(mode);
484 int s_isblk(mode_t mode)
486 return S_ISBLK(mode);
488 int s_isfifo(mode_t mode)
490 return S_ISFIFO(mode);
492 #ifndef LISP_FEATURE_WIN32
493 int s_islnk(mode_t mode)
495 #ifdef S_ISLNK
496 return S_ISLNK(mode);
497 #else
498 return ((mode & S_IFMT) == S_IFLNK);
499 #endif
501 int s_issock(mode_t mode)
503 #ifdef S_ISSOCK
504 return S_ISSOCK(mode);
505 #else
506 return ((mode & S_IFMT) == S_IFSOCK);
507 #endif
509 #endif /* !LISP_FEATURE_WIN32 */