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