aboutsummaryrefslogtreecommitdiff
path: root/src/mime.c
blob: 9e4bdce3ac10aa6d6665bf1620ff71d93b5e5b0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * @file mime.c
 */
#include "spm.h"
#include <fnmatch.h>

/**
 * Execute OS `file` command
 * @param _filename path to file
 * @return Process structure
 */
Process *file_command(const char *_filename) {
    char *filename = strdup(_filename);
    Process *proc_info = NULL;
    char sh_cmd[PATH_MAX];
    sh_cmd[0] = '\0';
#ifdef __APPLE__
    const char *fmt_cmd = "file -I \"%s\" 2>&1";
#else  // GNU
    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);

    // 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;
    }
    free(filename);
    return proc_info;
}

/**
 * Execute the `file` command, parse its output, and return the data in a `Mime` structure
 * @param filename path to file
 * @return Mime structure
 */
Mime *file_mimetype(const char *filename) {
    char **output = NULL;
    char **parts = NULL;
    Mime *type = NULL;
    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;
    }
    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 *charset = strdup(strchr(parts[1], '=') + 1);
    charset = lstrip(charset);
    charset[strlen(charset) - 1] = '\0';

    char *origin = realpath(filename, NULL);

    type = (Mime *)calloc(1, sizeof(Mime));
    type->origin = origin;
    type->type = what;
    type->charset = charset;

    split_free(output);
    split_free(parts);
    shell_free(proc);
    return type;
}

/**
 * Free a `Mime` structure
 * @param m
 */
void mime_free(Mime *m) {
    if (m != NULL) {
        free(m->origin);
        free(m->type);
        free(m->charset);
        free(m);
    }
}

/**
 * Determine if a file is a text file
 * @param filename
 * @return yes=1, no=0
 */
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;
    }
    free(path);
    mime_free(type);
    return result;
}

/**
 * Determine if a file is a binary data file
 * @param filename
 * @return yes=1, no=0
 */
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;
    }
    free(path);
    mime_free(type);
    return result;
}

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) {
        result = 1;
    }
    free(path);
    mime_free(type);
    return result;
}