Module Name: src
Committed By: christos
Date: Tue Apr 6 13:35:52 UTC 2021
Modified Files:
src/bin/ps: print.c
Log Message:
- dedup code
- add a safe_strftime() to handle error cases
To generate a diff of this commit:
cvs rdiff -u -r1.133 -r1.134 src/bin/ps/print.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/bin/ps/print.c
diff -u src/bin/ps/print.c:1.133 src/bin/ps/print.c:1.134
--- src/bin/ps/print.c:1.133 Tue Apr 6 01:13:24 2021
+++ src/bin/ps/print.c Tue Apr 6 09:35:52 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: print.c,v 1.133 2021/04/06 05:13:24 simonb Exp $ */
+/* $NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $ */
/*
* Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
#if 0
static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
#else
-__RCSID("$NetBSD: print.c,v 1.133 2021/04/06 05:13:24 simonb Exp $");
+__RCSID("$NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $");
#endif
#endif /* not lint */
@@ -109,6 +109,14 @@ static time_t now;
#define LSDEAD 6
#endif
+static void __attribute__((__format__(__strftime__, 3, 0)))
+safe_strftime(char *buf, size_t bufsiz, const char *fmt,
+ const struct tm *tp)
+{
+ if (tp == NULL || strftime(buf, bufsiz, fmt, tp) == 0)
+ strlcpy(buf, "-", sizeof(buf));
+}
+
static int
iwidth(u_int64_t v)
{
@@ -793,12 +801,12 @@ started(struct pinfo *pi, VARENT *ve, en
(void)time(&now);
if (now - k->p_ustart_sec < SECSPERDAY)
/* I *hate* SCCS... */
- (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
+ safe_strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
/* I *hate* SCCS... */
- (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
+ safe_strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
else
- (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
+ safe_strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
/* %e and %l can start with a space. */
cp = buf;
if (*cp == ' ')
@@ -829,20 +837,17 @@ lstarted(struct pinfo *pi, VARENT *ve, e
* XXX: The hardcoded "STARTED" string. Better or
* worse than a "<= 7" or some other arbitary number?
*/
- if (v->width <= (int)strlen("STARTED")) {
- (void)strftime(buf, sizeof(buf) -1, "%c",
- localtime(&startt));
- strprintorsetwidth(v, buf, mode);
+ if (v->width > (int)sizeof("STARTED") - 1) {
+ return;
}
} else {
if (!k->p_uvalid) {
(void)printf("%*s", v->width, "-");
- } else {
- (void)strftime(buf, sizeof(buf) -1, "%c",
- localtime(&startt));
- strprintorsetwidth(v, buf, mode);
+ return;
}
}
+ safe_strftime(buf, sizeof(buf) - 1, "%c", localtime(&startt));
+ strprintorsetwidth(v, buf, mode);
}
void