From c15a4a87f37bfc715b8e045221a957ae1d19cb99 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Fri, 27 Apr 2001 23:59:51 +0000 Subject: [PATCH] Always enable quick mode, even if we don't have mmap (christos) Autoconf help for mmap (Alexander Mai) --- Makefile.am | 2 +- Makefile.in | 2 +- config.h.in | 6 +++ configure | 114 ++++++++++++++++++++++++++++++++--------------- configure.in | 3 +- src/apprentice.c | 58 +++++++++++++++++------- src/file.c | 4 +- src/file.h | 6 ++- 8 files changed, 135 insertions(+), 60 deletions(-) diff --git a/Makefile.am b/Makefile.am index 5e03a74c..0daa200b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,7 +6,7 @@ bin_PROGRAMS = file data_DATA = magic magic.mime magic.mgc MAGIC = @datadir@/magic -CPPFLAGS = -DMAGIC='"$(MAGIC)"' -DQUICK +CPPFLAGS = -DMAGIC='"$(MAGIC)"' man_MANS = file.1 magic.4 diff --git a/Makefile.in b/Makefile.in index 851b0d95..5bbdd24d 100644 --- a/Makefile.in +++ b/Makefile.in @@ -72,7 +72,7 @@ bin_PROGRAMS = file data_DATA = magic magic.mime magic.mgc MAGIC = @datadir@/magic -CPPFLAGS = -DMAGIC='"$(MAGIC)"' -DQUICK +CPPFLAGS = -DMAGIC='"$(MAGIC)"' man_MANS = file.1 magic.4 diff --git a/config.h.in b/config.h.in index 116676b7..60dfc564 100644 --- a/config.h.in +++ b/config.h.in @@ -58,6 +58,9 @@ /* The number of bytes in a uint64_t. */ #define SIZEOF_UINT64_T 0 +/* Define if you have the mmap function. */ +#undef HAVE_MMAP + /* Define if you have the strerror function. */ #undef HAVE_STRERROR @@ -67,6 +70,9 @@ /* Define if you have the header file. */ #undef HAVE_LOCALE_H +/* Define if you have the header file. */ +#undef HAVE_SYS_MMAN_H + /* Define if you have the header file. */ #undef HAVE_UNISTD_H diff --git a/configure b/configure index fae71eff..6257b858 100755 --- a/configure +++ b/configure @@ -1553,14 +1553,54 @@ else fi done +for ac_hdr in sys/mman.h +do +ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'` +echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6 +echo "configure:1561: checking for $ac_hdr" >&5 +if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then + echo $ac_n "(cached) $ac_c" 1>&6 +else + cat > conftest.$ac_ext < +EOF +ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" +{ (eval echo configure:1571: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` +if test -z "$ac_err"; then + rm -rf conftest* + eval "ac_cv_header_$ac_safe=yes" +else + echo "$ac_err" >&5 + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + eval "ac_cv_header_$ac_safe=no" +fi +rm -f conftest* +fi +if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then + echo "$ac_t""yes" 1>&6 + ac_tr_hdr=HAVE_`echo $ac_hdr | sed 'y%abcdefghijklmnopqrstuvwxyz./-%ABCDEFGHIJKLMNOPQRSTUVWXYZ___%'` + cat >> confdefs.h <&6 +fi +done + echo $ac_n "checking for working const""... $ac_c" 1>&6 -echo "configure:1559: checking for working const" >&5 +echo "configure:1599: checking for working const" >&5 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1653: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_c_const=yes else @@ -1630,12 +1670,12 @@ EOF fi echo $ac_n "checking for off_t""... $ac_c" 1>&6 -echo "configure:1634: checking for off_t" >&5 +echo "configure:1674: checking for off_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_off_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1663,12 +1703,12 @@ EOF fi echo $ac_n "checking for size_t""... $ac_c" 1>&6 -echo "configure:1667: checking for size_t" >&5 +echo "configure:1707: checking for size_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_size_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1696,12 +1736,12 @@ EOF fi echo $ac_n "checking for st_rdev in struct stat""... $ac_c" 1>&6 -echo "configure:1700: checking for st_rdev in struct stat" >&5 +echo "configure:1740: checking for st_rdev in struct stat" >&5 if eval "test \"`echo '$''{'ac_cv_struct_st_rdev'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #include @@ -1709,7 +1749,7 @@ int main() { struct stat s; s.st_rdev; ; return 0; } EOF -if { (eval echo configure:1713: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1753: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_struct_st_rdev=yes else @@ -1731,12 +1771,12 @@ fi echo $ac_n "checking for uint8_t""... $ac_c" 1>&6 -echo "configure:1735: checking for uint8_t" >&5 +echo "configure:1775: checking for uint8_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_uint8_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1764,12 +1804,12 @@ EOF fi echo $ac_n "checking for uint16_t""... $ac_c" 1>&6 -echo "configure:1768: checking for uint16_t" >&5 +echo "configure:1808: checking for uint16_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_uint16_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1797,12 +1837,12 @@ EOF fi echo $ac_n "checking for uint32_t""... $ac_c" 1>&6 -echo "configure:1801: checking for uint32_t" >&5 +echo "configure:1841: checking for uint32_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_uint32_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1831,7 +1871,7 @@ fi echo $ac_n "checking for long long""... $ac_c" 1>&6 -echo "configure:1835: checking for long long" >&5 +echo "configure:1875: checking for long long" >&5 if eval "test \"`echo '$''{'ac_cv_c_long_long'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -1842,13 +1882,13 @@ if test "$cross_compiling" = yes; then { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:1892: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_c_long_long=yes else @@ -1877,12 +1917,12 @@ else long64='unsigned long'; fi echo $ac_n "checking for uint64_t""... $ac_c" 1>&6 -echo "configure:1881: checking for uint64_t" >&5 +echo "configure:1921: checking for uint64_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_uint64_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1913,7 +1953,7 @@ fi echo $ac_n "checking size of uint8_t""... $ac_c" 1>&6 -echo "configure:1917: checking size of uint8_t" >&5 +echo "configure:1957: checking size of uint8_t" >&5 if eval "test \"`echo '$''{'ac_cv_sizeof_uint8_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -1921,7 +1961,7 @@ else ac_cv_sizeof_uint8_t=0 else cat > conftest.$ac_ext < #ifdef STDC_HEADERS @@ -1937,7 +1977,7 @@ main() exit(0); } EOF -if { (eval echo configure:1941: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:1981: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_sizeof_uint8_t=`cat conftestval` else @@ -1958,7 +1998,7 @@ EOF echo $ac_n "checking size of uint16_t""... $ac_c" 1>&6 -echo "configure:1962: checking size of uint16_t" >&5 +echo "configure:2002: checking size of uint16_t" >&5 if eval "test \"`echo '$''{'ac_cv_sizeof_uint16_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -1966,7 +2006,7 @@ else ac_cv_sizeof_uint16_t=0 else cat > conftest.$ac_ext < #ifdef STDC_HEADERS @@ -1982,7 +2022,7 @@ main() exit(0); } EOF -if { (eval echo configure:1986: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:2026: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_sizeof_uint16_t=`cat conftestval` else @@ -2003,7 +2043,7 @@ EOF echo $ac_n "checking size of uint32_t""... $ac_c" 1>&6 -echo "configure:2007: checking size of uint32_t" >&5 +echo "configure:2047: checking size of uint32_t" >&5 if eval "test \"`echo '$''{'ac_cv_sizeof_uint32_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -2011,7 +2051,7 @@ else ac_cv_sizeof_uint32_t=0 else cat > conftest.$ac_ext < #ifdef STDC_HEADERS @@ -2027,7 +2067,7 @@ main() exit(0); } EOF -if { (eval echo configure:2031: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:2071: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_sizeof_uint32_t=`cat conftestval` else @@ -2048,7 +2088,7 @@ EOF echo $ac_n "checking size of uint64_t""... $ac_c" 1>&6 -echo "configure:2052: checking size of uint64_t" >&5 +echo "configure:2092: checking size of uint64_t" >&5 if eval "test \"`echo '$''{'ac_cv_sizeof_uint64_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -2056,7 +2096,7 @@ else ac_cv_sizeof_uint64_t=0 else cat > conftest.$ac_ext < #ifdef STDC_HEADERS @@ -2072,7 +2112,7 @@ main() exit(0); } EOF -if { (eval echo configure:2076: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:2116: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_sizeof_uint64_t=`cat conftestval` else @@ -2093,15 +2133,15 @@ EOF -for ac_func in strerror strtoul +for ac_func in mmap strerror strtoul do echo $ac_n "checking for $ac_func""... $ac_c" 1>&6 -echo "configure:2100: checking for $ac_func" >&5 +echo "configure:2140: checking for $ac_func" >&5 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then +if { (eval echo configure:2168: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then rm -rf conftest* eval "ac_cv_func_$ac_func=yes" else diff --git a/configure.in b/configure.in index ca437c4b..ccd84ca1 100644 --- a/configure.in +++ b/configure.in @@ -42,6 +42,7 @@ AC_HEADER_MAJOR AC_HEADER_SYS_WAIT AC_CHECK_HEADERS(unistd.h) AC_CHECK_HEADERS(locale.h) +AC_CHECK_HEADERS(sys/mman.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST @@ -69,6 +70,6 @@ AC_CHECK_SIZEOF_STDC_HEADERS(uint32_t, 0) AC_CHECK_SIZEOF_STDC_HEADERS(uint64_t, 0) dnl Checks for functions -AC_CHECK_FUNCS(strerror strtoul) +AC_CHECK_FUNCS(mmap strerror strtoul) AC_OUTPUT(Makefile) diff --git a/src/apprentice.c b/src/apprentice.c index 4cc703d7..563346d0 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -25,6 +25,7 @@ * 4. This notice may not be removed or altered. */ +#include "file.h" #include #include #include @@ -32,13 +33,13 @@ #include #ifdef QUICK #include +#include #include #include #endif -#include "file.h" #ifndef lint -FILE_RCSID("@(#)$Id: apprentice.c,v 1.39 2001/04/24 14:40:24 christos Exp $") +FILE_RCSID("@(#)$Id: apprentice.c,v 1.40 2001/04/27 23:59:51 christos Exp $") #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -78,17 +79,15 @@ static void eatsize __P((char **)); static int apprentice_1 __P((const char *, int)); static int apprentice_file __P((struct magic **, uint32 *, const char *, int)); -#ifdef QUICK static void byteswap __P((struct magic *, uint32)); static void bs1 __P((struct magic *)); static uint16 swap2 __P((uint16)); static uint32 swap4 __P((uint32)); -static char * mkdbname __P((const char *)); +static char *mkdbname __P((const char *)); static int apprentice_map __P((struct magic **, uint32 *, const char *, int)); static int apprentice_compile __P((struct magic **, uint32 *, const char *, int)); -#endif static int maxmagic = 0; @@ -108,7 +107,6 @@ apprentice_1(fn, action) struct mlist *ml; int rv = -1; -#ifdef QUICK if (action == COMPILE) { rv = apprentice_file(&magic, &nmagic, fn, action); if (rv == 0) @@ -119,7 +117,6 @@ apprentice_1(fn, action) if ((rv = apprentice_map(&magic, &nmagic, fn, action)) != 0) (void)fprintf(stderr, "%s: Using regular magic file `%s'\n", progname, fn); -#endif if (rv != 0) rv = apprentice_file(&magic, &nmagic, fn, action); @@ -128,7 +125,8 @@ apprentice_1(fn, action) return rv; if ((ml = malloc(sizeof(*ml))) == NULL) { - (void) fprintf(stderr, "%s: Out of memory.\n", progname); + (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname, + strerror(errno)); if (action == CHECK) return -1; } @@ -159,7 +157,8 @@ apprentice(fn, action) mlist.next = mlist.prev = &mlist; mfn = malloc(strlen(fn)+1); if (mfn == NULL) { - (void) fprintf(stderr, "%s: Out of memory.\n", progname); + (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname, + strerror(errno)); if (action == CHECK) return -1; else @@ -214,7 +213,8 @@ apprentice_file(magicp, nmagicp, fn, action) maxmagic = MAXMAGIS; *magicp = (struct magic *) calloc(sizeof(struct magic), maxmagic); if (*magicp == NULL) { - (void) fprintf(stderr, "%s: Out of memory.\n", progname); + (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname, + strerror(errno)); if (action == CHECK) return -1; } @@ -302,8 +302,8 @@ parse(magicp, nmagicp, l, action) maxmagic += ALLOC_INCR; if ((m = (struct magic *) realloc(*magicp, sizeof(struct magic) * maxmagic)) == NULL) { - (void) fprintf(stderr, "%s: Out of memory.\n", - progname); + (void) fprintf(stderr, "%s: Out of memory (%s).\n", + progname, strerror(errno)); if (*magicp) free(*magicp); if (action == CHECK) @@ -780,7 +780,6 @@ eatsize(p) *p = l; } -#ifdef QUICK /* * handle an mmaped file. */ @@ -798,6 +797,9 @@ apprentice_map(magicp, nmagicp, fn, action) int needsbyteswap; char *dbname = mkdbname(fn); + if (dbname == NULL) + return -1; + if ((fd = open(dbname, O_RDONLY)) == -1) return -1; @@ -807,12 +809,25 @@ apprentice_map(magicp, nmagicp, fn, action) goto error; } +#ifdef QUICK if ((*magicp = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) { (void)fprintf(stderr, "%s: Cannot map `%s' (%s)\n", progname, dbname, strerror(errno)); goto error; } +#else + if ((*magicp = malloc((size_t)st.st_size)) == NULL) { + (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname, + strerror(errno)); + goto error; + } + if (read(fd, *magicp, (size_t)st.st_size) != (size_t)st.st_size) { + (void) fprintf(stderr, "%s: Read failed (%s).\n", progname, + strerror(errno)); + goto error; + } +#endif (void)close(fd); fd = -1; ptr = (uint32 *) *magicp; @@ -844,9 +859,13 @@ apprentice_map(magicp, nmagicp, fn, action) error: if (fd != -1) (void)close(fd); - if (*magicp) + if (*magicp) { +#ifdef QUICK (void)munmap(*magicp, (size_t)st.st_size); - else { +#else + free(*magicp); +#endif + } else { *magicp = NULL; *nmagicp = 0; } @@ -869,6 +888,9 @@ apprentice_compile(magicp, nmagicp, fn, action) MAGICNO, VERSIONNO }; + if (dbname == NULL) + return -1; + if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) { (void)fprintf(stderr, "%s: Cannot open `%s' (%s)\n", progname, dbname, strerror(errno)); @@ -912,6 +934,11 @@ mkdbname(fn) buf = malloc(len); else buf = realloc(buf, len); + if (buf == NULL) { + (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname, + strerror(errno)); + return NULL; + } (void)strcpy(buf, fn); (void)strcat(buf, ext); return buf; @@ -976,4 +1003,3 @@ void bs1(m) m->value.l = swap4(m->value.l); m->mask = swap4(m->mask); } -#endif diff --git a/src/file.c b/src/file.c index 429e53a6..5ebcf388 100644 --- a/src/file.c +++ b/src/file.c @@ -56,7 +56,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$Id: file.c,v 1.56 2001/03/11 20:29:16 christos Exp $") +FILE_RCSID("@(#)$Id: file.c,v 1.57 2001/04/27 23:59:52 christos Exp $") #endif /* lint */ @@ -458,8 +458,6 @@ static void usage() { (void)fprintf(stderr, USAGE, progname); -#ifdef QUICK (void)fprintf(stderr, "Usage: %s -C [-m magic]\n", progname); -#endif exit(1); } diff --git a/src/file.h b/src/file.h index 67c86d0c..498d436a 100644 --- a/src/file.h +++ b/src/file.h @@ -1,6 +1,6 @@ /* * file.h - definitions for file(1) program - * @(#)$Id: file.h,v 1.35 2001/03/11 20:29:16 christos Exp $ + * @(#)$Id: file.h,v 1.36 2001/04/27 23:59:52 christos Exp $ * * Copyright (c) Ian F. Darwin, 1987. * Written by Ian F. Darwin. @@ -162,6 +162,10 @@ extern char *sys_errlist[]; #define strtoul(a, b, c) strtol(a, b, c) #endif +#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) +#define QUICK +#endif + #ifdef __STDC__ #define FILE_RCSID(id) \ static const char *rcsid(const char *p) { \ -- 2.40.0