]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util-print.c
Makefile.in: don't remove configure and config.h.in in make distclean.
[tcpdump] / util-print.c
index 6d408d15d4ad772a74b01712f44c10118185cdbf..f20139f2db2cc97d3d266ad2ef017148691cab72 100644 (file)
@@ -247,7 +247,8 @@ ts_date_hmsfrac_print(netdissect_options *ndo, long sec, long usec,
 {
        time_t Time = sec;
        struct tm *tm;
-       char timestr[32];
+       char timebuf[32];
+       const char *timestr;
 
        if ((unsigned)sec & 0x80000000) {
                ND_PRINT("[Error converting time]");
@@ -259,14 +260,13 @@ ts_date_hmsfrac_print(netdissect_options *ndo, long sec, long usec,
        else
                tm = gmtime(&Time);
 
-       if (!tm) {
-               ND_PRINT("[Error converting time]");
-               return;
+       if (date_flag == WITH_DATE) {
+               timestr = nd_format_time(timebuf, sizeof(timebuf),
+                   "%Y-%m-%d %H:%M:%S", tm);
+       } else {
+               timestr = nd_format_time(timebuf, sizeof(timebuf),
+                   "%H:%M:%S", tm);
        }
-       if (date_flag == WITH_DATE)
-               strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
-       else
-               strftime(timestr, sizeof(timestr), "%H:%M:%S", tm);
        ND_PRINT("%s", timestr);
 
        ts_frac_print(ndo, usec);
@@ -422,6 +422,26 @@ signed_relts_print(netdissect_options *ndo,
        unsigned_relts_print(ndo, secs);
 }
 
+/*
+ * Format a struct tm with strftime().
+ * If the pointer to the struct tm is null, that means that the
+ * routine to convert a time_t to a struct tm failed; the localtime()
+ * and gmtime() in the Microsoft Visual Studio C library will fail,
+ * returning null, if the value is before the UNIX Epoch.
+ */
+const char *
+nd_format_time(char *buf, size_t bufsize, const char *format,
+         const struct tm *timeptr)
+{
+       if (timeptr != NULL) {
+               if (strftime(buf, bufsize, format, timeptr) != 0)
+                       return (buf);
+               else
+                       return ("[nd_format_time() buffer is too small]");
+       } else
+               return ("[localtime() or gmtime() couldn't convert the date and time]");
+}
+
 /* Print the truncated string */
 void nd_print_trunc(netdissect_options *ndo)
 {