smemcap: fix compile warnings

Johannes Stezenbach js at sig21.net
Fri May 7 07:20:32 CDT 2010


In current hg verson (03e592b13789):

$ gcc -Wall -Os -o smemcap smemcap.c 
smemcap.c: In function ‘writeheader’:
smemcap.c:35: warning: embedded ‘\0’ in format
smemcap.c:36: warning: embedded ‘\0’ in format
smemcap.c:37: warning: embedded ‘\0’ in format
smemcap.c:38: warning: embedded ‘\0’ in format
smemcap.c:39: warning: embedded ‘\0’ in format
smemcap.c:45: warning: embedded ‘\0’ in format
smemcap.c:45: warning: too many arguments for format
smemcap.c: In function ‘archivefile’:
smemcap.c:83: warning: no return statement in function returning non-void
smemcap.c: In function ‘main’:
smemcap.c:103: warning: suggest parentheses around assignment used as truth value
smemcap.c:94: warning: unused variable ‘fd’

sprintf() stops parsing the format at the embedded \0,
and already writes the trailing \0 to the destination.

Johannes


diff -r 03e592b13789 smemcap.c
--- a/smemcap.c	Mon Mar 29 16:38:31 2010 -0500
+++ b/smemcap.c	Fri May 07 14:04:21 2010 +0200
@@ -32,17 +32,17 @@
 	memset(header, 0, 512);
 
 	sprintf(header, "%s", path);
-	sprintf(header + 100, "%07o\0", mode & 0777);
-	sprintf(header + 108, "%07o\0", uid);
-	sprintf(header + 116, "%07o\0", gid);
-	sprintf(header + 124, "%011o\0", size);
-	sprintf(header + 136, "%07o\0", mtime);
+	sprintf(header + 100, "%07o", mode & 0777);
+	sprintf(header + 108, "%07o", uid);
+	sprintf(header + 116, "%07o", gid);
+	sprintf(header + 124, "%011o", size);
+	sprintf(header + 136, "%07o", mtime);
 	sprintf(header + 148, "        %1d", type);
 
 	/* fix checksum */
 	for (i = sum = 0; i < 512; i++)
 		sum += header[i];
-	sprintf(header + 148, "%06o\0 %1d", sum, type);
+	sprintf(header + 148, "%06o", sum);
 
 	return write(destfd, header, 512);
 }
@@ -80,6 +80,7 @@
 		cur = cur->next;
 		free(start);
 	}
+	return 0;
 }
 
 int archivejoin(const char *sub, const char *name, int destfd)
@@ -91,7 +92,6 @@
 
 int main(int argc, char *argv[])
 {
-	int fd;
 	DIR *d;
 	struct dirent *de;
 
@@ -100,7 +100,7 @@
 	archivefile("version", 1);
 
 	d = opendir(".");
-	while (de = readdir(d))
+	while ((de = readdir(d)))
 		if (de->d_name[0] >= '0' && de->d_name[0] <= '9') {
 			writeheader(1, de->d_name, 0555, 0, 0, 0, 0, 5);
 			archivejoin(de->d_name, "smaps", 1);


More information about the Smem mailing list