efifs_ls.c revision 1.1 1 /* $NetBSD: efifs_ls.c,v 1.1 2006/04/07 14:21:32 cherry Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1996
34 * Matthias Drochner. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57
58 #include <dirent.h>
59 #include <sys/param.h>
60 #include <sys/stat.h>
61
62 #include <lib/libkern/libkern.h>
63
64 #include "stand.h"
65
66 #include <efi.h>
67 #include <efilib.h>
68
69 #include "efifs.h"
70
71 #define NELEM(x) (sizeof (x) / sizeof(*x))
72
73 typedef struct entry_t entry_t;
74 struct entry_t {
75 entry_t *e_next;
76 ino_t e_ino;
77 uint8_t e_type;
78 char e_name[1];
79 };
80
81 static const char *const typestr[] = {
82 "unknown",
83 "FIFO",
84 "CHR",
85 0,
86 "DIR",
87 0,
88 "BLK",
89 0,
90 "REG",
91 0,
92 "LNK",
93 0,
94 "SOCK",
95 0,
96 "WHT"
97 };
98
99 static int
100 fn_match(const char *fname, const char *pattern)
101 {
102 char fc, pc;
103
104 do {
105 fc = *fname++;
106 pc = *pattern++;
107 if (!fc && !pc)
108 return 1;
109 if (pc == '?' && fc)
110 pc = fc;
111 } while (fc == pc);
112
113 if (pc != '*')
114 return 0;
115 /* Too hard (and unnecessary really) too check for "*?name" etc....
116 "**" will look for a '*' and "*?" a '?' */
117 pc = *pattern++;
118 if (!pc)
119 return 1;
120 while ((fname = strchr(fname, pc)))
121 if (fn_match(++fname, pattern))
122 return 1;
123 return 0;
124 }
125
126 void
127 efifs_ls(const char *path)
128 {
129 int fd;
130 struct stat sb;
131 size_t size;
132 char dirbuf[DIRBLKSIZ];
133 const char *fname = 0;
134 char *p;
135 entry_t *names = 0, *n, **np;
136
137 if ((fd = open(path, 0)) < 0
138 || fstat(fd, &sb) < 0
139 || (sb.st_mode & S_IFMT) != S_IFDIR) {
140 /* Path supplied isn't a directory, open parent
141 directory and list matching files. */
142 if (fd >= 0)
143 close(fd);
144 fname = strrchr(path, '/');
145 if (fname) {
146 size = fname - path;
147 p = alloc(size + 1);
148 if (!p)
149 goto out;
150 memcpy(p, path, size);
151 p[size] = 0;
152 fd = open(p, 0);
153 free(p, size + 1);
154 } else {
155 fd = open("", 0);
156 fname = path;
157 }
158
159 if (fd < 0) {
160 printf("ls: %s\n", strerror(errno));
161 return;
162 }
163 if (fstat(fd, &sb) < 0) {
164 printf("stat: %s\n", strerror(errno));
165 goto out;
166 }
167 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
168 printf("%s: %s\n", path, strerror(ENOTDIR));
169 goto out;
170 }
171 }
172
173 while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) {
174 struct dirent *dp, *edp;
175
176 dp = (struct dirent *) dirbuf;
177 edp = (struct dirent *) (dirbuf + size);
178
179 for (; dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
180 const char *t;
181 if (dp->d_ino == 0)
182 continue;
183
184 if (dp->d_type >= NELEM(typestr) ||
185 !(t = typestr[dp->d_type])) {
186 /*
187 * This does not handle "old"
188 * filesystems properly. On little
189 * endian machines, we get a bogus
190 * type name if the namlen matches a
191 * valid type identifier. We could
192 * check if we read namlen "0" and
193 * handle this case specially, if
194 * there were a pressing need...
195 */
196 printf("bad dir entry\n");
197 goto out;
198 }
199 if (fname && !fn_match(dp->d_name, fname))
200 continue;
201 n = alloc(sizeof *n + strlen(dp->d_name));
202 if (!n) {
203 printf("%d: %s (%s)\n",
204 dp->d_ino, dp->d_name, t);
205 continue;
206 }
207 n->e_ino = dp->d_ino;
208 n->e_type = dp->d_type;
209 strcpy(n->e_name, dp->d_name);
210 for (np = &names; *np; np = &(*np)->e_next) {
211 if (strcmp(n->e_name, (*np)->e_name) < 0)
212 break;
213 }
214 n->e_next = *np;
215 *np = n;
216 }
217 }
218
219 if (names) {
220 do {
221 n = names;
222 printf("%d: %s (%s)\n",
223 n->e_ino, n->e_name, typestr[n->e_type]);
224 names = n->e_next;
225 free(n, 0);
226 } while (names);
227 } else {
228 printf( "%s not found\n", path );
229 }
230 out:
231 close(fd);
232 }
233