Commit | Line | Data |
---|---|---|
b47543fa | 1 | #ifndef lint |
b712d94e | 2 | static char *sccsid = "@(#)quot.c 4.16 (Berkeley) 89/05/15"; |
b47543fa | 3 | #endif |
6430f2d0 | 4 | |
bea492ce | 5 | /* |
6430f2d0 | 6 | * quot |
bea492ce BJ |
7 | */ |
8 | ||
bea492ce | 9 | #include <sys/param.h> |
bea492ce | 10 | #include <sys/inode.h> |
f6910205 | 11 | #include <sys/fs.h> |
359e6e11 | 12 | #include <sys/file.h> |
b712d94e KB |
13 | #include <stdio.h> |
14 | #include <ctype.h> | |
15 | #include <paths.h> | |
bea492ce | 16 | |
f6910205 | 17 | #define ISIZ (MAXBSIZE/sizeof(struct dinode)) |
f6910205 KM |
18 | union { |
19 | struct fs u_sblock; | |
20 | char dummy[SBSIZE]; | |
21 | } sb_un; | |
22 | #define sblock sb_un.u_sblock | |
23 | struct dinode itab[MAXBSIZE/sizeof(struct dinode)]; | |
359e6e11 SL |
24 | |
25 | struct du { | |
26 | struct du *next; | |
bea492ce | 27 | long blocks; |
359e6e11 SL |
28 | long blocks30; |
29 | long blocks60; | |
30 | long blocks90; | |
bea492ce BJ |
31 | long nfiles; |
32 | int uid; | |
359e6e11 SL |
33 | #define NDU 2048 |
34 | } du[NDU]; | |
35 | int ndu; | |
36 | #define DUHASH 8209 /* smallest prime >= 4 * NDU */ | |
37 | #define HASH(u) ((u) % DUHASH) | |
38 | struct du *duhash[DUHASH]; | |
39 | ||
bea492ce BJ |
40 | #define TSIZE 500 |
41 | int sizes[TSIZE]; | |
42 | long overflow; | |
43 | ||
44 | int nflg; | |
45 | int fflg; | |
46 | int cflg; | |
6430f2d0 | 47 | int vflg; |
f6910205 | 48 | int hflg; |
6430f2d0 | 49 | long now; |
bea492ce | 50 | |
bea492ce | 51 | unsigned ino; |
bea492ce | 52 | |
bea492ce | 53 | char *malloc(); |
359e6e11 | 54 | char *getname(); |
bea492ce BJ |
55 | |
56 | main(argc, argv) | |
359e6e11 SL |
57 | int argc; |
58 | char *argv[]; | |
bea492ce | 59 | { |
84cfb8d9 KB |
60 | extern char *optarg; |
61 | extern int optind; | |
62 | int ch; | |
63 | time_t time(); | |
64 | ||
65 | while ((ch = getopt(argc, argv, "cfhnv")) != EOF) | |
66 | switch((char)ch) { | |
67 | case 'c': | |
68 | cflg++; break; | |
69 | case 'f': | |
70 | fflg++; break; | |
71 | case 'h': /* undocumented */ | |
72 | hflg++; break; | |
73 | case 'n': | |
74 | nflg++; break; | |
75 | case 'v': /* undocumented */ | |
76 | vflg++; break; | |
77 | case '?': | |
78 | default: | |
79 | fputs("usage: quot [-cfn] [filesystem ...]\n", stderr); | |
80 | exit(1); | |
81 | } | |
82 | argc -= optind; | |
83 | argv += optind; | |
84 | ||
85 | (void)time(&now); | |
e6fecf21 | 86 | setpassent(1); |
84cfb8d9 KB |
87 | if (argc) |
88 | for (; *argv; ++argv) { | |
89 | if (check(*argv, (char *)NULL) == 0) | |
90 | report(); | |
91 | } | |
92 | else | |
359e6e11 | 93 | quotall(); |
84cfb8d9 | 94 | exit(0); |
359e6e11 SL |
95 | } |
96 | ||
84cfb8d9 | 97 | #include <sys/dir.h> |
359e6e11 SL |
98 | #include <fstab.h> |
99 | ||
100 | quotall() | |
101 | { | |
102 | register struct fstab *fs; | |
103 | register char *cp; | |
84cfb8d9 | 104 | char dev[MAXNAMLEN + 10], *rindex(); |
359e6e11 | 105 | |
359e6e11 SL |
106 | while (fs = getfsent()) { |
107 | if (strcmp(fs->fs_type, FSTAB_RO) && | |
108 | strcmp(fs->fs_type, FSTAB_RW) && | |
109 | strcmp(fs->fs_type, FSTAB_RQ)) | |
110 | continue; | |
111 | cp = rindex(fs->fs_spec, '/'); | |
112 | if (cp == 0) | |
113 | continue; | |
b712d94e | 114 | (void)sprintf(dev, "%s/r%s", _PATH_DEV, cp + 1); |
619bf80a | 115 | if (check(dev, fs->fs_file) == 0) |
359e6e11 | 116 | report(); |
bea492ce | 117 | } |
bea492ce BJ |
118 | } |
119 | ||
619bf80a | 120 | check(file, fsdir) |
359e6e11 | 121 | char *file; |
619bf80a | 122 | char *fsdir; |
bea492ce | 123 | { |
359e6e11 SL |
124 | register int i, j, nfiles; |
125 | register struct du **dp; | |
f6910205 | 126 | daddr_t iblk; |
84cfb8d9 | 127 | long dev_bsize; |
359e6e11 | 128 | int c, fd; |
bea492ce | 129 | |
359e6e11 SL |
130 | /* |
131 | * Initialize tables between checks; | |
132 | * because of the qsort done in report() | |
133 | * the hash tables must be rebuilt each time. | |
134 | */ | |
135 | for (i = 0; i < TSIZE; i++) | |
136 | sizes[i] = 0; | |
137 | overflow = 0; | |
138 | for (dp = duhash; dp < &duhash[DUHASH]; dp++) | |
139 | *dp = 0; | |
140 | ndu = 0; | |
141 | fd = open(file, O_RDONLY); | |
142 | if (fd < 0) { | |
143 | fprintf(stderr, "quot: "); | |
144 | perror(file); | |
b47543fa | 145 | return (-1); |
bea492ce | 146 | } |
619bf80a S |
147 | printf("%s", file); |
148 | if (fsdir == NULL) { | |
149 | register struct fstab *fs = getfsspec(file); | |
150 | if (fs != NULL) | |
151 | fsdir = fs->fs_file; | |
152 | } | |
153 | if (fsdir != NULL && *fsdir != '\0') | |
154 | printf(" (%s)", fsdir); | |
155 | printf(":\n"); | |
bea492ce | 156 | sync(); |
84cfb8d9 | 157 | bread(fd, (long)SBOFF, (char *)&sblock, SBSIZE); |
a66ab591 | 158 | dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); |
bea492ce BJ |
159 | if (nflg) { |
160 | if (isdigit(c = getchar())) | |
84cfb8d9 | 161 | (void)ungetc(c, stdin); |
359e6e11 | 162 | else while (c != '\n' && c != EOF) |
bea492ce BJ |
163 | c = getchar(); |
164 | } | |
f6910205 KM |
165 | nfiles = sblock.fs_ipg * sblock.fs_ncg; |
166 | for (ino = 0; ino < nfiles; ) { | |
167 | iblk = fsbtodb(&sblock, itod(&sblock, ino)); | |
84cfb8d9 | 168 | bread(fd, iblk * dev_bsize, (char *)itab, (int)sblock.fs_bsize); |
ed77f19d KM |
169 | for (j = 0; j < INOPB(&sblock) && ino < nfiles; j++, ino++) { |
170 | if (ino < ROOTINO) | |
f6910205 | 171 | continue; |
bea492ce BJ |
172 | acct(&itab[j]); |
173 | } | |
174 | } | |
359e6e11 | 175 | close(fd); |
619bf80a | 176 | return (0); |
bea492ce BJ |
177 | } |
178 | ||
179 | acct(ip) | |
f6910205 | 180 | register struct dinode *ip; |
bea492ce | 181 | { |
359e6e11 SL |
182 | register struct du *dp; |
183 | struct du **hp; | |
f6910205 | 184 | long blks, frags, size; |
619bf80a | 185 | int n; |
bea492ce BJ |
186 | static fino; |
187 | ||
359e6e11 | 188 | if ((ip->di_mode & IFMT) == 0) |
bea492ce | 189 | return; |
359e6e11 SL |
190 | /* |
191 | * By default, take block count in inode. Otherwise (-h), | |
192 | * take the size field and estimate the blocks allocated. | |
193 | * The latter does not account for holes in files. | |
194 | */ | |
195 | if (!hflg) | |
81ac5aae | 196 | size = ip->di_blocks / 2; |
359e6e11 | 197 | else { |
81ac5aae KM |
198 | blks = lblkno(&sblock, ip->di_size); |
199 | frags = blks * sblock.fs_frag + | |
200 | numfrags(&sblock, dblksize(&sblock, ip, blks)); | |
201 | size = frags * sblock.fs_fsize / 1024; | |
f6910205 | 202 | } |
bea492ce | 203 | if (cflg) { |
359e6e11 | 204 | if ((ip->di_mode&IFMT) != IFDIR && (ip->di_mode&IFMT) != IFREG) |
bea492ce | 205 | return; |
f6910205 KM |
206 | if (size >= TSIZE) { |
207 | overflow += size; | |
208 | size = TSIZE-1; | |
bea492ce | 209 | } |
f6910205 | 210 | sizes[size]++; |
bea492ce BJ |
211 | return; |
212 | } | |
359e6e11 SL |
213 | hp = &duhash[HASH(ip->di_uid)]; |
214 | for (dp = *hp; dp; dp = dp->next) | |
215 | if (dp->uid == ip->di_uid) | |
216 | break; | |
217 | if (dp == 0) { | |
218 | if (ndu >= NDU) | |
219 | return; | |
220 | dp = &du[ndu++]; | |
221 | dp->next = *hp; | |
222 | *hp = dp; | |
223 | dp->uid = ip->di_uid; | |
224 | dp->nfiles = 0; | |
225 | dp->blocks = 0; | |
226 | dp->blocks30 = 0; | |
227 | dp->blocks60 = 0; | |
228 | dp->blocks90 = 0; | |
229 | } | |
230 | dp->blocks += size; | |
6430f2d0 BJ |
231 | #define DAY (60 * 60 * 24) /* seconds per day */ |
232 | if (now - ip->di_atime > 30 * DAY) | |
359e6e11 | 233 | dp->blocks30 += size; |
6430f2d0 | 234 | if (now - ip->di_atime > 60 * DAY) |
359e6e11 | 235 | dp->blocks60 += size; |
6430f2d0 | 236 | if (now - ip->di_atime > 90 * DAY) |
359e6e11 SL |
237 | dp->blocks90 += size; |
238 | dp->nfiles++; | |
239 | while (nflg) { | |
240 | register char *np; | |
241 | ||
242 | if (fino == 0) | |
243 | if (scanf("%d", &fino) <= 0) | |
bea492ce BJ |
244 | return; |
245 | if (fino > ino) | |
246 | return; | |
359e6e11 SL |
247 | if (fino < ino) { |
248 | while ((n = getchar()) != '\n' && n != EOF) | |
bea492ce BJ |
249 | ; |
250 | fino = 0; | |
359e6e11 | 251 | continue; |
bea492ce | 252 | } |
359e6e11 | 253 | if (np = getname(dp->uid)) |
84cfb8d9 | 254 | printf("%.7s\t", np); |
bea492ce | 255 | else |
84cfb8d9 | 256 | printf("%u\t", ip->di_uid); |
359e6e11 | 257 | while ((n = getchar()) == ' ' || n == '\t') |
bea492ce BJ |
258 | ; |
259 | putchar(n); | |
359e6e11 | 260 | while (n != EOF && n != '\n') { |
bea492ce BJ |
261 | n = getchar(); |
262 | putchar(n); | |
263 | } | |
264 | fino = 0; | |
359e6e11 | 265 | break; |
bea492ce BJ |
266 | } |
267 | } | |
268 | ||
359e6e11 | 269 | bread(fd, bno, buf, cnt) |
84cfb8d9 | 270 | long bno; |
359e6e11 | 271 | char *buf; |
bea492ce | 272 | { |
84cfb8d9 | 273 | off_t lseek(); |
bea492ce | 274 | |
84cfb8d9 | 275 | (void)lseek(fd, bno, L_SET); |
359e6e11 | 276 | if (read(fd, buf, cnt) != cnt) { |
84cfb8d9 | 277 | fprintf(stderr, "quot: read error at block %ld\n", bno); |
bea492ce BJ |
278 | exit(1); |
279 | } | |
280 | } | |
281 | ||
282 | qcmp(p1, p2) | |
359e6e11 | 283 | register struct du *p1, *p2; |
bea492ce | 284 | { |
359e6e11 SL |
285 | char *s1, *s2; |
286 | ||
bea492ce | 287 | if (p1->blocks > p2->blocks) |
359e6e11 | 288 | return (-1); |
bea492ce | 289 | if (p1->blocks < p2->blocks) |
359e6e11 SL |
290 | return (1); |
291 | s1 = getname(p1->uid); | |
292 | if (s1 == 0) | |
293 | return (0); | |
294 | s2 = getname(p2->uid); | |
295 | if (s2 == 0) | |
296 | return (0); | |
297 | return (strcmp(s1, s2)); | |
bea492ce BJ |
298 | } |
299 | ||
300 | report() | |
301 | { | |
302 | register i; | |
359e6e11 | 303 | register struct du *dp; |
bea492ce BJ |
304 | |
305 | if (nflg) | |
306 | return; | |
307 | if (cflg) { | |
359e6e11 SL |
308 | register long t = 0; |
309 | ||
310 | for (i = 0; i < TSIZE - 1; i++) | |
bea492ce BJ |
311 | if (sizes[i]) { |
312 | t += i*sizes[i]; | |
84cfb8d9 | 313 | printf("%d\t%d\t%ld\n", i, sizes[i], t); |
bea492ce | 314 | } |
84cfb8d9 | 315 | printf("%d\t%d\t%ld\n", |
359e6e11 | 316 | TSIZE - 1, sizes[TSIZE - 1], overflow + t); |
bea492ce BJ |
317 | return; |
318 | } | |
359e6e11 SL |
319 | qsort(du, ndu, sizeof (du[0]), qcmp); |
320 | for (dp = du; dp < &du[ndu]; dp++) { | |
321 | register char *cp; | |
322 | ||
323 | if (dp->blocks == 0) | |
bea492ce | 324 | return; |
359e6e11 | 325 | printf("%5D\t", dp->blocks); |
bea492ce | 326 | if (fflg) |
359e6e11 SL |
327 | printf("%5D\t", dp->nfiles); |
328 | if (cp = getname(dp->uid)) | |
329 | printf("%-8.8s", cp); | |
bea492ce | 330 | else |
359e6e11 | 331 | printf("#%-8d", dp->uid); |
6430f2d0 BJ |
332 | if (vflg) |
333 | printf("\t%5D\t%5D\t%5D", | |
359e6e11 | 334 | dp->blocks30, dp->blocks60, dp->blocks90); |
6430f2d0 | 335 | printf("\n"); |
bea492ce BJ |
336 | } |
337 | } | |
338 | ||
e43e6c78 KM |
339 | /* rest should be done with nameserver or database */ |
340 | ||
359e6e11 | 341 | #include <pwd.h> |
e43e6c78 | 342 | #include <grp.h> |
359e6e11 SL |
343 | #include <utmp.h> |
344 | ||
345 | struct utmp utmp; | |
359e6e11 | 346 | #define NMAX (sizeof (utmp.ut_name)) |
e43e6c78 | 347 | #define SCPYN(a, b) strncpy(a, b, NMAX) |
359e6e11 | 348 | |
e43e6c78 KM |
349 | #define NUID 64 /* power of 2 */ |
350 | #define UIDMASK 0x3f | |
351 | ||
352 | struct ncache { | |
353 | int uid; | |
354 | char name[NMAX+1]; | |
355 | } nc[NUID]; | |
359e6e11 | 356 | |
bea492ce | 357 | char * |
359e6e11 | 358 | getname(uid) |
bea492ce | 359 | { |
359e6e11 | 360 | register struct passwd *pw; |
359e6e11 | 361 | struct passwd *getpwent(); |
e43e6c78 KM |
362 | register int cp; |
363 | ||
e43e6c78 KM |
364 | cp = uid & UIDMASK; |
365 | if (uid >= 0 && nc[cp].uid == uid && nc[cp].name[0]) | |
366 | return (nc[cp].name); | |
367 | pw = getpwuid(uid); | |
368 | if (!pw) | |
359e6e11 | 369 | return (0); |
e43e6c78 KM |
370 | nc[cp].uid = uid; |
371 | SCPYN(nc[cp].name, pw->pw_name); | |
372 | return (nc[cp].name); | |
bea492ce | 373 | } |