Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / lib / libsa / ufs_ls.c
blob01637d44a6f0d8e030746c958264f1d08df996e6
1 /* $NetBSD: ufs_ls.c,v 1.13 2006/01/25 18:27:23 christos Exp $ */
3 /*
4 * Copyright (c) 1993
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
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.
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.
33 * Copyright (c) 1996
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
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.
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>
63 #include "stand.h"
64 #include "ufs.h"
66 #define NELEM(x) (sizeof (x) / sizeof(*x))
69 typedef uint32_t ino32_t;
70 typedef struct entry_t entry_t;
71 struct entry_t {
72 entry_t *e_next;
73 ino32_t e_ino;
74 uint8_t e_type;
75 char e_name[1];
78 static const char *const typestr[] = {
79 "unknown",
80 "FIFO",
81 "CHR",
83 "DIR",
85 "BLK",
87 "REG",
89 "LNK",
91 "SOCK",
93 "WHT"
96 static int
97 fn_match(const char *fname, const char *pattern)
99 char fc, pc;
101 do {
102 fc = *fname++;
103 pc = *pattern++;
104 if (!fc && !pc)
105 return 1;
106 if (pc == '?' && fc)
107 pc = fc;
108 } while (fc == pc);
110 if (pc != '*')
111 return 0;
113 * Too hard (and unnecessary really) too check for "*?name" etc....
114 * "**" will look for a '*' and "*?" a '?'
116 pc = *pattern++;
117 if (!pc)
118 return 1;
119 while ((fname = strchr(fname, pc)))
120 if (fn_match(++fname, pattern))
121 return 1;
122 return 0;
125 void
126 ufs_ls(const char *path)
128 int fd;
129 struct stat sb;
130 size_t size;
131 char dirbuf[DIRBLKSIZ];
132 const char *fname = 0;
133 char *p;
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. */
141 if (fd >= 0)
142 close(fd);
143 fname = strrchr(path, '/');
144 if (fname) {
145 size = fname - path;
146 p = alloc(size + 1);
147 if (!p)
148 goto out;
149 memcpy(p, path, size);
150 p[size] = 0;
151 fd = open(p, 0);
152 dealloc(p, size + 1);
153 } else {
154 fd = open("", 0);
155 fname = path;
158 if (fd < 0) {
159 printf("ls: %s\n", strerror(errno));
160 return;
162 if (fstat(fd, &sb) < 0) {
163 printf("stat: %s\n", strerror(errno));
164 goto out;
166 if ((sb.st_mode & IFMT) != IFDIR) {
167 printf("%s: %s\n", path, strerror(ENOTDIR));
168 goto out;
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)) {
179 const char *t;
180 if (dp->d_ino == 0)
181 continue;
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");
196 goto out;
198 if (fname && !fn_match(dp->d_name, fname))
199 continue;
200 n = alloc(sizeof *n + strlen(dp->d_name));
201 if (!n) {
202 printf("%d: %s (%s)\n",
203 dp->d_ino, dp->d_name, t);
204 continue;
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)
211 break;
213 n->e_next = *np;
214 *np = n;
218 if (names) {
219 do {
220 n = names;
221 printf("%d: %s (%s)\n",
222 n->e_ino, n->e_name, typestr[n->e_type]);
223 names = n->e_next;
224 dealloc(n, 0);
225 } while (names);
226 } else {
227 printf( "%s not found\n", path );
229 out:
230 close(fd);