Skip to contentSkip to conversation

Fix flashcp display

PATCH In review Fabien Proriol 2 participants 2 messages Nov 7, 2014 – Nov 12, 2015 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix flashcp display

Quoted 1 line

From e65a012df12fa39ed0a6c5a71b5dc952b4c982fc Mon Sep 17 00:00:00 2001

From: Fabien Proriol <[email protected]>
Date: Thu, 6 Nov 2014 15:54:20 +0100
Subject: [PATCH] flashcp: Use %llu to print filestat.st_size

filestat.st_size type is off_t.
For some paltforms, off_t can be 32 or 64bit but there is no C99 format specifier for off_t. The best way to print it with printf is to cast it to long long and print with %llu

---
 flashcp.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
flashcp.c +16 -16
index d58c81b..04495bd 100644
@@ -296,7 +296,7 @@ int main (int argc,char *argv[])
 	 * write the entire file to flash *
 	 **********************************/
 
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%luk (0%%)",KB (filestat.st_size));
+	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
@@ -304,10 +304,10 @@ int main (int argc,char *argv[])
 	{
 		if (size < BUFSIZE) i = size;
 		if (flags & FLAG_VERBOSE)
-			log_printf (LOG_NORMAL,"\rWriting data: %dk/%luk (%lu%%)",
+			log_printf (LOG_NORMAL,"\rWriting data: %dk/%lluk (%llu%%)",
 					KB (written + i),
-					KB (filestat.st_size),
-					PERCENTAGE (written + i,filestat.st_size));
+					KB ((unsigned long long)filestat.st_size),
+					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
@@ -325,8 +325,8 @@ int main (int argc,char *argv[])
 				exit (EXIT_FAILURE);
 			}
 			log_printf (LOG_ERROR,
-					"Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%lu bytes written to flash\n",
-					written,written + i,device,written + result,filestat.st_size);
+					"Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%llu bytes written to flash\n",
+					written,written + i,device,written + result,(unsigned long long)filestat.st_size);
 			exit (EXIT_FAILURE);
 		}
 
@@ -335,10 +335,10 @@ int main (int argc,char *argv[])
 	}
 	if (flags & FLAG_VERBOSE)
 		log_printf (LOG_NORMAL,
-				"\rWriting data: %luk/%luk (100%%)\n",
-				KB (filestat.st_size),
-				KB (filestat.st_size));
-	DEBUG("Wrote %d / %luk bytes\n",written,filestat.st_size);
+				"\rWriting data: %lluk/%lluk (100%%)\n",
+				KB ((unsigned long long)filestat.st_size),
+				KB ((unsigned long long)filestat.st_size));
+	DEBUG("Wrote %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	/**********************************
 	 * verify that flash == file data *
@@ -349,16 +349,16 @@ int main (int argc,char *argv[])
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%luk (0%%)",KB (filestat.st_size));
+	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	while (size)
 	{
 		if (size < BUFSIZE) i = size;
 		if (flags & FLAG_VERBOSE)
 			log_printf (LOG_NORMAL,
-					"\rVerifying data: %dk/%luk (%lu%%)",
+					"\rVerifying data: %dk/%lluk (%lu%%)",
 					KB (written + i),
-					KB (filestat.st_size),
-					PERCENTAGE (written + i,filestat.st_size));
+					KB ((unsigned long long)filestat.st_size),
+					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
@@ -380,10 +380,10 @@ int main (int argc,char *argv[])
 	}
 	if (flags & FLAG_VERBOSE)
 		log_printf (LOG_NORMAL,
-				"\rVerifying data: %luk/%luk (100%%)\n",
+				"\rVerifying data: %lluk/%lluk (100%%)\n",
 				KB (filestat.st_size),
 				KB (filestat.st_size));
-	DEBUG("Verified %d / %luk bytes\n",written,filestat.st_size);
+	DEBUG("Verified %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	exit (EXIT_SUCCESS);
 }
Signature · 1 line
2.0.4
#1 lore raw
Quoted 3 lines · Fabien Proriol

From e65a012df12fa39ed0a6c5a71b5dc952b4c982fc Mon Sep 17 00:00:00 2001
From: Fabien Proriol <[email protected]>
Date: Thu, 6 Nov 2014 15:54:20 +0100

s/2014/2015/
and then we're all good, right? :)

Quoted 4 lines

Subject: [PATCH] flashcp: Use %llu to print filestat.st_size

filestat.st_size type is off_t.
For some paltforms, off_t can be 32 or 64bit but there is no C99 format specifier for off_t.

Hmm, if you're getting a 32-bit off_t, that means you can't support >4GB flash.

Quoted 12 lines

The best way to print it with printf is to cast it to long long and print with %llu

---
 flashcp.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
flashcp.c +0 -0
index d58c81b..04495bd 100644

flashcp.c int main (int argc,char *argv[]) · line 299

Quoted 5 lines
flashcp.c +1 -1
@@ -296,7 +296,7 @@ int main (int argc,char *argv[])
 	 * write the entire file to flash *
 	 **********************************/
 
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%luk (0%%)",KB (filestat.st_size));
+	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));

Personally, I don't think this is the absolute cleanest way to do this all. You're making 32-bit systems do 64-bit arithmetic to get a small value (in KB), when you could just do the cast afterward (since you only care about the printing; the casting doesn't actually help the computation).

Also, the long lines and awkward spacing is all pretty off-putting, but that's not your fault.

Anyway, applied to mtd-utils.git. Thanks.

Brian

Quoted 84 lines
flashcp.c +15 -15
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
@@ -304,10 +304,10 @@ int main (int argc,char *argv[])
 	{
 		if (size < BUFSIZE) i = size;
 		if (flags & FLAG_VERBOSE)
-			log_printf (LOG_NORMAL,"\rWriting data: %dk/%luk (%lu%%)",
+			log_printf (LOG_NORMAL,"\rWriting data: %dk/%lluk (%llu%%)",
 					KB (written + i),
-					KB (filestat.st_size),
-					PERCENTAGE (written + i,filestat.st_size));
+					KB ((unsigned long long)filestat.st_size),
+					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
@@ -325,8 +325,8 @@ int main (int argc,char *argv[])
 				exit (EXIT_FAILURE);
 			}
 			log_printf (LOG_ERROR,
-					"Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%lu bytes written to flash\n",
-					written,written + i,device,written + result,filestat.st_size);
+					"Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%llu bytes written to flash\n",
+					written,written + i,device,written + result,(unsigned long long)filestat.st_size);
 			exit (EXIT_FAILURE);
 		}
 
@@ -335,10 +335,10 @@ int main (int argc,char *argv[])
 	}
 	if (flags & FLAG_VERBOSE)
 		log_printf (LOG_NORMAL,
-				"\rWriting data: %luk/%luk (100%%)\n",
-				KB (filestat.st_size),
-				KB (filestat.st_size));
-	DEBUG("Wrote %d / %luk bytes\n",written,filestat.st_size);
+				"\rWriting data: %lluk/%lluk (100%%)\n",
+				KB ((unsigned long long)filestat.st_size),
+				KB ((unsigned long long)filestat.st_size));
+	DEBUG("Wrote %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	/**********************************
 	 * verify that flash == file data *
@@ -349,16 +349,16 @@ int main (int argc,char *argv[])
 	size = filestat.st_size;
 	i = BUFSIZE;
 	written = 0;
-	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%luk (0%%)",KB (filestat.st_size));
+	if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%lluk (0%%)",KB ((unsigned long long)filestat.st_size));
 	while (size)
 	{
 		if (size < BUFSIZE) i = size;
 		if (flags & FLAG_VERBOSE)
 			log_printf (LOG_NORMAL,
-					"\rVerifying data: %dk/%luk (%lu%%)",
+					"\rVerifying data: %dk/%lluk (%lu%%)",
 					KB (written + i),
-					KB (filestat.st_size),
-					PERCENTAGE (written + i,filestat.st_size));
+					KB ((unsigned long long)filestat.st_size),
+					PERCENTAGE (written + i,(unsigned long long)filestat.st_size));
 
 		/* read from filename */
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
@@ -380,10 +380,10 @@ int main (int argc,char *argv[])
 	}
 	if (flags & FLAG_VERBOSE)
 		log_printf (LOG_NORMAL,
-				"\rVerifying data: %luk/%luk (100%%)\n",
+				"\rVerifying data: %lluk/%lluk (100%%)\n",
 				KB (filestat.st_size),
 				KB (filestat.st_size));
-	DEBUG("Verified %d / %luk bytes\n",written,filestat.st_size);
+	DEBUG("Verified %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size);
 
 	exit (EXIT_SUCCESS);
 }
Signature · 6 lines
2.0.4


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/