asctime.c

Paul Eggert eggert at CS.UCLA.EDU
Thu Jul 29 18:36:42 UTC 2004


"Olson, Arthur David (NIH/NCI)" <olsona at dc37a.nci.nih.gov> writes:

> Here's my latest effort at asctime.c.

That looks reasonable (thanks for moderating :-).  I assume Makefile
will have new comments about STRICTLY_STANDARD_ASCTIME, and private.h
will define EOVERFLOW if it's not already defined.

A couple of comments.  First, the last %d in the strictly standard
ASCTIME_FMT should be an %ld.  Second, (perhaps this is gilding the
lily, but here goes anyway) it'd be a bit clearer (and more efficent
for asctime) if asctime never invokes strcpy, but instead invokes
sprintf directly into the output buffer.

Here's a proposed patch.

--- asctime.c	2004/07/29 15:19:52	2004.1.1.3
+++ asctime.c	2004/07/29 18:28:51	2004.1.0.6
@@ -15,7 +15,7 @@ static char	elsieid[] = "@(#)asctime.c	7
 #include "tzfile.h"
 
 #if STRICTLY_STANDARD_ASCTIME
-#define ASCTIME_FMT	"%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
+#define ASCTIME_FMT	"%.3s %.3s%3d %.2d:%.2d:%.2d %ld\n"
 #define ASCTIME_FMT_B	ASCTIME_FMT
 #else /* !STRICTLY_STANDARD_ASCTIME */
 /*
@@ -85,19 +85,20 @@ char *				buf;
 	/*
 	** We avoid using snprintf since it's not available on all systems.
 	*/
-	(void) sprintf(result,
+	(void) sprintf((buf == buf_asctime ? buf : result),
 		((year >= -999 && year <= 9999) ? ASCTIME_FMT : ASCTIME_FMT_B),
 		wn, mn,
 		timeptr->tm_mday, timeptr->tm_hour,
 		timeptr->tm_min, timeptr->tm_sec,
 		year);
-	if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) {
+	if (buf != buf_asctime) {
+		if (STD_ASCTIME_BUF_SIZE <= strlen(result)) {
+			errno = EOVERFLOW;
+			return NULL;
+		}
 		(void) strcpy(buf, result);
-		return buf;
-	} else {
-		errno = EOVERFLOW;
-		return NULL;
 	}
+	return buf;
 }
 
 /*



More information about the tz mailing list