Skip to content

Commit bd0d3ac

Browse files
snoggegarlick
andauthored
tests: Handle various time_t sizes in printf (#124)
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. Co-authored-by: Jim Garlick <[email protected]>
1 parent 5215639 commit bd0d3ac

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

tests/kern/pathwalk.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <unistd.h>
2323
#include <stdlib.h>
2424
#include <stdint.h>
25+
#include <inttypes.h>
2526
#include <stdarg.h>
2627
#include <stdio.h>
2728
#include <string.h>
@@ -149,8 +150,8 @@ main (int argc, char *argv[])
149150
err_exit ("gettimeofday");
150151
timersub (&t2, &t1, &elapsed);
151152
if (!qflag)
152-
msg ("Created %d objects in %lu.%.3lus", length * files + 1,
153-
elapsed.tv_sec, elapsed.tv_usec / 1000);
153+
msg ("Created %d objects in %jd.%.3jds", length * files + 1,
154+
(intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
154155
}
155156

156157
/* Test
@@ -164,9 +165,9 @@ main (int argc, char *argv[])
164165
err_exit ("gettimeofday");
165166
timersub (&t2, &t1, &elapsed);
166167
if (!qflag)
167-
msg ("Found %d/%d files in %d directories in %lu.%.3lus",
168+
msg ("Found %d/%d files in %d directories in %jd.%.3jds",
168169
count, 1, length,
169-
elapsed.tv_sec, elapsed.tv_usec / 1000);
170+
(intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
170171
}
171172
if (gettimeofday (&t1, NULL) < 0)
172173
err_exit ("gettimeofday");
@@ -175,8 +176,8 @@ main (int argc, char *argv[])
175176
err_exit ("gettimeofday");
176177
timersub (&t2, &t1, &elapsed);
177178
if (!qflag)
178-
msg ("Found %d/%d files in %d directories in %lu.%.3lus",
179-
count, files, length, elapsed.tv_sec, elapsed.tv_usec / 1000);
179+
msg ("Found %d/%d files in %d directories in %jd.%.3jds",
180+
count, files, length, (intmax_t)elapsed.tv_sec, (intmax_t)elapsed.tv_usec / 1000);
180181
}
181182

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

198199
searchpath_destroy (searchpath, length);

0 commit comments

Comments
 (0)