From: Christos Zoulas Date: Wed, 14 May 2014 23:15:42 +0000 (+0000) Subject: PR/347: Windows patches. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=091cf9a153289959fc365afd2a1a0a910b0d5ef6;p=file PR/347: Windows patches. --- diff --git a/src/apprentice.c b/src/apprentice.c index 2c19154e..48a60e44 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -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; diff --git a/src/cdf_time.c b/src/cdf_time.c index 190df900..1e572de5 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.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 @@ -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; } diff --git a/src/file.h b/src/file.h index b2a27c00..4f5d68da 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.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"))) diff --git a/src/fsmagic.c b/src/fsmagic.c index 2f63ebac..ef5492f2 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -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 +#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); diff --git a/src/funcs.c b/src/funcs.c index 29b8a1e0..3a2f67c4 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -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; diff --git a/src/magic.c b/src/magic.c index b4f5a4b0..e4bd12ba 100644 --- a/src/magic.c +++ b/src/magic.c @@ -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; } } diff --git a/src/softmagic.c b/src/softmagic.c index 4a0bbc9c..7c53aef0 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.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;