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