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