aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-03-03 23:41:10 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-03-03 23:41:10 -0500
commit71bbbd4aa9ab0de068b9f1cf47036a8f21ef4514 (patch)
tree41723fd7c4e9ae5105ea92563d1785c22c9c5d35 /src
parent5bdf2739a447e9645fc0544defd9e8b95c958080 (diff)
downloadspmc-71bbbd4aa9ab0de068b9f1cf47036a8f21ef4514.tar.gz
Use the POSIX invocation of the `file` command
* Trap general error string (there's probably more of them) * Report failures and actually fail out.
Diffstat (limited to 'src')
-rw-r--r--src/mime.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/mime.c b/src/mime.c
index 78c5ba0..9e4bdce 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -15,22 +15,20 @@ Process *file_command(const char *_filename) {
char sh_cmd[PATH_MAX];
sh_cmd[0] = '\0';
#ifdef __APPLE__
- const char *fmt_cmd = "file -I \"%s\"";
+ const char *fmt_cmd = "file -I \"%s\" 2>&1";
#else // GNU
- const char *fmt_cmd = "file -E -i \"%s\" 2>&1";
+ const char *fmt_cmd = "file -i \"%s\" 2>&1";
#endif
+ const char *fail_pattern = ": cannot open";
strchrdel(filename, SHELL_INVALID);
sprintf(sh_cmd, fmt_cmd, filename);
shell(&proc_info, SHELL_OUTPUT, sh_cmd);
-#ifdef __APPLE__
- // Force BSD command to return non-zero when a file can't be found
- const char *failmsg = ": cannot open";
- if (strstr(proc_info->output, failmsg) != NULL) {
+ // POSIXly ridiculous. Return non-zero when a file can't be found, or isn't accessible
+ if (strstr(proc_info->output, fail_pattern) != NULL) {
proc_info->returncode = 1;
}
-#endif
free(filename);
return proc_info;
}
@@ -47,6 +45,9 @@ Mime *file_mimetype(const char *filename) {
Process *proc = file_command(filename);
if (proc->returncode != 0) {
+ fprintf(stderr, "%s\n", proc->output);
+ fprintf(stderr, "file command returned: %d\n", proc->returncode);
+ fprintf(SYSERROR);
shell_free(proc);
return NULL;
}
@@ -103,6 +104,10 @@ int file_is_text(const char *filename) {
int result = 0;
char *path = normpath(filename);
Mime *type = file_mimetype(path);
+ if (type == NULL) {
+ fprintf(stderr, "type detection failed: %s\n", filename);
+ return -1;
+ }
if (startswith(type->type, "text/")) {
result = 1;
}
@@ -120,6 +125,10 @@ int file_is_binary(const char *filename) {
int result = 0;
char *path = normpath(filename);
Mime *type = file_mimetype(path);
+ if (type == NULL) {
+ fprintf(stderr, "type detection failed: %s\n", filename);
+ return -1;
+ }
if (startswith(type->type, "application/") && strcmp(type->charset, "binary") == 0) {
result = 1;
}
@@ -132,6 +141,10 @@ int file_is_binexec(const char *filename) {
int result = 0;
char *path = normpath(filename);
Mime *type = file_mimetype(path);
+ if (type == NULL) {
+ fprintf(stderr, "type detection failed: %s\n", filename);
+ return -1;
+ }
// file-5.38: changed mime name associated with executables
// TODO: implement compatibility function to return the correct search pattern
if (fnmatch("application/x-[pic|pie|ex|sh]*", type->type, FNM_PATHNAME) != FNM_NOMATCH && strcmp(type->charset, "binary") == 0) {