[BusyBox] save 22 binary bytes

Aaron Lehmann aaronl at vitelus.com
Wed May 16 17:21:39 UTC 2001


Also, a lot of code in init.c used snprintf (dest, "%s", ...) when a
simple strncpy would have sufficed...

Hmm. Oddly, this doesn't change the binary's size (although it's
likely to on some non-i386 architectures), but it's the Right Thing to do:


Index: init.c
===================================================================
RCS file: /var/cvs/busybox/init.c,v
retrieving revision 1.136
diff -u -r1.136 init.c
--- init.c	2001/04/25 05:39:18	1.136
+++ init.c	2001/05/16 23:19:28
@@ -328,7 +328,7 @@
 	}
 
 	if ((s = getenv("CONSOLE")) != NULL) {
-		snprintf(console, sizeof(console) - 1, "%s", s);
+		strncpy(console, s, sizeof(console) - 1);
 	}
 #if #cpu(sparc)
 	/* sparc kernel supports console=tty[ab] parameter which is also 
@@ -336,9 +336,9 @@
 	else if ((s = getenv("console")) != NULL) {
 		/* remap tty[ab] to /dev/ttyS[01] */
 		if (strcmp(s, "ttya") == 0)
-			snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
+			strncpy(console, SERIAL_CON0, sizeof(console) - 1);
 		else if (strcmp(s, "ttyb") == 0)
-			snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
+			strncpy(console, SERIAL_CON1, sizeof(console) - 1);
 	}
 #endif
 	else {
@@ -351,7 +351,7 @@
 			snprintf(console, sizeof(console) - 1, "/dev/tty%d",
 					 vt.v_active);
 		} else {
-			snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
+			strncpy(console, _PATH_CONSOLE, sizeof(console) - 1);
 			tried_devcons++;
 		}
 	}
@@ -360,20 +360,20 @@
 		/* Can't open selected console -- try /dev/console */
 		if (!tried_devcons) {
 			tried_devcons++;
-			snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
+			strncpy(console, _PATH_CONSOLE, sizeof(console) - 1);
 			continue;
 		}
 		/* Can't open selected console -- try vt1 */
 		if (!tried_vtprimary) {
 			tried_vtprimary++;
-			snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
+			strncpy(console, VT_PRIMARY, sizeof(console) - 1);
 			continue;
 		}
 		break;
 	}
 	if (fd < 0) {
 		/* Perhaps we should panic here? */
-		snprintf(console, sizeof(console) - 1, "/dev/null");
+		strncpy(console, "/dev/null", sizeof(console) - 1);
 	} else {
 		/* check for serial console and disable logging to tty5 & running a
 		   * shell to tty2-4 */
@@ -385,7 +385,7 @@
 			/* Force the TERM setting to vt102 for serial console --
 			 * iff TERM is set to linux (the default) */
 			if (strcmp( termType, "TERM=linux" ) == 0)
-				snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
+				strncpy(termType, "TERM=vt102", sizeof(termType) - 1);
 			message(LOG | CONSOLE,
 					"serial console detected.  Disabling virtual terminals.\r\n");
 		}





More information about the busybox mailing list