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