Browse Source

Reduced code size; fixed ANSI bug for day -- 5 not 05; mode days and mons const.

Manuel Novoa III 23 years ago
parent
commit
086815d394
1 changed files with 48 additions and 28 deletions
  1. 48 28
      libc/misc/time/asc_conv.c

+ 48 - 28
libc/misc/time/asc_conv.c

@@ -5,43 +5,63 @@
  * Internal ascii conversion routine, avoid use of printf, it's a bit big!
  */
 
-
-static void hit(buf, val)
-char *buf;
-int val;
-{
-	*buf = '0' + val % 10;
-}
+/*
+ * Modified      Manuel Novoa III       Jan 2001
+ *
+ * Removed static function "hit" and did time-field fills inline and
+ * put day, hour, min, and sec conversions in a loop using a small
+ * table to reduce code size.
+ *
+ * Made daysp[] and mons[] const to move them from bss to text.
+ *
+ * Also fixed day conversion ... ANSI says no leading 0.
+ *
+ */
 
 void __asctime(buffer, ptm)
 register char *buffer;
 struct tm *ptm;
 {
-	static char days[] = "SunMonTueWedThuFriSat";
-	static char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
-	int year;
+	static const char days[] = "SunMonTueWedThuFriSat";
+	static const char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+	/*                              012345678901234567890123456 */
+	static const char template[] = "Err Err 00 00:00:00 0000\n";
+	int tm_field[4];
+	int tmp, i;
+	char *p;
+
+	/* Since we need memcpy below, use it here instead of strcpy. */
+	memcpy(buffer, template, sizeof(template));
 
-	/*              012345678901234567890123456 */
-	strcpy(buffer, "Err Err .. ..:..:.. ....\n");
-	if ((ptm->tm_wday >= 0) && (ptm->tm_wday <= 6))
+	if ((ptm->tm_wday >= 0) && (ptm->tm_wday <= 6)) {
 		memcpy(buffer, days + 3 * (ptm->tm_wday), 3);
+	}
 
-	if ((ptm->tm_mon >= 0) && (ptm->tm_mon <= 11))
+	if ((ptm->tm_mon >= 0) && (ptm->tm_mon <= 11)) {
 		memcpy(buffer + 4, mons + 3 * (ptm->tm_mon), 3);
+	}
+
+	tm_field[0] = ptm->tm_mday;
+	tm_field[1] = ptm->tm_hour;
+	tm_field[2] = ptm->tm_min;
+	tm_field[3] = ptm->tm_sec;
+
+	p = buffer + 9;
+	for (i=0 ; i<4 ; i++) {
+		tmp = tm_field[i];
+		*p-- += tmp % 10;
+		*p += (tmp/10) % 10;
+		p += 4 ;				/* skip to end of next field */
+	}
 
+	tmp = ptm->tm_year + 1900;
+	p = buffer + 23;
+	for (i=0 ; i<4 ; i++) {
+		*p-- += tmp % 10;
+		tmp /= 10;
+	}
 
-	hit(buffer + 8, ptm->tm_mday / 10);
-	hit(buffer + 9, ptm->tm_mday);
-	hit(buffer + 11, ptm->tm_hour / 10);
-	hit(buffer + 12, ptm->tm_hour);
-	hit(buffer + 14, ptm->tm_min / 10);
-	hit(buffer + 15, ptm->tm_min);
-	hit(buffer + 17, ptm->tm_sec / 10);
-	hit(buffer + 18, ptm->tm_sec);
-
-	year = ptm->tm_year + 1900;
-	hit(buffer + 20, year / 1000);
-	hit(buffer + 21, year / 100);
-	hit(buffer + 22, year / 10);
-	hit(buffer + 23, year);
+	if (buffer[8] == '0') {
+		buffer[8] = ' ';
+	}
 }