xref: /illumos-gate/usr/src/cmd/fs.d/ufs/ncheck/ncheck.c (revision 6451fdbca2f79129a3a09d2fe3f6dd4d062bebff)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*6451fdbcSvsakar  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * ncheck -- obtain file names from reading filesystem
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	MAXNINDIR	(MAXBSIZE / sizeof (daddr_t))
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
517c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
527c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
537c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <stdlib.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <fcntl.h>
597c478bd9Sstevel@tonic-gate #include <unistd.h>
607c478bd9Sstevel@tonic-gate #include "roll_log.h"
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;
697c478bd9Sstevel@tonic-gate unsigned itab_size;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct 	dinode	*gip;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* inode list */
757c478bd9Sstevel@tonic-gate struct ilist {
767c478bd9Sstevel@tonic-gate 	ino_t	ino;
777c478bd9Sstevel@tonic-gate 	ushort_t	mode;
787c478bd9Sstevel@tonic-gate 	uid_t	uid;
797c478bd9Sstevel@tonic-gate 	gid_t	gid;
807c478bd9Sstevel@tonic-gate } *ilist;
817c478bd9Sstevel@tonic-gate int ilist_size = 0;	/* size of ilist[] */
827c478bd9Sstevel@tonic-gate int ilist_index = 0;	/* current index for storing into ilist; */
837c478bd9Sstevel@tonic-gate #define	ILIST_SZ_INCR	1000	/* initial size, amount to incr sz of ilist */
847c478bd9Sstevel@tonic-gate #define	MAX_ILIST_INDEX()	(ilist_size - 1)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct	htab
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	ino_t	h_ino;
897c478bd9Sstevel@tonic-gate 	ino_t	h_pino;
907c478bd9Sstevel@tonic-gate 	int	h_name_index;		/* index into string table */
917c478bd9Sstevel@tonic-gate } *htab;
927c478bd9Sstevel@tonic-gate unsigned htab_size;		/* how much malloc'd for htab */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * string table: used to hold filenames.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate char *strngtab;
987c478bd9Sstevel@tonic-gate int strngloc;
997c478bd9Sstevel@tonic-gate int strngtab_size;
1007c478bd9Sstevel@tonic-gate #define	STRNGTAB_INCR	(1024*16)	/* amount to grow strngtab */
1017c478bd9Sstevel@tonic-gate #define	MAX_STRNGTAB_INDEX()	(strngtab_size - 1)
1027c478bd9Sstevel@tonic-gate #define	AVG_PATH_LEN	30		/* average (?) length of name */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate long hsize;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct dirstuff {
1077c478bd9Sstevel@tonic-gate 	int loc;
1087c478bd9Sstevel@tonic-gate 	struct dinode *ip;
1097c478bd9Sstevel@tonic-gate 	char dbuf[MAXBSIZE];
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int	aflg = 0;
1147c478bd9Sstevel@tonic-gate int	sflg = 0;
1157c478bd9Sstevel@tonic-gate int	iflg = 0; /* number of inodes being searched for */
1167c478bd9Sstevel@tonic-gate int	mflg = 0;
1177c478bd9Sstevel@tonic-gate int	fi;
1187c478bd9Sstevel@tonic-gate ino_t	ino;
1197c478bd9Sstevel@tonic-gate int	nhent;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int	nerror;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate long	atol();
1247c478bd9Sstevel@tonic-gate daddr_t	bmap();
1257c478bd9Sstevel@tonic-gate void	bread(diskaddr_t bno, char *buf, int cnt);
1267c478bd9Sstevel@tonic-gate void	check(char *file);
1277c478bd9Sstevel@tonic-gate int	dotname(struct direct *dp);
1287c478bd9Sstevel@tonic-gate offset_t llseek();
1297c478bd9Sstevel@tonic-gate struct htab *lookup(ino_t i, int ef);
1307c478bd9Sstevel@tonic-gate void	pass1(struct dinode *ip);
1317c478bd9Sstevel@tonic-gate void	pass2(struct dinode *ip);
1327c478bd9Sstevel@tonic-gate void	pass3(struct dinode *ip);
1337c478bd9Sstevel@tonic-gate void	pname(ino_t i, int lev);
1347c478bd9Sstevel@tonic-gate char 	*strcpy();
1357c478bd9Sstevel@tonic-gate void	usage();
1367c478bd9Sstevel@tonic-gate struct direct *dreaddir();
1377c478bd9Sstevel@tonic-gate void extend_ilist();
1387c478bd9Sstevel@tonic-gate int extend_strngtab(unsigned int size);
1397c478bd9Sstevel@tonic-gate uchar_t *extend_tbl(uchar_t *tbl, unsigned int *current_size,
1407c478bd9Sstevel@tonic-gate 	unsigned int new_size);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate extern int	optind;
1437c478bd9Sstevel@tonic-gate extern char	*optarg;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate char *subopts [] = {
1467c478bd9Sstevel@tonic-gate #define	M_FLAG		0
1477c478bd9Sstevel@tonic-gate 	"m",
1487c478bd9Sstevel@tonic-gate 	NULL
1497c478bd9Sstevel@tonic-gate 	};
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate main(argc, argv)
1527c478bd9Sstevel@tonic-gate 	int argc;
1537c478bd9Sstevel@tonic-gate 	char *argv[];
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	long n;
1567c478bd9Sstevel@tonic-gate 	int	opt;
1577c478bd9Sstevel@tonic-gate 	char	*suboptions,	*value;
1587c478bd9Sstevel@tonic-gate 	int	suboption;
1597c478bd9Sstevel@tonic-gate 	char	*p;
1607c478bd9Sstevel@tonic-gate 	int	first = 0;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	extend_ilist();
1637c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "ao:i:s")) != EOF) {
1647c478bd9Sstevel@tonic-gate 		switch (opt) {
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		case 'a':
1677c478bd9Sstevel@tonic-gate 			aflg++;
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		case 'o':
1717c478bd9Sstevel@tonic-gate 			/*
1727c478bd9Sstevel@tonic-gate 			 * ufs specific options.
1737c478bd9Sstevel@tonic-gate 			 */
1747c478bd9Sstevel@tonic-gate 			suboptions = optarg;
1757c478bd9Sstevel@tonic-gate 			while (*suboptions != '\0') {
1767c478bd9Sstevel@tonic-gate 				suboption = getsubopt(&suboptions,
1777c478bd9Sstevel@tonic-gate 					subopts, &value);
1787c478bd9Sstevel@tonic-gate 				switch (suboption) {
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 				case M_FLAG:
1817c478bd9Sstevel@tonic-gate 					mflg++;
1827c478bd9Sstevel@tonic-gate 					break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 				default:
1857c478bd9Sstevel@tonic-gate 					usage();
1867c478bd9Sstevel@tonic-gate 				}
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		case 'i':
1917c478bd9Sstevel@tonic-gate 			while ((p = (char *)strtok((first++ == 0 ? optarg : 0),
1927c478bd9Sstevel@tonic-gate 						    ", ")) != NULL) {
1937c478bd9Sstevel@tonic-gate 				if ((n = atoi(p)) == 0)
1947c478bd9Sstevel@tonic-gate 					break;
1957c478bd9Sstevel@tonic-gate 				ilist[iflg].ino = n;
1967c478bd9Sstevel@tonic-gate 				iflg++;
1977c478bd9Sstevel@tonic-gate 				ilist_index = iflg;
1987c478bd9Sstevel@tonic-gate 				if (iflg > MAX_ILIST_INDEX())
1997c478bd9Sstevel@tonic-gate 					extend_ilist();
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		case 's':
2047c478bd9Sstevel@tonic-gate 			sflg++;
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate #if 0
2077c478bd9Sstevel@tonic-gate 		case 'V':
2087c478bd9Sstevel@tonic-gate 			{
2097c478bd9Sstevel@tonic-gate 				int	opt_count;
2107c478bd9Sstevel@tonic-gate 				char	*opt_text;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "ncheck -F ufs ");
2137c478bd9Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
2147c478bd9Sstevel@tonic-gate 						opt_count++) {
2157c478bd9Sstevel@tonic-gate 					opt_text = argv[opt_count];
2167c478bd9Sstevel@tonic-gate 					if (opt_text)
2177c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout, " %s ",
2187c478bd9Sstevel@tonic-gate 							opt_text);
2197c478bd9Sstevel@tonic-gate 				}
2207c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate #endif
2247c478bd9Sstevel@tonic-gate 		case '?':
2257c478bd9Sstevel@tonic-gate 			usage();
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	argc -= optind;
2297c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
2307c478bd9Sstevel@tonic-gate 	while (argc--) {
2317c478bd9Sstevel@tonic-gate 		check(*argv);
2327c478bd9Sstevel@tonic-gate 		argv++;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	return (nerror);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate void
2387c478bd9Sstevel@tonic-gate check(file)
2397c478bd9Sstevel@tonic-gate 	char *file;
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	register int i, j, c;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	fi = open64(file, 0);
2447c478bd9Sstevel@tonic-gate 	if (fi < 0) {
2457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: cannot open %s\n", file);
2467c478bd9Sstevel@tonic-gate 		nerror++;
2477c478bd9Sstevel@tonic-gate 		return;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	nhent = 0;
2507c478bd9Sstevel@tonic-gate 	(void) printf("%s:\n", file);
2517c478bd9Sstevel@tonic-gate 	sync();
2527c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2537c478bd9Sstevel@tonic-gate 	if ((sblock.fs_magic != FS_MAGIC) &&
2547c478bd9Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
2557c478bd9Sstevel@tonic-gate 		(void) printf("%s: not a ufs file system\n", file);
2567c478bd9Sstevel@tonic-gate 		nerror++;
2577c478bd9Sstevel@tonic-gate 		return;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
260*6451fdbcSvsakar 	if ((sblock.fs_magic == FS_MAGIC) &&
261*6451fdbcSvsakar 	    ((sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2) &&
262*6451fdbcSvsakar 	    (sblock.fs_version != UFS_VERSION_MIN))) {
263*6451fdbcSvsakar 		(void) printf("%s: unrecognized ufs version number %d\n",
264*6451fdbcSvsakar 		    file, sblock.fs_version);
265*6451fdbcSvsakar 		nerror++;
266*6451fdbcSvsakar 		return;
267*6451fdbcSvsakar 	}
268*6451fdbcSvsakar 
2697c478bd9Sstevel@tonic-gate 	if ((sblock.fs_magic == MTB_UFS_MAGIC) &&
2707c478bd9Sstevel@tonic-gate 	    ((sblock.fs_version > MTB_UFS_VERSION_1) ||
2717c478bd9Sstevel@tonic-gate 	    (sblock.fs_version < MTB_UFS_VERSION_MIN))) {
2727c478bd9Sstevel@tonic-gate 		(void) printf("%s: unrecognized ufs version number %d\n",
2737c478bd9Sstevel@tonic-gate 		    file, sblock.fs_version);
2747c478bd9Sstevel@tonic-gate 		nerror++;
2757c478bd9Sstevel@tonic-gate 		return;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
2797c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
2807c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(file)) {
2817c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
2847c478bd9Sstevel@tonic-gate 			 * changed it.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 			bread((diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
2897c478bd9Sstevel@tonic-gate 			(void) printf("Warning: cannot roll log for %s.  %s\n",
2907c478bd9Sstevel@tonic-gate 				file, strerror(errno));
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 		default:
2937c478bd9Sstevel@tonic-gate 			(void) printf("Warning: cannot roll log for %s.\n",
2947c478bd9Sstevel@tonic-gate 				file);
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	itab = (struct dinode *)extend_tbl((uchar_t *)itab, &itab_size,
3007c478bd9Sstevel@tonic-gate 		(unsigned)(sblock.fs_ipg * sizeof (struct dinode)));
3017c478bd9Sstevel@tonic-gate 	if (itab == 0) {
3027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3037c478bd9Sstevel@tonic-gate 			"ncheck: not enough memory for itab table\n");
3047c478bd9Sstevel@tonic-gate 		nerror++;
3057c478bd9Sstevel@tonic-gate 		return;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	hsize = sblock.fs_ipg * sblock.fs_ncg - sblock.fs_cstotal.cs_nifree + 1;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	htab = (struct htab *)extend_tbl((uchar_t *)htab, &htab_size,
3117c478bd9Sstevel@tonic-gate 		(unsigned)(hsize * sizeof (struct htab)));
3127c478bd9Sstevel@tonic-gate 	if (htab == 0) {
3137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3147c478bd9Sstevel@tonic-gate 			"ncheck: not enough memory for htab table\n");
3157c478bd9Sstevel@tonic-gate 		nerror++;
3167c478bd9Sstevel@tonic-gate 		return;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (!extend_strngtab(AVG_PATH_LEN * hsize)) {
3207c478bd9Sstevel@tonic-gate 		(void) printf("not enough memory to allocate tables\n");
3217c478bd9Sstevel@tonic-gate 		nerror++;
3227c478bd9Sstevel@tonic-gate 		return;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	strngloc = 0;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	ino = 0;
3277c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3287c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3297c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3307c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3317c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3327c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3337c478bd9Sstevel@tonic-gate 				if (itab[j].di_suid != UID_LONG)
3347c478bd9Sstevel@tonic-gate 					itab[j].di_uid = itab[j].di_suid;
3357c478bd9Sstevel@tonic-gate 				if (itab[j].di_sgid != GID_LONG)
3367c478bd9Sstevel@tonic-gate 					itab[j].di_gid = itab[j].di_sgid;
3377c478bd9Sstevel@tonic-gate 				pass1(&itab[j]);
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 			ino++;
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	ilist[ilist_index++].ino = 0;
3437c478bd9Sstevel@tonic-gate 	if (ilist_index > MAX_ILIST_INDEX())
3447c478bd9Sstevel@tonic-gate 		extend_ilist();
3457c478bd9Sstevel@tonic-gate 	ino = 0;
3467c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3477c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3487c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3497c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3527c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3537c478bd9Sstevel@tonic-gate 				pass2(&itab[j]);
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 			ino++;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	ino = 0;
3597c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3607c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3617c478bd9Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3627c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3637c478bd9Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3647c478bd9Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3657c478bd9Sstevel@tonic-gate 				pass3(&itab[j]);
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 			ino++;
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	(void) close(fi);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * Clear those elements after inodes specified by "-i" out of
3747c478bd9Sstevel@tonic-gate 	 * ilist.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	for (i = iflg; i < ilist_index; i++) {
3777c478bd9Sstevel@tonic-gate 		ilist[i].ino = 0;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	ilist_index = iflg;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate void
3837c478bd9Sstevel@tonic-gate pass1(ip)
3847c478bd9Sstevel@tonic-gate 	register struct dinode *ip;
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	int i;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (mflg) {
3897c478bd9Sstevel@tonic-gate 		for (i = 0; i < iflg; i++)
3907c478bd9Sstevel@tonic-gate 			if (ino == ilist[i].ino) {
3917c478bd9Sstevel@tonic-gate 				ilist[i].mode = ip->di_mode;
3927c478bd9Sstevel@tonic-gate 				ilist[i].uid = ip->di_uid;
3937c478bd9Sstevel@tonic-gate 				ilist[i].gid = ip->di_gid;
3947c478bd9Sstevel@tonic-gate 			}
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) != IFDIR) {
3977c478bd9Sstevel@tonic-gate 		if (sflg == 0)
3987c478bd9Sstevel@tonic-gate 			return;
3997c478bd9Sstevel@tonic-gate 		if ((ip->di_mode & IFMT) == IFBLK ||
4007c478bd9Sstevel@tonic-gate 				(ip->di_mode & IFMT) == IFCHR ||
4017c478bd9Sstevel@tonic-gate 				ip->di_mode&(ISUID|ISGID)) {
4027c478bd9Sstevel@tonic-gate 			ilist[ilist_index].ino = ino;
4037c478bd9Sstevel@tonic-gate 			ilist[ilist_index].mode = ip->di_mode;
4047c478bd9Sstevel@tonic-gate 			ilist[ilist_index].uid = ip->di_uid;
4057c478bd9Sstevel@tonic-gate 			ilist[ilist_index].gid = ip->di_gid;
4067c478bd9Sstevel@tonic-gate 			if (++ilist_index > MAX_ILIST_INDEX())
4077c478bd9Sstevel@tonic-gate 				extend_ilist();
4087c478bd9Sstevel@tonic-gate 			return;
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	(void) lookup(ino, 1);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate void
4157c478bd9Sstevel@tonic-gate pass2(ip)
4167c478bd9Sstevel@tonic-gate 	register struct dinode *ip;
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	register struct direct *dp;
4197c478bd9Sstevel@tonic-gate 	struct dirstuff dirp;
4207c478bd9Sstevel@tonic-gate 	struct htab *hp;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4247c478bd9Sstevel@tonic-gate 		return;
4257c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
4267c478bd9Sstevel@tonic-gate 	dirp.ip = ip;
4277c478bd9Sstevel@tonic-gate 	gip = ip;
4287c478bd9Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
4297c478bd9Sstevel@tonic-gate 		int nmlen;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
4327c478bd9Sstevel@tonic-gate 			continue;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		hp = lookup(dp->d_ino, 0);
4357c478bd9Sstevel@tonic-gate 		if (hp == 0)
4367c478bd9Sstevel@tonic-gate 			continue;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		if (dotname(dp))
4397c478bd9Sstevel@tonic-gate 			continue;
4407c478bd9Sstevel@tonic-gate 		hp->h_pino = ino;
4417c478bd9Sstevel@tonic-gate 		nmlen = strlen(dp->d_name);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		if (strngloc + nmlen + 1 > MAX_STRNGTAB_INDEX()) {
4447c478bd9Sstevel@tonic-gate 			if (!extend_strngtab(STRNGTAB_INCR)) {
4457c478bd9Sstevel@tonic-gate 				perror("ncheck: can't grow string table\n");
4467c478bd9Sstevel@tonic-gate 				exit(32);
4477c478bd9Sstevel@tonic-gate 			}
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		hp->h_name_index = strngloc;
4517c478bd9Sstevel@tonic-gate 		(void) strcpy(&strngtab[strngloc], dp->d_name);
4527c478bd9Sstevel@tonic-gate 		strngloc += nmlen + 1;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate void
4577c478bd9Sstevel@tonic-gate pass3(ip)
4587c478bd9Sstevel@tonic-gate 	register struct dinode *ip;
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	register struct direct *dp;
4617c478bd9Sstevel@tonic-gate 	struct dirstuff dirp;
4627c478bd9Sstevel@tonic-gate 	int k;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4657c478bd9Sstevel@tonic-gate 		return;
4667c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
4677c478bd9Sstevel@tonic-gate 	dirp.ip = ip;
4687c478bd9Sstevel@tonic-gate 	gip = ip;
4697c478bd9Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
4707c478bd9Sstevel@tonic-gate 		if (aflg == 0 && dotname(dp))
4717c478bd9Sstevel@tonic-gate 			continue;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		if (sflg == 0 && iflg == 0)
4747c478bd9Sstevel@tonic-gate 			goto pr;
4757c478bd9Sstevel@tonic-gate 		for (k = 0; k < ilist_index && ilist[k].ino != 0; k++) {
4767c478bd9Sstevel@tonic-gate 			if (ilist[k].ino == dp->d_ino) {
4777c478bd9Sstevel@tonic-gate 				break;
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 		if (ilist[k].ino == 0)
4817c478bd9Sstevel@tonic-gate 			continue;
4827c478bd9Sstevel@tonic-gate 		if (mflg)
4837c478bd9Sstevel@tonic-gate 			(void) printf("mode %-6o uid %-5ld gid %-5ld ino ",
4847c478bd9Sstevel@tonic-gate 			    ilist[k].mode, ilist[k].uid, ilist[k].gid);
4857c478bd9Sstevel@tonic-gate 	pr:
4867c478bd9Sstevel@tonic-gate 		(void) printf("%-5u\t", dp->d_ino);
4877c478bd9Sstevel@tonic-gate 		pname(ino, 0);
4887c478bd9Sstevel@tonic-gate 		(void) printf("/%s", dp->d_name);
4897c478bd9Sstevel@tonic-gate 		if (lookup(dp->d_ino, 0))
4907c478bd9Sstevel@tonic-gate 			(void) printf("/.");
4917c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * get next entry in a directory.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate struct direct *
4997c478bd9Sstevel@tonic-gate dreaddir(dirp)
5007c478bd9Sstevel@tonic-gate 	register struct dirstuff *dirp;
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	register struct direct *dp;
5037c478bd9Sstevel@tonic-gate 	daddr_t lbn, d;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (;;) {
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		if (dirp->loc >= (int)dirp->ip->di_size)
5087c478bd9Sstevel@tonic-gate 			return (NULL);
5097c478bd9Sstevel@tonic-gate 		if (blkoff(&sblock, dirp->loc) == 0) {
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 			lbn = lblkno(&sblock, dirp->loc);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			d = bmap(lbn);
5147c478bd9Sstevel@tonic-gate 			if (d == 0)
5157c478bd9Sstevel@tonic-gate 				return (NULL);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			bread(fsbtodb(&sblock, d), dirp->dbuf,
5187c478bd9Sstevel@tonic-gate 			    (int)dblksize(&sblock, dirp->ip, (int)lbn));
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		dp = (struct direct *)
5227c478bd9Sstevel@tonic-gate 		    (dirp->dbuf + blkoff(&sblock, dirp->loc));
5237c478bd9Sstevel@tonic-gate 		dirp->loc += dp->d_reclen;
5247c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0) {
5257c478bd9Sstevel@tonic-gate 			continue;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 		return (dp);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate dotname(dp)
5327c478bd9Sstevel@tonic-gate 	register struct direct *dp;
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (dp->d_name[0] == '.') {
5367c478bd9Sstevel@tonic-gate 		if (dp->d_name[1] == 0 ||
5377c478bd9Sstevel@tonic-gate 		    (dp->d_name[1] == '.' && dp->d_name[2] == 0))
5387c478bd9Sstevel@tonic-gate 			return (1);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	return (0);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate void
5447c478bd9Sstevel@tonic-gate pname(i, lev)
5457c478bd9Sstevel@tonic-gate 	ino_t i;
5467c478bd9Sstevel@tonic-gate 	int lev;
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	register struct htab *hp;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (i == UFSROOTINO)
5517c478bd9Sstevel@tonic-gate 		return;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if ((hp = lookup(i, 0)) == 0) {
5547c478bd9Sstevel@tonic-gate 		(void) printf("???");
5557c478bd9Sstevel@tonic-gate 		return;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	if (lev > 10) {
5587c478bd9Sstevel@tonic-gate 		(void) printf("...");
5597c478bd9Sstevel@tonic-gate 		return;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	pname(hp->h_pino, ++lev);
5627c478bd9Sstevel@tonic-gate 	(void) printf("/%s", &(strngtab[hp->h_name_index]));
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate struct htab *
5677c478bd9Sstevel@tonic-gate lookup(i, ef)
5687c478bd9Sstevel@tonic-gate 	ino_t i;
5697c478bd9Sstevel@tonic-gate 	int ef;
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	register struct htab *hp;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	for (hp = &htab[(int)i%hsize]; hp->h_ino; ) {
5747c478bd9Sstevel@tonic-gate 		if (hp->h_ino == i)
5757c478bd9Sstevel@tonic-gate 			return (hp);
5767c478bd9Sstevel@tonic-gate 		if (++hp >= &htab[hsize])
5777c478bd9Sstevel@tonic-gate 			hp = htab;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (ef == 0)
5817c478bd9Sstevel@tonic-gate 		return (0);
5827c478bd9Sstevel@tonic-gate 	if (++nhent >= hsize) {
5837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: hsize of %ld is too small\n",
5847c478bd9Sstevel@tonic-gate 									hsize);
5857c478bd9Sstevel@tonic-gate 		exit(32);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 	hp->h_ino = i;
5887c478bd9Sstevel@tonic-gate 	return (hp);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate void
5927c478bd9Sstevel@tonic-gate bread(bno, buf, cnt)
5937c478bd9Sstevel@tonic-gate 	diskaddr_t bno;
5947c478bd9Sstevel@tonic-gate 	char *buf;
5957c478bd9Sstevel@tonic-gate 	int cnt;
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	register i;
5987c478bd9Sstevel@tonic-gate 	int got;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (llseek(fi, (offset_t)bno * DEV_BSIZE, 0) == -1) {
6017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: lseek error %lld\n",
6027c478bd9Sstevel@tonic-gate 		    (offset_t)bno * DEV_BSIZE);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i++) {
6057c478bd9Sstevel@tonic-gate 			buf[i] = 0;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		return;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	got = read((int)fi, buf, cnt);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (got != cnt) {
6147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6157c478bd9Sstevel@tonic-gate 		    "ncheck: read error at block %lld (wanted %d got %d)\n",
6167c478bd9Sstevel@tonic-gate 		    bno, cnt, got);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i++)
6197c478bd9Sstevel@tonic-gate 			buf[i] = 0;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate daddr_t
6247c478bd9Sstevel@tonic-gate bmap(i)
6257c478bd9Sstevel@tonic-gate 	daddr_t i;
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	daddr_t ibuf[MAXNINDIR];
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (i < NDADDR)
6307c478bd9Sstevel@tonic-gate 		return (gip->di_db[i]);
6317c478bd9Sstevel@tonic-gate 	i -= NDADDR;
6327c478bd9Sstevel@tonic-gate 	if (i > NINDIR(&sblock)) {
6337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: %lu - huge directory\n", ino);
6347c478bd9Sstevel@tonic-gate 		return ((daddr_t)0);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	bread(fsbtodb(&sblock, gip->di_ib[0]), (char *)ibuf, sizeof (ibuf));
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	return (ibuf[i]);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate void
6437c478bd9Sstevel@tonic-gate usage()
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6467c478bd9Sstevel@tonic-gate 		/*CSTYLED*/
6477c478bd9Sstevel@tonic-gate 		"ufs usage: ncheck [-F ufs] [generic options] [-a -i #list -s] [-o m] special\n");
6487c478bd9Sstevel@tonic-gate 	exit(32);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * Extend or create the inode list;
6547c478bd9Sstevel@tonic-gate  * this is used to contains the list of inodes we've been
6557c478bd9Sstevel@tonic-gate  * asked to check using the "-i" flag and to hold the
6567c478bd9Sstevel@tonic-gate  * inode numbers of files which we detect as being
6577c478bd9Sstevel@tonic-gate  * blk|char|setuid|setgid ("-s" flag support).
6587c478bd9Sstevel@tonic-gate  * Preserves contents.
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate void
6617c478bd9Sstevel@tonic-gate extend_ilist()
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	ilist_size += ILIST_SZ_INCR;
6647c478bd9Sstevel@tonic-gate 	ilist = (struct ilist *)realloc(ilist,
6657c478bd9Sstevel@tonic-gate 		(ilist_size * sizeof (struct ilist)));
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (ilist == NULL) {
6687c478bd9Sstevel@tonic-gate 		perror("ncheck: not enough memory to grow ilist\n");
6697c478bd9Sstevel@tonic-gate 		exit(32);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Extend or create the string table.
6757c478bd9Sstevel@tonic-gate  * Preserves contents.
6767c478bd9Sstevel@tonic-gate  * Return non-zero for success.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate int
6797c478bd9Sstevel@tonic-gate extend_strngtab(size)
6807c478bd9Sstevel@tonic-gate 	unsigned int size;
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	strngtab_size += size;
6837c478bd9Sstevel@tonic-gate 	strngtab = (char *)realloc(strngtab, strngtab_size);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return ((int)strngtab);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * Extend or create a table, throwing away previous
6907c478bd9Sstevel@tonic-gate  * contents.
6917c478bd9Sstevel@tonic-gate  * Return null on failure.
6927c478bd9Sstevel@tonic-gate  */
6937c478bd9Sstevel@tonic-gate uchar_t *
6947c478bd9Sstevel@tonic-gate extend_tbl(tbl, current_size, new_size)
6957c478bd9Sstevel@tonic-gate 	uchar_t *tbl;
6967c478bd9Sstevel@tonic-gate 	unsigned int *current_size;	/* current size */
6977c478bd9Sstevel@tonic-gate 	unsigned int new_size;		/* bytes required */
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * if we've already allocated tbl,
7017c478bd9Sstevel@tonic-gate 	 * but it is too small, free it.
7027c478bd9Sstevel@tonic-gate 	 * we don't realloc because we are throwing
7037c478bd9Sstevel@tonic-gate 	 * away its contents.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (tbl && (*current_size < new_size)) {
7077c478bd9Sstevel@tonic-gate 		free(tbl);
7087c478bd9Sstevel@tonic-gate 		tbl = NULL;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (tbl == NULL) {
7127c478bd9Sstevel@tonic-gate 		tbl = (uchar_t *)malloc(new_size);
7137c478bd9Sstevel@tonic-gate 		if (tbl == 0)
7147c478bd9Sstevel@tonic-gate 			return ((uchar_t *)0);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		*current_size = new_size;
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	(void) memset(tbl, 0, new_size);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	return (tbl);
7217c478bd9Sstevel@tonic-gate }
722