]> granicus.if.org Git - file/commitdiff
use ctime_r, asctime_r.
authorChristos Zoulas <christos@zoulas.com>
Tue, 15 May 2012 17:14:36 +0000 (17:14 +0000)
committerChristos Zoulas <christos@zoulas.com>
Tue, 15 May 2012 17:14:36 +0000 (17:14 +0000)
ChangeLog
configure.ac
src/asctime_r.c [new file with mode: 0644]
src/cdf.h
src/cdf_time.c
src/ctime_r.c [new file with mode: 0644]
src/file.h
src/print.c
src/readcdf.c
src/softmagic.c

index 31fac6f12f9afcfa8d68f62891454dc311d780d6..31b9655300f84a685c4400d3f37a1c61ae768917 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-05-15  13:12  Christos Zoulas <christos@zoulas.com>
+
+       * use ctime_r, asctime_r
+
 2012-04-06  17:18  Christos Zoulas <christos@zoulas.com>
 
        * Fixes for indirect offsets to handle apple disk formats
index 1511c9ac33cb27607132882162036e0d3ce09235..29d0893bc8856151ddabaea4a70b2e6d2d52503c 100644 (file)
@@ -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 (file)
index 0000000..3a0ec04
--- /dev/null
@@ -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 <time.h>
+#include <string.h>
+
+/* 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;
+}
index 6fe2646e935d91599f35a8bc69eb3c2d0a1ecea6..22e1577422726d2d245f547941cdf719115a5c20 100644 (file)
--- 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 *);
index 5061b46cd3120af5aa3733bf65690337605a0de2..674e1a41d09ae65baa7fd862ef9273c6c2157832 100644 (file)
@@ -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 <time.h>
@@ -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 (file)
index 0000000..ca7ad2f
--- /dev/null
@@ -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 <time.h>
+#include <string.h>
+
+/* 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;
+}
index fa1371fedd5caa35ddf95ce6ae0b27d31d4f7aef..b0f9e9dbfc964d8e312573077bc5bd5f9ba68c75 100644 (file)
@@ -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 <inttypes.h>
 #endif
 #include <regex.h>
+#include <time.h>
 #include <sys/types.h>
 #include <sys/param.h>
 /* 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
index 9ac4ce5daf24a99f84e8d4c9025524af0fcbd011..38377a3ffd9b01e4e3d58f4a51117fcdfacee7f0 100644 (file)
@@ -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 <string.h>
@@ -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*");
 }
index 6d8db784c946fdf3f426dd4a4af830bd134999ff..a4e12fb9d634c70fb7df73a1fccb4fabb3d4f352 100644 (file)
@@ -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 <stdlib.h>
@@ -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';
 
index e5aeeea4e65bdcb458f40b55d38f8cdb570888e3..ae0532a5386321150513dd0843664d5ffd465a09 100644 (file)
@@ -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;