Skip to content

Commit f603ab5

Browse files
committed
Reuse the struct timespec based pieces in libmagic
1 parent 26b0385 commit f603ab5

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

ext/fileinfo/libmagic/cdf.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737

3838
#ifdef PHP_WIN32
3939
#include <winsock2.h>
40-
#define timespec timeval
41-
#define tv_nsec tv_usec
4240
#define asctime_r php_asctime_r
4341
#define ctime_r php_ctime_r
4442
#endif
@@ -283,9 +281,9 @@ typedef struct {
283281
cdf_catalog_entry_t cat_e[1];
284282
} cdf_catalog_t;
285283

286-
struct timeval;
287-
int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t);
288-
int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *);
284+
struct timespec;
285+
int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
286+
int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
289287
int cdf_read_header(const cdf_info_t *, cdf_header_t *);
290288
void cdf_swap_header(cdf_header_t *);
291289
void cdf_unpack_header(cdf_header_t *, char *);

ext/fileinfo/libmagic/cdf_time.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ cdf_getmonth(int year, int days)
9696
}
9797

9898
int
99-
cdf_timestamp_to_timespec(struct timeval *ts, cdf_timestamp_t t)
99+
cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
100100
{
101101
struct tm tm;
102102
#ifdef HAVE_STRUCT_TM_TM_ZONE
103103
static char UTC[] = "UTC";
104104
#endif
105105
int rdays;
106106

107-
/* XXX 5.14 at least introdced 100 ns intervals, this is to do */
108-
/* Time interval, in microseconds */
109-
ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC;
107+
/* Unit is 100's of nanoseconds */
108+
ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
110109

111110
t /= CDF_TIME_PREC;
112111
tm.tm_sec = (int)(t % 60);
@@ -145,7 +144,7 @@ cdf_timestamp_to_timespec(struct timeval *ts, cdf_timestamp_t t)
145144

146145
int
147146
/*ARGSUSED*/
148-
cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timeval *ts)
147+
cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
149148
{
150149
#ifndef __lint__
151150
(void)&t;
@@ -157,7 +156,7 @@ cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timeval *ts)
157156
errno = EINVAL;
158157
return -1;
159158
}
160-
*t = (ts->ts_usec / CDF_TIME_PREC) * CDF_TIME_PREC;
159+
*t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
161160
*t = tm.tm_sec;
162161
*t += tm.tm_min * 60;
163162
*t += tm.tm_hour * 60 * 60;
@@ -182,7 +181,7 @@ cdf_ctime(const time_t *sec, char *buf)
182181
int
183182
main(int argc, char *argv[])
184183
{
185-
struct timeval ts;
184+
struct timespec ts;
186185
char buf[25];
187186
static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
188187
static const char *ref = "Sat Apr 23 01:30:00 1977";

ext/fileinfo/libmagic/print.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ file_fmttime(uint64_t v, int flags, char *buf)
244244
struct tm *tm = NULL;
245245

246246
if (flags & FILE_T_WINDOWS) {
247-
struct timeval ts;
248-
cdf_timestamp_to_timespec(&ts, t);
247+
struct timespec ts;
248+
cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
249249
t = ts.tv_sec;
250250
} else {
251251
// XXX: perhaps detect and print something if overflow

ext/fileinfo/libmagic/readcdf.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
133133
{
134134
size_t i;
135135
cdf_timestamp_t tp;
136-
struct timeval ts;
136+
struct timespec ts;
137137
char buf[64];
138138
const char *str = NULL;
139139
const char *s, *e;
@@ -220,11 +220,8 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
220220
return -1;
221221
} else {
222222
char *c, *ec;
223-
const time_t sec = ts.tv_sec;
224-
if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
225-
return -1;
226-
}
227-
c = cdf_ctime(&sec, tbuf);
223+
cdf_timestamp_to_timespec(&ts, tp);
224+
c = cdf_ctime(&ts.tv_sec, tbuf);
228225
if (c != NULL &&
229226
(ec = strchr(c, '\n')) != NULL)
230227
*ec = '\0';

0 commit comments

Comments
 (0)