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>
35 #ifndef LISP_FEATURE_WIN32
42 #if defined(LISP_FEATURE_WIN32)
43 #define WIN32_LEAN_AND_MEAN
52 /* Although it might seem as though this should be in some standard
53 Unix header, according to Perry E. Metzger, in a message on
54 sbcl-devel dated 2004-03-29, this is the POSIXly-correct way of
55 using environ: by an explicit declaration. -- CSR, 2004-03-30 */
56 extern char **environ
;
59 * stuff needed by CL:DIRECTORY and other Lisp directory operations
62 /* Unix directory operations think of "." and ".." as filenames, but
63 * Lisp directory operations do not. */
65 is_lispy_filename(const char *filename
)
67 return strcmp(filename
, ".") && strcmp(filename
, "..");
70 /* Return a zero-terminated array of strings holding the Lispy filenames
71 * (i.e. excluding the Unix magic "." and "..") in the named directory. */
73 alloc_directory_lispy_filenames(const char *directory_name
)
75 DIR *dir_ptr
= opendir(directory_name
);
78 if (dir_ptr
) { /* if opendir success */
82 if (0 == voidacc_ctor(&va
)) { /* if voidacc_ctor success */
83 struct dirent
*dirent_ptr
;
85 while ( (dirent_ptr
= readdir(dir_ptr
)) ) { /* until end of data */
86 char* original_name
= dirent_ptr
->d_name
;
87 if (is_lispy_filename(original_name
)) {
88 /* strdup(3) is in Linux and *BSD. If you port
89 * somewhere else that doesn't have it, it's easy
91 char* dup_name
= strdup(original_name
);
92 if (!dup_name
) { /* if strdup failure */
95 if (voidacc_acc(&va
, dup_name
)) { /* if acc failure */
100 result
= (char**)voidacc_give_away_result(&va
);
105 /* ignoring closedir(3) return code, since what could we do?
107 * "Never ask questions you don't want to know the answer to."
108 * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
115 /* Free a result returned by alloc_directory_lispy_filenames(). */
117 free_directory_lispy_filenames(char** directory_lispy_filenames
)
121 /* Free the strings. */
122 for (p
= directory_lispy_filenames
; *p
; ++p
) {
126 /* Free the table of strings. */
127 free(directory_lispy_filenames
);
134 #ifndef LISP_FEATURE_WIN32
135 /* a wrapped version of readlink(2):
136 * -- If path isn't a symlink, or is a broken symlink, return 0.
137 * -- If path is a symlink, return a newly allocated string holding
138 * the thing it's linked to. */
140 wrapped_readlink(char *path
)
142 int bufsiz
= strlen(path
) + 16;
144 char *result
= malloc(bufsiz
);
145 int n_read
= readlink(path
, result
, bufsiz
);
149 } else if (n_read
< bufsiz
) {
165 copy_to_stat_wrapper(struct stat_wrapper
*to
, struct stat
*from
)
167 #define FROB(stem) to->wrapped_st_##stem = from->st_##stem
168 #ifndef LISP_FEATURE_WIN32
169 #define FROB2(stem) to->wrapped_st_##stem = from->st_##stem
171 #define FROB2(stem) to->wrapped_st_##stem = 0;
190 stat_wrapper(const char *file_name
, struct stat_wrapper
*buf
)
192 struct stat real_buf
;
195 #ifdef LISP_FEATURE_WIN32
197 * Windows won't match the last component of a pathname if there
198 * is a trailing #\/ or #\\, except if it's <drive>:\ or <drive>:/
199 * in which case it behaves the other way around. So we remove the
200 * trailing directory separator unless we are being passed just a
201 * drive name (e.g. "c:\\"). Some, but not all, of this
202 * strangeness is documented at Microsoft's support site (as of
204 * <http://support.microsoft.com/default.aspx?scid=kb;en-us;168439>)
206 char file_buf
[MAX_PATH
];
207 strcpy(file_buf
, file_name
);
208 int len
= strlen(file_name
);
209 if (len
!= 0 && (file_name
[len
-1] == '/' || file_name
[len
-1] == '\\') &&
210 !(len
== 3 && file_name
[1] == ':' && isalpha(file_name
[0])))
211 file_buf
[len
-1] = '\0';
212 file_name
= file_buf
;
215 if ((ret
= stat(file_name
,&real_buf
)) >= 0)
216 copy_to_stat_wrapper(buf
, &real_buf
);
220 #ifndef LISP_FEATURE_WIN32
222 lstat_wrapper(const char *file_name
, struct stat_wrapper
*buf
)
224 struct stat real_buf
;
226 if ((ret
= lstat(file_name
,&real_buf
)) >= 0)
227 copy_to_stat_wrapper(buf
, &real_buf
);
231 /* cleaner to do it here than in Lisp */
232 int lstat_wrapper(const char *file_name
, struct stat_wrapper
*buf
)
234 return stat_wrapper(file_name
, buf
);
239 fstat_wrapper(int filedes
, struct stat_wrapper
*buf
)
241 struct stat real_buf
;
243 if ((ret
= fstat(filedes
,&real_buf
)) >= 0)
244 copy_to_stat_wrapper(buf
, &real_buf
);
248 /* A wrapper for mkstemp(3), which seems not to exist on Windows. */
249 int sb_mkstemp (char *template) {
250 #ifdef LISP_FEATURE_WIN32
255 strcpy((char*)&buf
, template);
256 if (_mktemp((char*)&buf
)) {
257 if ((fd
=open((char*)&buf
, O_CREAT
|O_EXCL
|O_RDWR
, S_IRUSR
|S_IWUSR
))!=-1) {
258 strcpy(template, (char*)&buf
);
267 return(mkstemp(template));
276 #ifndef LISP_FEATURE_WIN32
277 /* Return a newly-allocated string holding the username for "uid", or
278 * NULL if there's no such user.
280 * KLUDGE: We also return NULL if malloc() runs out of memory
281 * (returning strdup() result) since it's not clear how to handle that
282 * error better. -- WHN 2001-12-28 */
284 uid_username(int uid
)
286 struct passwd
*p
= getpwuid(uid
);
288 /* The object *p is a static struct which'll be overwritten by
289 * the next call to getpwuid(), so it'd be unsafe to return
290 * p->pw_name without copying. */
291 return strdup(p
->pw_name
);
298 uid_homedir(uid_t uid
)
300 struct passwd
*p
= getpwuid(uid
);
302 /* Let's be careful about this, shall we? */
303 size_t len
= strlen(p
->pw_dir
);
304 if (p
->pw_dir
[len
-1] == '/') {
305 return strdup(p
->pw_dir
);
307 char *result
= malloc(len
+ 2);
309 int nchars
= sprintf(result
,"%s/",p
->pw_dir
);
310 if (nchars
== len
+ 1) {
323 #endif /* !LISP_FEATURE_WIN32 */
326 * functions to get miscellaneous C-level variables
328 * (Doing this by calling functions lets us borrow the smarts of the C
329 * linker, so that things don't blow up when libc versions and thus
330 * variable locations change between compile time and run time.)
339 #ifdef LISP_FEATURE_WIN32
343 * faked-up implementation of select(). Right now just enough to get through
346 int select(int top_fd
, DWORD
*read_set
, DWORD
*write_set
, DWORD
*except_set
, time_t *timeout
)
349 * FIXME: Going forward, we may want to use MsgWaitForMultipleObjects
350 * in order to support a windows message loop inside serve-event.
352 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
353 int fds
[MAXIMUM_WAIT_OBJECTS
];
362 for (i
= 0; i
< top_fd
; i
++) {
363 if (except_set
) except_set
[i
>> 5] = 0;
364 if (write_set
&& (write_set
[i
>> 5] & (1 << (i
& 31)))) polling_write
= 1;
365 if (read_set
[i
>> 5] & (1 << (i
& 31))) {
366 read_set
[i
>> 5] &= ~(1 << (i
& 31));
367 fds
[num_handles
] = i
;
368 handles
[num_handles
++] = (HANDLE
) _get_osfhandle(i
);
372 win_timeout
= INFINITE
;
373 if (timeout
) win_timeout
= (timeout
[0] * 1000) + timeout
[1];
375 /* Last parameter here is timeout in milliseconds. */
376 /* retval = WaitForMultipleObjects(num_handles, handles, 0, INFINITE); */
377 retval
= WaitForMultipleObjects(num_handles
, handles
, 0, win_timeout
);
379 if (retval
< WAIT_ABANDONED
) {
380 /* retval, at this point, is the index of the single live HANDLE/fd. */
381 read_set
[fds
[retval
] >> 5] |= (1 << (fds
[retval
] & 31));
384 return polling_write
;
388 * Windows doesn't have gettimeofday(), and we need it for the compiler,
389 * for serve-event, and for a couple other things. We don't need a timezone
390 * yet, however, and the closest we can easily get to a timeval is the
391 * seconds part. So that's what we do.
393 int gettimeofday(long *timeval
, long *timezone
)
395 timeval
[0] = time(NULL
);
403 /* We will need to define these things or their equivalents for Win32
404 eventually, but for now let's get it working for everyone else. */
405 #ifndef LISP_FEATURE_WIN32
406 /* From SB-BSD-SOCKETS, to get h_errno */
412 /* From SB-POSIX, wait-macros */
413 int wifexited(int status
) {
414 return WIFEXITED(status
);
416 int wexitstatus(int status
) {
417 return WEXITSTATUS(status
);
419 int wifsignaled(int status
) {
420 return WIFSIGNALED(status
);
422 int wtermsig(int status
) {
423 return WTERMSIG(status
);
425 int wifstopped(int status
) {
426 return WIFSTOPPED(status
);
428 int wstopsig(int status
) {
429 return WSTOPSIG(status
);
431 /* FIXME: POSIX also defines WIFCONTINUED, but that appears not to
432 exist on at least Linux... */
433 #endif /* !LISP_FEATURE_WIN32 */
435 /* From SB-POSIX, stat-macros */
436 int s_isreg(mode_t mode
)
438 return S_ISREG(mode
);
440 int s_isdir(mode_t mode
)
442 return S_ISDIR(mode
);
444 int s_ischr(mode_t mode
)
446 return S_ISCHR(mode
);
448 int s_isblk(mode_t mode
)
450 return S_ISBLK(mode
);
452 int s_isfifo(mode_t mode
)
454 return S_ISFIFO(mode
);
456 #ifndef LISP_FEATURE_WIN32
457 int s_islnk(mode_t mode
)
460 return S_ISLNK(mode
);
462 return ((mode
& S_IFMT
) == S_IFLNK
);
465 int s_issock(mode_t mode
)
468 return S_ISSOCK(mode
);
470 return ((mode
& S_IFMT
) == S_IFSOCK
);
473 #endif /* !LISP_FEATURE_WIN32 */