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
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.
28 #include <sys/types.h>
39 #ifndef LISP_FEATURE_WIN32
46 #if defined(LISP_FEATURE_WIN32)
47 #define WIN32_LEAN_AND_MEAN
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. */
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. */
76 alloc_directory_lispy_filenames(const char *directory_name
)
78 DIR *dir_ptr
= opendir(directory_name
);
81 if (dir_ptr
) { /* if opendir success */
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
94 char* dup_name
= strdup(original_name
);
95 if (!dup_name
) { /* if strdup failure */
98 if (voidacc_acc(&va
, dup_name
)) { /* if acc failure */
103 result
= (char**)voidacc_give_away_result(&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_) */
118 /* Free a result returned by alloc_directory_lispy_filenames(). */
120 free_directory_lispy_filenames(char** directory_lispy_filenames
)
124 /* Free the strings. */
125 for (p
= directory_lispy_filenames
; *p
; ++p
) {
129 /* Free the table of strings. */
130 free(directory_lispy_filenames
);
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. */
143 wrapped_readlink(char *path
)
145 int bufsiz
= strlen(path
) + 16;
147 char *result
= malloc(bufsiz
);
148 int n_read
= readlink(path
, result
, bufsiz
);
152 } else if (n_read
< bufsiz
) {
164 * realpath(3), including a wrapper for Windows.
166 char * sb_realpath (char *path
)
168 #ifndef LISP_FEATURE_WIN32
172 if ((ret
= calloc(PATH_MAX
, sizeof(char))) == NULL
)
174 if (realpath(path
, ret
) == NULL
) {
186 if ((ret
= calloc(MAX_PATH
, sizeof(char))) == NULL
)
188 if (GetFullPathName(path
, MAX_PATH
, ret
, cp
) == 0) {
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
209 #define FROB2(stem) to->wrapped_st_##stem = 0;
228 stat_wrapper(const char *file_name
, struct stat_wrapper
*buf
)
230 struct stat real_buf
;
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
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
;
253 if ((ret
= stat(file_name
,&real_buf
)) >= 0)
254 copy_to_stat_wrapper(buf
, &real_buf
);
258 #ifndef LISP_FEATURE_WIN32
260 lstat_wrapper(const char *file_name
, struct stat_wrapper
*buf
)
262 struct stat real_buf
;
264 if ((ret
= lstat(file_name
,&real_buf
)) >= 0)
265 copy_to_stat_wrapper(buf
, &real_buf
);
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
);
277 fstat_wrapper(int filedes
, struct stat_wrapper
*buf
)
279 struct stat real_buf
;
281 if ((ret
= fstat(filedes
,&real_buf
)) >= 0)
282 copy_to_stat_wrapper(buf
, &real_buf
);
285 #ifdef LISP_FEATURE_WIN32
286 /* Eventually, probably all Windows file system probes should go
287 through GetFileAttributesEx, but for now, we'll use it just to poke
288 for device files when stat() fails. */
289 int sb_probe_win32_file (char *filename
)
291 WIN32_FILE_ATTRIBUTE_DATA info
;
292 return (GetFileAttributes(filename
)!=INVALID_FILE_ATTRIBUTES
);
300 #ifndef LISP_FEATURE_WIN32
301 /* Return a newly-allocated string holding the username for "uid", or
302 * NULL if there's no such user.
304 * KLUDGE: We also return NULL if malloc() runs out of memory
305 * (returning strdup() result) since it's not clear how to handle that
306 * error better. -- WHN 2001-12-28 */
308 uid_username(int uid
)
310 struct passwd
*p
= getpwuid(uid
);
312 /* The object *p is a static struct which'll be overwritten by
313 * the next call to getpwuid(), so it'd be unsafe to return
314 * p->pw_name without copying. */
315 return strdup(p
->pw_name
);
322 uid_homedir(uid_t uid
)
324 struct passwd
*p
= getpwuid(uid
);
326 /* Let's be careful about this, shall we? */
327 size_t len
= strlen(p
->pw_dir
);
328 if (p
->pw_dir
[len
-1] == '/') {
329 return strdup(p
->pw_dir
);
331 char *result
= malloc(len
+ 2);
333 unsigned int nchars
= sprintf(result
,"%s/",p
->pw_dir
);
334 if (nchars
== len
+ 1) {
347 #endif /* !LISP_FEATURE_WIN32 */
350 * functions to get miscellaneous C-level variables
352 * (Doing this by calling functions lets us borrow the smarts of the C
353 * linker, so that things don't blow up when libc versions and thus
354 * variable locations change between compile time and run time.)
363 #ifdef LISP_FEATURE_WIN32
367 * faked-up implementation of select(). Right now just enough to get through
370 int select(int top_fd
, DWORD
*read_set
, DWORD
*write_set
, DWORD
*except_set
, time_t *timeout
)
373 * FIXME: Going forward, we may want to use MsgWaitForMultipleObjects
374 * in order to support a windows message loop inside serve-event.
376 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
377 int fds
[MAXIMUM_WAIT_OBJECTS
];
386 for (i
= 0; i
< top_fd
; i
++) {
387 if (except_set
) except_set
[i
>> 5] = 0;
388 if (write_set
&& (write_set
[i
>> 5] & (1 << (i
& 31)))) polling_write
= 1;
389 if (read_set
[i
>> 5] & (1 << (i
& 31))) {
390 read_set
[i
>> 5] &= ~(1 << (i
& 31));
391 fds
[num_handles
] = i
;
392 handles
[num_handles
++] = (HANDLE
) _get_osfhandle(i
);
396 win_timeout
= INFINITE
;
397 if (timeout
) win_timeout
= (timeout
[0] * 1000) + timeout
[1];
399 /* Last parameter here is timeout in milliseconds. */
400 /* retval = WaitForMultipleObjects(num_handles, handles, 0, INFINITE); */
401 retval
= WaitForMultipleObjects(num_handles
, handles
, 0, win_timeout
);
403 if (retval
< WAIT_ABANDONED
) {
404 /* retval, at this point, is the index of the single live HANDLE/fd. */
405 read_set
[fds
[retval
] >> 5] |= (1 << (fds
[retval
] & 31));
408 return polling_write
;
412 * Windows doesn't have gettimeofday(), and we need it for the compiler,
413 * for serve-event, and for a couple other things. We don't need a timezone
414 * yet, however, and the closest we can easily get to a timeval is the
415 * seconds part. So that's what we do.
417 int gettimeofday(long *timeval
, long *timezone
)
419 timeval
[0] = time(NULL
);
427 /* We will need to define these things or their equivalents for Win32
428 eventually, but for now let's get it working for everyone else. */
429 #ifndef LISP_FEATURE_WIN32
430 /* From SB-BSD-SOCKETS, to get h_errno */
436 /* From SB-POSIX, wait-macros */
437 int wifexited(int status
) {
438 return WIFEXITED(status
);
440 int wexitstatus(int status
) {
441 return WEXITSTATUS(status
);
443 int wifsignaled(int status
) {
444 return WIFSIGNALED(status
);
446 int wtermsig(int status
) {
447 return WTERMSIG(status
);
449 int wifstopped(int status
) {
450 return WIFSTOPPED(status
);
452 int wstopsig(int status
) {
453 return WSTOPSIG(status
);
455 /* FIXME: POSIX also defines WIFCONTINUED, but that appears not to
456 exist on at least Linux... */
457 #endif /* !LISP_FEATURE_WIN32 */
459 /* From SB-POSIX, stat-macros */
460 int s_isreg(mode_t mode
)
462 return S_ISREG(mode
);
464 int s_isdir(mode_t mode
)
466 return S_ISDIR(mode
);
468 int s_ischr(mode_t mode
)
470 return S_ISCHR(mode
);
472 int s_isblk(mode_t mode
)
474 return S_ISBLK(mode
);
476 int s_isfifo(mode_t mode
)
478 return S_ISFIFO(mode
);
480 #ifndef LISP_FEATURE_WIN32
481 int s_islnk(mode_t mode
)
484 return S_ISLNK(mode
);
486 return ((mode
& S_IFMT
) == S_IFLNK
);
489 int s_issock(mode_t mode
)
492 return S_ISSOCK(mode
);
494 return ((mode
& S_IFMT
) == S_IFSOCK
);
497 #endif /* !LISP_FEATURE_WIN32 */