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