From: Christos Zoulas Date: Tue, 15 May 2012 17:14:36 +0000 (+0000) Subject: use ctime_r, asctime_r. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ba82f469b2b677736770f55807c17c65dbecae16;p=file use ctime_r, asctime_r. --- diff --git a/ChangeLog b/ChangeLog index 31fac6f1..31b96553 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2012-05-15 13:12 Christos Zoulas + + * use ctime_r, asctime_r + 2012-04-06 17:18 Christos Zoulas * Fixes for indirect offsets to handle apple disk formats diff --git a/configure.ac b/configure.ac index 1511c9ac..29d0893b 100644 --- a/configure.ac +++ b/configure.ac @@ -159,7 +159,7 @@ dnl Checks for functions AC_CHECK_FUNCS(mmap strerror strndup strtoul mbrtowc mkstemp utimes utime wcwidth strtof fork) dnl Provide implementation of some required functions if necessary -AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline) +AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline ctime_r asctime_r) dnl Checks for libraries AC_CHECK_LIB(z,gzopen) diff --git a/src/asctime_r.c b/src/asctime_r.c new file mode 100644 index 00000000..3a0ec044 --- /dev/null +++ b/src/asctime_r.c @@ -0,0 +1,19 @@ +/* $File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */ + +#include "file.h" +#ifndef lint +FILE_RCSID("@(#)$File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $") +#endif /* lint */ +#include +#include + +/* asctime_r is not thread-safe anyway */ +char * +asctime_r(const struct tm *t, char *dst) +{ + char *p = asctime(t); + if (p == NULL) + return NULL; + memcpy(dst, p, 26); + return dst; +} diff --git a/src/cdf.h b/src/cdf.h index 6fe2646e..22e15774 100644 --- a/src/cdf.h +++ b/src/cdf.h @@ -308,7 +308,7 @@ int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); uint16_t cdf_tole2(uint16_t); uint32_t cdf_tole4(uint32_t); uint64_t cdf_tole8(uint64_t); -char *cdf_ctime(const time_t *); +char *cdf_ctime(const time_t *, char *); #ifdef CDF_DEBUG void cdf_dump_header(const cdf_header_t *); diff --git a/src/cdf_time.c b/src/cdf_time.c index 5061b46c..674e1a41 100644 --- a/src/cdf_time.c +++ b/src/cdf_time.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: cdf_time.c,v 1.11 2011/12/13 13:48:41 christos Exp $") +FILE_RCSID("@(#)$File: cdf_time.c,v 1.12 2012/05/15 17:14:36 christos Exp $") #endif #include @@ -166,15 +166,13 @@ cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts) } char * -cdf_ctime(const time_t *sec) +cdf_ctime(const time_t *sec, char *buf) { - static char ctbuf[26]; - char *ptr = ctime(sec); + char *ptr = ctime_r(sec, buf); if (ptr != NULL) - return ptr; - (void)snprintf(ctbuf, sizeof(ctbuf), "*Bad* 0x%16.16llx\n", - (long long)*sec); - return ctbuf; + return buf; + (void)snprintf(buf, 26, "*Bad* 0x%16.16llx\n", (long long)*sec); + return buf; } @@ -183,12 +181,13 @@ int main(int argc, char *argv[]) { struct timespec ts; + char buf[25]; static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL; static const char *ref = "Sat Apr 23 01:30:00 1977"; char *p, *q; cdf_timestamp_to_timespec(&ts, tst); - p = cdf_ctime(&ts.tv_sec); + p = cdf_ctime(&ts.tv_sec, buf); if ((q = strchr(p, '\n')) != NULL) *q = '\0'; if (strcmp(ref, p) != 0) diff --git a/src/ctime_r.c b/src/ctime_r.c new file mode 100644 index 00000000..ca7ad2fc --- /dev/null +++ b/src/ctime_r.c @@ -0,0 +1,19 @@ +/* $File: ctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */ + +#include "file.h" +#ifndef lint +FILE_RCSID("@(#)$File: ctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $") +#endif /* lint */ +#include +#include + +/* ctime_r is not thread-safe anyway */ +char * +ctime_r(const time_t *t, char *dst) +{ + char *p = ctime(t); + if (p == NULL) + return NULL; + memcpy(dst, p, 26); + return dst; +} diff --git a/src/file.h b/src/file.h index fa1371fe..b0f9e9db 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.136 2012/04/03 22:25:07 christos Exp $ + * @(#)$File: file.h,v 1.137 2012/05/15 17:14:36 christos Exp $ */ #ifndef __file_h__ @@ -62,6 +62,7 @@ #include #endif #include +#include #include #include /* Do this here and now, because struct stat gets re-defined on solaris */ @@ -391,7 +392,7 @@ typedef unsigned long unichar; struct stat; #define FILE_T_LOCAL 1 #define FILE_T_WINDOWS 2 -protected const char *file_fmttime(uint64_t, int); +protected const char *file_fmttime(uint64_t, int, char *); protected int file_buffer(struct magic_set *, int, const char *, const void *, size_t); protected int file_fsmagic(struct magic_set *, const char *, struct stat *); @@ -482,6 +483,12 @@ size_t strlcat(char *dst, const char *src, size_t siz); ssize_t getline(char **dst, size_t *len, FILE *fp); ssize_t getdelim(char **dst, size_t *len, int delimiter, FILE *fp); #endif +#ifndef HAVE_CTIME_R +char *ctime_r(const time_t *, char *); +#endif +#ifndef HAVE_ASCTIME_R +char *asctime_r(const struct tm *, char *); +#endif #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) #define QUICK diff --git a/src/print.c b/src/print.c index 9ac4ce5d..38377a3f 100644 --- a/src/print.c +++ b/src/print.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: print.c,v 1.73 2012/05/07 18:23:11 christos Exp $") +FILE_RCSID("@(#)$File: print.c,v 1.74 2012/05/15 17:14:36 christos Exp $") #endif /* lint */ #include @@ -51,7 +51,8 @@ FILE_RCSID("@(#)$File: print.c,v 1.73 2012/05/07 18:23:11 christos Exp $") protected void file_mdump(struct magic *m) { - private const char optyp[] = { FILE_OPS }; + static const char optyp[] = { FILE_OPS }; + char tbuf[26]; (void) fprintf(stderr, "%u: %.*s %u", m->lineno, (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); @@ -155,31 +156,31 @@ file_mdump(struct magic *m) case FILE_BEDATE: case FILE_MEDATE: (void)fprintf(stderr, "%s,", - file_fmttime(m->value.l, FILE_T_LOCAL)); + file_fmttime(m->value.l, FILE_T_LOCAL, tbuf)); break; case FILE_LDATE: case FILE_LELDATE: case FILE_BELDATE: case FILE_MELDATE: (void)fprintf(stderr, "%s,", - file_fmttime(m->value.l, 0)); + file_fmttime(m->value.l, 0, tbuf)); case FILE_QDATE: case FILE_LEQDATE: case FILE_BEQDATE: (void)fprintf(stderr, "%s,", - file_fmttime(m->value.q, FILE_T_LOCAL)); + file_fmttime(m->value.q, FILE_T_LOCAL, tbuf)); break; case FILE_QLDATE: case FILE_LEQLDATE: case FILE_BEQLDATE: (void)fprintf(stderr, "%s,", - file_fmttime(m->value.q, 0)); + file_fmttime(m->value.q, 0, tbuf)); break; case FILE_QWDATE: case FILE_LEQWDATE: case FILE_BEQWDATE: (void)fprintf(stderr, "%s,", - file_fmttime(m->value.q, FILE_T_WINDOWS)); + file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf)); break; case FILE_FLOAT: case FILE_BEFLOAT: @@ -223,7 +224,7 @@ file_magwarn(struct magic_set *ms, const char *f, ...) } protected const char * -file_fmttime(uint64_t v, int flags) +file_fmttime(uint64_t v, int flags, char *buf) { char *pp; time_t t = (time_t)v; @@ -236,7 +237,7 @@ file_fmttime(uint64_t v, int flags) } if (flags & FILE_T_LOCAL) { - pp = ctime(&t); + pp = ctime_r(&t, buf); } else { #ifndef HAVE_DAYLIGHT private int daylight = 0; @@ -258,7 +259,7 @@ file_fmttime(uint64_t v, int flags) tm = gmtime(&t); if (tm == NULL) goto out; - pp = asctime(tm); + pp = asctime_r(tm, buf); } if (pp == NULL) @@ -266,5 +267,5 @@ file_fmttime(uint64_t v, int flags) pp[strcspn(pp, "\n")] = '\0'; return pp; out: - return "*Invalid time*"; + return strcpy(buf, "*Invalid time*"); } diff --git a/src/readcdf.c b/src/readcdf.c index 6d8db784..a4e12fb9 100644 --- a/src/readcdf.c +++ b/src/readcdf.c @@ -26,7 +26,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readcdf.c,v 1.30 2012/03/03 15:44:58 christos Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.31 2012/05/15 17:14:36 christos Exp $") #endif #include @@ -125,8 +125,8 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info, case CDF_FILETIME: tp = info[i].pi_tp; if (tp != 0) { + char tbuf[64]; if (tp < 1000000000000000LL) { - char tbuf[64]; cdf_print_elapsed_time(tbuf, sizeof(tbuf), tp); if (NOTMIME(ms) && file_printf(ms, @@ -135,7 +135,7 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info, } else { char *c, *ec; cdf_timestamp_to_timespec(&ts, tp); - c = cdf_ctime(&ts.tv_sec); + c = cdf_ctime(&ts.tv_sec, tbuf); if ((ec = strchr(c, '\n')) != NULL) *ec = '\0'; diff --git a/src/softmagic.c b/src/softmagic.c index e5aeeea4..ae0532a5 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.149 2012/04/06 21:15:54 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.150 2012/05/15 17:14:36 christos Exp $") #endif /* lint */ #include "magic.h" @@ -363,7 +363,7 @@ mprint(struct magic_set *ms, struct magic *m) float vf; double vd; int64_t t = 0; - char buf[128]; + char buf[128], tbuf[26]; union VALUETYPE *p = &ms->ms_value; switch (m->type) { @@ -462,8 +462,8 @@ mprint(struct magic_set *ms, struct magic *m) case FILE_BEDATE: case FILE_LEDATE: case FILE_MEDATE: - if (file_printf(ms, m->desc, file_fmttime(p->l, - FILE_T_LOCAL)) == -1) + if (file_printf(ms, m->desc, file_fmttime(p->l, FILE_T_LOCAL, + tbuf)) == -1) return -1; t = ms->offset + sizeof(uint32_t); break; @@ -472,7 +472,7 @@ mprint(struct magic_set *ms, struct magic *m) case FILE_BELDATE: case FILE_LELDATE: case FILE_MELDATE: - if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1) + if (file_printf(ms, m->desc, file_fmttime(p->l, 0, tbuf)) == -1) return -1; t = ms->offset + sizeof(uint32_t); break; @@ -480,8 +480,8 @@ mprint(struct magic_set *ms, struct magic *m) case FILE_QDATE: case FILE_BEQDATE: case FILE_LEQDATE: - if (file_printf(ms, m->desc, file_fmttime(p->q, - FILE_T_LOCAL)) == -1) + if (file_printf(ms, m->desc, file_fmttime(p->q, FILE_T_LOCAL, + tbuf)) == -1) return -1; t = ms->offset + sizeof(uint64_t); break; @@ -489,7 +489,7 @@ mprint(struct magic_set *ms, struct magic *m) case FILE_QLDATE: case FILE_BEQLDATE: case FILE_LEQLDATE: - if (file_printf(ms, m->desc, file_fmttime(p->q, 0)) == -1) + if (file_printf(ms, m->desc, file_fmttime(p->q, 0, tbuf)) == -1) return -1; t = ms->offset + sizeof(uint64_t); break; @@ -497,8 +497,8 @@ mprint(struct magic_set *ms, struct magic *m) case FILE_QWDATE: case FILE_BEQWDATE: case FILE_LEQWDATE: - if (file_printf(ms, m->desc, file_fmttime(p->q, - FILE_T_WINDOWS)) == -1) + if (file_printf(ms, m->desc, file_fmttime(p->q, FILE_T_WINDOWS, + tbuf)) == -1) return -1; t = ms->offset + sizeof(uint64_t); break;