-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-util.c
425 lines (321 loc) · 7.74 KB
/
core-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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <dirent.h>
#include <time.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#ifdef HAVE_UNAME
#include <sys/utsname.h>
#endif
#if defined(HAVE_REGEX_H)
#include <regex.h>
#elif defined(HAVE_PCREPOSIX_H)
#include <pcreposix.h>
#endif
#ifdef HAVE_STRTOF_L
#include <locale.h>
#endif
#ifdef HAVE_SCHED_H
#include <sched.h>
#if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
#define SCHED_RESET_ON_FORK 0x40000000
#endif
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SYS_CAPABILITY_H
#include <sys/capability.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#ifndef ENOTSUP
#define ENOTSUP 135
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_LIBSAMPLERATE
#include <samplerate.h>
#endif
#ifdef __APPLE__
#include <xlocale.h>
#include <mach/mach_init.h>
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <sys/sysctl.h>
#endif
#ifdef HAVE_DBUS
#include "rtkit.h"
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#include <sys/personality.h>
#endif
#include "xmalloc.h"
#include "core-util.h"
/* Not all platforms have this */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#define NEWLINE "\r\n"
#define WHITESPACE "\n\r \t"
/* Returns nonzero when *s starts with *pfx */
bool pa_startswith(const char *s, const char *pfx) {
size_t l;
pa_assert(s);
pa_assert(pfx);
l = strlen(pfx);
return strlen(s) >= l && strncmp(s, pfx, l) == 0;
}
char *pa_strip(char *s) {
char *e, *l = NULL;
/* Drops trailing whitespace. Modifies the string in
* place. Returns pointer to first non-space character */
s += strspn(s, WHITESPACE);
for (e = s; *e; e++)
if (!strchr(WHITESPACE, *e))
l = e;
if (l)
*(l+1) = 0;
else
*s = 0;
return s;
}
/* The following function is based on an example from the GNU libc
* documentation. This function is similar to GNU's asprintf(). */
char *pa_sprintf_malloc(const char *format, ...) {
size_t size = 100;
char *c = NULL;
pa_assert(format);
for(;;) {
int r;
va_list ap;
c = pa_xrealloc(c, size);
va_start(ap, format);
r = vsnprintf(c, size, format, ap);
va_end(ap);
c[size-1] = 0;
if (r > -1 && (size_t) r < size)
return c;
if (r > -1) /* glibc 2.1 */
size = (size_t) r+1;
else /* glibc 2.0 */
size *= 2;
}
}
/* Set the FD_CLOEXEC flag for a fd */
void pa_make_fd_cloexec(int fd) {
#ifdef FD_CLOEXEC
int v = 0;
pa_assert(fd >= 0);
pa_assert_se((v = fcntl(fd, F_GETFD, 0)) >= 0);
if (!(v & FD_CLOEXEC))
pa_assert_se(fcntl(fd, F_SETFD, v|FD_CLOEXEC) >= 0);
#endif
}
FILE* pa_fopen_cloexec(const char *path, const char *mode) {
FILE *f;
char *m;
m = pa_sprintf_malloc("%se", mode);
errno = 0;
if ((f = fopen(path, m))) {
pa_xfree(m);
goto finish;
}
pa_xfree(m);
if (errno != EINVAL)
return NULL;
if (!(f = fopen(path, mode)))
return NULL;
finish:
pa_make_fd_cloexec(fileno(f));
return f;
}
bool pa_is_path_absolute(const char *fn) {
pa_assert(fn);
#ifndef OS_IS_WIN32
return *fn == '/';
#else
return strlen(fn) >= 3 && isalpha(fn[0]) && fn[1] == ':' && fn[2] == '\\';
#endif
}
/* Convert the string s to a signed integer in *ret_i */
int pa_atoi(const char *s, int32_t *ret_i) {
long l;
pa_assert(s);
pa_assert(ret_i);
if (pa_atol(s, &l) < 0)
return -1;
if ((int32_t) l != l) {
errno = ERANGE;
return -1;
}
*ret_i = (int32_t) l;
return 0;
}
/* Convert the string s to an unsigned integer in *ret_u */
int pa_atou(const char *s, uint32_t *ret_u) {
char *x = NULL;
unsigned long l;
pa_assert(s);
pa_assert(ret_u);
errno = 0;
l = strtoul(s, &x, 0);
if (!x || *x || errno) {
if (!errno)
errno = EINVAL;
return -1;
}
if ((uint32_t) l != l) {
errno = ERANGE;
return -1;
}
*ret_u = (uint32_t) l;
return 0;
}
/* Convert the string s to a signed long integer in *ret_l. */
int pa_atol(const char *s, long *ret_l) {
char *x = NULL;
long l;
pa_assert(s);
pa_assert(ret_l);
errno = 0;
l = strtol(s, &x, 0);
if (!x || *x || errno) {
if (!errno)
errno = EINVAL;
return -1;
}
*ret_l = l;
return 0;
}
/* Try to parse a boolean string value.*/
int pa_parse_boolean(const char *v) {
pa_assert(v);
/* First we check language independent */
if (pa_streq(v, "1") || !strcasecmp(v, "y") || !strcasecmp(v, "t")
|| !strcasecmp(v, "yes") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
return 1;
else if (pa_streq(v, "0") || !strcasecmp(v, "n") || !strcasecmp(v, "f")
|| !strcasecmp(v, "no") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
return 0;
#ifdef HAVE_LANGINFO_H
{
const char *expr;
/* And then we check language dependent */
if ((expr = nl_langinfo(YESEXPR)))
if (expr[0])
if (pa_match(expr, v) > 0)
return 1;
if ((expr = nl_langinfo(NOEXPR)))
if (expr[0])
if (pa_match(expr, v) > 0)
return 0;
}
#endif
errno = EINVAL;
return -1;
}
/** Similar to pa_read(), but handles writes */
ssize_t pa_write(int fd, const void *buf, size_t count, int *type) {
if (!type || *type == 0) {
ssize_t r;
for (;;) {
if ((r = send(fd, buf, count, MSG_NOSIGNAL)) < 0) {
if (errno == EINTR)
continue;
break;
}
return r;
}
#ifdef OS_IS_WIN32
if (WSAGetLastError() != WSAENOTSOCK) {
errno = WSAGetLastError();
return r;
}
#else
if (errno != ENOTSOCK)
return r;
#endif
if (type)
*type = 1;
}
for (;;) {
ssize_t r;
if ((r = write(fd, buf, count)) < 0)
if (errno == EINTR)
continue;
return r;
}
}
/** Similar to pa_loop_read(), but wraps write() */
ssize_t pa_loop_write(int fd, const void*data, size_t size, int *type) {
ssize_t ret = 0;
int _type;
pa_assert(fd >= 0);
pa_assert(data);
pa_assert(size);
if (!type) {
_type = 0;
type = &_type;
}
while (size > 0) {
ssize_t r;
if ((r = pa_write(fd, data, size, type)) < 0)
return r;
if (r == 0)
break;
ret += r;
data = (const uint8_t*) data + r;
size -= (size_t) r;
}
return ret;
}
bool is_file_exist(const char* filename)
{
FILE *f;
pa_assert(filename);
if (!(f = fopen(filename, "r")))
return 0;
fclose(f);
return 1;
}
char * get_current_date_time(void)
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime); /* get the current time */
timeinfo = localtime(&rawtime);
return pa_sprintf_malloc("%4d-%02d-%02d %02d:%02d:%02d\n",
1900+timeinfo->tm_year, /* year */
1+timeinfo->tm_mon, /* month */
timeinfo->tm_mday, /* date */
timeinfo->tm_hour, /* hour */
timeinfo->tm_min, /* minute */
timeinfo->tm_sec); /* second */
}