aboutsummaryrefslogtreecommitdiff
path: root/lib/mime.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-05-21 17:26:54 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-05-21 17:26:54 -0400
commit5f1a5d8368d82dc1e5a0042f8e88f339fdfcdf0c (patch)
tree3ccd8db7284a4d78fe1978116f709b3e5c76ad3a /lib/mime.c
parente5efe3dfa398113914e64b5a0f9ee336ed078c62 (diff)
downloadspmc-5f1a5d8368d82dc1e5a0042f8e88f339fdfcdf0c.tar.gz
Handle Darwin
Diffstat (limited to 'lib/mime.c')
-rw-r--r--lib/mime.c52
1 files changed, 40 insertions, 12 deletions
diff --git a/lib/mime.c b/lib/mime.c
index 9d0205c..12e8cf4 100644
--- a/lib/mime.c
+++ b/lib/mime.c
@@ -51,25 +51,51 @@ Mime *file_mimetype(const char *filename) {
shell_free(proc);
return NULL;
}
+
+#if OS_DARWIN
+ if (proc->output) {
+ // Universal binaries spit out multiple lines, but we only require the first
+ char *terminate_multi_part = strchr(proc->output, '\n');
+ if (terminate_multi_part != NULL) {
+ *(terminate_multi_part + 1) = '\0';
+ }
+ }
+#endif
+
output = split(proc->output, ":");
if (!output || output[1] == NULL) {
shell_free(proc);
return NULL;
}
- parts = split(output[1], ";");
- if (!parts || !parts[0] || !parts[1]) {
- shell_free(proc);
- return NULL;
- }
- char *what = strdup(parts[0]);
- what = lstrip(what);
+ char *origin = NULL;
+ char *what = NULL;
+ char *charset = NULL;
+
+ if (strchr(output[1], ';')) {
+ parts = split(output[1], ";");
+ if (!parts || !parts[0] || !parts[1]) {
+ shell_free(proc);
+ return NULL;
+ }
- char *charset = strdup(strchr(parts[1], '=') + 1);
- charset = lstrip(charset);
- charset[strlen(charset) - 1] = '\0';
+ what = strdup(parts[0]);
+ what = lstrip(what);
- char *origin = realpath(filename, NULL);
+ charset = strdup(strchr(parts[1], '=') + 1);
+ charset = lstrip(charset);
+ charset[strlen(charset) - 1] = '\0';
+ } else {
+ // this branch is for Darwin; the 'charset=' string is not guaranteed to exist using argument `-I`
+ what = strdup(output[1]);
+ what = lstrip(what);
+ what = strip(what);
+ if (strstr(output[1], "binary") != NULL) {
+ charset = strdup("binary");
+ }
+ }
+
+ origin = realpath(filename, NULL);
type = (Mime *)calloc(1, sizeof(Mime));
type->origin = origin;
@@ -77,7 +103,9 @@ Mime *file_mimetype(const char *filename) {
type->charset = charset;
split_free(output);
- split_free(parts);
+ if (parts != NULL) {
+ split_free(parts);
+ }
shell_free(proc);
return type;
}