quot.c revision 1.11 1 1.11 lukem /* $NetBSD: quot.c,v 1.11 1997/10/17 12:36:36 lukem 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.11 lukem __RCSID("$NetBSD: quot.c,v 1.11 1997/10/17 12:36:36 lukem 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.5 cgd #include <ufs/ffs/fs.h>
43 1.5 cgd #include <ufs/ufs/quota.h>
44 1.5 cgd #include <ufs/ufs/inode.h>
45 1.4 cgd
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.1 ws #define SIZE(n) (n)
73 1.1 ws #else
74 1.9 ws #define SIZE(n) howmany((n) * DEV_BSIZE, blocksize)
75 1.1 ws #endif
76 1.1 ws
77 1.1 ws #define INOCNT(fs) ((fs)->fs_ipg)
78 1.1 ws #define INOSZ(fs) (sizeof(struct dinode) * INOCNT(fs))
79 1.1 ws
80 1.11 lukem static int cmpusers __P((const void *, const void *));
81 1.11 lukem static void dofsizes __P((int, struct fs *, char *));
82 1.11 lukem static void donames __P((int, struct fs *, char *));
83 1.11 lukem static void douser __P((int, struct fs *, char *));
84 1.11 lukem static struct dinode *get_inode __P((int, struct fs*, ino_t));
85 1.11 lukem static void ffs_oldfscompat __P((struct fs *));
86 1.11 lukem static void initfsizes __P((void));
87 1.11 lukem static void inituser __P((void));
88 1.11 lukem static int isfree __P((struct dinode *));
89 1.11 lukem int main __P((int, char **));
90 1.11 lukem void quot __P((char *, char *));
91 1.11 lukem static void usage __P((void));
92 1.11 lukem static struct user *user __P((uid_t));
93 1.11 lukem static void uses __P((uid_t, daddr_t, time_t));
94 1.11 lukem static void usrrehash __P((void));
95 1.11 lukem static int virtualblocks __P((struct fs *, struct dinode *));
96 1.11 lukem
97 1.11 lukem
98 1.9 ws static struct dinode *
99 1.9 ws get_inode(fd, super, ino)
100 1.9 ws int fd;
101 1.1 ws struct fs *super;
102 1.1 ws ino_t ino;
103 1.1 ws {
104 1.1 ws static struct dinode *ip;
105 1.1 ws static ino_t last;
106 1.1 ws
107 1.1 ws if (fd < 0) { /* flush cache */
108 1.1 ws if (ip) {
109 1.1 ws free(ip);
110 1.1 ws ip = 0;
111 1.1 ws }
112 1.1 ws return 0;
113 1.1 ws }
114 1.1 ws
115 1.1 ws if (!ip || ino < last || ino >= last + INOCNT(super)) {
116 1.1 ws if (!ip
117 1.1 ws && !(ip = (struct dinode *)malloc(INOSZ(super)))) {
118 1.1 ws perror("allocate inodes");
119 1.1 ws exit(1);
120 1.1 ws }
121 1.1 ws last = (ino / INOCNT(super)) * INOCNT(super);
122 1.9 ws if (lseek(fd,
123 1.10 mycroft (off_t)ino_to_fsba(super, last) << super->fs_fshift,
124 1.10 mycroft 0) < 0 ||
125 1.10 mycroft read(fd, ip, INOSZ(super)) != INOSZ(super)) {
126 1.1 ws perror("read inodes");
127 1.1 ws exit(1);
128 1.1 ws }
129 1.1 ws }
130 1.1 ws
131 1.1 ws return ip + ino % INOCNT(super);
132 1.1 ws }
133 1.1 ws
134 1.1 ws #ifdef COMPAT
135 1.9 ws #define actualblocks(super, ip) ((ip)->di_blocks / 2)
136 1.1 ws #else
137 1.9 ws #define actualblocks(super, ip) ((ip)->di_blocks)
138 1.1 ws #endif
139 1.1 ws
140 1.9 ws static int
141 1.9 ws virtualblocks(super, ip)
142 1.1 ws struct fs *super;
143 1.1 ws struct dinode *ip;
144 1.1 ws {
145 1.1 ws register off_t nblk, sz;
146 1.1 ws
147 1.1 ws sz = ip->di_size;
148 1.1 ws #ifdef COMPAT
149 1.9 ws if (lblkno(super, sz) >= NDADDR) {
150 1.9 ws nblk = blkroundup(super, sz);
151 1.1 ws if (sz == nblk)
152 1.1 ws nblk += super->fs_bsize;
153 1.1 ws }
154 1.1 ws
155 1.1 ws return sz / 1024;
156 1.1 ws #else /* COMPAT */
157 1.1 ws
158 1.9 ws if (lblkno(super, sz) >= NDADDR) {
159 1.9 ws nblk = blkroundup(super, sz);
160 1.9 ws sz = lblkno(super, nblk);
161 1.9 ws sz = howmany(sz - NDADDR, NINDIR(super));
162 1.1 ws while (sz > 0) {
163 1.1 ws nblk += sz * super->fs_bsize;
164 1.9 ws /* One block on this level is in the inode itself */
165 1.9 ws sz = howmany(sz - 1, NINDIR(super));
166 1.1 ws }
167 1.1 ws } else
168 1.9 ws nblk = fragroundup(super, sz);
169 1.1 ws
170 1.9 ws return nblk / DEV_BSIZE;
171 1.1 ws #endif /* COMPAT */
172 1.1 ws }
173 1.1 ws
174 1.9 ws static int
175 1.9 ws isfree(ip)
176 1.1 ws struct dinode *ip;
177 1.1 ws {
178 1.1 ws #ifdef COMPAT
179 1.1 ws return (ip->di_mode&IFMT) == 0;
180 1.1 ws #else /* COMPAT */
181 1.1 ws switch (ip->di_mode&IFMT) {
182 1.1 ws case IFIFO:
183 1.1 ws case IFLNK: /* should check FASTSYMLINK? */
184 1.1 ws case IFDIR:
185 1.1 ws case IFREG:
186 1.1 ws return 0;
187 1.1 ws default:
188 1.1 ws return 1;
189 1.1 ws }
190 1.1 ws #endif
191 1.1 ws }
192 1.1 ws
193 1.1 ws static struct user {
194 1.1 ws uid_t uid;
195 1.1 ws char *name;
196 1.1 ws daddr_t space;
197 1.1 ws long count;
198 1.1 ws daddr_t spc30;
199 1.1 ws daddr_t spc60;
200 1.1 ws daddr_t spc90;
201 1.1 ws } *users;
202 1.1 ws static int nusers;
203 1.1 ws
204 1.9 ws static void
205 1.9 ws inituser()
206 1.1 ws {
207 1.1 ws register i;
208 1.1 ws register struct user *usr;
209 1.1 ws
210 1.1 ws if (!nusers) {
211 1.1 ws nusers = 8;
212 1.2 cgd if (!(users =
213 1.9 ws (struct user *)calloc(nusers, sizeof(struct user)))) {
214 1.1 ws perror("allocate users");
215 1.1 ws exit(1);
216 1.1 ws }
217 1.1 ws } else {
218 1.1 ws for (usr = users, i = nusers; --i >= 0; usr++) {
219 1.1 ws usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
220 1.1 ws usr->count = 0;
221 1.1 ws }
222 1.1 ws }
223 1.1 ws }
224 1.1 ws
225 1.9 ws static void
226 1.9 ws usrrehash()
227 1.1 ws {
228 1.1 ws register i;
229 1.1 ws register struct user *usr, *usrn;
230 1.1 ws struct user *svusr;
231 1.1 ws
232 1.1 ws svusr = users;
233 1.1 ws nusers <<= 1;
234 1.9 ws if (!(users = (struct user *)calloc(nusers, sizeof(struct user)))) {
235 1.1 ws perror("allocate users");
236 1.1 ws exit(1);
237 1.1 ws }
238 1.1 ws for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
239 1.9 ws for (usrn = users + (usr->uid&(nusers - 1));
240 1.9 ws usrn->name;
241 1.9 ws usrn--) {
242 1.1 ws if (usrn <= users)
243 1.1 ws usrn = users + nusers;
244 1.1 ws }
245 1.1 ws *usrn = *usr;
246 1.1 ws }
247 1.1 ws }
248 1.1 ws
249 1.9 ws static struct user *
250 1.9 ws user(uid)
251 1.1 ws uid_t uid;
252 1.1 ws {
253 1.1 ws register struct user *usr;
254 1.1 ws register i;
255 1.1 ws struct passwd *pwd;
256 1.1 ws
257 1.1 ws while (1) {
258 1.9 ws for (usr = users + (uid&(nusers - 1)), i = nusers;
259 1.9 ws --i >= 0;
260 1.9 ws usr--) {
261 1.1 ws if (!usr->name) {
262 1.1 ws usr->uid = uid;
263 1.1 ws
264 1.1 ws if (!(pwd = getpwuid(uid))) {
265 1.11 lukem if ((usr->name =
266 1.11 lukem (char *)malloc(7)) != NULL)
267 1.9 ws sprintf(usr->name, "#%d", uid);
268 1.1 ws } else {
269 1.11 lukem if ((usr->name =
270 1.11 lukem (char *)malloc(
271 1.11 lukem strlen(pwd->pw_name) + 1))
272 1.11 lukem != NULL)
273 1.9 ws strcpy(usr->name, pwd->pw_name);
274 1.1 ws }
275 1.1 ws if (!usr->name) {
276 1.1 ws perror("allocate users");
277 1.1 ws exit(1);
278 1.1 ws }
279 1.1 ws return usr;
280 1.1 ws } else if (usr->uid == uid)
281 1.1 ws return usr;
282 1.1 ws
283 1.1 ws if (usr <= users)
284 1.1 ws usr = users + nusers;
285 1.1 ws }
286 1.1 ws usrrehash();
287 1.1 ws }
288 1.1 ws }
289 1.1 ws
290 1.9 ws static int
291 1.9 ws cmpusers(u1, u2)
292 1.11 lukem const void *u1, *u2;
293 1.1 ws {
294 1.11 lukem return ((struct user *)u2)->space - ((struct user *)u1)->space;
295 1.1 ws }
296 1.2 cgd
297 1.9 ws #define sortusers(users) (qsort((users), nusers, sizeof(struct user), \
298 1.9 ws cmpusers))
299 1.1 ws
300 1.9 ws static void
301 1.9 ws uses(uid, blks, act)
302 1.1 ws uid_t uid;
303 1.1 ws daddr_t blks;
304 1.1 ws time_t act;
305 1.1 ws {
306 1.1 ws static time_t today;
307 1.1 ws register struct user *usr;
308 1.1 ws
309 1.1 ws if (!today)
310 1.1 ws time(&today);
311 1.1 ws
312 1.1 ws usr = user(uid);
313 1.1 ws usr->count++;
314 1.1 ws usr->space += blks;
315 1.1 ws
316 1.1 ws if (today - act > 90L * 24L * 60L * 60L)
317 1.1 ws usr->spc90 += blks;
318 1.1 ws if (today - act > 60L * 24L * 60L * 60L)
319 1.1 ws usr->spc60 += blks;
320 1.1 ws if (today - act > 30L * 24L * 60L * 60L)
321 1.1 ws usr->spc30 += blks;
322 1.1 ws }
323 1.1 ws
324 1.1 ws #ifdef COMPAT
325 1.1 ws #define FSZCNT 500
326 1.1 ws #else
327 1.1 ws #define FSZCNT 512
328 1.1 ws #endif
329 1.1 ws struct fsizes {
330 1.1 ws struct fsizes *fsz_next;
331 1.1 ws daddr_t fsz_first, fsz_last;
332 1.1 ws ino_t fsz_count[FSZCNT];
333 1.1 ws daddr_t fsz_sz[FSZCNT];
334 1.1 ws } *fsizes;
335 1.1 ws
336 1.9 ws static void
337 1.9 ws initfsizes()
338 1.1 ws {
339 1.1 ws register struct fsizes *fp;
340 1.1 ws register i;
341 1.1 ws
342 1.1 ws for (fp = fsizes; fp; fp = fp->fsz_next) {
343 1.1 ws for (i = FSZCNT; --i >= 0;) {
344 1.1 ws fp->fsz_count[i] = 0;
345 1.1 ws fp->fsz_sz[i] = 0;
346 1.1 ws }
347 1.1 ws }
348 1.1 ws }
349 1.1 ws
350 1.9 ws static void
351 1.9 ws dofsizes(fd, super, name)
352 1.9 ws int fd;
353 1.1 ws struct fs *super;
354 1.1 ws char *name;
355 1.1 ws {
356 1.1 ws ino_t inode, maxino;
357 1.1 ws struct dinode *ip;
358 1.1 ws daddr_t sz, ksz;
359 1.1 ws struct fsizes *fp, **fsp;
360 1.1 ws register i;
361 1.1 ws
362 1.1 ws maxino = super->fs_ncg * super->fs_ipg - 1;
363 1.1 ws #ifdef COMPAT
364 1.1 ws if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes)))) {
365 1.1 ws perror("alloc fsize structure");
366 1.1 ws exit(1);
367 1.1 ws }
368 1.1 ws #endif /* COMPAT */
369 1.1 ws for (inode = 0; inode < maxino; inode++) {
370 1.1 ws errno = 0;
371 1.9 ws if ((ip = get_inode(fd, super, inode))
372 1.1 ws #ifdef COMPAT
373 1.1 ws && ((ip->di_mode&IFMT) == IFREG
374 1.1 ws || (ip->di_mode&IFMT) == IFDIR)
375 1.1 ws #else /* COMPAT */
376 1.1 ws && !isfree(ip)
377 1.1 ws #endif /* COMPAT */
378 1.1 ws ) {
379 1.9 ws sz = estimate ? virtualblocks(super, ip) :
380 1.9 ws actualblocks(super, ip);
381 1.1 ws #ifdef COMPAT
382 1.1 ws if (sz >= FSZCNT) {
383 1.1 ws fsizes->fsz_count[FSZCNT-1]++;
384 1.1 ws fsizes->fsz_sz[FSZCNT-1] += sz;
385 1.1 ws } else {
386 1.1 ws fsizes->fsz_count[sz]++;
387 1.1 ws fsizes->fsz_sz[sz] += sz;
388 1.1 ws }
389 1.1 ws #else /* COMPAT */
390 1.1 ws ksz = SIZE(sz);
391 1.11 lukem for (fsp = &fsizes; (fp = *fsp) != NULL;
392 1.11 lukem fsp = &fp->fsz_next) {
393 1.1 ws if (ksz < fp->fsz_last)
394 1.1 ws break;
395 1.1 ws }
396 1.1 ws if (!fp || ksz < fp->fsz_first) {
397 1.2 cgd if (!(fp = (struct fsizes *)
398 1.9 ws malloc(sizeof(struct fsizes)))) {
399 1.1 ws perror("alloc fsize structure");
400 1.1 ws exit(1);
401 1.1 ws }
402 1.1 ws fp->fsz_next = *fsp;
403 1.1 ws *fsp = fp;
404 1.1 ws fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
405 1.1 ws fp->fsz_last = fp->fsz_first + FSZCNT;
406 1.1 ws for (i = FSZCNT; --i >= 0;) {
407 1.1 ws fp->fsz_count[i] = 0;
408 1.1 ws fp->fsz_sz[i] = 0;
409 1.1 ws }
410 1.1 ws }
411 1.1 ws fp->fsz_count[ksz % FSZCNT]++;
412 1.1 ws fp->fsz_sz[ksz % FSZCNT] += sz;
413 1.1 ws #endif /* COMPAT */
414 1.1 ws } else if (errno) {
415 1.1 ws perror(name);
416 1.1 ws exit(1);
417 1.1 ws }
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.11 lukem printf("%ld\t%ld\t%ld\n",
424 1.11 lukem (long)(fp->fsz_first + i),
425 1.11 lukem (long)fp->fsz_count[i],
426 1.11 lukem (long)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.1 ws struct dinode *ip;
440 1.1 ws register 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.9 ws if ((ip = get_inode(fd, super, inode))
446 1.1 ws && !isfree(ip))
447 1.10 mycroft uses(ip->di_uid, estimate ? virtualblocks(super, ip) :
448 1.10 mycroft actualblocks(super, ip), ip->di_atime);
449 1.1 ws else if (errno) {
450 1.1 ws perror(name);
451 1.1 ws exit(1);
452 1.1 ws }
453 1.1 ws }
454 1.1 ws if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user)))) {
455 1.1 ws perror("allocate users");
456 1.1 ws exit(1);
457 1.1 ws }
458 1.9 ws bcopy(users, usrs, nusers * sizeof(struct user));
459 1.1 ws sortusers(usrs);
460 1.1 ws for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
461 1.11 lukem printf("%5ld", (long)SIZE(usr->space));
462 1.1 ws if (count)
463 1.11 lukem printf("\t%5ld", (long)usr->count);
464 1.9 ws printf("\t%-8s", usr->name);
465 1.1 ws if (unused)
466 1.11 lukem printf("\t%5ld\t%5ld\t%5ld",
467 1.10 mycroft SIZE(usr->spc30), SIZE(usr->spc60),
468 1.10 mycroft SIZE(usr->spc90));
469 1.1 ws printf("\n");
470 1.1 ws }
471 1.1 ws free(usrs);
472 1.1 ws }
473 1.1 ws
474 1.9 ws static void
475 1.9 ws donames(fd, super, name)
476 1.9 ws int fd;
477 1.1 ws struct fs *super;
478 1.1 ws char *name;
479 1.1 ws {
480 1.1 ws int c;
481 1.1 ws ino_t inode, inode1;
482 1.1 ws ino_t maxino;
483 1.1 ws struct dinode *ip;
484 1.1 ws
485 1.1 ws maxino = super->fs_ncg * super->fs_ipg - 1;
486 1.1 ws /* first skip the name of the filesystem */
487 1.1 ws while ((c = getchar()) != EOF && (c < '0' || c > '9'))
488 1.1 ws while ((c = getchar()) != EOF && c != '\n');
489 1.9 ws ungetc(c, stdin);
490 1.1 ws inode1 = -1;
491 1.9 ws while (scanf("%d", &inode) == 1) {
492 1.1 ws if (inode < 0 || inode > maxino) {
493 1.9 ws #ifndef COMPAT
494 1.9 ws fprintf(stderr, "invalid inode %d\n", inode);
495 1.9 ws #endif
496 1.1 ws return;
497 1.1 ws }
498 1.9 ws #ifdef COMPAT
499 1.9 ws if (inode < inode1)
500 1.9 ws continue;
501 1.9 ws #endif
502 1.1 ws errno = 0;
503 1.9 ws if ((ip = get_inode(fd, super, inode))
504 1.1 ws && !isfree(ip)) {
505 1.9 ws printf("%s\t", user(ip->di_uid)->name);
506 1.1 ws /* now skip whitespace */
507 1.1 ws while ((c = getchar()) == ' ' || c == '\t');
508 1.1 ws /* and print out the remainder of the input line */
509 1.1 ws while (c != EOF && c != '\n') {
510 1.1 ws putchar(c);
511 1.1 ws c = getchar();
512 1.1 ws }
513 1.1 ws putchar('\n');
514 1.1 ws inode1 = inode;
515 1.1 ws } else {
516 1.1 ws if (errno) {
517 1.1 ws perror(name);
518 1.1 ws exit(1);
519 1.1 ws }
520 1.1 ws /* skip this line */
521 1.1 ws while ((c = getchar()) != EOF && c != '\n');
522 1.1 ws }
523 1.1 ws if (c == EOF)
524 1.1 ws break;
525 1.1 ws }
526 1.1 ws }
527 1.1 ws
528 1.9 ws static void
529 1.9 ws usage()
530 1.1 ws {
531 1.1 ws #ifdef COMPAT
532 1.9 ws fprintf(stderr, "Usage: quot [-nfcvha] [filesystem ...]\n");
533 1.1 ws #else /* COMPAT */
534 1.9 ws fprintf(stderr, "Usage: quot [ -acfhknv ] [ filesystem ... ]\n");
535 1.1 ws #endif /* COMPAT */
536 1.1 ws exit(1);
537 1.1 ws }
538 1.1 ws
539 1.1 ws static char superblock[SBSIZE];
540 1.1 ws
541 1.9 ws #define max(a,b) MAX((a),(b))
542 1.9 ws /*
543 1.9 ws * Sanity checks for old file systems.
544 1.9 ws * Stolen from <sys/lib/libsa/ufs.c>
545 1.9 ws */
546 1.9 ws static void
547 1.9 ws ffs_oldfscompat(fs)
548 1.9 ws struct fs *fs;
549 1.9 ws {
550 1.9 ws int i;
551 1.9 ws
552 1.9 ws fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
553 1.9 ws fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
554 1.9 ws if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
555 1.9 ws fs->fs_nrpos = 8; /* XXX */
556 1.9 ws if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
557 1.9 ws quad_t sizepb = fs->fs_bsize; /* XXX */
558 1.9 ws /* XXX */
559 1.9 ws fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
560 1.9 ws for (i = 0; i < NIADDR; i++) { /* XXX */
561 1.9 ws sizepb *= NINDIR(fs); /* XXX */
562 1.9 ws fs->fs_maxfilesize += sizepb; /* XXX */
563 1.9 ws } /* XXX */
564 1.9 ws fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
565 1.9 ws fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
566 1.9 ws } /* XXX */
567 1.9 ws }
568 1.9 ws
569 1.9 ws void
570 1.9 ws quot(name, mp)
571 1.1 ws char *name, *mp;
572 1.1 ws {
573 1.1 ws int fd;
574 1.1 ws
575 1.11 lukem get_inode(-1, 0, 0); /* flush cache */
576 1.1 ws inituser();
577 1.1 ws initfsizes();
578 1.9 ws if ((fd = open(name, 0)) < 0
579 1.9 ws || lseek(fd, SBOFF, 0) != SBOFF
580 1.9 ws || read(fd, superblock, SBSIZE) != SBSIZE) {
581 1.1 ws perror(name);
582 1.1 ws close(fd);
583 1.1 ws return;
584 1.1 ws }
585 1.9 ws if (((struct fs *)superblock)->fs_magic != FS_MAGIC
586 1.9 ws || ((struct fs *)superblock)->fs_bsize > MAXBSIZE
587 1.9 ws || ((struct fs *)superblock)->fs_bsize < sizeof(struct fs)) {
588 1.9 ws fprintf(stderr, "%s: not a BSD filesystem\n", name);
589 1.1 ws close(fd);
590 1.1 ws return;
591 1.1 ws }
592 1.11 lukem ffs_oldfscompat((struct fs *)superblock);
593 1.9 ws printf("%s:", name);
594 1.1 ws if (mp)
595 1.9 ws printf(" (%s)", mp);
596 1.1 ws putchar('\n');
597 1.11 lukem (*func)(fd, (struct fs *)superblock, name);
598 1.1 ws close(fd);
599 1.1 ws }
600 1.1 ws
601 1.9 ws int
602 1.9 ws main(argc, argv)
603 1.9 ws int argc;
604 1.1 ws char **argv;
605 1.1 ws {
606 1.1 ws char all = 0;
607 1.1 ws struct statfs *mp;
608 1.1 ws char dev[MNAMELEN + 1];
609 1.1 ws char *nm;
610 1.1 ws int cnt;
611 1.1 ws
612 1.1 ws func = douser;
613 1.1 ws #ifndef COMPAT
614 1.9 ws header = getbsize(&headerlen, &blocksize);
615 1.1 ws #endif
616 1.1 ws while (--argc > 0 && **++argv == '-') {
617 1.1 ws while (*++*argv) {
618 1.1 ws switch (**argv) {
619 1.1 ws case 'n':
620 1.1 ws func = donames;
621 1.1 ws break;
622 1.1 ws case 'c':
623 1.1 ws func = dofsizes;
624 1.1 ws break;
625 1.1 ws case 'a':
626 1.1 ws all = 1;
627 1.1 ws break;
628 1.1 ws case 'f':
629 1.1 ws count = 1;
630 1.1 ws break;
631 1.1 ws case 'h':
632 1.1 ws estimate = 1;
633 1.1 ws break;
634 1.1 ws #ifndef COMPAT
635 1.1 ws case 'k':
636 1.1 ws blocksize = 1024;
637 1.1 ws break;
638 1.1 ws #endif /* COMPAT */
639 1.1 ws case 'v':
640 1.1 ws unused = 1;
641 1.1 ws break;
642 1.1 ws default:
643 1.1 ws usage();
644 1.1 ws }
645 1.1 ws }
646 1.1 ws }
647 1.1 ws if (all) {
648 1.9 ws cnt = getmntinfo(&mp, MNT_NOWAIT);
649 1.1 ws for (; --cnt >= 0; mp++) {
650 1.7 jtc if (!strncmp(mp->f_fstypename, MOUNT_FFS, MFSNAMELEN)) {
651 1.11 lukem if ((nm =
652 1.11 lukem strrchr(mp->f_mntfromname, '/')) != NULL) {
653 1.9 ws sprintf(dev, "/dev/r%s", nm + 1);
654 1.1 ws nm = dev;
655 1.1 ws } else
656 1.1 ws nm = mp->f_mntfromname;
657 1.9 ws quot(nm, mp->f_mntonname);
658 1.1 ws }
659 1.1 ws }
660 1.1 ws }
661 1.1 ws while (--argc >= 0)
662 1.9 ws quot(*argv++, 0);
663 1.1 ws return 0;
664 1.1 ws }
665