From 5f1a5d8368d82dc1e5a0042f8e88f339fdfcdf0c Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 21 May 2020 17:26:54 -0400 Subject: Handle Darwin --- lib/mime.c | 52 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'lib/mime.c') 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; } -- cgit