quot.c revision 1.18 1 1.18 fvdl /* $NetBSD: quot.c,v 1.18 2003/04/02 10:39:50 fvdl Exp $ */
2 1.9 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1991, 1994 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1991, 1994 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.2 cgd
34 1.11 lukem #include <sys/cdefs.h>
35 1.2 cgd #ifndef lint
36 1.18 fvdl __RCSID("$NetBSD: quot.c,v 1.18 2003/04/02 10:39:50 fvdl Exp $");
37 1.2 cgd #endif /* not lint */
38 1.2 cgd
39 1.4 cgd #include <sys/param.h>
40 1.4 cgd #include <sys/mount.h>
41 1.4 cgd #include <sys/time.h>
42 1.18 fvdl #include <ufs/ufs/dinode.h>
43 1.14 fvdl #include <ufs/ffs/fs.h>
44 1.4 cgd
45 1.12 lukem #include <err.h>
46 1.11 lukem #include <errno.h>
47 1.11 lukem #include <fcntl.h>
48 1.11 lukem #include <pwd.h>
49 1.1 ws #include <stdio.h>
50 1.1 ws #include <stdlib.h>
51 1.1 ws #include <string.h>
52 1.8 mrg #include <unistd.h>
53 1.1 ws
54 1.1 ws /* some flags of what to do: */
55 1.1 ws static char estimate;
56 1.1 ws static char count;
57 1.1 ws static char unused;
58 1.11 lukem static void (*func) __P((int, struct fs *, char *));
59 1.1 ws static long blocksize;
60 1.1 ws static char *header;
61 1.1 ws static int headerlen;
62 1.1 ws
63 1.2 cgd /*
64 1.2 cgd * Original BSD quot doesn't round to number of frags/blocks,
65 1.2 cgd * doesn't account for indirection blocks and gets it totally
66 1.2 cgd * wrong if the size is a multiple of the blocksize.
67 1.9 ws * The new code always counts the number of DEV_BSIZE byte blocks
68 1.2 cgd * instead of the number of kilobytes and converts them to
69 1.2 cgd * kByte when done (on request).
70 1.2 cgd */
71 1.1 ws #ifdef COMPAT
72 1.16 mycroft #define SIZE(n) ((long long)(n))
73 1.1 ws #else
74 1.16 mycroft #define SIZE(n) howmany((long long)(n) * DEV_BSIZE, (long long)blocksize)
75 1.1 ws #endif
76 1.1 ws
77 1.1 ws #define INOCNT(fs) ((fs)->fs_ipg)
78 1.18 fvdl #define INOSZ(fs) \
79 1.18 fvdl (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
80 1.18 fvdl sizeof(struct ufs2_dinode)) * INOCNT(fs))
81 1.18 fvdl
82 1.18 fvdl union dinode {
83 1.18 fvdl struct ufs1_dinode dp1;
84 1.18 fvdl struct ufs2_dinode dp2;
85 1.18 fvdl };
86 1.18 fvdl #define DIP(fs, dp, field) \
87 1.18 fvdl (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
88 1.18 fvdl (dp)->dp1.di_##field : (dp)->dp2.di_##field)
89 1.18 fvdl
90 1.1 ws
91 1.11 lukem static int cmpusers __P((const void *, const void *));
92 1.11 lukem static void dofsizes __P((int, struct fs *, char *));
93 1.11 lukem static void donames __P((int, struct fs *, char *));
94 1.11 lukem static void douser __P((int, struct fs *, char *));
95 1.18 fvdl static union dinode *get_inode __P((int, struct fs*, ino_t));
96 1.11 lukem static void ffs_oldfscompat __P((struct fs *));
97 1.11 lukem static void initfsizes __P((void));
98 1.11 lukem static void inituser __P((void));
99 1.18 fvdl static int isfree __P((struct fs *, union dinode *));
100 1.11 lukem int main __P((int, char **));
101 1.11 lukem void quot __P((char *, char *));
102 1.11 lukem static void usage __P((void));
103 1.11 lukem static struct user *user __P((uid_t));
104 1.11 lukem static void uses __P((uid_t, daddr_t, time_t));
105 1.11 lukem static void usrrehash __P((void));
106 1.18 fvdl static int virtualblocks __P((struct fs *, union dinode *));
107 1.11 lukem
108 1.11 lukem
109 1.18 fvdl static union dinode *
110 1.9 ws get_inode(fd, super, ino)
111 1.9 ws int fd;
112 1.1 ws struct fs *super;
113 1.1 ws ino_t ino;
114 1.1 ws {
115 1.18 fvdl static char *ipbuf;
116 1.1 ws static ino_t last;
117 1.1 ws
118 1.1 ws if (fd < 0) { /* flush cache */
119 1.18 fvdl if (ipbuf) {
120 1.18 fvdl free(ipbuf);
121 1.18 fvdl ipbuf = NULL;
122 1.1 ws }
123 1.1 ws return 0;
124 1.1 ws }
125 1.1 ws
126 1.18 fvdl if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
127 1.18 fvdl if (!ipbuf
128 1.18 fvdl && !(ipbuf = malloc(INOSZ(super))))
129 1.12 lukem errx(1, "allocate inodes");
130 1.1 ws last = (ino / INOCNT(super)) * INOCNT(super);
131 1.9 ws if (lseek(fd,
132 1.10 mycroft (off_t)ino_to_fsba(super, last) << super->fs_fshift,
133 1.10 mycroft 0) < 0 ||
134 1.18 fvdl read(fd, ipbuf, INOSZ(super)) != INOSZ(super))
135 1.12 lukem errx(1, "read inodes");
136 1.1 ws }
137 1.18 fvdl
138 1.18 fvdl if (super->fs_magic == FS_UFS1_MAGIC)
139 1.18 fvdl return ((union dinode *)
140 1.18 fvdl &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
141 1.18 fvdl return ((union dinode *)
142 1.18 fvdl &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]);
143 1.1 ws }
144 1.1 ws
145 1.1 ws #ifdef COMPAT
146 1.18 fvdl #define actualblocks(fs, dp) (DIP(fs, dp, blocks) / 2)
147 1.1 ws #else
148 1.18 fvdl #define actualblocks(fs, dp) (DIP(fs, dp, blocks))
149 1.1 ws #endif
150 1.1 ws
151 1.9 ws static int
152 1.18 fvdl virtualblocks(super, dp)
153 1.1 ws struct fs *super;
154 1.18 fvdl union dinode *dp;
155 1.1 ws {
156 1.12 lukem off_t nblk, sz;
157 1.1 ws
158 1.18 fvdl sz = DIP(super, dp, size);
159 1.1 ws #ifdef COMPAT
160 1.9 ws if (lblkno(super, sz) >= NDADDR) {
161 1.9 ws nblk = blkroundup(super, sz);
162 1.1 ws if (sz == nblk)
163 1.1 ws nblk += super->fs_bsize;
164 1.1 ws }
165 1.1 ws
166 1.1 ws return sz / 1024;
167 1.1 ws #else /* COMPAT */
168 1.1 ws
169 1.9 ws if (lblkno(super, sz) >= NDADDR) {
170 1.9 ws nblk = blkroundup(super, sz);
171 1.9 ws sz = lblkno(super, nblk);
172 1.9 ws sz = howmany(sz - NDADDR, NINDIR(super));
173 1.1 ws while (sz > 0) {
174 1.1 ws nblk += sz * super->fs_bsize;
175 1.9 ws /* One block on this level is in the inode itself */
176 1.9 ws sz = howmany(sz - 1, NINDIR(super));
177 1.1 ws }
178 1.1 ws } else
179 1.9 ws nblk = fragroundup(super, sz);
180 1.1 ws
181 1.9 ws return nblk / DEV_BSIZE;
182 1.1 ws #endif /* COMPAT */
183 1.1 ws }
184 1.1 ws
185 1.9 ws static int
186 1.18 fvdl isfree(fs, dp)
187 1.18 fvdl struct fs *fs;
188 1.18 fvdl union dinode *dp;
189 1.1 ws {
190 1.1 ws #ifdef COMPAT
191 1.18 fvdl return (DIP(fs, dp, mode) & IFMT) == 0;
192 1.1 ws #else /* COMPAT */
193 1.18 fvdl switch (DIP(fs, dp, mode) & IFMT) {
194 1.1 ws case IFIFO:
195 1.1 ws case IFLNK: /* should check FASTSYMLINK? */
196 1.1 ws case IFDIR:
197 1.1 ws case IFREG:
198 1.1 ws return 0;
199 1.1 ws default:
200 1.1 ws return 1;
201 1.1 ws }
202 1.1 ws #endif
203 1.1 ws }
204 1.1 ws
205 1.1 ws static struct user {
206 1.1 ws uid_t uid;
207 1.1 ws char *name;
208 1.1 ws daddr_t space;
209 1.1 ws long count;
210 1.1 ws daddr_t spc30;
211 1.1 ws daddr_t spc60;
212 1.1 ws daddr_t spc90;
213 1.1 ws } *users;
214 1.1 ws static int nusers;
215 1.1 ws
216 1.9 ws static void
217 1.9 ws inituser()
218 1.1 ws {
219 1.12 lukem int i;
220 1.12 lukem struct user *usr;
221 1.1 ws
222 1.1 ws if (!nusers) {
223 1.1 ws nusers = 8;
224 1.2 cgd if (!(users =
225 1.12 lukem (struct user *)calloc(nusers, sizeof(struct user))))
226 1.12 lukem errx(1, "allocate users");
227 1.1 ws } else {
228 1.1 ws for (usr = users, i = nusers; --i >= 0; usr++) {
229 1.1 ws usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
230 1.1 ws usr->count = 0;
231 1.1 ws }
232 1.1 ws }
233 1.1 ws }
234 1.1 ws
235 1.9 ws static void
236 1.9 ws usrrehash()
237 1.1 ws {
238 1.12 lukem int i;
239 1.12 lukem struct user *usr, *usrn;
240 1.1 ws struct user *svusr;
241 1.1 ws
242 1.1 ws svusr = users;
243 1.1 ws nusers <<= 1;
244 1.12 lukem if (!(users = (struct user *)calloc(nusers, sizeof(struct user))))
245 1.12 lukem errx(1, "allocate users");
246 1.1 ws for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
247 1.9 ws for (usrn = users + (usr->uid&(nusers - 1));
248 1.9 ws usrn->name;
249 1.9 ws usrn--) {
250 1.1 ws if (usrn <= users)
251 1.1 ws usrn = users + nusers;
252 1.1 ws }
253 1.1 ws *usrn = *usr;
254 1.1 ws }
255 1.1 ws }
256 1.1 ws
257 1.9 ws static struct user *
258 1.9 ws user(uid)
259 1.1 ws uid_t uid;
260 1.1 ws {
261 1.12 lukem struct user *usr;
262 1.12 lukem int i;
263 1.1 ws struct passwd *pwd;
264 1.1 ws
265 1.1 ws while (1) {
266 1.9 ws for (usr = users + (uid&(nusers - 1)), i = nusers;
267 1.9 ws --i >= 0;
268 1.9 ws usr--) {
269 1.1 ws if (!usr->name) {
270 1.1 ws usr->uid = uid;
271 1.1 ws
272 1.1 ws if (!(pwd = getpwuid(uid))) {
273 1.11 lukem if ((usr->name =
274 1.11 lukem (char *)malloc(7)) != NULL)
275 1.9 ws sprintf(usr->name, "#%d", uid);
276 1.1 ws } else {
277 1.11 lukem if ((usr->name =
278 1.11 lukem (char *)malloc(
279 1.11 lukem strlen(pwd->pw_name) + 1))
280 1.11 lukem != NULL)
281 1.9 ws strcpy(usr->name, pwd->pw_name);
282 1.1 ws }
283 1.12 lukem if (!usr->name)
284 1.12 lukem errx(1, "allocate users");
285 1.1 ws return usr;
286 1.1 ws } else if (usr->uid == uid)
287 1.1 ws return usr;
288 1.1 ws
289 1.1 ws if (usr <= users)
290 1.1 ws usr = users + nusers;
291 1.1 ws }
292 1.1 ws usrrehash();
293 1.1 ws }
294 1.1 ws }
295 1.1 ws
296 1.9 ws static int
297 1.9 ws cmpusers(u1, u2)
298 1.11 lukem const void *u1, *u2;
299 1.1 ws {
300 1.11 lukem return ((struct user *)u2)->space - ((struct user *)u1)->space;
301 1.1 ws }
302 1.2 cgd
303 1.9 ws #define sortusers(users) (qsort((users), nusers, sizeof(struct user), \
304 1.9 ws cmpusers))
305 1.1 ws
306 1.9 ws static void
307 1.9 ws uses(uid, blks, act)
308 1.1 ws uid_t uid;
309 1.1 ws daddr_t blks;
310 1.1 ws time_t act;
311 1.1 ws {
312 1.1 ws static time_t today;
313 1.12 lukem struct user *usr;
314 1.1 ws
315 1.1 ws if (!today)
316 1.1 ws time(&today);
317 1.1 ws
318 1.1 ws usr = user(uid);
319 1.1 ws usr->count++;
320 1.1 ws usr->space += blks;
321 1.1 ws
322 1.1 ws if (today - act > 90L * 24L * 60L * 60L)
323 1.1 ws usr->spc90 += blks;
324 1.1 ws if (today - act > 60L * 24L * 60L * 60L)
325 1.1 ws usr->spc60 += blks;
326 1.1 ws if (today - act > 30L * 24L * 60L * 60L)
327 1.1 ws usr->spc30 += blks;
328 1.1 ws }
329 1.1 ws
330 1.1 ws #ifdef COMPAT
331 1.1 ws #define FSZCNT 500
332 1.1 ws #else
333 1.1 ws #define FSZCNT 512
334 1.1 ws #endif
335 1.1 ws struct fsizes {
336 1.1 ws struct fsizes *fsz_next;
337 1.1 ws daddr_t fsz_first, fsz_last;
338 1.1 ws ino_t fsz_count[FSZCNT];
339 1.1 ws daddr_t fsz_sz[FSZCNT];
340 1.1 ws } *fsizes;
341 1.1 ws
342 1.9 ws static void
343 1.9 ws initfsizes()
344 1.1 ws {
345 1.12 lukem struct fsizes *fp;
346 1.12 lukem int i;
347 1.1 ws
348 1.1 ws for (fp = fsizes; fp; fp = fp->fsz_next) {
349 1.1 ws for (i = FSZCNT; --i >= 0;) {
350 1.1 ws fp->fsz_count[i] = 0;
351 1.1 ws fp->fsz_sz[i] = 0;
352 1.1 ws }
353 1.1 ws }
354 1.1 ws }
355 1.1 ws
356 1.9 ws static void
357 1.9 ws dofsizes(fd, super, name)
358 1.9 ws int fd;
359 1.1 ws struct fs *super;
360 1.1 ws char *name;
361 1.1 ws {
362 1.1 ws ino_t inode, maxino;
363 1.18 fvdl union dinode *dp;
364 1.1 ws daddr_t sz, ksz;
365 1.1 ws struct fsizes *fp, **fsp;
366 1.12 lukem int i;
367 1.1 ws
368 1.1 ws maxino = super->fs_ncg * super->fs_ipg - 1;
369 1.1 ws #ifdef COMPAT
370 1.12 lukem if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
371 1.12 lukem errx(1, "alloc fsize structure");
372 1.1 ws #endif /* COMPAT */
373 1.1 ws for (inode = 0; inode < maxino; inode++) {
374 1.1 ws errno = 0;
375 1.18 fvdl if ((dp = get_inode(fd, super, inode))
376 1.1 ws #ifdef COMPAT
377 1.18 fvdl && ((DIP(super, dp, mode) & IFMT) == IFREG
378 1.18 fvdl || (DIP(dp, mode) & IFMT) == IFDIR)
379 1.1 ws #else /* COMPAT */
380 1.18 fvdl && !isfree(super, dp)
381 1.1 ws #endif /* COMPAT */
382 1.1 ws ) {
383 1.18 fvdl sz = estimate ? virtualblocks(super, dp) :
384 1.18 fvdl actualblocks(super, dp);
385 1.1 ws #ifdef COMPAT
386 1.1 ws if (sz >= FSZCNT) {
387 1.1 ws fsizes->fsz_count[FSZCNT-1]++;
388 1.1 ws fsizes->fsz_sz[FSZCNT-1] += sz;
389 1.1 ws } else {
390 1.1 ws fsizes->fsz_count[sz]++;
391 1.1 ws fsizes->fsz_sz[sz] += sz;
392 1.1 ws }
393 1.1 ws #else /* COMPAT */
394 1.1 ws ksz = SIZE(sz);
395 1.11 lukem for (fsp = &fsizes; (fp = *fsp) != NULL;
396 1.11 lukem fsp = &fp->fsz_next) {
397 1.1 ws if (ksz < fp->fsz_last)
398 1.1 ws break;
399 1.1 ws }
400 1.1 ws if (!fp || ksz < fp->fsz_first) {
401 1.2 cgd if (!(fp = (struct fsizes *)
402 1.12 lukem malloc(sizeof(struct fsizes))))
403 1.12 lukem errx(1, "alloc fsize structure");
404 1.1 ws fp->fsz_next = *fsp;
405 1.1 ws *fsp = fp;
406 1.1 ws fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
407 1.1 ws fp->fsz_last = fp->fsz_first + FSZCNT;
408 1.1 ws for (i = FSZCNT; --i >= 0;) {
409 1.1 ws fp->fsz_count[i] = 0;
410 1.1 ws fp->fsz_sz[i] = 0;
411 1.1 ws }
412 1.1 ws }
413 1.1 ws fp->fsz_count[ksz % FSZCNT]++;
414 1.1 ws fp->fsz_sz[ksz % FSZCNT] += sz;
415 1.1 ws #endif /* COMPAT */
416 1.12 lukem } else if (errno)
417 1.12 lukem errx(1, "%s", name);
418 1.1 ws }
419 1.1 ws sz = 0;
420 1.1 ws for (fp = fsizes; fp; fp = fp->fsz_next) {
421 1.1 ws for (i = 0; i < FSZCNT; i++) {
422 1.1 ws if (fp->fsz_count[i])
423 1.16 mycroft printf("%ld\t%ld\t%lld\n",
424 1.11 lukem (long)(fp->fsz_first + i),
425 1.11 lukem (long)fp->fsz_count[i],
426 1.15 mycroft SIZE(sz += fp->fsz_sz[i]));
427 1.1 ws }
428 1.1 ws }
429 1.1 ws }
430 1.1 ws
431 1.9 ws static void
432 1.9 ws douser(fd, super, name)
433 1.9 ws int fd;
434 1.1 ws struct fs *super;
435 1.1 ws char *name;
436 1.1 ws {
437 1.1 ws ino_t inode, maxino;
438 1.1 ws struct user *usr, *usrs;
439 1.18 fvdl union dinode *dp;
440 1.12 lukem int n;
441 1.1 ws
442 1.1 ws maxino = super->fs_ncg * super->fs_ipg - 1;
443 1.1 ws for (inode = 0; inode < maxino; inode++) {
444 1.1 ws errno = 0;
445 1.18 fvdl if ((dp = get_inode(fd, super, inode))
446 1.18 fvdl && !isfree(super, dp))
447 1.18 fvdl uses(DIP(super, dp, uid),
448 1.18 fvdl estimate ? virtualblocks(super, dp) :
449 1.18 fvdl actualblocks(super, dp), DIP(super, dp, atime));
450 1.12 lukem else if (errno)
451 1.12 lukem errx(1, "%s", name);
452 1.1 ws }
453 1.12 lukem if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
454 1.12 lukem errx(1, "allocate users");
455 1.12 lukem memmove(usrs, users, nusers * sizeof(struct user));
456 1.1 ws sortusers(usrs);
457 1.1 ws for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
458 1.16 mycroft printf("%5lld", SIZE(usr->space));
459 1.1 ws if (count)
460 1.15 mycroft printf("\t%5ld", usr->count);
461 1.9 ws printf("\t%-8s", usr->name);
462 1.1 ws if (unused)
463 1.16 mycroft printf("\t%5lld\t%5lld\t%5lld",
464 1.10 mycroft SIZE(usr->spc30), SIZE(usr->spc60),
465 1.10 mycroft SIZE(usr->spc90));
466 1.1 ws printf("\n");
467 1.1 ws }
468 1.1 ws free(usrs);
469 1.1 ws }
470 1.1 ws
471 1.9 ws static void
472 1.9 ws donames(fd, super, name)
473 1.9 ws int fd;
474 1.1 ws struct fs *super;
475 1.1 ws char *name;
476 1.1 ws {
477 1.1 ws int c;
478 1.1 ws ino_t inode, inode1;
479 1.1 ws ino_t maxino;
480 1.18 fvdl union dinode *dp;
481 1.1 ws
482 1.1 ws maxino = super->fs_ncg * super->fs_ipg - 1;
483 1.1 ws /* first skip the name of the filesystem */
484 1.1 ws while ((c = getchar()) != EOF && (c < '0' || c > '9'))
485 1.1 ws while ((c = getchar()) != EOF && c != '\n');
486 1.9 ws ungetc(c, stdin);
487 1.1 ws inode1 = -1;
488 1.9 ws while (scanf("%d", &inode) == 1) {
489 1.1 ws if (inode < 0 || inode > maxino) {
490 1.9 ws #ifndef COMPAT
491 1.12 lukem warnx("invalid inode %d", inode);
492 1.9 ws #endif
493 1.1 ws return;
494 1.1 ws }
495 1.9 ws #ifdef COMPAT
496 1.9 ws if (inode < inode1)
497 1.9 ws continue;
498 1.9 ws #endif
499 1.1 ws errno = 0;
500 1.18 fvdl if ((dp = get_inode(fd, super, inode))
501 1.18 fvdl && !isfree(super, dp)) {
502 1.18 fvdl printf("%s\t", user(DIP(super, dp, uid))->name);
503 1.1 ws /* now skip whitespace */
504 1.1 ws while ((c = getchar()) == ' ' || c == '\t');
505 1.1 ws /* and print out the remainder of the input line */
506 1.1 ws while (c != EOF && c != '\n') {
507 1.1 ws putchar(c);
508 1.1 ws c = getchar();
509 1.1 ws }
510 1.1 ws putchar('\n');
511 1.1 ws inode1 = inode;
512 1.1 ws } else {
513 1.12 lukem if (errno)
514 1.12 lukem errx(1, "%s", name);
515 1.1 ws /* skip this line */
516 1.1 ws while ((c = getchar()) != EOF && c != '\n');
517 1.1 ws }
518 1.1 ws if (c == EOF)
519 1.1 ws break;
520 1.1 ws }
521 1.1 ws }
522 1.1 ws
523 1.9 ws static void
524 1.9 ws usage()
525 1.1 ws {
526 1.1 ws #ifdef COMPAT
527 1.9 ws fprintf(stderr, "Usage: quot [-nfcvha] [filesystem ...]\n");
528 1.1 ws #else /* COMPAT */
529 1.9 ws fprintf(stderr, "Usage: quot [ -acfhknv ] [ filesystem ... ]\n");
530 1.1 ws #endif /* COMPAT */
531 1.1 ws exit(1);
532 1.1 ws }
533 1.1 ws
534 1.9 ws #define max(a,b) MAX((a),(b))
535 1.9 ws /*
536 1.9 ws * Sanity checks for old file systems.
537 1.9 ws * Stolen from <sys/lib/libsa/ufs.c>
538 1.9 ws */
539 1.9 ws static void
540 1.9 ws ffs_oldfscompat(fs)
541 1.9 ws struct fs *fs;
542 1.9 ws {
543 1.9 ws int i;
544 1.9 ws
545 1.18 fvdl if (fs->fs_old_inodefmt < FS_44INODEFMT) {
546 1.18 fvdl quad_t sizepb = fs->fs_bsize;
547 1.18 fvdl
548 1.18 fvdl fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;
549 1.18 fvdl for (i = 0; i < NIADDR; i++) {
550 1.18 fvdl sizepb *= NINDIR(fs);
551 1.18 fvdl fs->fs_maxfilesize += sizepb;
552 1.18 fvdl }
553 1.18 fvdl fs->fs_qbmask = ~fs->fs_bmask;
554 1.18 fvdl fs->fs_qfmask = ~fs->fs_fmask;
555 1.18 fvdl }
556 1.9 ws }
557 1.9 ws
558 1.18 fvdl /*
559 1.18 fvdl * Possible superblock locations ordered from most to least likely.
560 1.18 fvdl */
561 1.18 fvdl static int sblock_try[] = SBLOCKSEARCH;
562 1.18 fvdl static char superblock[SBLOCKSIZE];
563 1.18 fvdl
564 1.18 fvdl
565 1.9 ws void
566 1.9 ws quot(name, mp)
567 1.1 ws char *name, *mp;
568 1.1 ws {
569 1.18 fvdl int fd, i;
570 1.18 fvdl struct fs *fs;
571 1.1 ws
572 1.11 lukem get_inode(-1, 0, 0); /* flush cache */
573 1.1 ws inituser();
574 1.1 ws initfsizes();
575 1.18 fvdl if ((fd = open(name, 0)) < 0) {
576 1.13 mrg warn("%s", name);
577 1.1 ws return;
578 1.1 ws }
579 1.18 fvdl
580 1.18 fvdl for (i = 0; sblock_try[i] != -1; i++) {
581 1.18 fvdl if (lseek(fd, sblock_try[i], 0) != sblock_try[i])
582 1.18 fvdl continue;
583 1.18 fvdl if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE)
584 1.18 fvdl continue;
585 1.18 fvdl fs = (struct fs *)superblock;
586 1.18 fvdl
587 1.18 fvdl if ((fs->fs_magic == FS_UFS1_MAGIC ||
588 1.18 fvdl (fs->fs_magic == FS_UFS2_MAGIC &&
589 1.18 fvdl fs->fs_sblockloc == numfrags(fs, sblock_try[i]))) &&
590 1.18 fvdl fs->fs_bsize <= MAXBSIZE &&
591 1.18 fvdl fs->fs_bsize >= sizeof(struct fs))
592 1.18 fvdl break;
593 1.18 fvdl }
594 1.18 fvdl if (sblock_try[i] == -1) {
595 1.12 lukem warnx("%s: not a BSD filesystem", name);
596 1.1 ws close(fd);
597 1.1 ws return;
598 1.1 ws }
599 1.11 lukem ffs_oldfscompat((struct fs *)superblock);
600 1.9 ws printf("%s:", name);
601 1.1 ws if (mp)
602 1.9 ws printf(" (%s)", mp);
603 1.1 ws putchar('\n');
604 1.18 fvdl (*func)(fd, fs, name);
605 1.1 ws close(fd);
606 1.1 ws }
607 1.1 ws
608 1.9 ws int
609 1.9 ws main(argc, argv)
610 1.9 ws int argc;
611 1.1 ws char **argv;
612 1.1 ws {
613 1.1 ws char all = 0;
614 1.1 ws struct statfs *mp;
615 1.1 ws char dev[MNAMELEN + 1];
616 1.1 ws char *nm;
617 1.1 ws int cnt;
618 1.1 ws
619 1.1 ws func = douser;
620 1.1 ws #ifndef COMPAT
621 1.9 ws header = getbsize(&headerlen, &blocksize);
622 1.1 ws #endif
623 1.1 ws while (--argc > 0 && **++argv == '-') {
624 1.1 ws while (*++*argv) {
625 1.1 ws switch (**argv) {
626 1.1 ws case 'n':
627 1.1 ws func = donames;
628 1.1 ws break;
629 1.1 ws case 'c':
630 1.1 ws func = dofsizes;
631 1.1 ws break;
632 1.1 ws case 'a':
633 1.1 ws all = 1;
634 1.1 ws break;
635 1.1 ws case 'f':
636 1.1 ws count = 1;
637 1.1 ws break;
638 1.1 ws case 'h':
639 1.1 ws estimate = 1;
640 1.1 ws break;
641 1.1 ws #ifndef COMPAT
642 1.1 ws case 'k':
643 1.1 ws blocksize = 1024;
644 1.1 ws break;
645 1.1 ws #endif /* COMPAT */
646 1.1 ws case 'v':
647 1.1 ws unused = 1;
648 1.1 ws break;
649 1.1 ws default:
650 1.1 ws usage();
651 1.1 ws }
652 1.1 ws }
653 1.1 ws }
654 1.1 ws if (all) {
655 1.9 ws cnt = getmntinfo(&mp, MNT_NOWAIT);
656 1.1 ws for (; --cnt >= 0; mp++) {
657 1.7 jtc if (!strncmp(mp->f_fstypename, MOUNT_FFS, MFSNAMELEN)) {
658 1.11 lukem if ((nm =
659 1.11 lukem strrchr(mp->f_mntfromname, '/')) != NULL) {
660 1.9 ws sprintf(dev, "/dev/r%s", nm + 1);
661 1.1 ws nm = dev;
662 1.1 ws } else
663 1.1 ws nm = mp->f_mntfromname;
664 1.9 ws quot(nm, mp->f_mntonname);
665 1.1 ws }
666 1.1 ws }
667 1.1 ws }
668 1.1 ws while (--argc >= 0)
669 1.9 ws quot(*argv++, 0);
670 1.1 ws return 0;
671 1.1 ws }
672