1 /* $NetBSD: ufs_ls.c,v 1.13 2006/01/25 18:27:23 christos 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.
58 #include <sys/param.h>
59 #include <lib/libkern/libkern.h>
60 #include <ufs/ufs/dinode.h>
61 #include <ufs/ufs/dir.h>
66 #define NELEM(x) (sizeof (x) / sizeof(*x))
69 typedef uint32_t ino32_t
;
70 typedef struct entry_t entry_t
;
78 static const char *const typestr
[] = {
97 fn_match(const char *fname
, const char *pattern
)
113 * Too hard (and unnecessary really) too check for "*?name" etc....
114 * "**" will look for a '*' and "*?" a '?'
119 while ((fname
= strchr(fname
, pc
)))
120 if (fn_match(++fname
, pattern
))
126 ufs_ls(const char *path
)
131 char dirbuf
[DIRBLKSIZ
];
132 const char *fname
= 0;
134 entry_t
*names
= 0, *n
, **np
;
136 if ((fd
= open(path
, 0)) < 0
137 || fstat(fd
, &sb
) < 0
138 || (sb
.st_mode
& IFMT
) != IFDIR
) {
139 /* Path supplied isn't a directory, open parent
140 directory and list matching files. */
143 fname
= strrchr(path
, '/');
149 memcpy(p
, path
, size
);
152 dealloc(p
, size
+ 1);
159 printf("ls: %s\n", strerror(errno
));
162 if (fstat(fd
, &sb
) < 0) {
163 printf("stat: %s\n", strerror(errno
));
166 if ((sb
.st_mode
& IFMT
) != IFDIR
) {
167 printf("%s: %s\n", path
, strerror(ENOTDIR
));
172 while ((size
= read(fd
, dirbuf
, DIRBLKSIZ
)) == DIRBLKSIZ
) {
173 struct direct
*dp
, *edp
;
175 dp
= (struct direct
*)dirbuf
;
176 edp
= (struct direct
*)(dirbuf
+ size
);
178 for (; dp
< edp
; dp
= (void *)((char *)dp
+ dp
->d_reclen
)) {
183 if (dp
->d_type
>= NELEM(typestr
) ||
184 !(t
= typestr
[dp
->d_type
])) {
186 * This does not handle "old"
187 * filesystems properly. On little
188 * endian machines, we get a bogus
189 * type name if the namlen matches a
190 * valid type identifier. We could
191 * check if we read namlen "0" and
192 * handle this case specially, if
193 * there were a pressing need...
195 printf("bad dir entry\n");
198 if (fname
&& !fn_match(dp
->d_name
, fname
))
200 n
= alloc(sizeof *n
+ strlen(dp
->d_name
));
202 printf("%d: %s (%s)\n",
203 dp
->d_ino
, dp
->d_name
, t
);
206 n
->e_ino
= dp
->d_ino
;
207 n
->e_type
= dp
->d_type
;
208 strcpy(n
->e_name
, dp
->d_name
);
209 for (np
= &names
; *np
; np
= &(*np
)->e_next
) {
210 if (strcmp(n
->e_name
, (*np
)->e_name
) < 0)
221 printf("%d: %s (%s)\n",
222 n
->e_ino
, n
->e_name
, typestr
[n
->e_type
]);
227 printf( "%s not found\n", path
);