-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.c
123 lines (103 loc) · 2.77 KB
/
tools.c
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
/*-
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return.
*/
#define _POSIX_C_SOURCE 200809
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <magic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "send.h"
#include "tools.h"
#define INITIALCAPACITY 32
char *
tool_mimetype(const char *path, FILE *out)
{
assert(path != NULL);
assert(out != NULL);
magic_t mh = magic_open(MAGIC_MIME_TYPE);
if (mh == NULL) {
syslog(LOG_ERR, "magic_open error: %m");
send_error(out, "E: magic_open", strerror(errno));
send_info(out, "I: I could not open a libmagic handle.", NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
if (magic_load(mh, NULL) == -1) {
syslog(LOG_ERR, "magic_load error: %s", magic_error(mh));
send_error(out, "E: magic_load", magic_error(mh));
send_info(out, "I: I could not load the magic database.", NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
const char *mime = magic_file(mh, path);
if (mime == NULL) {
syslog(LOG_ERR, "magic_file error: %s", magic_error(mh));
send_error(out, "E: magic_file", magic_error(mh));
send_info(out, "I: I could not identify the content of this "
"file", path);
send_eom(out);
exit(EXIT_FAILURE);
}
char *ret = strdup(mime);
if (ret == NULL)
{
syslog(LOG_ERR, "strdup error: %m");
send_error(out, "E: strdup mime", strerror(errno));
send_info(out, "I: I could not copy a string", mime);
send_eom(out);
exit(EXIT_FAILURE);
}
magic_close(mh);
return (ret);
}
char *
tool_join_path(const char *part1, const char *part2, FILE *out)
{
assert(part1 != NULL);
assert(part2 != NULL);
assert(out != NULL);
char *joined = malloc(PATH_MAX);
if (joined == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc joined", strerror(errno));
send_info(out, "I: I could not join path elements.", NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
char *pj = stpncpy(joined, part1, PATH_MAX);
if ((pj > joined) && (*(pj-1) != '/')) {
*pj++ = '/';
*pj = '\0';
}
size_t rl = PATH_MAX - (pj - joined);
const char *p2 = part2;
if (*p2 == '/')
p2++;
strncpy(pj, p2, rl);
if (joined[PATH_MAX-1] != '\0') {
syslog(LOG_ERR, "joinpath too long: \"%s\" + \"%s\"", part1,
part2);
send_error(out, "E: joinpath: joined too long", NULL);
send_info(out, "I: A joined path was too long.", NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
return (joined);
}
void
tool_strip_crlf(char *line)
{
assert(line != NULL);
char *p = strchr(line, '\n');
if (p != NULL && p > line && *(p-1) == '\r')
p--;
if (p != NULL)
*p = '\0';
}