#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: magic.c,v 1.115 2021/09/20 17:45:41 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.121 2023/02/09 17:45:19 christos Exp $")
#endif /* lint */
#include "magic.h"
#endif
#endif
-private void close_and_restore(const struct magic_set *, const char *, int,
+file_private void close_and_restore(const struct magic_set *, const char *, int,
const struct stat *);
-private int unreadable_info(struct magic_set *, mode_t, const char *);
-private const char* get_default_magic(void);
+file_private int unreadable_info(struct magic_set *, mode_t, const char *);
+file_private const char* get_default_magic(void);
#ifndef COMPILE_ONLY
-private const char *file_or_fd(struct magic_set *, const char *, int);
+file_private const char *file_or_fd(struct magic_set *, const char *, int);
#endif
#ifndef STDIN_FILENO
#endif
#endif
-private const char *
+file_private const char *
get_default_magic(void)
{
static const char hmagic[] = "/.magic/magic.mgc";
default_magic = NULL;
}
+ /* Before anything else, try to get a magic file from user HOME */
+ if ((home = getenv("HOME")) != NULL)
+ _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
+
/* First, try to get a magic file from user-application data */
if ((home = getenv("LOCALAPPDATA")) != NULL)
_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
#endif
}
-public const char *
+file_public const char *
magic_getpath(const char *magicfile, int action)
{
if (magicfile != NULL)
return action == FILE_LOAD ? get_default_magic() : MAGIC;
}
-public struct magic_set *
+file_public struct magic_set *
magic_open(int flags)
{
return file_ms_alloc(flags);
}
-private int
+file_private int
unreadable_info(struct magic_set *ms, mode_t md, const char *file)
{
if (file) {
if (access(file, W_OK) == 0)
if (file_printf(ms, "writable, ") == -1)
return -1;
+#ifndef WIN32
if (access(file, X_OK) == 0)
if (file_printf(ms, "executable, ") == -1)
return -1;
+#else
+ /* X_OK doesn't work well on MS-Windows */
+ {
+ const char *p = strrchr(file, '.');
+ if (p && (stricmp(p, ".exe")
+ || stricmp(p, ".dll")
+ || stricmp(p, ".bat")
+ || stricmp(p, ".cmd")))
+ if (file_printf(ms, "writable, ") == -1)
+ return -1;
+ }
+#endif
}
if (S_ISREG(md))
if (file_printf(ms, "regular file, ") == -1)
return 0;
}
-public void
+file_public void
magic_close(struct magic_set *ms)
{
if (ms == NULL)
/*
* load a magic file
*/
-public int
+file_public int
magic_load(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
/*
* Install a set of compiled magic buffers.
*/
-public int
+file_public int
magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
size_t nbufs)
{
}
#endif
-public int
+file_public int
magic_compile(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return file_apprentice(ms, magicfile, FILE_COMPILE);
}
-public int
+file_public int
magic_check(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return file_apprentice(ms, magicfile, FILE_CHECK);
}
-public int
+file_public int
magic_list(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return file_apprentice(ms, magicfile, FILE_LIST);
}
-private void
+file_private void
close_and_restore(const struct magic_set *ms, const char *name, int fd,
const struct stat *sb)
{
/*
* find type of descriptor
*/
-public const char *
+file_public const char *
magic_descriptor(struct magic_set *ms, int fd)
{
if (ms == NULL)
/*
* find type of named file
*/
-public const char *
+file_public const char *
magic_file(struct magic_set *ms, const char *inname)
{
if (ms == NULL)
return file_or_fd(ms, inname, STDIN_FILENO);
}
-private const char *
+file_private const char *
file_or_fd(struct magic_set *ms, const char *inname, int fd)
{
int rv = -1;
errno = 0;
if ((fd = open(inname, flags)) < 0) {
okstat = stat(inname, &sb) == 0;
- if (okstat && S_ISFIFO(sb.st_mode))
- ispipe = 1;
#ifdef WIN32
/*
* Can't stat, can't open. It may have been opened in
rv = 0;
goto done;
}
-#if O_CLOEXEC == 0
+#if O_CLOEXEC == 0 && defined(F_SETFD)
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
}
} else if (fd != -1) {
/* Windows refuses to read from a big console buffer. */
size_t howmany =
-#if defined(WIN32)
+#ifdef WIN32
_isatty(fd) ? 8 * 1024 :
#endif
ms->bytes_max;
}
-public const char *
+file_public const char *
magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
{
if (ms == NULL)
}
#endif
-public const char *
+file_public const char *
magic_error(struct magic_set *ms)
{
if (ms == NULL)
return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
}
-public int
+file_public int
magic_errno(struct magic_set *ms)
{
if (ms == NULL)
return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
}
-public int
+file_public int
magic_getflags(struct magic_set *ms)
{
if (ms == NULL)
return ms->flags;
}
-public int
+file_public int
magic_setflags(struct magic_set *ms, int flags)
{
if (ms == NULL)
return 0;
}
-public int
+file_public int
magic_version(void)
{
return MAGIC_VERSION;
}
-public int
+file_public int
magic_setparam(struct magic_set *ms, int param, const void *val)
{
if (ms == NULL)
case MAGIC_PARAM_ELF_SHNUM_MAX:
ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
return 0;
+ case MAGIC_PARAM_ELF_SHSIZE_MAX:
+ ms->elf_shsize_max = *CAST(const size_t *, val);
+ return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
return 0;
}
}
-public int
+file_public int
magic_getparam(struct magic_set *ms, int param, void *val)
{
if (ms == NULL)
case MAGIC_PARAM_ELF_SHNUM_MAX:
*CAST(size_t *, val) = ms->elf_shnum_max;
return 0;
+ case MAGIC_PARAM_ELF_SHSIZE_MAX:
+ *CAST(size_t *, val) = ms->elf_shsize_max;
+ return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
*CAST(size_t *, val) = ms->elf_notes_max;
return 0;