0.7.1.33:
[sbcl.git] / src / runtime / wrap.c
1 /*
2  * wrappers around low-level operations to provide a simpler interface
3  * to the operations that Lisp needs
4  *
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). */
14
15 /*
16  * This software is part of the SBCL system. See the README file for
17  * more information.
18  *
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.
24  */
25
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <pwd.h>
33
34 #include "runtime.h"
35 #include "sbcl.h"
36 #include "util.h"
37
38 /* KLUDGE: Neither the OpenBSD nor the Linux man page give a header
39  * file to find this in (?). -- WHN 2002-02-07 */
40 extern char **environ;
41 \f   
42 /*
43  * stuff needed by CL:DIRECTORY and other Lisp directory operations
44  */
45
46 /* Unix directory operations think of "." and ".." as filenames, but
47  * Lisp directory operations do not. */
48 int
49 is_lispy_filename(const char *filename)
50 {
51     return strcmp(filename, ".") && strcmp(filename, "..");
52 }
53
54 /* Return a zero-terminated array of strings holding the Lispy filenames
55  * (i.e. excluding the Unix magic "." and "..") in the named directory. */
56 char**
57 alloc_directory_lispy_filenames(const char *directory_name)
58 {
59     DIR *dir_ptr = opendir(directory_name);
60     char **result = 0;
61
62     if (dir_ptr) { /* if opendir success */
63
64         struct voidacc va;
65
66         if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
67             struct dirent *dirent_ptr;
68
69             while ( (dirent_ptr = readdir(dir_ptr)) ) { /* until end of data */
70                 char* original_name = dirent_ptr->d_name;
71                 if (is_lispy_filename(original_name)) {
72                     /* strdup(3) is in Linux and *BSD. If you port
73                      * somewhere else that doesn't have it, it's easy
74                      * to reimplement. */
75                     char* dup_name = strdup(original_name);
76                     if (!dup_name) { /* if strdup failure */
77                         goto dtors;
78                     }
79                     if (voidacc_acc(&va, dup_name)) { /* if acc failure */
80                         goto dtors; 
81                     }
82                 }
83             }
84             result = (char**)voidacc_give_away_result(&va);
85         }
86
87     dtors:
88         voidacc_dtor(&va);
89         /* ignoring closedir(3) return code, since what could we do?
90          *
91          * "Never ask questions you don't want to know the answer to."
92          * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
93         closedir(dir_ptr);
94     }
95
96     return result;
97 }
98
99 /* Free a result returned by alloc_directory_lispy_filenames(). */
100 void
101 free_directory_lispy_filenames(char** directory_lispy_filenames)
102 {
103     char** p;
104
105     /* Free the strings. */
106     for (p = directory_lispy_filenames; *p; ++p) {
107         free(*p);
108     }
109
110     /* Free the table of strings. */
111     free(directory_lispy_filenames);
112 }
113 \f
114 /*
115  * readlink(2) stuff
116  */
117
118 /* a wrapped version of readlink(2):
119  *   -- If path isn't a symlink, or is a broken symlink, return 0.
120  *   -- If path is a symlink, return a newly allocated string holding
121  *      the thing it's linked to. */
122 char *
123 wrapped_readlink(char *path)
124 {
125     int bufsiz = strlen(path) + 16;
126     while (1) {
127         char *result = malloc(bufsiz);
128         int n_read = readlink(path, result, n_read);
129         if (n_read < 0) {
130             free(result);
131             return 0;
132         } else if (n_read < bufsiz) {
133             result[n_read] = 0;
134             return result;
135         } else {
136             free(result);
137             bufsiz *= 2;
138         }
139     }
140 }
141 \f
142 /*
143  * stat(2) stuff
144  */
145
146 /* As of 0.6.12, the FFI can't handle 64-bit values. For now, we use
147  * these munged-to-32-bits values for might-be-64-bit slots of
148  * stat_wrapper as a workaround, so that at least we can still work
149  * when values are small.
150  *
151  * FIXME: But of course we should fix the FFI so that we can use the
152  * actual 64-bit values instead. */
153 typedef long ffi_dev_t; /* since Linux dev_t can be 64 bits */
154 typedef u32 ffi_off_t; /* since OpenBSD 2.8 st_size is 64 bits */
155
156 /* a representation of stat(2) results which doesn't depend on CPU or OS */
157 struct stat_wrapper {
158     /* KLUDGE: The verbose wrapped_st_ prefixes are to protect us from
159      * the C preprocessor as wielded by the fiends of OpenBSD, who do
160      * things like
161      *    #define st_atime        st_atimespec.tv_sec
162      * I remember when I was young and innocent, I read about how the
163      * C preprocessor isn't to be used to globally munge random
164      * lowercase symbols like this, because things like this could
165      * happen, and I nodded sagely. But now I know better.:-| This is
166      * another entry for Dan Barlow's ongoing episodic rant about C
167      * header files, I guess.. -- WHN 2001-05-10 */
168     ffi_dev_t     wrapped_st_dev;         /* device */
169     ino_t         wrapped_st_ino;         /* inode */
170     mode_t        wrapped_st_mode;        /* protection */
171     nlink_t       wrapped_st_nlink;       /* number of hard links */
172     uid_t         wrapped_st_uid;         /* user ID of owner */
173     gid_t         wrapped_st_gid;         /* group ID of owner */
174     ffi_dev_t     wrapped_st_rdev;        /* device type (if inode device) */
175     ffi_off_t     wrapped_st_size;        /* total size, in bytes */
176     unsigned long wrapped_st_blksize;     /* blocksize for filesystem I/O */
177     unsigned long wrapped_st_blocks;      /* number of blocks allocated */
178     time_t        wrapped_st_atime;       /* time_t of last access */
179     time_t        wrapped_st_mtime;       /* time_t of last modification */
180     time_t        wrapped_st_ctime;       /* time_t of last change */
181 };
182
183 static void 
184 copy_to_stat_wrapper(struct stat_wrapper *to, struct stat *from)
185 {
186 #define FROB(stem) to->wrapped_st_##stem = from->st_##stem
187     FROB(dev);
188     FROB(ino);
189     FROB(mode);
190     FROB(nlink);
191     FROB(uid);
192     FROB(gid);
193     FROB(rdev);
194     FROB(size);
195     FROB(blksize);
196     FROB(blocks);
197     FROB(atime);
198     FROB(mtime);
199     FROB(ctime);
200 #undef FROB
201 }
202
203 int
204 stat_wrapper(const char *file_name, struct stat_wrapper *buf)
205 {
206     struct stat real_buf;
207     int ret;
208     if ((ret = stat(file_name,&real_buf)) >= 0)
209         copy_to_stat_wrapper(buf, &real_buf); 
210     return ret;
211 }
212
213 int
214 lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
215 {
216     struct stat real_buf;
217     int ret;
218     if ((ret = lstat(file_name,&real_buf)) >= 0) 
219         copy_to_stat_wrapper(buf, &real_buf); 
220     return ret;
221 }
222
223 int
224 fstat_wrapper(int filedes, struct stat_wrapper *buf)
225 {
226     struct stat real_buf;
227     int ret;
228     if ((ret = fstat(filedes,&real_buf)) >= 0)
229         copy_to_stat_wrapper(buf, &real_buf); 
230     return ret;
231 }
232 \f
233 /*
234  * getpwuid() stuff
235  */
236
237 /* Return a newly-allocated string holding the username for "uid", or
238  * NULL if there's no such user.
239  *
240  * KLUDGE: We also return NULL if malloc() runs out of memory
241  * (returning strdup() result) since it's not clear how to handle that
242  * error better. -- WHN 2001-12-28 */
243 char *
244 uid_username(int uid)
245 {
246     struct passwd *p = getpwuid(uid);
247     if (p) {
248         /* The object *p is a static struct which'll be overwritten by
249          * the next call to getpwuid(), so it'd be unsafe to return
250          * p->pw_name without copying. */
251         return strdup(p->pw_name);
252     } else {
253         return 0;
254     }
255 }
256 \f
257 /*
258  * functions to get miscellaneous C-level variables
259  *
260  * (Doing this by calling functions lets us borrow the smarts of the C
261  * linker, so that things don't blow up when libc versions and thus
262  * variable locations change between compile time and run time.)
263  */
264
265 char **
266 wrapped_environ()
267 {
268     return environ;
269 }