xref: /illumos-gate/usr/src/cmd/fs.d/ufs/ff/ff.c (revision 2a8bcb4e)
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 /*
226451fdbcSvsakar  * 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) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * ff -- obtain file names from reading filesystem
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	NB		500
397c478bd9Sstevel@tonic-gate #define	MAXNINDIR	(MAXBSIZE / sizeof (daddr32_t))
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
447c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
457c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
487c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <strings.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <fcntl.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate #include <pwd.h>
567c478bd9Sstevel@tonic-gate #include "roll_log.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	MIN_PHYS_READ	BBSIZE
597c478bd9Sstevel@tonic-gate #define	DAY		(24*60*60)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate union {
637c478bd9Sstevel@tonic-gate 	struct	fs	sblk;
647c478bd9Sstevel@tonic-gate 	char xxx[SBSIZE];	/* because fs is variable length */
657c478bd9Sstevel@tonic-gate } real_fs;
667c478bd9Sstevel@tonic-gate #define	sblock real_fs.sblk
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate struct	dinode  *itab;	/*  = (struct dinode *)itab; */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate struct 	dinode	*gip;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct ilist {
737c478bd9Sstevel@tonic-gate 	ino_t	ino;
747c478bd9Sstevel@tonic-gate 	ushort_t	mode;
757c478bd9Sstevel@tonic-gate 	uid_t	uid;
767c478bd9Sstevel@tonic-gate 	gid_t	gid;
777c478bd9Sstevel@tonic-gate } ilist[NB];
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct	htab
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	ino_t	h_ino;
827c478bd9Sstevel@tonic-gate 	ino_t	h_pino;
837c478bd9Sstevel@tonic-gate 	int	h_name_index;		/* index into string table */
847c478bd9Sstevel@tonic-gate } *htab;
857c478bd9Sstevel@tonic-gate char *strngtab;
867c478bd9Sstevel@tonic-gate long hsize;
877c478bd9Sstevel@tonic-gate int strngloc;
887c478bd9Sstevel@tonic-gate int strngtab_size;
897c478bd9Sstevel@tonic-gate #define	STRNGTAB_INCR	(1024*16)	/* amount to grow strngtab */
907c478bd9Sstevel@tonic-gate #define	MAX_STRNGTAB_INDEX()	(strngtab_size - 1)
917c478bd9Sstevel@tonic-gate #define	AVG_PATH_LEN	30		/* average (?) length of name */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate struct dirstuff {
947c478bd9Sstevel@tonic-gate 	int loc;
957c478bd9Sstevel@tonic-gate 	struct dinode *ip;
967c478bd9Sstevel@tonic-gate 	char dbuf[MAXBSIZE];
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate int	Aflg = 0;	/* accessed in n days */
997c478bd9Sstevel@tonic-gate int	Mflg = 0;	/* modified in n days */
1007c478bd9Sstevel@tonic-gate int	Nflg = 0;	/* modified more recently than 'file' */
1017c478bd9Sstevel@tonic-gate int	Cflg = 0;	/* changed within n days */
1027c478bd9Sstevel@tonic-gate int	aflg = 0;	/* print the names `.'  and  `..' */
1037c478bd9Sstevel@tonic-gate int	sflg = 0; /* print only special files and files with set-user-ID mode */
1047c478bd9Sstevel@tonic-gate int	Sflg = 0;	/* print file size */
1057c478bd9Sstevel@tonic-gate int	iflg = 0;	/* number of inodes being searched for */
1067c478bd9Sstevel@tonic-gate int	Iflg = 0;	/* do not print i-number */
1077c478bd9Sstevel@tonic-gate int	Lflg = 0;	/* supplementary list of multiply linked files */
1087c478bd9Sstevel@tonic-gate int	mflg = 0;
1097c478bd9Sstevel@tonic-gate int	pflg = 0;	/* a prefix exists */
1107c478bd9Sstevel@tonic-gate int	uflg = 0;	/* print the owner's login name */
1117c478bd9Sstevel@tonic-gate int	fi;
1127c478bd9Sstevel@tonic-gate ino_t	ino;
1137c478bd9Sstevel@tonic-gate int	nhent;
1147c478bd9Sstevel@tonic-gate int	nxfile;
1157c478bd9Sstevel@tonic-gate int	imax;		/* highest inode number */
1167c478bd9Sstevel@tonic-gate int	inode_reads;
1177c478bd9Sstevel@tonic-gate int	passwd_lookups;
1187c478bd9Sstevel@tonic-gate int	Adelay;		/* Access delay */
1197c478bd9Sstevel@tonic-gate int	Asign;		/* Access sign */
1207c478bd9Sstevel@tonic-gate int	Mdelay;		/* Modify delay */
1217c478bd9Sstevel@tonic-gate int	Msign;		/* Modify sign */
1227c478bd9Sstevel@tonic-gate int	Cdelay;		/* change delay */
1237c478bd9Sstevel@tonic-gate int	Csign;		/* change sign */
1247c478bd9Sstevel@tonic-gate time_t	Nage;		/* Last modification time of the file */
1257c478bd9Sstevel@tonic-gate char	*Lname;		/* filename for supplementary list */
1267c478bd9Sstevel@tonic-gate FILE	*Lfile;		/* file for supplementary list */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Function prototypes
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate void check(char *file);
1327c478bd9Sstevel@tonic-gate void pass1(struct dinode *ip);
1337c478bd9Sstevel@tonic-gate void pass2(struct dinode *ip);
1347c478bd9Sstevel@tonic-gate void pass3(struct dinode *ip);
1357c478bd9Sstevel@tonic-gate struct direct *dreaddir(struct dirstuff *dirp);
1367c478bd9Sstevel@tonic-gate int dotname(struct direct *dp);
1377c478bd9Sstevel@tonic-gate void pname(FILE *stream, ino_t i, int lev);
1387c478bd9Sstevel@tonic-gate struct htab *lookup(ino_t i, int ef);
1397c478bd9Sstevel@tonic-gate void bread(diskaddr_t bno, char *buf, int cnt);
1407c478bd9Sstevel@tonic-gate diskaddr_t bmap(diskaddr_t i);
1417c478bd9Sstevel@tonic-gate struct dinode *ginode(ino_t inumber);
1427c478bd9Sstevel@tonic-gate char *user_name(int uid);
1437c478bd9Sstevel@tonic-gate int cmp(int a, int b, int s);
1447c478bd9Sstevel@tonic-gate time_t mod_time(char *file);
1457c478bd9Sstevel@tonic-gate void out_multilinks();
1467c478bd9Sstevel@tonic-gate void usage();
1477c478bd9Sstevel@tonic-gate int extend_strngtab(unsigned int size);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate long	atol();
1507c478bd9Sstevel@tonic-gate offset_t llseek();
1517c478bd9Sstevel@tonic-gate char 	*strcpy();
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate char	*prefix;
1547c478bd9Sstevel@tonic-gate time_t	Today;
1557c478bd9Sstevel@tonic-gate int	nerror;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate extern int	optind;
1597c478bd9Sstevel@tonic-gate extern char	*optarg;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate char *subopts [] = {
1627c478bd9Sstevel@tonic-gate #define	A_FLAG		0
1637c478bd9Sstevel@tonic-gate 	"a",
1647c478bd9Sstevel@tonic-gate #define	M_FLAG		1
1657c478bd9Sstevel@tonic-gate 	"m",
1667c478bd9Sstevel@tonic-gate #define	S_FLAG		2
1677c478bd9Sstevel@tonic-gate 	"s",
1687c478bd9Sstevel@tonic-gate 	NULL
1697c478bd9Sstevel@tonic-gate 	};
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])172*d1a180b0Smaheshvs main(int argc, char *argv[])
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	long n;
1757c478bd9Sstevel@tonic-gate 	int	opt;
1767c478bd9Sstevel@tonic-gate 	char	*suboptions,	*value;
1777c478bd9Sstevel@tonic-gate 	char *p;
1787c478bd9Sstevel@tonic-gate 	int first = 0;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	Today = time((time_t *)0);
1817c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "Ia:c:i:lm:n:o:p:su")) != EOF) {
1827c478bd9Sstevel@tonic-gate 		switch (opt) {
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case 'a':
1857c478bd9Sstevel@tonic-gate 			Aflg++;
1867c478bd9Sstevel@tonic-gate 			Adelay = atoi(optarg);
1877c478bd9Sstevel@tonic-gate 			Asign = optarg[0];
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		case 'I':
1917c478bd9Sstevel@tonic-gate 			Iflg++;
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		case 'c':
1957c478bd9Sstevel@tonic-gate 			Cflg++;
1967c478bd9Sstevel@tonic-gate 			Cdelay = atoi(optarg);
1977c478bd9Sstevel@tonic-gate 			Csign = optarg[0];
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		case 'l':
2017c478bd9Sstevel@tonic-gate 			Lflg++;
2027c478bd9Sstevel@tonic-gate 			Lname = tmpnam((char *)0);
2037c478bd9Sstevel@tonic-gate 			if ((Lfile = fopen(Lname, "w+")) == NULL) {
2047c478bd9Sstevel@tonic-gate 				perror("open");
2057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2067c478bd9Sstevel@tonic-gate 				"ff: unable to open temp file, -l ignored\n");
2077c478bd9Sstevel@tonic-gate 				Lflg = 0;
2087c478bd9Sstevel@tonic-gate 			}
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		case 'm':
2127c478bd9Sstevel@tonic-gate 			Mflg++;
2137c478bd9Sstevel@tonic-gate 			Mdelay = atoi(optarg);
2147c478bd9Sstevel@tonic-gate 			Msign = optarg[0];
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		case 'n':
2187c478bd9Sstevel@tonic-gate 			Nflg++;
2197c478bd9Sstevel@tonic-gate 			Nage = mod_time(optarg);
2207c478bd9Sstevel@tonic-gate 			break;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		case 'o':
2237c478bd9Sstevel@tonic-gate 			/*
2247c478bd9Sstevel@tonic-gate 			 * ufs specific options.
2257c478bd9Sstevel@tonic-gate 			 */
2267c478bd9Sstevel@tonic-gate 			suboptions = optarg;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 			if (*suboptions == '\0')
2297c478bd9Sstevel@tonic-gate 				usage();
2307c478bd9Sstevel@tonic-gate 			while (*suboptions != '\0') {
2317c478bd9Sstevel@tonic-gate 				switch ((getsubopt(&suboptions,
2327c478bd9Sstevel@tonic-gate 							subopts, &value))) {
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 				case A_FLAG:
2357c478bd9Sstevel@tonic-gate 					aflg++;
2367c478bd9Sstevel@tonic-gate 					break;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 				case M_FLAG:
2397c478bd9Sstevel@tonic-gate 					mflg++;
2407c478bd9Sstevel@tonic-gate 					break;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 				case S_FLAG:
2437c478bd9Sstevel@tonic-gate 					sflg++;
2447c478bd9Sstevel@tonic-gate 					break;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 				default:
2477c478bd9Sstevel@tonic-gate 					usage();
2487c478bd9Sstevel@tonic-gate 				}
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		case 'i':
2537c478bd9Sstevel@tonic-gate 			while ((p = (char *)strtok(((first++ == 0) ?
2547c478bd9Sstevel@tonic-gate 			optarg: ((char *)0)), ", ")) != NULL) {
2557c478bd9Sstevel@tonic-gate 				if ((n = atoi(p)) == 0)
2567c478bd9Sstevel@tonic-gate 					break;
2577c478bd9Sstevel@tonic-gate 				ilist[iflg].ino = n;
2587c478bd9Sstevel@tonic-gate 				nxfile = iflg;
2597c478bd9Sstevel@tonic-gate 				iflg++;
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		case 'p':
2647c478bd9Sstevel@tonic-gate 			prefix = optarg;
2657c478bd9Sstevel@tonic-gate 			pflg++;
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		case 's':
2697c478bd9Sstevel@tonic-gate 			Sflg++;
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		case 'u':
2737c478bd9Sstevel@tonic-gate 			uflg++;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		case '?':
2777c478bd9Sstevel@tonic-gate 			usage();
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	argc -= optind;
2817c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
2827c478bd9Sstevel@tonic-gate 	while (argc--) {
2837c478bd9Sstevel@tonic-gate 		check(*argv);
2847c478bd9Sstevel@tonic-gate 		argv++;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	if (Lflg) {
2877c478bd9Sstevel@tonic-gate 		out_multilinks();
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	if (nerror)
2907c478bd9Sstevel@tonic-gate 		return (32);
2917c478bd9Sstevel@tonic-gate 	return (0);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate void
check(char * file)2957c478bd9Sstevel@tonic-gate check(char *file)
2967c478bd9Sstevel@tonic-gate {
297*d1a180b0Smaheshvs 	int i, j, c;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	fi = open64(file, 0);
3007c478bd9Sstevel@tonic-gate 	if (fi < 0) {
3017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ff: cannot open %s\n", file);
3027c478bd9Sstevel@tonic-gate 		nerror++;
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	nhent = 0;
3067c478bd9Sstevel@tonic-gate 	(void) printf("%s:\n", file);
3077c478bd9Sstevel@tonic-gate 	sync();
3087c478bd9Sstevel@tonic-gate 	bread(SBLOCK, (char *)&sblock, SBSIZE);
3097c478bd9Sstevel@tonic-gate 	if ((sblock.fs_magic != FS_MAGIC) &&
3107c478bd9Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
3117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: not a ufs file system\n", file);
3127c478bd9Sstevel@tonic-gate 		nerror++;
3137c478bd9Sstevel@tonic-gate 		return;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3166451fdbcSvsakar 	if (sblock.fs_magic == FS_MAGIC &&
3176451fdbcSvsakar 	    (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
3186451fdbcSvsakar 	    sblock.fs_version != UFS_VERSION_MIN)) {
3196451fdbcSvsakar 		(void) fprintf(stderr, "%s: unrecognized version of UFS: %d\n",
3206451fdbcSvsakar 		    file, sblock.fs_version);
3216451fdbcSvsakar 		nerror++;
3226451fdbcSvsakar 		return;
3236451fdbcSvsakar 	}
3246451fdbcSvsakar 
3257c478bd9Sstevel@tonic-gate 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
3267c478bd9Sstevel@tonic-gate 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
3277c478bd9Sstevel@tonic-gate 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
3287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unrecognized version of UFS: %d\n",
3297c478bd9Sstevel@tonic-gate 		    file, sblock.fs_version);
3307c478bd9Sstevel@tonic-gate 		nerror++;
3317c478bd9Sstevel@tonic-gate 		return;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
3357c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
3367c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(file)) {
3377c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
3387c478bd9Sstevel@tonic-gate 			/*
3397c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
3407c478bd9Sstevel@tonic-gate 			 * changed it.
3417c478bd9Sstevel@tonic-gate 			 */
3427c478bd9Sstevel@tonic-gate 			bread(SBLOCK, (char *)&sblock, SBSIZE);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
3457c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s\n",
3467c478bd9Sstevel@tonic-gate 				file, strerror(errno));
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 		default:
3497c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.\n ",
3507c478bd9Sstevel@tonic-gate 				file);
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	itab = (struct dinode *)calloc(sblock.fs_ipg, sizeof (struct dinode));
3577c478bd9Sstevel@tonic-gate 	imax = sblock.fs_ncg * sblock.fs_ipg;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	hsize = sblock.fs_ipg * sblock.fs_ncg - sblock.fs_cstotal.cs_nifree + 1;
3607c478bd9Sstevel@tonic-gate 	htab = (struct htab *)calloc(hsize, sizeof (struct htab));
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (!extend_strngtab(AVG_PATH_LEN * hsize)) {
3637c478bd9Sstevel@tonic-gate 		(void) printf("not enough memory to allocate tables\n");
3647c478bd9Sstevel@tonic-gate 		nerror++;
3657c478bd9Sstevel@tonic-gate 		return;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	strngloc = 0;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if ((itab == NULL) || (htab == NULL)) {
3707c478bd9Sstevel@tonic-gate 		(void) printf("not enough memory to allocate tables\n");
3717c478bd9Sstevel@tonic-gate 		nerror++;
3727c478bd9Sstevel@tonic-gate 		return;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	ino = 0;
3757c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3767c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3777c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3787c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3797c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3807c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3817c478bd9Sstevel@tonic-gate 				if (itab[j].di_suid != (o_uid_t)UID_LONG)
3827c478bd9Sstevel@tonic-gate 				itab[j].di_uid = (unsigned int)itab[j].di_suid;
3837c478bd9Sstevel@tonic-gate 				if (itab[j].di_sgid != GID_LONG)
3847c478bd9Sstevel@tonic-gate 				itab[j].di_gid = (unsigned int)itab[j].di_sgid;
3857c478bd9Sstevel@tonic-gate 				pass1(&itab[j]);
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 			ino++;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	ilist[nxfile+1].ino = 0;
3917c478bd9Sstevel@tonic-gate 	ino = 0;
3927c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3937c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3947c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3957c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3967c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3977c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3987c478bd9Sstevel@tonic-gate 				pass2(&itab[j]);
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 			ino++;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	ino = 0;
4047c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
4057c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
4067c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
4077c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
4087c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
4097c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
4107c478bd9Sstevel@tonic-gate 				pass3(&itab[j]);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 			ino++;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	(void) close(fi);
4167c478bd9Sstevel@tonic-gate 	for (i = iflg; i < NB; i++)
4177c478bd9Sstevel@tonic-gate 		ilist[i].ino = 0;
4187c478bd9Sstevel@tonic-gate 	nxfile = iflg;
4197c478bd9Sstevel@tonic-gate 	free(itab);
4207c478bd9Sstevel@tonic-gate 	free(htab);
4217c478bd9Sstevel@tonic-gate 	free(strngtab);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate void
pass1(struct dinode * ip)4257c478bd9Sstevel@tonic-gate pass1(struct dinode *ip)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	int i;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (mflg)
4307c478bd9Sstevel@tonic-gate 		for (i = 0; i < iflg; i++)
4317c478bd9Sstevel@tonic-gate 			if (ino == ilist[i].ino) {
4327c478bd9Sstevel@tonic-gate 				ilist[i].mode = ip->di_mode;
4337c478bd9Sstevel@tonic-gate 				ilist[i].uid = ip->di_uid;
4347c478bd9Sstevel@tonic-gate 				ilist[i].gid = ip->di_gid;
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) != IFDIR) {
4377c478bd9Sstevel@tonic-gate 		if (sflg == 0 || nxfile >= NB)
4387c478bd9Sstevel@tonic-gate 			return;
4397c478bd9Sstevel@tonic-gate 		if ((ip->di_mode&IFMT) == IFBLK ||
4407c478bd9Sstevel@tonic-gate 		    (ip->di_mode&IFMT) == IFCHR || ip->di_mode&(ISUID|ISGID)) {
4417c478bd9Sstevel@tonic-gate 			ilist[nxfile].ino = ino;
4427c478bd9Sstevel@tonic-gate 			ilist[nxfile].mode = ip->di_mode;
4437c478bd9Sstevel@tonic-gate 			ilist[nxfile].uid = ip->di_uid;
4447c478bd9Sstevel@tonic-gate 			ilist[nxfile++].gid = ip->di_gid;
4457c478bd9Sstevel@tonic-gate 			return;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	(void) lookup(ino, 1);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate void
pass2(struct dinode * ip)4527c478bd9Sstevel@tonic-gate pass2(struct dinode *ip)
4537c478bd9Sstevel@tonic-gate {
454*d1a180b0Smaheshvs 	struct direct *dp;
4557c478bd9Sstevel@tonic-gate 	struct dirstuff dirp;
4567c478bd9Sstevel@tonic-gate 	struct htab *hp;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4597c478bd9Sstevel@tonic-gate 		return;
4607c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
4617c478bd9Sstevel@tonic-gate 	dirp.ip = ip;
4627c478bd9Sstevel@tonic-gate 	gip = ip;
4637c478bd9Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
4647c478bd9Sstevel@tonic-gate 		int nmlen;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
4677c478bd9Sstevel@tonic-gate 			continue;
4687c478bd9Sstevel@tonic-gate 		hp = lookup(dp->d_ino, 0);
4697c478bd9Sstevel@tonic-gate 		if (hp == 0)
4707c478bd9Sstevel@tonic-gate 			continue;
4717c478bd9Sstevel@tonic-gate 		if (dotname(dp))
4727c478bd9Sstevel@tonic-gate 			continue;
4737c478bd9Sstevel@tonic-gate 		hp->h_pino = ino;
4747c478bd9Sstevel@tonic-gate 		nmlen = strlen(dp->d_name);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		if (strngloc + nmlen + 1 > MAX_STRNGTAB_INDEX()) {
4777c478bd9Sstevel@tonic-gate 			if (!extend_strngtab(STRNGTAB_INCR)) {
4787c478bd9Sstevel@tonic-gate 				perror("ncheck: can't grow string table\n");
4797c478bd9Sstevel@tonic-gate 				exit(32);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		hp->h_name_index = strngloc;
4847c478bd9Sstevel@tonic-gate 		(void) strcpy(&strngtab[strngloc], dp->d_name);
4857c478bd9Sstevel@tonic-gate 		strngloc += nmlen + 1;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate void
pass3(struct dinode * ip)4907c478bd9Sstevel@tonic-gate pass3(struct dinode *ip)
4917c478bd9Sstevel@tonic-gate {
492*d1a180b0Smaheshvs 	struct direct *dp;
4937c478bd9Sstevel@tonic-gate 	struct dirstuff dirp;
4947c478bd9Sstevel@tonic-gate 	struct dinode   *dip;
4957c478bd9Sstevel@tonic-gate 	int k;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4987c478bd9Sstevel@tonic-gate 		return;
4997c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
5007c478bd9Sstevel@tonic-gate 	dirp.ip = ip;
5017c478bd9Sstevel@tonic-gate 	gip = ip;
5027c478bd9Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
5037c478bd9Sstevel@tonic-gate 		if (aflg == 0 && dotname(dp))
5047c478bd9Sstevel@tonic-gate 			continue;
5057c478bd9Sstevel@tonic-gate 		if (sflg == 0 && iflg == 0)
5067c478bd9Sstevel@tonic-gate 			goto pr;
5077c478bd9Sstevel@tonic-gate 		for (k = 0; ilist[k].ino != 0; k++)
5087c478bd9Sstevel@tonic-gate 			if (ilist[k].ino == dp->d_ino)
5097c478bd9Sstevel@tonic-gate 				break;
5107c478bd9Sstevel@tonic-gate 		if (ilist[k].ino == 0)
5117c478bd9Sstevel@tonic-gate 			continue;
5127c478bd9Sstevel@tonic-gate 		if (mflg)
5137c478bd9Sstevel@tonic-gate 			(void) printf("mode %-6o uid %-5ld gid %-5ld ino ",
5147c478bd9Sstevel@tonic-gate 			    ilist[k].mode, ilist[k].uid, ilist[k].gid);
5157c478bd9Sstevel@tonic-gate 	pr:
5167c478bd9Sstevel@tonic-gate 		if (Sflg || uflg || Aflg || Mflg || Cflg || Nflg || Lflg)
5177c478bd9Sstevel@tonic-gate 			dip = ginode(dp->d_ino);
5187c478bd9Sstevel@tonic-gate 		if ((!Aflg ||
5197c478bd9Sstevel@tonic-gate 		cmp((Today - dip->di_un.di_icom.ic_atime)/DAY, Adelay,
5207c478bd9Sstevel@tonic-gate 		    Asign)) &&
5217c478bd9Sstevel@tonic-gate 		    (!Mflg || cmp((Today - dip->di_un.di_icom.ic_mtime)/DAY,
5227c478bd9Sstevel@tonic-gate 			Mdelay, Msign)) &&
5237c478bd9Sstevel@tonic-gate 		    (!Cflg || cmp((Today - dip->di_un.di_icom.ic_mtime)/DAY,
5247c478bd9Sstevel@tonic-gate 			Cdelay, Csign)) &&
5257c478bd9Sstevel@tonic-gate 		    (!Nflg || cmp(dip->di_un.di_icom.ic_mtime, Nage, '+'))) {
5267c478bd9Sstevel@tonic-gate 			if (Iflg == 0)
5277c478bd9Sstevel@tonic-gate 				(void) printf("%-5u\t", dp->d_ino);
5287c478bd9Sstevel@tonic-gate 			pname(stdout, ino, 0);
5297c478bd9Sstevel@tonic-gate 			(void) printf("/%s", dp->d_name);
5307c478bd9Sstevel@tonic-gate 			if (lookup(dp->d_ino, 0))
5317c478bd9Sstevel@tonic-gate 				(void) printf("/.");
5327c478bd9Sstevel@tonic-gate 			if (Sflg)
5337c478bd9Sstevel@tonic-gate 				(void) printf("\t%6lld",
5347c478bd9Sstevel@tonic-gate 				    dip->di_un.di_icom.ic_lsize);
5357c478bd9Sstevel@tonic-gate 			if (uflg)
5367c478bd9Sstevel@tonic-gate 				(void) printf("\t%s",
5377c478bd9Sstevel@tonic-gate 				    user_name(dip->di_un.di_icom.ic_uid));
5387c478bd9Sstevel@tonic-gate 			(void) printf("\n");
5397c478bd9Sstevel@tonic-gate 			if (Lflg && (dip->di_un.di_icom.ic_nlink > 1)) {
5407c478bd9Sstevel@tonic-gate 				(void) fprintf(Lfile, "%-5u\t",
5417c478bd9Sstevel@tonic-gate 					dp->d_ino);
5427c478bd9Sstevel@tonic-gate 				(void) fprintf(Lfile, "%-5u\t",
5437c478bd9Sstevel@tonic-gate 					dip->di_un.di_icom.ic_nlink);
5447c478bd9Sstevel@tonic-gate 				pname(Lfile, ino, 0);
5457c478bd9Sstevel@tonic-gate 				(void) fprintf(Lfile, "/%s\n", dp->d_name);
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * get next entry in a directory.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate struct direct *
dreaddir(struct dirstuff * dirp)5577c478bd9Sstevel@tonic-gate dreaddir(struct dirstuff *dirp)
5587c478bd9Sstevel@tonic-gate {
559*d1a180b0Smaheshvs 	struct direct *dp;
5607c478bd9Sstevel@tonic-gate 	diskaddr_t lbn, d;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	for (;;) {
5637c478bd9Sstevel@tonic-gate 		if (dirp->loc >= (int)dirp->ip->di_size)
5647c478bd9Sstevel@tonic-gate 			return (NULL);
5657c478bd9Sstevel@tonic-gate 		if (blkoff(&sblock, dirp->loc) == 0) {
5667c478bd9Sstevel@tonic-gate 			lbn = lblkno(&sblock, dirp->loc);
5677c478bd9Sstevel@tonic-gate 			d = bmap(lbn);
5687c478bd9Sstevel@tonic-gate 			if (d == 0)
5697c478bd9Sstevel@tonic-gate 				return (NULL);
5707c478bd9Sstevel@tonic-gate 			bread(fsbtodb(&sblock, d), dirp->dbuf,
5717c478bd9Sstevel@tonic-gate 			    (int)dblksize(&sblock, dirp->ip, (int)lbn));
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		dp = (struct direct *)
5747c478bd9Sstevel@tonic-gate 		    (dirp->dbuf + blkoff(&sblock, dirp->loc));
5757c478bd9Sstevel@tonic-gate 		dirp->loc += dp->d_reclen;
5767c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
5777c478bd9Sstevel@tonic-gate 			continue;
5787c478bd9Sstevel@tonic-gate 		return (dp);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate int
dotname(struct direct * dp)5837c478bd9Sstevel@tonic-gate dotname(struct direct *dp)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (dp->d_name[0] == '.')
5877c478bd9Sstevel@tonic-gate 		if (dp->d_name[1] == 0 ||
5887c478bd9Sstevel@tonic-gate 		    (dp->d_name[1] == '.' && dp->d_name[2] == 0))
5897c478bd9Sstevel@tonic-gate 			return (1);
5907c478bd9Sstevel@tonic-gate 	return (0);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate void
pname(FILE * stream,ino_t i,int lev)5947c478bd9Sstevel@tonic-gate pname(FILE *stream, ino_t i, int lev)
5957c478bd9Sstevel@tonic-gate {
596*d1a180b0Smaheshvs 	struct htab *hp;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (i == UFSROOTINO)
5997c478bd9Sstevel@tonic-gate 		return;
6007c478bd9Sstevel@tonic-gate 	if ((hp = lookup(i, 0)) == 0) {
6017c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "???");
6027c478bd9Sstevel@tonic-gate 		return;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	if (lev > 10) {
6057c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "...");
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	pname(stream, hp->h_pino, ++lev);
6097c478bd9Sstevel@tonic-gate 	if (pflg)
6107c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "%s/%s", prefix,
6117c478bd9Sstevel@tonic-gate 			&(strngtab[hp->h_name_index]));
6127c478bd9Sstevel@tonic-gate 	else
6137c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "/%s",
6147c478bd9Sstevel@tonic-gate 			&(strngtab[hp->h_name_index]));
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate struct htab *
lookup(ino_t i,int ef)6187c478bd9Sstevel@tonic-gate lookup(ino_t i, int ef)
6197c478bd9Sstevel@tonic-gate {
620*d1a180b0Smaheshvs 	struct htab *hp;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	for (hp = &htab[(int)i%hsize]; hp->h_ino; ) {
6237c478bd9Sstevel@tonic-gate 		if (hp->h_ino == i)
6247c478bd9Sstevel@tonic-gate 			return (hp);
6257c478bd9Sstevel@tonic-gate 		if (++hp >= &htab[hsize])
6267c478bd9Sstevel@tonic-gate 			hp = htab;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	if (ef == 0)
6297c478bd9Sstevel@tonic-gate 		return (0);
6307c478bd9Sstevel@tonic-gate 	if (++nhent >= hsize) {
6317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6327c478bd9Sstevel@tonic-gate 		    "ff: hsize of %ld is too small\n", hsize);
6337c478bd9Sstevel@tonic-gate 		exit(32);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	hp->h_ino = i;
6367c478bd9Sstevel@tonic-gate 	return (hp);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate void
bread(diskaddr_t bno,char * buf,int cnt)6407c478bd9Sstevel@tonic-gate bread(diskaddr_t bno, char *buf, int cnt)
6417c478bd9Sstevel@tonic-gate {
642*d1a180b0Smaheshvs 	int i;
6437c478bd9Sstevel@tonic-gate 	int got;
6447c478bd9Sstevel@tonic-gate 	offset_t offset;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	offset = (offset_t)bno * DEV_BSIZE;
6477c478bd9Sstevel@tonic-gate 	if (llseek(fi, offset, 0) == (offset_t)-1) {
6487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6497c478bd9Sstevel@tonic-gate 		    "ff: llseek error %lx %lx\n",
6507c478bd9Sstevel@tonic-gate 		    ((long *)&offset)[0], ((long *)&offset)[1]);
6517c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i++)
6527c478bd9Sstevel@tonic-gate 			buf[i] = 0;
6537c478bd9Sstevel@tonic-gate 		return;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	got = read((int)fi, buf, cnt);
6577c478bd9Sstevel@tonic-gate 	if (got != cnt) {
6587c478bd9Sstevel@tonic-gate 		perror("read");
6597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6607c478bd9Sstevel@tonic-gate 			"ff: (wanted %d got %d blk %lld)\n", cnt, got, bno);
6617c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i++)
6627c478bd9Sstevel@tonic-gate 			buf[i] = 0;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate diskaddr_t
bmap(diskaddr_t i)6677c478bd9Sstevel@tonic-gate bmap(diskaddr_t i)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	daddr32_t ibuf[MAXNINDIR];
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (i < NDADDR)
6727c478bd9Sstevel@tonic-gate 		return ((diskaddr_t)gip->di_db[i]);
6737c478bd9Sstevel@tonic-gate 	i -= NDADDR;
6747c478bd9Sstevel@tonic-gate 	if (i > NINDIR(&sblock)) {
6757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ff    : %lu - huge directory\n", ino);
6767c478bd9Sstevel@tonic-gate 		return ((diskaddr_t)0);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	bread(fsbtodb(&sblock, gip->di_ib[0]), (char *)ibuf, sizeof (ibuf));
6797c478bd9Sstevel@tonic-gate 	return ((diskaddr_t)ibuf[i]);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate struct dinode *
ginode(ino_t inumber)6837c478bd9Sstevel@tonic-gate ginode(ino_t inumber)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	diskaddr_t		iblk;
6867c478bd9Sstevel@tonic-gate 	diskaddr_t		dblk;
6877c478bd9Sstevel@tonic-gate 	int		ioff;
6887c478bd9Sstevel@tonic-gate 	static diskaddr_t	curr_dblk;
6897c478bd9Sstevel@tonic-gate 	static char	buf[MIN_PHYS_READ];
6907c478bd9Sstevel@tonic-gate 	struct dinode	*ibuf;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (inumber < UFSROOTINO || (int)inumber > imax) {
6937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6947c478bd9Sstevel@tonic-gate 		    "bad inode number %ld to ginode\n", inumber);
6957c478bd9Sstevel@tonic-gate 		exit(32);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	iblk = itod(&sblock, (int)inumber);
6987c478bd9Sstevel@tonic-gate 	dblk = fsbtodb(&sblock, iblk);
6997c478bd9Sstevel@tonic-gate 	ioff = itoo(&sblock, (int)inumber);
7007c478bd9Sstevel@tonic-gate 	if (dblk != curr_dblk) {
7017c478bd9Sstevel@tonic-gate 		bread(dblk, &buf[0], sizeof (buf));
7027c478bd9Sstevel@tonic-gate 		curr_dblk = dblk;
7037c478bd9Sstevel@tonic-gate 		inode_reads++;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 	ibuf = (struct dinode *)&buf[0];
7067c478bd9Sstevel@tonic-gate 	ibuf += ioff;
7077c478bd9Sstevel@tonic-gate 	return (ibuf);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate #define	HASHNAMESIZE 16
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate struct name_ent {
7137c478bd9Sstevel@tonic-gate 	struct name_ent	*name_nxt;
7147c478bd9Sstevel@tonic-gate 	int		name_uid;
7157c478bd9Sstevel@tonic-gate 	char		*name_string;
7167c478bd9Sstevel@tonic-gate };
7177c478bd9Sstevel@tonic-gate struct name_ent *hashtable[HASHNAMESIZE];
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate char *
user_name(int uid)7207c478bd9Sstevel@tonic-gate user_name(int uid)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	int		h_index;
7237c478bd9Sstevel@tonic-gate 	struct name_ent	*hp;
7247c478bd9Sstevel@tonic-gate 	struct passwd	*pwent;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	h_index = uid % HASHNAMESIZE;
7277c478bd9Sstevel@tonic-gate 	for (hp = hashtable[h_index]; hp != NULL; hp = hp->name_nxt) {
7287c478bd9Sstevel@tonic-gate 		if (hp->name_uid == uid) {
7297c478bd9Sstevel@tonic-gate 			return (hp->name_string);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	hp = (struct name_ent *)calloc(1, sizeof (struct name_ent));
7337c478bd9Sstevel@tonic-gate 	hp->name_nxt = hashtable[h_index];
7347c478bd9Sstevel@tonic-gate 	hp->name_uid = uid;
7357c478bd9Sstevel@tonic-gate 	hashtable[h_index] = hp;
7367c478bd9Sstevel@tonic-gate 	if ((pwent = getpwuid(uid)) == NULL) {
7377c478bd9Sstevel@tonic-gate 		hp->name_string = "unknown";
7387c478bd9Sstevel@tonic-gate 	} else {
7397c478bd9Sstevel@tonic-gate 		hp->name_string = (char *)strdup(pwent->pw_name);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	passwd_lookups++;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	return (hp->name_string);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate int
cmp(int a,int b,int s)7477c478bd9Sstevel@tonic-gate cmp(int a, int b, int s)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	if (s == '+')
7507c478bd9Sstevel@tonic-gate 		return (a > b);
7517c478bd9Sstevel@tonic-gate 	if (s == '-')
7527c478bd9Sstevel@tonic-gate 		return (a < -(b));
7537c478bd9Sstevel@tonic-gate 	return (a == b);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * We can't do this one by reading the disk directly, since there
7587c478bd9Sstevel@tonic-gate  * is no guarantee that the file is even on a local disk.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate time_t
mod_time(char * file)7617c478bd9Sstevel@tonic-gate mod_time(char *file)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	struct stat64	stat_buf;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (stat64(file, &stat_buf) < 0) {
7667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ff: can't stat '%s' - ignored\n", file);
7677c478bd9Sstevel@tonic-gate 		return (0);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	return (stat_buf.st_mtime);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate void
out_multilinks()7737c478bd9Sstevel@tonic-gate out_multilinks()
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	int	length;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if ((length = fseek(Lfile, 0L, 2)) < 0) {
7787c478bd9Sstevel@tonic-gate 		perror("fseek");
7797c478bd9Sstevel@tonic-gate 		exit(32);
7807c478bd9Sstevel@tonic-gate 	} else
7817c478bd9Sstevel@tonic-gate 		if ((length = ftell(Lfile)) > 0) {
7827c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
7837c478bd9Sstevel@tonic-gate 			    "\nmultilink files\nIno\tLinks\tPathname\n\n");
7847c478bd9Sstevel@tonic-gate 			rewind(Lfile);
7857c478bd9Sstevel@tonic-gate 			while (length-- > 0)
7867c478bd9Sstevel@tonic-gate 				(void) putc(getc(Lfile), stdout);
7877c478bd9Sstevel@tonic-gate 		} else
7887c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "No multilink files\n");
7897c478bd9Sstevel@tonic-gate 	(void) fclose(Lfile);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate void
usage()7937c478bd9Sstevel@tonic-gate usage()
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
7967c478bd9Sstevel@tonic-gate 	    "ufs usage: ff [-F ufs] [generic options] [-o a,m,s] special\n");
7977c478bd9Sstevel@tonic-gate 	exit(32);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * Extend or create the string table.
8027c478bd9Sstevel@tonic-gate  * Preserves contents.
8037c478bd9Sstevel@tonic-gate  * Return non-zero for success.
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate int
extend_strngtab(unsigned int size)8067c478bd9Sstevel@tonic-gate extend_strngtab(unsigned int size)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	strngtab_size += size;
8097c478bd9Sstevel@tonic-gate 	strngtab = (char *)realloc(strngtab, strngtab_size);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	return ((int)strngtab);
8127c478bd9Sstevel@tonic-gate }
813