]> granicus.if.org Git - file/commitdiff
PR/347: Windows patches.
authorChristos Zoulas <christos@zoulas.com>
Wed, 14 May 2014 23:15:42 +0000 (23:15 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 14 May 2014 23:15:42 +0000 (23:15 +0000)
src/apprentice.c
src/cdf_time.c
src/file.h
src/fsmagic.c
src/funcs.c
src/magic.c
src/softmagic.c

index 2c19154e719a9ce672a8110efcf832e1d9c77e34..48a60e44b7954c5fa1084513eb3b444747653c1e 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.209 2014/05/13 16:42:17 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.210 2014/05/14 23:15:42 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -2767,7 +2767,8 @@ apprentice_map(struct magic_set *ms, const char *fn)
        }
        entries = (uint32_t)(st.st_size / sizeof(struct magic));
        if ((off_t)(entries * sizeof(struct magic)) != st.st_size) {
-               file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
+               file_error(ms, 0, "Size of `%s' %" INT64_T_FORMAT "u is not "
+                   "a multiple of %" SIZE_T_FORMAT "u",
                    dbname, (unsigned long long)st.st_size,
                    sizeof(struct magic));
                goto error;
index 190df900c3250022e9b1e86f2846464a36225d71..1e572de539f8abffbb1077beb24c79747112da29 100644 (file)
@@ -27,7 +27,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: cdf_time.c,v 1.14 2014/04/17 12:44:01 christos Exp $")
+FILE_RCSID("@(#)$File: cdf_time.c,v 1.15 2014/05/14 23:15:42 christos Exp $")
 #endif
 
 #include <time.h>
@@ -171,7 +171,8 @@ cdf_ctime(const time_t *sec, char *buf)
        char *ptr = ctime_r(sec, buf);
        if (ptr != NULL)
                return buf;
-       (void)snprintf(buf, 26, "*Bad* 0x%16.16llx\n", (long long)*sec);
+       (void)snprintf(buf, 26, "*Bad* 0x%16.16" INT64_T_FORMAT "x\n",
+           (long long)*sec);
        return buf;
 }
 
index b2a27c00eaa2d25ff95cb23dc6f42364f5605580..4f5d68da337a53440dc6bfc1ab649647021a5853 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.150 2014/05/05 20:53:10 christos Exp $
+ * @(#)$File: file.h,v 1.151 2014/05/14 23:15:42 christos Exp $
  */
 
 #ifndef __file_h__
@@ -83,7 +83,7 @@
 
 #define private static
 
-#if HAVE_VISIBILITY
+#if HAVE_VISIBILITY && !defined(WIN32)
 #define public  __attribute__ ((__visibility__("default")))
 #ifndef protected
 #define protected __attribute__ ((__visibility__("hidden")))
index 2f63ebacb6cdbc1873a39d277f327e1146a1e24f..ef5492f2a10672291d7b838ad11ca89eada1eab8 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: fsmagic.c,v 1.72 2014/04/17 12:47:11 christos Exp $")
+FILE_RCSID("@(#)$File: fsmagic.c,v 1.73 2014/05/14 23:15:42 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -53,7 +53,11 @@ FILE_RCSID("@(#)$File: fsmagic.c,v 1.72 2014/04/17 12:47:11 christos Exp $")
 #ifdef major                   /* Might be defined in sys/types.h.  */
 # define HAVE_MAJOR
 #endif
-  
+#ifdef WIN32
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#endif
+
 #ifndef HAVE_MAJOR
 # define major(dev)  (((dev) >> 8) & 0xff)
 # define minor(dev)  ((dev) & 0xff)
@@ -123,6 +127,35 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
 #endif
        ret = stat(fn, sb);     /* don't merge into if; see "ret =" above */
 
+#ifdef WIN32
+       {
+               HANDLE hFile = CreateFile(fn, 0, FILE_SHARE_DELETE |
+                   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
+                   NULL);
+               if (hFile != INVALID_HANDLE_VALUE) {
+                       /*
+                        * Stat failed, but we can still open it - assume it's
+                        * a block device, if nothing else.
+                        */
+                       if (ret) {
+                               sb->st_mode = S_IFBLK;
+                               ret = 0;
+                       }
+                       switch (GetFileType(hFile)) {
+                       case FILE_TYPE_CHAR:
+                               sb->st_mode |= S_IFCHR;
+                               sb->st_mode &= ~S_IFREG;
+                               break;
+                       case FILE_TYPE_PIPE:
+                               sb->st_mode |= S_IFIFO;
+                               sb->st_mode &= ~S_IFREG;
+                               break;
+                       }
+                       CloseHandle(hFile);
+               }
+       }
+#endif
+
        if (ret) {
                if (ms->flags & MAGIC_ERROR) {
                        file_error(ms, errno, "cannot stat `%s'", fn);
index 29b8a1e04e2cbfdfdb81241f8a0994585e2a5a97..3a2f67c4dab8a2908fabc95faa9ffe1023165f6c 100644 (file)
@@ -27,7 +27,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.71 2014/05/05 20:53:10 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.72 2014/05/14 23:15:42 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -280,7 +280,9 @@ simple:
                if (file_printf(ms, "%s", code_mime) == -1)
                        rv = -1;
        }
+#if HAVE_FORK
  done_encoding:
+#endif
        free(u8buf);
        if (rv)
                return rv;
index b4f5a4b012dea09180f89486f73b0e3bf00c2aa8..e4bd12ba229190dfd3e25ed07fe4b8b0b5218115 100644 (file)
@@ -33,7 +33,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.83 2014/05/13 16:44:24 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.84 2014/05/14 23:15:42 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -126,8 +126,9 @@ out:
        free(hmagicpath);
        return MAGIC;
 #else
-       char *hmagicp = hmagicpath;
+       char *hmagicp;
        char *tmppath = NULL;
+       hmagicpath = NULL;
 
 #define APPENDPATH() \
        do { \
@@ -368,6 +369,12 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
                goto done;
        }
 
+#ifdef WIN32
+       /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
+       if (fd == STDIN_FILENO)
+               _setmode(STDIN_FILENO, O_BINARY);
+#endif
+
        if (inname == NULL) {
                if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
                        ispipe = 1;
@@ -386,6 +393,18 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
 
                errno = 0;
                if ((fd = open(inname, flags)) < 0) {
+#ifdef WIN32
+                       /*
+                        * Can't stat, can't open.  It may have been opened in
+                        * fsmagic, so if the user doesn't have read permission,
+                        * allow it to say so; otherwise an error was probably
+                        * displayed in fsmagic.
+                        */
+                       if (!okstat && errno == EACCES) {
+                               sb.st_mode = S_IFBLK;
+                               okstat = 1;
+                       }
+#endif
                        if (okstat &&
                            unreadable_info(ms, sb.st_mode, inname) == -1)
                                goto done;
@@ -421,8 +440,18 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
                }
 
        } else {
-               if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
-                       file_error(ms, errno, "cannot read `%s'", inname);
+               /* Windows refuses to read from a big console buffer. */
+               size_t howmany =
+#if defined(WIN32) && HOWMANY > 8 * 1024
+                               _isatty(fd) ? 8 * 1024 :
+#endif
+                               HOWMANY;
+               if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
+                       if (inname == NULL && fd != STDIN_FILENO)
+                               file_error(ms, errno, "cannot read fd %d", fd);
+                       else
+                               file_error(ms, errno, "cannot read `%s'",
+                                   inname == NULL ? "/dev/stdin" : inname);
                        goto done;
                }
        }
index 4a0bbc9c20badc910c015f1dfd49a6225501c05c..7c53aef04c7c64d2e9b36a064bd5f44070426262 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.187 2014/05/13 16:42:17 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.188 2014/05/14 23:15:42 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -482,13 +482,13 @@ mprint(struct magic_set *ms, struct magic *m)
                case -1:
                        return -1;
                case 1:
-                       (void)snprintf(buf, sizeof(buf), "%llu",
+                       (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
                            (unsigned long long)v);
                        if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
                                return -1;
                        break;
                default:
-                       if (file_printf(ms, F(ms, m, "%llu"),
+                       if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
                            (unsigned long long) v) == -1)
                                return -1;
                        break;