0.6.12.3:
[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 "util.h"
33 \f   
34 /*
35  * stuff needed by CL:DIRECTORY and other Lisp directory operations
36  */
37
38 /* Unix directory operations think of "." and ".." as filenames, but
39  * Lisp directory operations do not. */
40 int
41 is_lispy_filename(const char *filename)
42 {
43     return strcmp(filename, ".") && strcmp(filename, "..");
44 }
45
46 /* Return a zero-terminated array of strings holding the Lispy filenames
47  * (i.e. excluding the Unix magic "." and "..") in the named directory. */
48 char**
49 alloc_directory_lispy_filenames(const char *directory_name)
50 {
51     DIR *dir_ptr;
52     char **result = 0;
53
54     if (dir_ptr = opendir(directory_name)) { /* if opendir success */
55
56         struct voidacc va;
57
58         if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
59             struct dirent *dirent_ptr;
60
61             while (dirent_ptr = readdir(dir_ptr)) { /* until end of data */
62                 char* original_name = dirent_ptr->d_name;
63                 if (is_lispy_filename(original_name)) {
64                     /* strdup(3) is in Linux and *BSD. If you port
65                      * somewhere else that doesn't have it, it's easy
66                      * to reimplement. */
67                     char* dup_name = strdup(original_name);
68                     if (!dup_name) { /* if strdup failure */
69                         goto dtors;
70                     }
71                     if (voidacc_acc(&va, dup_name)) { /* if acc failure */
72                         goto dtors; 
73                     }
74                 }
75             }
76             result = (char**)voidacc_give_away_result(&va);
77         }
78
79     dtors:
80         voidacc_dtor(&va);
81         /* ignoring closedir(3) return code, since what could we do?
82          *
83          * "Never ask questions you don't want to know the answer to."
84          * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
85         closedir(dir_ptr);
86     }
87
88     return result;
89 }
90
91 /* Free a result returned by alloc_directory_lispy_filenames(). */
92 void
93 free_directory_lispy_filenames(char** directory_lispy_filenames)
94 {
95     char** p;
96
97     /* Free the strings. */
98     for (p = directory_lispy_filenames; *p; ++p) {
99         free(*p);
100     }
101
102     /* Free the table of strings. */
103     free(directory_lispy_filenames);
104 }
105 \f
106 /*
107  * stat(2) stuff
108  */
109
110 typedef long my_dev_t;
111
112 /* a representation of stat(2) results which doesn't depend on CPU or OS */
113 struct stat_wrapper {
114     my_dev_t      st_dev;         /* device */
115     ino_t         st_ino;         /* inode */
116     mode_t        st_mode;        /* protection */
117     nlink_t       st_nlink;       /* number of hard links */
118     uid_t         st_uid;         /* user ID of owner */
119     gid_t         st_gid;         /* group ID of owner */
120     my_dev_t      st_rdev;        /* device type (if inode device) */
121     off_t         st_size;        /* total size, in bytes */
122     unsigned long st_blksize;     /* blocksize for filesystem I/O */
123     unsigned long st_blocks;      /* number of blocks allocated */
124     time_t        st_atime;       /* time of last access */
125     time_t        st_mtime;       /* time of last modification */
126     time_t        st_ctime;       /* time of last change */
127 };
128
129 static void 
130 copy_to_stat_wrapper(struct stat_wrapper *to, struct stat *from)
131 {
132 #define FROB(stem) to->st_##stem = from->st_##stem
133     FROB(dev);
134     FROB(ino);
135     FROB(mode);
136     FROB(nlink);
137     FROB(uid);
138     FROB(gid);
139     FROB(rdev);
140     FROB(size);
141     FROB(blksize);
142     FROB(blocks);
143     FROB(atime);
144     FROB(mtime);
145     FROB(ctime);
146 #undef FROB
147 }
148
149 int
150 stat_wrapper(const char *file_name, struct stat_wrapper *buf)
151 {
152     struct stat real_buf;
153     int ret;
154     if ((ret = stat(file_name,&real_buf)) >= 0)
155         copy_to_stat_wrapper(buf, &real_buf); 
156     return ret;
157 }
158
159 int
160 lstat_wrapper(const char *file_name, struct stat_wrapper *buf)
161 {
162     struct stat real_buf;
163     int ret;
164     if ((ret = lstat(file_name,&real_buf)) >= 0) 
165         copy_to_stat_wrapper(buf, &real_buf); 
166     return ret;
167 }
168
169 int
170 fstat_wrapper(int filedes, struct stat_wrapper *buf)
171 {
172     struct stat real_buf;
173     int ret;
174     if ((ret = fstat(filedes,&real_buf)) >= 0)
175         copy_to_stat_wrapper(buf, &real_buf); 
176     return ret;
177 }