[PATCH] fix format strings for intmax_t/uintmax_t

Atsushi Nemoto anemo at mba.ocn.ne.jp
Tue Mar 28 07:48:20 UTC 2006


I found busybox's stat command shows bogus value on some fields.  This
is due to mismatch of printing format string and intmax_t/uintmax_t.

The "j" modifier can be used to print intmax_t/uintmax_t.

diff -u busybox-1.1.1.org/coreutils/stat.c busybox-1.1.1/coreutils/stat.c
--- busybox-1.1.1.org/coreutils/stat.c	2006-03-23 06:16:21.000000000 +0900
+++ busybox-1.1.1/coreutils/stat.c	2006-03-28 16:16:22.000000000 +0900
@@ -157,15 +157,15 @@
 		printf(pformat, human_fstype(statfsbuf->f_type));
 		break;
 	case 'b':
-		strncat(pformat, "ld", buf_len);
+		strncat(pformat, "jd", buf_len);
 		printf(pformat, (intmax_t) (statfsbuf->f_blocks));
 		break;
 	case 'f':
-		strncat(pformat, "ld", buf_len);
+		strncat(pformat, "jd", buf_len);
 		printf(pformat, (intmax_t) (statfsbuf->f_bfree));
 		break;
 	case 'a':
-		strncat(pformat, "ld", buf_len);
+		strncat(pformat, "jd", buf_len);
 		printf(pformat, (intmax_t) (statfsbuf->f_bavail));
 		break;
 	case 'S':
@@ -174,11 +174,11 @@
 		printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
 		break;
 	case 'c':
-		strncat(pformat, "ld", buf_len);
+		strncat(pformat, "jd", buf_len);
 		printf(pformat, (intmax_t) (statfsbuf->f_files));
 		break;
 	case 'd':
-		strncat(pformat, "ld", buf_len);
+		strncat(pformat, "jd", buf_len);
 		printf(pformat, (intmax_t) (statfsbuf->f_ffree));
 		break;
 	default:
@@ -219,15 +219,15 @@
 		}
 		break;
 	case 'd':
-		strncat(pformat, "lu", buf_len);
+		strncat(pformat, "ju", buf_len);
 		printf(pformat, (uintmax_t) statbuf->st_dev);
 		break;
 	case 'D':
-		strncat(pformat, "lx", buf_len);
+		strncat(pformat, "jx", buf_len);
 		printf(pformat, (uintmax_t) statbuf->st_dev);
 		break;
 	case 'i':
-		strncat(pformat, "lu", buf_len);
+		strncat(pformat, "ju", buf_len);
 		printf(pformat, (uintmax_t) statbuf->st_ino);
 		break;
 	case 'a':
@@ -279,7 +279,7 @@
 		printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
 		break;
 	case 's':
-		strncat(pformat, "lu", buf_len);
+		strncat(pformat, "ju", buf_len);
 		printf(pformat, (uintmax_t) (statbuf->st_size));
 		break;
 	case 'B':
@@ -287,7 +287,7 @@
 		printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
 		break;
 	case 'b':
-		strncat(pformat, "lu", buf_len);
+		strncat(pformat, "ju", buf_len);
 		printf(pformat, (uintmax_t) statbuf->st_blocks);
 		break;
 	case 'o':
@@ -414,10 +414,10 @@
 		printf("Type: %s\n", human_fstype(statfsbuf.f_type));
 
 	format = (flags & OPT_TERSE
-		? "%lu %ld %ld %ld %ld %ld\n"
+		? "%lu %jd %jd %jd %jd %jd\n"
 		: "Block size: %-10lu\n"
-		  "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
-		  "Inodes: Total: %-10ld Free: %ld\n");
+		  "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
+		  "Inodes: Total: %-10jd Free: %jd\n");
 	printf(format,
 	       (unsigned long int) (statfsbuf.f_bsize),
 	       (intmax_t) (statfsbuf.f_blocks),
@@ -466,7 +466,7 @@
 	print_it(format, filename, print_stat, &statbuf);
 #else
 	if (flags & OPT_TERSE) {
-		printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n",
+		printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
 		       filename,
 		       (uintmax_t) (statbuf.st_size),
 		       (uintmax_t) statbuf.st_blocks,
@@ -500,8 +500,8 @@
 		else
 			printf("  File: \"%s\"\n", filename);
 
-		printf("  Size: %-10lu\tBlocks: %-10lu IO Block: %-6lu %s\n"
-		       "Device: %lxh/%lud\tInode: %-10lu  Links: %-5lu",
+		printf("  Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
+		       "Device: %jxh/%jud\tInode: %-10ju  Links: %-5lu",
 		       (uintmax_t) (statbuf.st_size),
 		       (uintmax_t) statbuf.st_blocks,
 		       (unsigned long int) statbuf.st_blksize,


More information about the busybox mailing list