Module Name:    src
Committed By:   apb
Date:           Thu Jan  6 21:39:01 UTC 2011

Modified Files:
        src/sbin/disklabel: interact.c main.c printlabel.c

Log Message:
Change printf formats to match the data type of the values being
printed.  There's now a lot of PRIu16 and PRIu32, some PRIu8, some
SCNu32, and a few cases where %u and %d were reversed.  Multiplication
of 32-bit and 8-bit values is cast to uint64_t and printed with PRIu64.

Inspired by a report from Patrick Welche on current-users.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/sbin/disklabel/interact.c
cvs rdiff -u -r1.23 -r1.24 src/sbin/disklabel/main.c
cvs rdiff -u -r1.15 -r1.16 src/sbin/disklabel/printlabel.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/disklabel/interact.c
diff -u src/sbin/disklabel/interact.c:1.34 src/sbin/disklabel/interact.c:1.35
--- src/sbin/disklabel/interact.c:1.34	Fri May 28 07:40:53 2010
+++ src/sbin/disklabel/interact.c	Thu Jan  6 21:39:01 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: interact.c,v 1.34 2010/05/28 07:40:53 dholland Exp $	*/
+/*	$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $	*/
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: interact.c,v 1.34 2010/05/28 07:40:53 dholland Exp $");
+__RCSID("$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $");
 #endif /* lint */
 
 #include <sys/param.h>
@@ -203,13 +203,13 @@
 
 	/* d_npartitions */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_npartitions);
+		snprintf(def, sizeof(def), "%" PRIu16, lp->d_npartitions);
 		i = getinput(":", "Number of partitions", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid number of partitions `%s'\n", line);
 			continue;
 		}
@@ -219,13 +219,13 @@
 
 	/* d_secsize */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_secsize);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_secsize);
 		i = getinput(":", "Sector size (bytes)", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid sector size `%s'\n", line);
 			continue;
 		}
@@ -235,13 +235,13 @@
 
 	/* d_nsectors */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_nsectors);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_nsectors);
 		i = getinput(":", "Number of sectors per track", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid number of sectors `%s'\n", line);
 			continue;
 		}
@@ -251,13 +251,13 @@
 
 	/* d_ntracks */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_ntracks);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_ntracks);
 		i = getinput(":", "Number of tracks per cylinder", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid number of tracks `%s'\n", line);
 			continue;
 		}
@@ -267,13 +267,13 @@
 
 	/* d_secpercyl */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_secpercyl);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_secpercyl);
 		i = getinput(":", "Number of sectors/cylinder", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid number of sector/cylinder `%s'\n",
 			    line);
 			continue;
@@ -284,13 +284,13 @@
 
 	/* d_ncylinders */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_ncylinders);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_ncylinders);
 		i = getinput(":", "Total number of cylinders", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid sector size `%s'\n", line);
 			continue;
 		}
@@ -300,13 +300,13 @@
 
 	/* d_secperunit */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_secperunit);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_secperunit);
 		i = getinput(":", "Total number of sectors", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid number of sectors `%s'\n", line);
 			continue;
 		}
@@ -318,13 +318,13 @@
 
 	/* d_interleave */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_interleave);
+		snprintf(def, sizeof(def), "%" PRIu16, lp->d_interleave);
 		i = getinput(":", "Hardware sectors interleave", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid sector interleave `%s'\n", line);
 			continue;
 		}
@@ -334,13 +334,13 @@
 
 	/* d_trackskew */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_trackskew);
+		snprintf(def, sizeof(def), "%" PRIu16, lp->d_trackskew);
 		i = getinput(":", "Sector 0 skew, per track", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid track sector skew `%s'\n", line);
 			continue;
 		}
@@ -350,13 +350,13 @@
 
 	/* d_cylskew */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_cylskew);
+		snprintf(def, sizeof(def), "%" PRIu16, lp->d_cylskew);
 		i = getinput(":", "Sector 0 skew, per cylinder", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid cylinder sector `%s'\n", line);
 			continue;
 		}
@@ -366,13 +366,13 @@
 
 	/* d_headswitch */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_headswitch);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_headswitch);
 		i = getinput(":", "Head switch time (usec)", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid head switch time `%s'\n", line);
 			continue;
 		}
@@ -382,13 +382,13 @@
 
 	/* d_trkseek */
 	for (;;) {
-		snprintf(def, sizeof(def), "%u", lp->d_trkseek);
+		snprintf(def, sizeof(def), "%" PRIu32, lp->d_trkseek);
 		i = getinput(":", "Track seek time (usec)", def, line);
 		if (i == -1)
 			return;
 		else if (i == 0)
 			break;
-		if (sscanf(line, "%u", &u) != 1) {
+		if (sscanf(line, "%" SCNu32, &u) != 1) {
 			printf("Invalid track seek time `%s'\n", line);
 			continue;
 		}
@@ -704,7 +704,7 @@
 defnum(struct disklabel *lp, char *buf, uint32_t size)
 {
 
-	(void) snprintf(buf, BUFSIZ, "%.40gc, %us, %.40gM",
+	(void) snprintf(buf, BUFSIZ, "%.40gc, %" PRIu32 "s, %.40gM",
 	    size / (float) lp->d_secpercyl,
 	    size, size  * (lp->d_secsize / (float) (1024 * 1024)));
 }

Index: src/sbin/disklabel/main.c
diff -u src/sbin/disklabel/main.c:1.23 src/sbin/disklabel/main.c:1.24
--- src/sbin/disklabel/main.c:1.23	Thu Jan  6 19:34:28 2011
+++ src/sbin/disklabel/main.c	Thu Jan  6 21:39:01 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.23 2011/01/06 19:34:28 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.24 2011/01/06 21:39:01 apb Exp $	*/
 
 /*
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -76,7 +76,7 @@
 static char sccsid[] = "@(#)disklabel.c	8.4 (Berkeley) 5/4/95";
 /* from static char sccsid[] = "@(#)disklabel.c	1.2 (Symmetric) 11/28/85"; */
 #else
-__RCSID("$NetBSD: main.c,v 1.23 2011/01/06 19:34:28 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.24 2011/01/06 21:39:01 apb Exp $");
 #endif
 #endif	/* not lint */
 
@@ -624,7 +624,7 @@
 		if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE)
 		    != sizeof(mbr)) {
 			if (verbose)
-				warn("Can't read master boot record %d",
+				warn("Can't read master boot record %u",
 				    this_ext);
 			break;
 		}
@@ -632,7 +632,7 @@
 		/* Check if table is valid. */
 		if (mbr.mbr_magic != htole16(MBR_MAGIC)) {
 			if (verbose)
-				warnx("Invalid signature in mbr record %d",
+				warnx("Invalid signature in mbr record %u",
 				    this_ext);
 			break;
 		}
@@ -902,7 +902,8 @@
 find_label(int f, u_int sector)
 {
 	struct disklabel *disk_lp, hlp;
-	int i, offset;
+	int i;
+	u_int offset;
 	const char *is_deleted;
 
 	bootarea_len = pread(f, bootarea, sizeof bootarea,
@@ -914,7 +915,7 @@
 	}
 
 	if (verbose > 2)
-		warnx("read sector %u len %u looking for label",
+		warnx("read sector %u len %d looking for label",
 		    sector, bootarea_len);
 
 	/* Check expected offset first */
@@ -1000,9 +1001,9 @@
 	if (wlen == bootarea_len)
 		return;
 	if (wlen == -1)
-		err(1, "disklabel write (sector %u) size %u failed",
+		err(1, "disklabel write (sector %u) size %d failed",
 		    sector, bootarea_len);
-	errx(1, "disklabel write (sector %u) size %u truncated to %d",
+	errx(1, "disklabel write (sector %u) size %d truncated to %d",
 		    sector, bootarea_len, wlen);
 }
 
@@ -1164,8 +1165,9 @@
 					(void) fprintf(f, "t%c=%s:", c,
 					    fstypenames[pp->p_fstype]);
 				else
-					(void) fprintf(f, "t%c=unknown%" PRIu8
-					    ":", c, pp->p_fstype);
+					(void) fprintf(f,
+					    "t%c=unknown%" PRIu8 ":",
+					    c, pp->p_fstype);
 			}
 			switch (pp->p_fstype) {
 
@@ -1742,7 +1744,8 @@
 	} else if (lp->d_sbsize % lp->d_secsize)
 		warnx("warning, super block size %% sector-size != 0");
 	if (lp->d_npartitions > MAXPARTITIONS)
-		warnx("warning, number of partitions (%d) > MAXPARTITIONS (%d)",
+		warnx("warning, number of partitions (%" PRIu16 ") > "
+		    "MAXPARTITIONS (%d)",
 		    lp->d_npartitions, MAXPARTITIONS);
 	else
 		for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) {
@@ -1750,7 +1753,8 @@
 			pp = &lp->d_partitions[i];
 			if (pp->p_size || pp->p_offset) {
 				warnx("warning, partition %c increased "
-				    "number of partitions from %d to %d",
+				    "number of partitions from %" PRIu16
+				    " to %d",
 				    part, lp->d_npartitions, i + 1);
 				lp->d_npartitions = i + 1;
 				break;
@@ -1760,7 +1764,8 @@
 		part = 'a' + i;
 		pp = &lp->d_partitions[i];
 		if (pp->p_size == 0 && pp->p_offset != 0)
-			warnx("warning, partition %c: size 0, but offset %d",
+			warnx("warning, partition %c: size 0, but "
+			    "offset %" PRIu32,
 			    part, pp->p_offset);
 #ifdef STRICT_CYLINDER_ALIGNMENT
 		if (pp->p_offset % lp->d_secpercyl) {

Index: src/sbin/disklabel/printlabel.c
diff -u src/sbin/disklabel/printlabel.c:1.15 src/sbin/disklabel/printlabel.c:1.16
--- src/sbin/disklabel/printlabel.c:1.15	Sun Jan 18 21:15:14 2009
+++ src/sbin/disklabel/printlabel.c	Thu Jan  6 21:39:01 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: printlabel.c,v 1.15 2009/01/18 21:15:14 apb Exp $	*/
+/*	$NetBSD: printlabel.c,v 1.16 2011/01/06 21:39:01 apb Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: printlabel.c,v 1.15 2009/01/18 21:15:14 apb Exp $");
+__RCSID("$NetBSD: printlabel.c,v 1.16 2011/01/06 21:39:01 apb Exp $");
 #endif	/* not lint */
 
 #include <sys/param.h>
@@ -65,7 +65,7 @@
 	if ((unsigned) lp->d_type < DKMAXTYPES)
 		(void)fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
 	else
-		(void)fprintf(f, "type: %d\n", lp->d_type);
+		(void)fprintf(f, "type: %" PRIu16 "\n", lp->d_type);
 	(void)fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename),
 	    lp->d_typename);
 	(void)fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname),
@@ -99,7 +99,7 @@
 	if (i < 0)
 		i = 0;
 	for (j = 0; j <= i; j++)
-		(void)fprintf(f, "%d ", lp->d_drivedata[j]);
+		(void)fprintf(f, "%" PRIu32 " ", lp->d_drivedata[j]);
 	(void)fprintf(f, "\n\n");
 	(void)fflush(f);
 }
@@ -114,12 +114,14 @@
 	if (ctsformat && lp->d_secpercyl && lp->d_nsectors) {
 		char sbuf[64], obuf[64];
 
-		(void)snprintf(sbuf, sizeof(sbuf), "%u/%u/%u",
+		(void)snprintf(sbuf, sizeof(sbuf),
+                    "%" PRIu32 "/%" PRIu32 "/%" PRIu32,
 		    pp->p_size/lp->d_secpercyl,
 		    (pp->p_size%lp->d_secpercyl) / lp->d_nsectors,
 		    pp->p_size%lp->d_nsectors);
 
-		(void)snprintf(obuf, sizeof(obuf), "%u/%u/%u",
+		(void)snprintf(obuf, sizeof(obuf),
+                    "%" PRIu32 "/%" PRIu32 "/%" PRIu32,
 		    pp->p_offset/lp->d_secpercyl,
 		    (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors,
 		    pp->p_offset%lp->d_nsectors);
@@ -127,40 +129,41 @@
 		(void)fprintf(f, " %c: %9s %9s ",
 		    'a' + i, sbuf, obuf);
 	} else {
-		(void)fprintf(f, " %c: %9u %9u ", 'a' + i,
-		    pp->p_size, pp->p_offset);
+		(void)fprintf(f, " %c: %9" PRIu32 " %9" PRIu32 " ",
+		    'a' + i, pp->p_size, pp->p_offset);
 	}
 
 	if ((unsigned) pp->p_fstype < FSMAXTYPES)
 		(void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]);
 	else
-		(void)fprintf(f, "%10d", pp->p_fstype);
+		(void)fprintf(f, "%10" PRIu8, pp->p_fstype);
 
 	switch (pp->p_fstype) {
 	case FS_UNUSED:				/* XXX */
-		(void)fprintf(f, "  %5u %5u %5.5s ",
-		    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
+		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5.5s ",
+		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, "");
 		break;
 
 	case FS_BSDFFS:
 	case FS_ADOS:
 	case FS_APPLEUFS:
-		(void)fprintf(f, "  %5u %5u %5u ",
-		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_cpg);
+		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5" PRIu16 " ",
+		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, pp->p_cpg);
 		break;
 
 	case FS_BSDLFS:
-		(void)fprintf(f, "  %5u %5u %5u ",
-		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_sgs);
+		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5" PRIu16 " ",
+		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, pp->p_sgs);
 		break;
 
 	case FS_EX2FS:
-		(void)fprintf(f, "  %5u %5u       ",
-		    pp->p_fsize, pp->p_fsize * pp->p_frag);
+		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 "       ",
+		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag);
 		break;
 
 	case FS_ISO9660:
-		(void)fprintf(f, "  %6u            ", pp->p_cdsession);
+		(void)fprintf(f, "  %6" PRIu32 "            ",
+		    pp->p_cdsession);
 		break;
 
 	default:
@@ -168,7 +171,7 @@
 		break;
 	}
 	if (lp->d_secpercyl != 0) {
-		(void)fprintf(f, " # (Cyl. %6u",
+		(void)fprintf(f, " # (Cyl. %6" PRIu32,
 		    pp->p_offset / lp->d_secpercyl);
 
 		if (pp->p_offset > lp->d_secperunit)
@@ -178,7 +181,7 @@
 		else
 		    putc(' ', f);
 
-		(void)fprintf(f, "- %6u",
+		(void)fprintf(f, "- %6" PRIu32,
 		    (pp->p_offset + 
 		    pp->p_size + lp->d_secpercyl - 1) /
 		    lp->d_secpercyl - 1);
@@ -198,7 +201,7 @@
 {
 	int	i;
 
-	(void)fprintf(f, "%d partitions:\n", lp->d_npartitions);
+	(void)fprintf(f, "%" PRIu16 " partitions:\n", lp->d_npartitions);
 	(void)fprintf(f,
 	    "#        size    offset     fstype [fsize bsize cpg/sgs]\n");
 

Reply via email to