forked from dogeystamp/MinRSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
103 lines (78 loc) · 2 KB
/
util.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
/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
© 2021 dogeystamp <[email protected]>
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
void
logMsg(int lvl, char *msg, ...)
{
va_list args;
va_start(args, msg);
if (lvl <= logLevel) {
if (lvl == LOG_OUTPUT) {
vfprintf(stdout, msg, args);
} else {
fprintf(stderr, "minrss: ");
vfprintf(stderr, msg, args);
}
}
va_end(args);
if (!lvl)
exit(1);
}
void *
ecalloc(size_t nmemb, size_t size)
{
void *p = calloc(nmemb, size);
if (!p)
logMsg(LOG_FATAL, "Error allocating memory.\n");
return p;
}
void *
erealloc(void *p, size_t size)
{
p = realloc(p, size);
if (!p)
logMsg(LOG_FATAL, "Error reallocating memory.\n");
return p;
}
char *
san(char *str)
{
if (!str)
return "";
unsigned long long int len = strlen(str);
unsigned long long int offset = 0;
len = len > 255 ? 255 : len;
char *dup = ecalloc(len + 1, sizeof(char));
memcpy(dup, str, (len + 1) * sizeof(char));
for (unsigned long long int i = 0; i < len; i++) {
char c = dup[i];
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '.' && i - offset != 0) ||
c == '-' || c == '_' ||
c == ' ')
dup[i - offset] = dup[i];
else
offset++;
}
dup[len-offset] = '\0';
return dup;
}
char fsep()
{
#ifdef _WIN32
return '\\';
#else
return '/';
#endif
}