fee9df9f28b82e53c6b41c3b00365ad6bbf8658e
[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 <string.h>
29
30 #include "util.h"
31 \f   
32 /*
33  * stuff needed by CL:DIRECTORY and other Lisp directory operations
34  */
35
36 /* Unix directory operations think of "." and ".." as filenames, but
37  * Lisp directory operations do not. */
38 int
39 is_lispy_filename(const char *filename)
40 {
41     return strcmp(filename, ".") && strcmp(filename, "..");
42 }
43
44 /* Return a zero-terminated array of strings holding the Lispy filenames
45  * (i.e. excluding the Unix magic "." and "..") in the named directory. */
46 char**
47 alloc_directory_lispy_filenames(const char *directory_name)
48 {
49     DIR *dir_ptr;
50     char **result = 0;
51
52     if (dir_ptr = opendir(directory_name)) { /* if opendir success */
53
54         struct voidacc va;
55
56         if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
57             struct dirent *dirent_ptr;
58
59             while (dirent_ptr = readdir(dir_ptr)) { /* until end of data */
60                 char* original_name = dirent_ptr->d_name;
61                 if (is_lispy_filename(original_name)) {
62                     /* strdup(3) is in Linux and *BSD. If you port
63                      * somewhere else that doesn't have it, it's easy
64                      * to reimplement. */
65                     char* dup_name = strdup(original_name);
66                     if (!dup_name) { /* if strdup failure */
67                         goto dtors;
68                     }
69                     if (voidacc_acc(&va, dup_name)) { /* if acc failure */
70                         goto dtors; 
71                     }
72                 }
73             }
74             result = (char**)voidacc_give_away_result(&va);
75         }
76
77     dtors:
78         voidacc_dtor(&va);
79         /* ignoring closedir(3) return code, since what could we do?
80          *
81          * "Never ask questions you don't want to know the answer to."
82          * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
83         closedir(dir_ptr);
84     }
85
86     return result;
87 }
88
89 /* Free a result returned by alloc_directory_lispy_filenames. */
90 void
91 free_directory_lispy_filenames(char** directory_lispy_filenames)
92 {
93     char** p;
94
95     /* Free the strings. */
96     for (p = directory_lispy_filenames; *p; ++p) {
97         free(*p);
98     }
99
100     /* Free the table of strings. */
101     free(directory_lispy_filenames);
102 }