-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.c
165 lines (140 loc) · 2.61 KB
/
utils.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
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
157
158
159
160
161
162
163
164
165
#include <utils.h>
#include <logging.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* taken from ffmpeg project */
#ifdef CONFIG_WIN32
# include <sys/types.h>
# include <sys/timeb.h>
#elif defined(CONFIG_OS2)
# include <string.h>
# include <sys/time.h>
#else
# include <unistd.h>
# include <fcntl.h>
# include <sys/time.h>
#endif
LOG_DEFAULT_CATEGORY(utils)
double
get_time (void)
{
#ifdef CONFIG_WIN32
/* mingw was complaining */
struct _timeb tb;
_ftime(&tb);
return tb.time + tb.millitm / 1e3;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1e6;
#endif
}
int
xstrcasecmp (const char *a, const char *b)
{
while (*a) {
if (tolower (*a & 0xff) != tolower (*b & 0xff))
break;
a++;
b++;
}
return (tolower(*a) - tolower(*b));
}
int
xstrncasecmp (const char *a, const char *b, size_t n)
{
while (n && *a) {
if (tolower (*a & 0xff) != tolower (*b & 0xff))
break;
a++;
b++;
n--;
}
return (tolower(*a) - tolower(*b));
}
void *
xmalloc (size_t n)
{
void *p = malloc(n);
if (!p)
{
CRITICAL2("allocation failure: %s", strerror(errno));
exit(EXIT_FAILURE);
}
return (p);
}
void *
xcalloc (size_t a, size_t b)
{
void *p = calloc(a, b);
if (!p)
{
CRITICAL2("callocation failure: %s", strerror(errno));
exit(EXIT_FAILURE);
}
return (p);
}
/** duplicates a string and returns a pointer to the new string
*/
char *
xstrdup (const char *s)
{
void *p;
p = xmalloc (strlen (s) + 1);
strcpy (p, s);
return (p);
}
char *
xstrcat2 (const char* s1, const char* s2)
{
char *s = NULL;
assert(s1 && s2);
s = xmalloc(strlen(s1) + strlen(s2) + 1);
strcpy(s, s1);
strcat(s, s2);
return s;
}
void *
xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (!p)
{
CRITICAL2("reallocation failure: %s", strerror(errno));
exit(EXIT_FAILURE);
}
return (p);
}
ssize_t
fread_dyn(char **destp, size_t *n, FILE *stream)
{
const unsigned bsize = 8192;
size_t total = 0, cnt = 0;
assert(destp);
assert(n);
assert(stream);
if (!*destp)
*destp = xmalloc(*n = bsize);
while (1) {
cnt = fread(*destp+total, 1, *n-total, stream);
if (cnt != *n-total)
{
if (feof(stream))
return total+cnt;
else if (ferror(stream))
{
CWARNING3(filesys, "read error: %s", strerror(errno));
return -1;
}
}
total += cnt;
if (*n <= total)
*destp = xrealloc(*destp, *n *= 2);
}
}
/* vim: set noet ts=4 sw=4 tw=77: */