1 /* $NetBSD: efifs_ls.c,v 1.1 2006/04/07 14:21:32 cherry Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 * Matthias Drochner. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
57 /* Based on libsa/ufs_ls.c */
60 #include <sys/param.h>
63 #include <lib/libkern/libkern.h>
72 #define NELEM(x) (sizeof (x) / sizeof(*x))
74 typedef struct entry_t entry_t
;
82 static const char *const typestr
[] = {
101 fn_match(const char *fname
, const char *pattern
)
116 /* Too hard (and unnecessary really) too check for "*?name" etc....
117 "**" will look for a '*' and "*?" a '?' */
121 while ((fname
= strchr(fname
, pc
)))
122 if (fn_match(++fname
, pattern
))
128 efifs_ls(const char *path
)
133 char dirbuf
[DIRBLKSIZ
];
134 const char *fname
= 0;
136 entry_t
*names
= 0, *n
, **np
;
138 if ((fd
= open(path
, 0)) < 0
139 || fstat(fd
, &sb
) < 0
140 || (sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
141 /* Path supplied isn't a directory, open parent
142 directory and list matching files. */
145 fname
= strrchr(path
, '/');
151 memcpy(p
, path
, size
);
161 printf("ls: %s\n", strerror(errno
));
164 if (fstat(fd
, &sb
) < 0) {
165 printf("stat: %s\n", strerror(errno
));
168 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
169 printf("%s: %s\n", path
, strerror(ENOTDIR
));
174 while ((size
= read(fd
, dirbuf
, DIRBLKSIZ
)) == DIRBLKSIZ
) {
175 struct dirent
*dp
, *edp
;
177 dp
= (struct dirent
*) dirbuf
;
178 edp
= (struct dirent
*) (dirbuf
+ size
);
180 for (; dp
< edp
; dp
= (void *)((char *)dp
+ dp
->d_reclen
)) {
185 if (dp
->d_type
>= NELEM(typestr
) ||
186 !(t
= typestr
[dp
->d_type
])) {
188 * This does not handle "old"
189 * filesystems properly. On little
190 * endian machines, we get a bogus
191 * type name if the namlen matches a
192 * valid type identifier. We could
193 * check if we read namlen "0" and
194 * handle this case specially, if
195 * there were a pressing need...
197 printf("bad dir entry\n");
200 if (fname
&& !fn_match(dp
->d_name
, fname
))
202 n
= alloc(sizeof *n
+ strlen(dp
->d_name
));
204 printf("%d: %s (%s)\n",
205 dp
->d_ino
, dp
->d_name
, t
);
208 n
->e_ino
= dp
->d_ino
;
209 n
->e_type
= dp
->d_type
;
210 strcpy(n
->e_name
, dp
->d_name
);
211 for (np
= &names
; *np
; np
= &(*np
)->e_next
) {
212 if (strcmp(n
->e_name
, (*np
)->e_name
) < 0)
223 printf("%d: %s (%s)\n",
224 n
->e_ino
, n
->e_name
, typestr
[n
->e_type
]);
229 printf( "%s not found\n", path
);