Skip to content

Commit

Permalink
tests: Handle various time_t sizes in printf
Browse files Browse the repository at this point in the history
The members of the timeval struct are both signed (defined by POSIX)
and typically both 64 bits on a system where time_t is 64 bits.  This
is possible also on 32 bit systems where time_t is larger to handle
the 2038 problem.

It's practically impossible to find a type and printf format that
works even on all glibc systems.  Play it safe and always use printf
with intmax_t.
  • Loading branch information
snogge committed Jan 17, 2025
1 parent 597be9d commit 9eefd2d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tests/kern/pathwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -149,8 +150,8 @@ main (int argc, char *argv[])
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
if (!qflag)
msg ("Created %d objects in %lu.%.3lus", length * files + 1,
elapsed.tv_sec, elapsed.tv_usec / 1000);
msg ("Created %d objects in %jd.%.3jds", length * files + 1,
(intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
}

/* Test
Expand All @@ -164,9 +165,9 @@ main (int argc, char *argv[])
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
if (!qflag)
msg ("Found %d/%d files in %d directories in %lu.%.3lus",
msg ("Found %d/%d files in %d directories in %jd.%.3jds",
count, 1, length,
elapsed.tv_sec, elapsed.tv_usec / 1000);
(intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
}
if (gettimeofday (&t1, NULL) < 0)
err_exit ("gettimeofday");
Expand All @@ -175,8 +176,8 @@ main (int argc, char *argv[])
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
if (!qflag)
msg ("Found %d/%d files in %d directories in %lu.%.3lus",
count, files, length, elapsed.tv_sec, elapsed.tv_usec / 1000);
msg ("Found %d/%d files in %d directories in %jd.%.3jds",
count, files, length, (intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
}

/* Remove root + fs objects.
Expand All @@ -191,8 +192,8 @@ main (int argc, char *argv[])
err_exit ("gettimeofday");
timersub (&t2, &t1, &elapsed);
if (!qflag)
msg ("Removed %d objects in %lu.%.3lus", length * files + 1,
elapsed.tv_sec, elapsed.tv_usec / 1000);
msg ("Removed %d objects in %jd.%.3jds", length * files + 1,
(intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
}

searchpath_destroy (searchpath, length);
Expand Down

0 comments on commit 9eefd2d

Please sign in to comment.