mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-03 20:22:38 +00:00
Fix ctime/asctime and refactor r_asctime_r ##refactor (#17911)
This commit is contained in:
parent
26919b37f9
commit
d9000c0214
@ -2,6 +2,7 @@
|
||||
#define R2_TIME_H
|
||||
|
||||
#include <r_types.h>
|
||||
#include <time.h>
|
||||
|
||||
#define R_NSEC_PER_SEC 1000000000
|
||||
#define R_NSEC_PER_MSEC 1000000
|
||||
@ -22,8 +23,9 @@ R_API ut32 r_time_dos_time_stamp_to_posix(ut32 timeStamp);
|
||||
R_API bool r_time_stamp_is_dos_format(const ut32 certainPosixTimeStamp, const ut32 possiblePosixOrDosTimeStamp);
|
||||
R_API const char *r_time_to_string(ut64 ts);
|
||||
|
||||
// Cross platform asctime_r
|
||||
R_API char *r_asctime_r(const struct tm *tm, char *buf, size_t size);
|
||||
// Cross platform thread-safe time functions
|
||||
R_API char *r_asctime_r(const struct tm *tm, char *buf);
|
||||
R_API char *r_ctime_r(const time_t *timer, char *buf);
|
||||
|
||||
#define R_TIME_PROFILE_ENABLED 0
|
||||
|
||||
|
@ -54,7 +54,7 @@ typedef unsigned int ssize_t;
|
||||
typedef unsigned long unichar;
|
||||
|
||||
struct stat;
|
||||
const char *file_fmttime(unsigned int, int);
|
||||
const char *file_fmttime(unsigned int, int, char *);
|
||||
int file_buffer(struct r_magic_set *, int, const char *, const void *, size_t);
|
||||
int file_fsmagic(struct r_magic_set *, const char *, struct stat *);
|
||||
int file_pipe2file(struct r_magic_set *, int, const void *, size_t);
|
||||
|
@ -46,6 +46,7 @@
|
||||
#ifndef COMPILE_ONLY
|
||||
void file_mdump(struct r_magic *m) {
|
||||
static const char optyp[] = { FILE_OPS };
|
||||
char pp[ASCTIME_BUF_MINLEN];
|
||||
|
||||
(void) eprintf ("[%u", m->lineno);
|
||||
(void) eprintf ("%.*s %u", m->cont_level & 7, ">>>>>>>>", m->offset);
|
||||
@ -127,26 +128,26 @@ void file_mdump(struct r_magic *m) {
|
||||
case FILE_BEDATE:
|
||||
case FILE_MEDATE:
|
||||
(void)eprintf ("%s,",
|
||||
file_fmttime(m->value.l, 1));
|
||||
file_fmttime (m->value.l, 1, pp));
|
||||
break;
|
||||
case FILE_LDATE:
|
||||
case FILE_LELDATE:
|
||||
case FILE_BELDATE:
|
||||
case FILE_MELDATE:
|
||||
(void)eprintf ("%s,",
|
||||
file_fmttime(m->value.l, 0));
|
||||
file_fmttime (m->value.l, 0, pp));
|
||||
break;
|
||||
case FILE_QDATE:
|
||||
case FILE_LEQDATE:
|
||||
case FILE_BEQDATE:
|
||||
(void)eprintf ("%s,",
|
||||
file_fmttime((ut32)m->value.q, 1));
|
||||
file_fmttime ((ut32)m->value.q, 1, pp));
|
||||
break;
|
||||
case FILE_QLDATE:
|
||||
case FILE_LEQLDATE:
|
||||
case FILE_BEQLDATE:
|
||||
(void)eprintf ("%s,",
|
||||
file_fmttime((ut32)m->value.q, 0));
|
||||
file_fmttime ((ut32)m->value.q, 0, pp));
|
||||
break;
|
||||
case FILE_FLOAT:
|
||||
case FILE_BEFLOAT:
|
||||
@ -187,14 +188,13 @@ void file_magwarn(struct r_magic_set *ms, const char *f, ...) {
|
||||
(void) fputc('\n', stderr);
|
||||
}
|
||||
|
||||
const char *file_fmttime(ut32 v, int local) {
|
||||
char *pp;
|
||||
const char *file_fmttime(ut32 v, int local, char *pp) {
|
||||
time_t t = (time_t)v;
|
||||
struct tm *tm;
|
||||
struct tm timestruct;
|
||||
|
||||
if (local) {
|
||||
pp = ctime(&t);
|
||||
r_ctime_r (&t, pp);
|
||||
} else {
|
||||
#ifndef HAVE_DAYLIGHT
|
||||
static int daylight = 0;
|
||||
@ -203,8 +203,8 @@ const char *file_fmttime(ut32 v, int local) {
|
||||
|
||||
if (now == (time_t)0) {
|
||||
struct tm *tm1;
|
||||
(void)time(&now);
|
||||
tm1 = localtime(&now);
|
||||
(void)time (&now);
|
||||
tm1 = localtime (&now);
|
||||
if (!tm1)
|
||||
return "*Invalid time*";
|
||||
daylight = tm1->tm_isdst;
|
||||
@ -213,10 +213,10 @@ const char *file_fmttime(ut32 v, int local) {
|
||||
#endif /* HAVE_DAYLIGHT */
|
||||
if (daylight)
|
||||
t += 3600;
|
||||
tm = gmtime_r(&t, ×truct);
|
||||
tm = gmtime_r (&t, ×truct);
|
||||
if (!tm)
|
||||
return "*Invalid time*";
|
||||
pp = asctime (tm);
|
||||
r_asctime_r (tm, pp);
|
||||
}
|
||||
|
||||
pp[strcspn (pp, "\n")] = '\0';
|
||||
|
@ -38,7 +38,7 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "r_util/r_time.h"
|
||||
|
||||
static int match(RMagic *, struct r_magic *, ut32,
|
||||
const ut8 *, size_t, int);
|
||||
@ -316,6 +316,7 @@ static st32 mprint(RMagic *ms, struct r_magic *m) {
|
||||
ut64 t = 0;
|
||||
char *buf = NULL;
|
||||
union VALUETYPE *p = &ms->ms_value;
|
||||
char pp[ASCTIME_BUF_MINLEN];
|
||||
|
||||
switch (m->type) {
|
||||
case FILE_BYTE:
|
||||
@ -432,7 +433,7 @@ static st32 mprint(RMagic *ms, struct r_magic *m) {
|
||||
case FILE_BEDATE:
|
||||
case FILE_LEDATE:
|
||||
case FILE_MEDATE:
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime (p->l, 1)) == -1) {
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime (p->l, 1, pp)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
t = ms->offset + sizeof(time_t);
|
||||
@ -441,7 +442,7 @@ static st32 mprint(RMagic *ms, struct r_magic *m) {
|
||||
case FILE_BELDATE:
|
||||
case FILE_LELDATE:
|
||||
case FILE_MELDATE:
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime (p->l, 0)) == -1) {
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime (p->l, 0, pp)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
t = ms->offset + sizeof(time_t);
|
||||
@ -449,7 +450,7 @@ static st32 mprint(RMagic *ms, struct r_magic *m) {
|
||||
case FILE_QDATE:
|
||||
case FILE_BEQDATE:
|
||||
case FILE_LEQDATE:
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime ((ut32)p->q, 1)) == -1) {
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime ((ut32)p->q, 1, pp)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
t = ms->offset + sizeof(ut64);
|
||||
@ -457,7 +458,7 @@ static st32 mprint(RMagic *ms, struct r_magic *m) {
|
||||
case FILE_QLDATE:
|
||||
case FILE_BEQLDATE:
|
||||
case FILE_LEQLDATE:
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime ((ut32)p->q, 0)) == -1) {
|
||||
if (file_printf (ms, R_MAGIC_DESC, file_fmttime ((ut32)p->q, 0, pp)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
t = ms->offset + sizeof(ut64);
|
||||
|
@ -4,9 +4,6 @@
|
||||
#include "r_util.h"
|
||||
#include "r_util/r_print.h"
|
||||
#include "r_reg.h"
|
||||
#ifdef _MSC_VER
|
||||
#include <time.h>
|
||||
#endif
|
||||
#define NOPTR 0
|
||||
#define PTRSEEK 1
|
||||
#define PTRBACK 2
|
||||
@ -510,7 +507,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
|
||||
if (!timestr) {
|
||||
return;
|
||||
}
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr, ASCTIME_BUF_MINLEN);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr);
|
||||
*(timestr+24) = '\0';
|
||||
if (!SEEVALUE && !ISQUIET) {
|
||||
p->cb_printf ("0x%08" PFMT64x " = ", seeki + ((elem >= 0) ? elem * 4 : 0));
|
||||
@ -523,7 +520,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
|
||||
}
|
||||
while (size--) {
|
||||
updateAddr (buf + i, size - i, endian, &addr, NULL);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr, ASCTIME_BUF_MINLEN);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr);
|
||||
*(timestr+24) = '\0';
|
||||
if (elem == -1 || elem == 0) {
|
||||
p->cb_printf ("%s", timestr);
|
||||
@ -549,7 +546,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
|
||||
if (!timestr) {
|
||||
return;
|
||||
}
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr, ASCTIME_BUF_MINLEN);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr);
|
||||
*(timestr+24) = '\0';
|
||||
if (size==-1) {
|
||||
p->cb_printf ("\"%s\"", timestr);
|
||||
@ -557,7 +554,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
|
||||
p->cb_printf ("[ ");
|
||||
while (size--) {
|
||||
updateAddr (buf + i, size - i, endian, &addr, NULL);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr, ASCTIME_BUF_MINLEN);
|
||||
r_asctime_r (gmtime_r ((time_t*)&addr, ×truct), timestr);
|
||||
*(timestr+24) = '\0';
|
||||
if (elem == -1 || elem == 0) {
|
||||
p->cb_printf ("\"%s\"", timestr);
|
||||
|
@ -63,8 +63,9 @@ R_API char *r_time_stamp_to_str(ut32 timeStamp) {
|
||||
gettimeofday (&tv, (void*) &tz);
|
||||
gmtoff = (int) (tz.tz_minuteswest * 60); // in seconds
|
||||
ts += (time_t)gmtoff;
|
||||
char *res = strdup (ctime (&ts));
|
||||
char *res = malloc(ASCTIME_BUF_MINLEN);
|
||||
if (res) {
|
||||
ctime_r (&ts, res);
|
||||
r_str_trim (res); // XXX we probably need an r_str_trim_dup()
|
||||
}
|
||||
return res;
|
||||
@ -198,11 +199,20 @@ R_API const char *r_time_to_string (ut64 ts) {
|
||||
return r_time_stamp_to_str (l);
|
||||
}
|
||||
|
||||
R_API char *r_asctime_r(const struct tm *tm, char *buf, size_t size) {
|
||||
R_API char *r_asctime_r(const struct tm *tm, char *buf) {
|
||||
#if __WINDOWS__
|
||||
errno_t err = asctime_s (buf, size, tm);
|
||||
errno_t err = asctime_s (buf, ASCTIME_BUF_MINLEN, tm);
|
||||
return err? NULL: buf;
|
||||
#else
|
||||
return asctime_r (tm, buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
R_API char *r_ctime_r(const time_t *timer, char *buf) {
|
||||
#if __WINDOWS__
|
||||
errno_t err = ctime_s (buf, ASCTIME_BUF_MINLEN, timer);
|
||||
return err? NULL: buf;
|
||||
#else
|
||||
return ctime_r (timer, buf);
|
||||
#endif
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user