-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.h
62 lines (45 loc) · 1.77 KB
/
util.h
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
#pragma once
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define PACKAGE "command-not-found"
#define LOCALEDIR "/usr/share/locale"
#define USER "debian-command-not-found"
#define GROUP "debian-command-not-found"
#define Cleanup(x) __attribute__((cleanup(x)))
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
static inline void func##p(type *p) { \
if (*p) \
func(*p); \
} \
struct __useless_struct_to_allow_trailing_semicolon__
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, free);
DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
#define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
#define STRV_FOREACH(s, l) \
for ((s) = (l); (s) && *(s); (s)++)
#define MAX(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define MIN(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
static inline char *startswith(const char *s, const char *prefix) {
size_t l;
l = strlen(prefix);
if (strncmp(s, prefix, l) == 0)
return (char*) s + l;
return NULL;
}
static char *strv_find_prefix(char **l, const char *name) {
char **i;
assert(name);
STRV_FOREACH(i, l)
if (startswith(*i, name))
return *i;
return NULL;
}
int read_full_file(const char *fn, char **contents, size_t *size);
int read_full_stream(FILE *f, char **contents, size_t *size);