changeset 13426:f92414e708c0

81 remove 'is_z_a_power_of_x_le_y' from pcfs utils Reviewed by: Eric Schrock <eric.schrock@delphix.com> Reviewed by: Gordon Ross <gwr@nexenta.com> Approved by: Garrett D'Amore <garrett@nexenta.com>
author Gary Mills <mills@cc.umanitoba.ca>
date Thu, 11 Aug 2011 10:31:52 -0700
parents 8640246a3548
children 2b3b7ee313ef
files usr/src/cmd/fs.d/pcfs/fsck/bpb.c usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.c usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.h usr/src/cmd/fs.d/pcfs/mkfs/mkfs.c
diffstat 4 files changed, 37 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/fs.d/pcfs/fsck/bpb.c	Thu Aug 11 02:33:25 2011 -0700
+++ b/usr/src/cmd/fs.d/pcfs/fsck/bpb.c	Thu Aug 11 10:31:52 2011 -0700
@@ -20,12 +20,12 @@
  * CDDL HEADER END
  */
 /*
+ * Copyright (c) 2011 Gary Mills
+ *
  * Copyright (c) 1999,2000 by Sun Microsystems, Inc.
  * All rights reserved.
  */
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 /*
  * fsck_pcfs -- routines for manipulating the BPB (BIOS parameter block)
  * of the file system.
@@ -66,7 +66,7 @@
 	 * Compute bytes/cluster for later reference
 	 */
 	BytesPerCluster =  TheBIOSParameterBlock.bpb.sectors_per_cluster *
-		TheBIOSParameterBlock.bpb.bytes_per_sector;
+	    TheBIOSParameterBlock.bpb.bytes_per_sector;
 
 	/*
 	 * First we'll find total number of sectors in the file area...
@@ -80,7 +80,7 @@
 	overhead = TheBIOSParameterBlock.bpb.resv_sectors;
 
 	RootDirSize = TheBIOSParameterBlock.bpb.num_root_entries *
-		sizeof (struct pcdir);
+	    sizeof (struct pcdir);
 	overhead += RootDirSize / TheBIOSParameterBlock.bpb.bytes_per_sector;
 
 	if (TheBIOSParameterBlock.bpb.sectors_per_fat) {
@@ -88,13 +88,13 @@
 		 * Good old FAT12 or FAT16
 		 */
 		overhead += TheBIOSParameterBlock.bpb.num_fats *
-			TheBIOSParameterBlock.bpb.sectors_per_fat;
+		    TheBIOSParameterBlock.bpb.sectors_per_fat;
 		/*
 		 * Compute this for later - when we actually pull in a copy
 		 * of the FAT
 		 */
 		FATSize = TheBIOSParameterBlock.bpb.sectors_per_fat *
-			TheBIOSParameterBlock.bpb.bytes_per_sector;
+		    TheBIOSParameterBlock.bpb.bytes_per_sector;
 	} else {
 		/*
 		 *  FAT32
@@ -109,13 +109,13 @@
 		 *  believing what I am told.
 		 */
 		overhead += TheBIOSParameterBlock.bpb.num_fats *
-			TheBIOSParameterBlock.bpb32.big_sectors_per_fat;
+		    TheBIOSParameterBlock.bpb32.big_sectors_per_fat;
 		/*
 		 * Compute this for later - when we actually pull in a copy
 		 * of the FAT
 		 */
 		FATSize = TheBIOSParameterBlock.bpb32.big_sectors_per_fat *
-			TheBIOSParameterBlock.bpb.bytes_per_sector;
+		    TheBIOSParameterBlock.bpb.bytes_per_sector;
 	}
 
 	/*
@@ -124,7 +124,7 @@
 	 */
 	dataSectors -= overhead;
 	TotalClusters = dataSectors /
-		TheBIOSParameterBlock.bpb.sectors_per_cluster;
+	    TheBIOSParameterBlock.bpb.sectors_per_cluster;
 
 	/*
 	 *  Also need to compute last cluster and offset of the first cluster
@@ -153,8 +153,8 @@
 		    "bytes.\n"), TotalClusters,
 		    TheBIOSParameterBlock.bpb.sectors_per_cluster,
 		    (uint64_t)TotalClusters *
-			TheBIOSParameterBlock.bpb.sectors_per_cluster *
-			TheBIOSParameterBlock.bpb.bytes_per_sector);
+		    TheBIOSParameterBlock.bpb.sectors_per_cluster *
+		    TheBIOSParameterBlock.bpb.bytes_per_sector);
 		(void) fprintf(stderr,
 		    gettext("File system overhead of %d sectors.\n"), overhead);
 		(void) fprintf(stderr,
@@ -193,7 +193,7 @@
 	if (ltohs(ubpb.mb.signature) != BOOTSECSIG) {
 		mountSanityCheckFails();
 		(void) fprintf(stderr,
-			gettext("Bad signature on BPB. Giving up.\n"));
+		    gettext("Bad signature on BPB. Giving up.\n"));
 		exit(2);
 	}
 
@@ -201,9 +201,9 @@
 	swap_pack_grabbpb(&TheBIOSParameterBlock, &(ubpb.bs));
 #else
 	(void) memcpy(&(TheBIOSParameterBlock.bpb), &(ubpb.bs.bs_front.bs_bpb),
-		sizeof (TheBIOSParameterBlock.bpb));
+	    sizeof (TheBIOSParameterBlock.bpb));
 	(void) memcpy(&(TheBIOSParameterBlock.ebpb), &(ubpb.bs.bs_ebpb),
-		sizeof (TheBIOSParameterBlock.ebpb));
+	    sizeof (TheBIOSParameterBlock.ebpb));
 #endif
 	/*
 	 * In general, we would expect the bytes per sector to
@@ -220,8 +220,9 @@
 		    gettext("Bogus bytes per sector value.  Giving up.\n"));
 		exit(2);
 	}
-	if (!(is_z_a_power_of_x_le_y(2, 128,
-		TheBIOSParameterBlock.bpb.sectors_per_cluster))) {
+	if (!(ISP2(TheBIOSParameterBlock.bpb.sectors_per_cluster) &&
+	    IN_RANGE(TheBIOSParameterBlock.bpb.sectors_per_cluster,
+	    1, 128))) {
 		mountSanityCheckFails();
 		(void) fprintf(stderr,
 		    gettext("Bogus sectors per cluster value.  Giving up.\n"));
@@ -233,8 +234,8 @@
 		swap_pack_grab32bpb(&TheBIOSParameterBlock, &(ubpb.bs));
 #else
 		(void) memcpy(&(TheBIOSParameterBlock.bpb32),
-			&(ubpb.bs32.bs_bpb32),
-			sizeof (TheBIOSParameterBlock.bpb32));
+		    &(ubpb.bs32.bs_bpb32),
+		    sizeof (TheBIOSParameterBlock.bpb32));
 #endif
 		IsFAT32 = 1;
 	}
@@ -246,7 +247,7 @@
 			mountSanityCheckFails();
 			(void) fprintf(stderr,
 			    gettext("Bogus number of root entries.  "
-				"Giving up.\n"));
+			    "Giving up.\n"));
 			exit(2);
 		}
 	} else {
@@ -254,7 +255,7 @@
 			mountSanityCheckFails();
 			(void) fprintf(stderr,
 			    gettext("Bogus number of root entries.  "
-				"Giving up.\n"));
+			    "Giving up.\n"));
 			exit(2);
 		}
 	}
--- a/usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.c	Thu Aug 11 02:33:25 2011 -0700
+++ b/usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.c	Thu Aug 11 10:31:52 2011 -0700
@@ -19,6 +19,8 @@
  * CDDL HEADER END
  */
 /*
+ * Copyright (c) 2011 Gary Mills
+ *
  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
@@ -63,23 +65,6 @@
 extern int AlwaysYes;
 extern int AlwaysNo;
 
-int
-is_z_a_power_of_x_le_y(int x, int y, int z)
-{
-	int ispower = 0;
-	int pow = 1;
-
-	do {
-		if (pow == z) {
-			ispower = 1;
-			break;
-		}
-		pow *= x;
-	} while (pow <= y);
-
-	return (ispower);
-}
-
 /*
  * store_16_bits
  *	Save the lower 16 bits of a 32 bit value (v) into the provided
--- a/usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.h	Thu Aug 11 02:33:25 2011 -0700
+++ b/usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.h	Thu Aug 11 10:31:52 2011 -0700
@@ -20,6 +20,8 @@
  * CDDL HEADER END
  */
 /*
+ * Copyright (c) 2011 Gary Mills
+ *
  * Copyright (c) 1999 by Sun Microsystems, Inc.
  * All rights reserved.
  */
@@ -27,8 +29,6 @@
 #ifndef _PCFS_COMMON_H
 #define	_PCFS_COMMON_H
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 /*
  * Common routines for the pcfs user-level utilities
  */
@@ -40,8 +40,11 @@
 #include <sys/isa_defs.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/sysmacros.h>
 #include "pcfs_bpb.h"
 
+#define	IN_RANGE(n, x, y) (((n) >= (x)) && ((n) <= (y)))
+
 /*
  *  A macro implementing a ceiling function for integer divides.
  */
@@ -78,7 +81,6 @@
 extern void dump_bytes(uchar_t *b, int n);
 extern void bad_arg(char *option);
 extern void usage(void);
-extern int  is_z_a_power_of_x_le_y(int x, int y, int z);
 extern int  yes(void);
 
 /*
--- a/usr/src/cmd/fs.d/pcfs/mkfs/mkfs.c	Thu Aug 11 02:33:25 2011 -0700
+++ b/usr/src/cmd/fs.d/pcfs/mkfs/mkfs.c	Thu Aug 11 10:31:52 2011 -0700
@@ -19,6 +19,8 @@
  * CDDL HEADER END
  */
 /*
+ * Copyright (c) 2011 Gary Mills
+ *
  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
@@ -51,6 +53,7 @@
  *
  *	XXX -- floppy opens need O_NDELAY?
  */
+#define	IN_RANGE(n, x, y) (((n) >= (x)) && ((n) <= (y)))
 #define	DEFAULT_LABEL "NONAME"
 
 static char	*BootBlkFn = NULL;
@@ -165,7 +168,6 @@
 static int open_and_examine(char *dn, bpb_t *wbpb);
 static int verify_firstfile(char *fn, ulong_t *filesize);
 static int lookup_FAT_size(uchar_t partid);
-static int powerofx_le_y(int x, int y, int value);
 static int open_and_seek(char *dn, bpb_t *wbpb, off64_t *seekto);
 static int warn_mismatch(char *desc, char *src, int expect, int assigned);
 static int copy_bootblk(char *fn, boot_sector_t *bootsect,
@@ -231,33 +233,6 @@
 	return (*a);
 }
 
-/*
- * powerofx_le_y
- *	args of x,y, and value to be checked
- *	returns 1 if x**n == value and n >= 0 and value <= y
- *	returns 0 otherwise
- */
-static
-int
-powerofx_le_y(int x, int y, int value)
-{
-	int ispower = 0;
-	int pow = 1;
-
-	if (value < 1 || value > y)
-		return (ispower);
-
-	do {
-		if (pow == value) {
-			ispower = 1;
-			break;
-		}
-		pow *= x;
-	} while (pow <= y);
-
-	return (ispower);
-}
-
 static
 int
 ask_nicely(char *special)
@@ -1878,7 +1853,8 @@
 	if (wbpb->bpb.bytes_sector != BPSEC) {
 		(void) fprintf(stderr,
 		    gettext("Bogus bytes per sector value.\n"));
-		if (!powerofx_le_y(2, BPSEC * 8, wbpb->bpb.bytes_sector)) {
+		if (!(ISP2(wbpb->bpb.bytes_sector) &&
+		    IN_RANGE(wbpb->bpb.bytes_sector, 1, BPSEC * 8))) {
 			(void) fprintf(stderr,
 			    gettext("The device name may be missing a "
 			    "logical drive specifier.\n"));
@@ -1895,7 +1871,8 @@
 			exit(6);
 		}
 	}
-	if (!(powerofx_le_y(2, 128, wbpb->bpb.sectors_per_cluster))) {
+	if (!(ISP2(wbpb->bpb.sectors_per_cluster) &&
+	    IN_RANGE(wbpb->bpb.sectors_per_cluster, 1, 128))) {
 		(void) fprintf(stderr,
 		    gettext("Bogus sectors per cluster value.\n"));
 		(void) fprintf(stderr,
@@ -3435,7 +3412,8 @@
 		(void) fprintf(stderr, gettext("Invalid Bits/Fat value."
 		    " Must be 12, 16 or 32.\n"));
 		exit(2);
-	} else if (!GetSPC && !powerofx_le_y(2, 128, SecPerClust)) {
+	} else if (!GetSPC && !(ISP2(SecPerClust) &&
+	    IN_RANGE(SecPerClust, 1, 128))) {
 		(void) fprintf(stderr,
 		    gettext("Invalid Sectors/Cluster value.  Must be a "
 		    "power of 2 between 1 and 128.\n"));