xref: /titanic_51/usr/src/cmd/fs.d/ufs/quot/quot.c (revision d1a180b0452ce86577a43be3245d2eacdeec1a34)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d1a180b0Smaheshvs  * Common Development and Distribution License (the "License").
6*d1a180b0Smaheshvs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d1a180b0Smaheshvs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * quot
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
507c478bd9Sstevel@tonic-gate #include <pwd.h>
517c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
527c478bd9Sstevel@tonic-gate #include <sys/param.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
567c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
587c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
597c478bd9Sstevel@tonic-gate #include <sys/file.h>
607c478bd9Sstevel@tonic-gate #include <sys/stat.h>
617c478bd9Sstevel@tonic-gate #include <fcntl.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	ISIZ	(MAXBSIZE/sizeof (struct dinode))
647c478bd9Sstevel@tonic-gate static union {
657c478bd9Sstevel@tonic-gate 	struct fs u_sblock;
667c478bd9Sstevel@tonic-gate 	char dummy[SBSIZE];
677c478bd9Sstevel@tonic-gate } sb_un;
687c478bd9Sstevel@tonic-gate #define	sblock sb_un.u_sblock
697c478bd9Sstevel@tonic-gate static struct dinode *itab;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate struct du {
727c478bd9Sstevel@tonic-gate 	struct	du *next;
737c478bd9Sstevel@tonic-gate 	long	blocks;
747c478bd9Sstevel@tonic-gate 	long	blocks30;
757c478bd9Sstevel@tonic-gate 	long	blocks60;
767c478bd9Sstevel@tonic-gate 	long	blocks90;
777c478bd9Sstevel@tonic-gate 	long	nfiles;
787c478bd9Sstevel@tonic-gate 	uid_t	uid;
797c478bd9Sstevel@tonic-gate 	char	*u_name;
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate static struct du **du;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	UHASH 8209
847c478bd9Sstevel@tonic-gate static int	ndu;
857c478bd9Sstevel@tonic-gate #define	HASH(u) ((uint_t)(u) % UHASH)
867c478bd9Sstevel@tonic-gate static struct	du *duhashtbl[UHASH];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	TSIZE	2048
897c478bd9Sstevel@tonic-gate static int	sizes[TSIZE];
907c478bd9Sstevel@tonic-gate static offset_t overflow;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static int	nflg;
937c478bd9Sstevel@tonic-gate static int	fflg;
947c478bd9Sstevel@tonic-gate static int	cflg;
957c478bd9Sstevel@tonic-gate static int	vflg;
967c478bd9Sstevel@tonic-gate static int	hflg;
977c478bd9Sstevel@tonic-gate static int	aflg;
987c478bd9Sstevel@tonic-gate static long	now;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static unsigned	ino;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static void usage(void);
1037c478bd9Sstevel@tonic-gate static void quotall(void);
1047c478bd9Sstevel@tonic-gate static void qacct(struct dinode *);
1057c478bd9Sstevel@tonic-gate static void bread(int, diskaddr_t, char *, int);
1067c478bd9Sstevel@tonic-gate static void report(void);
1077c478bd9Sstevel@tonic-gate static int getdev(char **);
1087c478bd9Sstevel@tonic-gate static int check(char *, char *);
1097c478bd9Sstevel@tonic-gate static struct du *adduid(uid_t);
1107c478bd9Sstevel@tonic-gate static struct du *lookup(uid_t);
1117c478bd9Sstevel@tonic-gate static void sortprep(void);
1127c478bd9Sstevel@tonic-gate static void cleanup(void);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void
1157c478bd9Sstevel@tonic-gate usage()
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ufs usage: quot [-nfcvha] [filesystem ...]\n");
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate int
1217c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	int	opt;
1247c478bd9Sstevel@tonic-gate 	int	i;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1287c478bd9Sstevel@tonic-gate 		    "ufs Usage: quot [-nfcvha] [filesystem ...]\n");
1297c478bd9Sstevel@tonic-gate 		return (32);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	now = time(0);
1337c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "nfcvhaV")) != EOF) {
1347c478bd9Sstevel@tonic-gate 		switch (opt) {
1357c478bd9Sstevel@tonic-gate 		case 'n':
1367c478bd9Sstevel@tonic-gate 			nflg++;
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		case 'f':
1397c478bd9Sstevel@tonic-gate 			fflg++;
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		case 'c':
1427c478bd9Sstevel@tonic-gate 			cflg++;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case 'v':
1457c478bd9Sstevel@tonic-gate 			vflg++;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 		case 'h':
1487c478bd9Sstevel@tonic-gate 			hflg++;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case 'a':
1517c478bd9Sstevel@tonic-gate 			aflg++;
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		case 'V':		/* Print command line */
1547c478bd9Sstevel@tonic-gate 			{
1557c478bd9Sstevel@tonic-gate 				char		*opt_text;
1567c478bd9Sstevel@tonic-gate 				int		opt_count;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "quot -F UFS ");
1597c478bd9Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
1607c478bd9Sstevel@tonic-gate 				    opt_count++) {
1617c478bd9Sstevel@tonic-gate 					opt_text = argv[opt_count];
1627c478bd9Sstevel@tonic-gate 					if (opt_text)
1637c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout, " %s ",
1647c478bd9Sstevel@tonic-gate 						    opt_text);
1657c478bd9Sstevel@tonic-gate 				}
1667c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		case '?':
1707c478bd9Sstevel@tonic-gate 			usage();
1717c478bd9Sstevel@tonic-gate 			return (32);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (aflg) {
1767c478bd9Sstevel@tonic-gate 		quotall();
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
1807c478bd9Sstevel@tonic-gate 		if ((getdev(&argv[i]) == 0) &&
1817c478bd9Sstevel@tonic-gate 			(check(argv[i], (char *)NULL) == 0)) {
1827c478bd9Sstevel@tonic-gate 				report();
1837c478bd9Sstevel@tonic-gate 				cleanup();
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	return (0);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static void
1907c478bd9Sstevel@tonic-gate quotall()
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	FILE *fstab;
1937c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
1947c478bd9Sstevel@tonic-gate 	char *cp;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	fstab = fopen(MNTTAB, "r");
1997c478bd9Sstevel@tonic-gate 	if (fstab == NULL) {
2007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
2017c478bd9Sstevel@tonic-gate 		exit(32);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	while (getmntent(fstab, &mntp) == NULL) {
2047c478bd9Sstevel@tonic-gate 		if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0)
2057c478bd9Sstevel@tonic-gate 			continue;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		if ((cp = getfullrawname(mntp.mnt_special)) == NULL)
2087c478bd9Sstevel@tonic-gate 			continue;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (*cp == '\0')
2117c478bd9Sstevel@tonic-gate 			continue;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		if (check(cp, mntp.mnt_mountp) == 0) {
2147c478bd9Sstevel@tonic-gate 			report();
2157c478bd9Sstevel@tonic-gate 			cleanup();
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		free(cp);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	(void) fclose(fstab);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
2247c478bd9Sstevel@tonic-gate check(char *file, char *fsdir)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	FILE *fstab;
2277c478bd9Sstevel@tonic-gate 	int i, j;
2287c478bd9Sstevel@tonic-gate 	int c, fd;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Initialize tables between checks;
2337c478bd9Sstevel@tonic-gate 	 * because of the qsort done in report()
2347c478bd9Sstevel@tonic-gate 	 * the hash tables must be rebuilt each time.
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSIZE; i++)
2377c478bd9Sstevel@tonic-gate 		sizes[i] = 0;
2387c478bd9Sstevel@tonic-gate 	overflow = 0LL;
2397c478bd9Sstevel@tonic-gate 	ndu = 0;
2407c478bd9Sstevel@tonic-gate 	fd = open64(file, O_RDONLY);
2417c478bd9Sstevel@tonic-gate 	if (fd < 0) {
2427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: ");
2437c478bd9Sstevel@tonic-gate 		perror(file);
2447c478bd9Sstevel@tonic-gate 		exit(32);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	(void) printf("%s", file);
2477c478bd9Sstevel@tonic-gate 	if (fsdir == NULL) {
2487c478bd9Sstevel@tonic-gate 		struct mnttab mntp;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		fstab = fopen(MNTTAB, "r");
2517c478bd9Sstevel@tonic-gate 		if (fstab == NULL) {
2527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
2537c478bd9Sstevel@tonic-gate 			exit(32);
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 		while (getmntent(fstab, &mntp) == NULL) {
2567c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0)
2577c478bd9Sstevel@tonic-gate 				continue;
2587c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_special, file) == 0) {
2597c478bd9Sstevel@tonic-gate 				fsdir = mntp.mnt_mountp;
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	if (fsdir != NULL && *fsdir != '\0')
2657c478bd9Sstevel@tonic-gate 		(void) printf(" (%s)", fsdir);
2667c478bd9Sstevel@tonic-gate 	(void) printf(":\n");
2677c478bd9Sstevel@tonic-gate 	sync();
2687c478bd9Sstevel@tonic-gate 	bread(fd, (diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2697c478bd9Sstevel@tonic-gate 	if (nflg) {
2707c478bd9Sstevel@tonic-gate 		if (isdigit(c = getchar()))
2717c478bd9Sstevel@tonic-gate 			(void) ungetc(c, stdin);
2727c478bd9Sstevel@tonic-gate 		else while (c != '\n' && c != EOF)
2737c478bd9Sstevel@tonic-gate 			c = getchar();
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	itab = (struct dinode *)calloc(sblock.fs_ipg, sizeof (struct dinode));
2777c478bd9Sstevel@tonic-gate 	if (itab == NULL) {
2787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2797c478bd9Sstevel@tonic-gate 				"not enough memory to allocate tables\n");
2807c478bd9Sstevel@tonic-gate 		return (1);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	ino = 0;
2847c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
2857c478bd9Sstevel@tonic-gate 		bread(fd, (diskaddr_t)fsbtodb(&sblock, cgimin(&sblock, c)),
2867c478bd9Sstevel@tonic-gate 				(char *)itab,
2877c478bd9Sstevel@tonic-gate 				(int)(sblock.fs_ipg * sizeof (struct dinode)));
2887c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++, ino++) {
2897c478bd9Sstevel@tonic-gate 			if (ino < UFSROOTINO)
2907c478bd9Sstevel@tonic-gate 				continue;
2917c478bd9Sstevel@tonic-gate 			qacct(&itab[j]);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	(void) close(fd);
2957c478bd9Sstevel@tonic-gate 	return (0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate static void
2997c478bd9Sstevel@tonic-gate qacct(struct dinode *ip)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	struct du *dp;
3027c478bd9Sstevel@tonic-gate 	long blks, frags, size;
3037c478bd9Sstevel@tonic-gate 	int n;
304*d1a180b0Smaheshvs 	static int fino;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	ip->di_mode = ip->di_smode;
3077c478bd9Sstevel@tonic-gate 	if (ip->di_suid != UID_LONG) {
3087c478bd9Sstevel@tonic-gate 		ip->di_uid = ip->di_suid;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) == 0)
3117c478bd9Sstevel@tonic-gate 		return;
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * By default, take block count in inode.  Otherwise (-h),
3147c478bd9Sstevel@tonic-gate 	 * take the size field and estimate the blocks allocated.
3157c478bd9Sstevel@tonic-gate 	 * The latter does not account for holes in files.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	if (!hflg)
3187c478bd9Sstevel@tonic-gate 		size = ip->di_blocks / 2;
3197c478bd9Sstevel@tonic-gate 	else {
3207c478bd9Sstevel@tonic-gate 		blks = lblkno(&sblock, ip->di_size);
3217c478bd9Sstevel@tonic-gate 		frags = blks * sblock.fs_frag +
3227c478bd9Sstevel@tonic-gate 			numfrags(&sblock, dblksize(&sblock, ip, blks));
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * Must cast to offset_t because for a large file,
3257c478bd9Sstevel@tonic-gate 		 * frags multiplied by sblock.fs_fsize will not fit in a long.
3267c478bd9Sstevel@tonic-gate 		 * However, when divided by 1024, the end result will fit in
3277c478bd9Sstevel@tonic-gate 		 * the 32 bit size variable (40 bit UFS).
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 	    size = (long)((offset_t)frags * (offset_t)sblock.fs_fsize / 1024);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	if (cflg) {
3327c478bd9Sstevel@tonic-gate 		if ((ip->di_mode&IFMT) != IFDIR && (ip->di_mode&IFMT) != IFREG)
3337c478bd9Sstevel@tonic-gate 			return;
3347c478bd9Sstevel@tonic-gate 		if (size >= TSIZE) {
3357c478bd9Sstevel@tonic-gate 			overflow += (offset_t)size;
3367c478bd9Sstevel@tonic-gate 			size = TSIZE-1;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		sizes[size]++;
3397c478bd9Sstevel@tonic-gate 		return;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	dp = lookup(ip->di_uid);
3427c478bd9Sstevel@tonic-gate 	if (dp == NULL)
3437c478bd9Sstevel@tonic-gate 		return;
3447c478bd9Sstevel@tonic-gate 	dp->blocks += size;
3457c478bd9Sstevel@tonic-gate #define	DAY (60 * 60 * 24)	/* seconds per day */
3467c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 30 * DAY)
3477c478bd9Sstevel@tonic-gate 		dp->blocks30 += size;
3487c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 60 * DAY)
3497c478bd9Sstevel@tonic-gate 		dp->blocks60 += size;
3507c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 90 * DAY)
3517c478bd9Sstevel@tonic-gate 		dp->blocks90 += size;
3527c478bd9Sstevel@tonic-gate 	dp->nfiles++;
3537c478bd9Sstevel@tonic-gate 	while (nflg) {
3547c478bd9Sstevel@tonic-gate 		if (fino == 0)
3557c478bd9Sstevel@tonic-gate 			if (scanf("%d", &fino) <= 0)
3567c478bd9Sstevel@tonic-gate 				return;
3577c478bd9Sstevel@tonic-gate 		if (fino > ino)
3587c478bd9Sstevel@tonic-gate 			return;
3597c478bd9Sstevel@tonic-gate 		if (fino < ino) {
3607c478bd9Sstevel@tonic-gate 			while ((n = getchar()) != '\n' && n != EOF)
3617c478bd9Sstevel@tonic-gate 				;
3627c478bd9Sstevel@tonic-gate 			fino = 0;
3637c478bd9Sstevel@tonic-gate 			continue;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if (dp->u_name)
3667c478bd9Sstevel@tonic-gate 			(void) printf("%.7s	", dp->u_name);
3677c478bd9Sstevel@tonic-gate 		else
3687c478bd9Sstevel@tonic-gate 			(void) printf("%ld	", (long)ip->di_uid);
3697c478bd9Sstevel@tonic-gate 		while ((n = getchar()) == ' ' || n == '\t')
3707c478bd9Sstevel@tonic-gate 			;
3717c478bd9Sstevel@tonic-gate 		(void) putchar(n);
3727c478bd9Sstevel@tonic-gate 		while (n != EOF && n != '\n') {
3737c478bd9Sstevel@tonic-gate 			n = getchar();
3747c478bd9Sstevel@tonic-gate 			(void) putchar(n);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		fino = 0;
3777c478bd9Sstevel@tonic-gate 		break;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate bread(int fd, diskaddr_t bno, char *buf, int cnt)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	int	ret;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (llseek(fd, (offset_t)(bno * DEV_BSIZE), SEEK_SET) < 0) {
3877c478bd9Sstevel@tonic-gate 		perror("llseek");
3887c478bd9Sstevel@tonic-gate 		exit(32);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if ((ret = read(fd, buf, cnt)) != cnt) {
3927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: read returns %d (cnt = %d)\n",
3937c478bd9Sstevel@tonic-gate 						ret, cnt);
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: read error at block %lld\n", bno);
3957c478bd9Sstevel@tonic-gate 		perror("read");
3967c478bd9Sstevel@tonic-gate 		exit(32);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate qcmp(const void *arg1, const void *arg2)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	struct du **p1 = (struct du **)arg1;
4047c478bd9Sstevel@tonic-gate 	struct du **p2 = (struct du **)arg2;
4057c478bd9Sstevel@tonic-gate 	char *s1, *s2;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ((*p1)->blocks > (*p2)->blocks)
4087c478bd9Sstevel@tonic-gate 		return (-1);
4097c478bd9Sstevel@tonic-gate 	if ((*p1)->blocks < (*p2)->blocks)
4107c478bd9Sstevel@tonic-gate 		return (1);
4117c478bd9Sstevel@tonic-gate 	s1 = (*p1)->u_name;
4127c478bd9Sstevel@tonic-gate 	if (s1 == NULL)
4137c478bd9Sstevel@tonic-gate 		return (0);
4147c478bd9Sstevel@tonic-gate 	s2 = (*p2)->u_name;
4157c478bd9Sstevel@tonic-gate 	if (s2 == NULL)
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate 	return (strcmp(s1, s2));
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static void
4217c478bd9Sstevel@tonic-gate report()
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int i;
4247c478bd9Sstevel@tonic-gate 	struct du **dp;
4257c478bd9Sstevel@tonic-gate 	int cnt;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (nflg)
4287c478bd9Sstevel@tonic-gate 		return;
4297c478bd9Sstevel@tonic-gate 	if (cflg) {
4307c478bd9Sstevel@tonic-gate 		long t = 0;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSIZE - 1; i++)
4337c478bd9Sstevel@tonic-gate 			if (sizes[i]) {
4347c478bd9Sstevel@tonic-gate 				t += i*sizes[i];
4357c478bd9Sstevel@tonic-gate 				(void) printf("%d	%d	%ld\n",
4367c478bd9Sstevel@tonic-gate 								i, sizes[i], t);
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 		if (sizes[TSIZE -1 ])
4397c478bd9Sstevel@tonic-gate 			(void) printf("%d	%d	%lld\n", TSIZE - 1,
4407c478bd9Sstevel@tonic-gate 			    sizes[TSIZE - 1], overflow + (offset_t)t);
4417c478bd9Sstevel@tonic-gate 		return;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	sortprep();
4447c478bd9Sstevel@tonic-gate 	qsort(du, ndu, sizeof (du[0]), qcmp);
4457c478bd9Sstevel@tonic-gate 	for (cnt = 0, dp = &du[0]; dp && cnt != ndu; dp++, cnt++) {
4467c478bd9Sstevel@tonic-gate 		if ((*dp)->blocks == 0)
4477c478bd9Sstevel@tonic-gate 			return;
4487c478bd9Sstevel@tonic-gate 		(void) printf("%5ld\t", (*dp)->blocks);
4497c478bd9Sstevel@tonic-gate 		if (fflg)
4507c478bd9Sstevel@tonic-gate 			(void) printf("%5ld\t", (*dp)->nfiles);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		if ((*dp)->u_name)
4537c478bd9Sstevel@tonic-gate 			(void) printf("%-8s", (*dp)->u_name);
4547c478bd9Sstevel@tonic-gate 		else
4557c478bd9Sstevel@tonic-gate 			(void) printf("#%-8ld", (long)(*dp)->uid);
4567c478bd9Sstevel@tonic-gate 		if (vflg)
4577c478bd9Sstevel@tonic-gate 			(void) printf("\t%5ld\t%5ld\t%5ld",
4587c478bd9Sstevel@tonic-gate 			    (*dp)->blocks30, (*dp)->blocks60, (*dp)->blocks90);
4597c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate static int
4667c478bd9Sstevel@tonic-gate getdev(char **devpp)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	struct stat64 statb;
4697c478bd9Sstevel@tonic-gate 	FILE *fstab;
4707c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
4717c478bd9Sstevel@tonic-gate 	char *cp;	/* Pointer to raw device name */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (stat64(*devpp, &statb) < 0) {
4767c478bd9Sstevel@tonic-gate 		perror(*devpp);
4777c478bd9Sstevel@tonic-gate 		exit(32);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == S_IFCHR)
4807c478bd9Sstevel@tonic-gate 		return (0);
4817c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == S_IFBLK) {
4827c478bd9Sstevel@tonic-gate 		/* If we can't get the raw name, keep the block name */
4837c478bd9Sstevel@tonic-gate 		if ((cp = getfullrawname(*devpp)) != NULL)
4847c478bd9Sstevel@tonic-gate 			*devpp = strdup(cp);
4857c478bd9Sstevel@tonic-gate 		return (0);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	fstab = fopen(MNTTAB, "r");
4887c478bd9Sstevel@tonic-gate 	if (fstab == NULL) {
4897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
4907c478bd9Sstevel@tonic-gate 		exit(32);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	while (getmntent(fstab, &mntp) == NULL) {
4937c478bd9Sstevel@tonic-gate 		if (strcmp(mntp.mnt_mountp, *devpp) == 0) {
4947c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0) {
4957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4967c478bd9Sstevel@tonic-gate 				    "quot: %s not ufs filesystem\n",
4977c478bd9Sstevel@tonic-gate 				    *devpp);
4987c478bd9Sstevel@tonic-gate 				exit(32);
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			/* If we can't get the raw name, use the block name */
5017c478bd9Sstevel@tonic-gate 			if ((cp = getfullrawname(mntp.mnt_special)) == NULL)
5027c478bd9Sstevel@tonic-gate 				cp = mntp.mnt_special;
5037c478bd9Sstevel@tonic-gate 			*devpp = strdup(cp);
5047c478bd9Sstevel@tonic-gate 			(void) fclose(fstab);
5057c478bd9Sstevel@tonic-gate 			return (0);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	(void) fclose(fstab);
5097c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "quot: %s doesn't appear to be a filesystem.\n",
5107c478bd9Sstevel@tonic-gate 	    *devpp);
5117c478bd9Sstevel@tonic-gate 	usage();
5127c478bd9Sstevel@tonic-gate 	exit(32);
5137c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static struct du *
5177c478bd9Sstevel@tonic-gate lookup(uid_t uid)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct	passwd *pwp;
5207c478bd9Sstevel@tonic-gate 	struct	du *up;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	for (up = duhashtbl[HASH(uid)]; up != NULL; up = up->next) {
5237c478bd9Sstevel@tonic-gate 		if (up->uid == uid)
5247c478bd9Sstevel@tonic-gate 			return (up);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	pwp = getpwuid(uid);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	up = adduid(uid);
5307c478bd9Sstevel@tonic-gate 	if (up && pwp) {
5317c478bd9Sstevel@tonic-gate 		up->u_name = strdup(pwp->pw_name);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 	return (up);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate static struct du *
5377c478bd9Sstevel@tonic-gate adduid(uid_t uid)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	struct du *up, **uhp;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	up = (struct du *)calloc(1, sizeof (struct du));
5427c478bd9Sstevel@tonic-gate 	if (up == NULL) {
5437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5447c478bd9Sstevel@tonic-gate 			"out of memory for du structures\n");
5457c478bd9Sstevel@tonic-gate 			exit(32);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	uhp = &duhashtbl[HASH(uid)];
5497c478bd9Sstevel@tonic-gate 	up->next = *uhp;
5507c478bd9Sstevel@tonic-gate 	*uhp = up;
5517c478bd9Sstevel@tonic-gate 	up->uid = uid;
5527c478bd9Sstevel@tonic-gate 	up->u_name = NULL;
5537c478bd9Sstevel@tonic-gate 	ndu++;
5547c478bd9Sstevel@tonic-gate 	return (up);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static void
5587c478bd9Sstevel@tonic-gate sortprep()
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	struct du **dp, *ep;
5617c478bd9Sstevel@tonic-gate 	struct du **hp;
5627c478bd9Sstevel@tonic-gate 	int i, cnt = 0;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	dp = NULL;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	dp = (struct du **)calloc(ndu, sizeof (struct du **));
5677c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
5687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5697c478bd9Sstevel@tonic-gate 			"out of memory for du structures\n");
5707c478bd9Sstevel@tonic-gate 			exit(32);
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	for (hp = duhashtbl, i = 0; i != UHASH; i++) {
5747c478bd9Sstevel@tonic-gate 		if (hp[i] == NULL)
5757c478bd9Sstevel@tonic-gate 			continue;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		for (ep = hp[i]; ep; ep = ep->next) {
5787c478bd9Sstevel@tonic-gate 			dp[cnt++] = ep;
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	du = dp;
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate static void
5857c478bd9Sstevel@tonic-gate cleanup()
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	int		i;
5887c478bd9Sstevel@tonic-gate 	struct du 	*ep, *next;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/*
5917c478bd9Sstevel@tonic-gate 	 * Release memory from hash table and du
5927c478bd9Sstevel@tonic-gate 	 */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (du) {
5957c478bd9Sstevel@tonic-gate 		free(du);
5967c478bd9Sstevel@tonic-gate 		du = NULL;
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	for (i = 0; i != UHASH; i++) {
6017c478bd9Sstevel@tonic-gate 		if (duhashtbl[i] == NULL)
6027c478bd9Sstevel@tonic-gate 			continue;
6037c478bd9Sstevel@tonic-gate 		ep = duhashtbl[i];
6047c478bd9Sstevel@tonic-gate 		while (ep) {
6057c478bd9Sstevel@tonic-gate 			next = ep->next;
6067c478bd9Sstevel@tonic-gate 			if (ep->u_name) {
6077c478bd9Sstevel@tonic-gate 				free(ep->u_name);
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 			free(ep);
6107c478bd9Sstevel@tonic-gate 			ep = next;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		duhashtbl[i] = NULL;
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate }
615