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
|
/*
* helpers.h
* This is private to us and not for user consumption
*/
/*
* Copyright (c) 2007-2015 The OpenRC Authors.
* See the Authors file at the top-level directory of this distribution and
* https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
*
* This file is part of OpenRC. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file.
*/
#ifndef __HELPERS_H__
#define __HELPERS_H__
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define ERRX fprintf (stderr, "out of memory\n"); exit (1)
#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
#define RC_UNUSED __attribute__((__unused__))
#define RC_NORETURN __attribute__((__noreturn__))
#define RC_PRINTF(a, b) __attribute__((__format__(__printf__, a, b)))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#ifndef HAVE_STRLCPY
# define strlcpy(dst, src, size) snprintf(dst, size, "%s", src)
#endif
#ifndef timespecsub
#define timespecsub(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) { \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (/* CONSTCOND */ 0)
#endif
RC_UNUSED static void *xmalloc (size_t size)
{
void *value = malloc(size);
if (value)
return (value);
ERRX;
/* NOTREACHED */
}
RC_UNUSED static void *xrealloc(void *ptr, size_t size)
{
void *value = realloc(ptr, size);
if (value)
return (value);
ERRX;
/* NOTREACHED */
}
RC_UNUSED static char *xstrdup(const char *str)
{
char *value;
if (!str)
return (NULL);
value = strdup(str);
if (value)
return (value);
ERRX;
/* NOTREACHED */
}
#undef ERRX
/*
* basename_c never modifies the argument. As such, if there is a trailing
* slash then an empty string is returned.
*/
RC_UNUSED static const char *basename_c(const char *path)
{
const char *slash = strrchr(path, '/');
if (slash)
return (++slash);
return (path);
}
RC_UNUSED static bool exists(const char *pathname)
{
struct stat buf;
return (stat(pathname, &buf) == 0);
}
RC_UNUSED static bool existss(const char *pathname)
{
struct stat buf;
return (stat(pathname, &buf) == 0 && buf.st_size != 0);
}
/*
* This is an OpenRC specific version of the asprintf() function.
* We do this to avoid defining the _GNU_SOURCE feature test macro on
* glibc systems and to ensure that we have a consistent function across
* platforms. This also allows us to call our xmalloc and xrealloc
* functions to handle memory allocation.
* this function was originally written by Mike Frysinger.
*/
RC_UNUSED RC_PRINTF(2,3) static int xasprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int len;
int memlen;
char *ret;
/*
* Start with a buffer size that should cover the vast majority of uses
* (path construction).
*/
memlen = 4096;
ret = xmalloc(memlen);
va_start(ap, fmt);
len = vsnprintf(ret, memlen, fmt, ap);
va_end(ap);
if (len >= memlen) {
/*
* Output was truncated, so increase buffer to exactly what we need.
*/
memlen = len + 1;
ret = xrealloc(ret, memlen);
va_start(ap, fmt);
len = vsnprintf(ret, len + 1, fmt, ap);
va_end(ap);
}
if (len < 0 || len >= memlen) {
/* Give up! */
fprintf(stderr, "xasprintf: unable to format a buffer\n");
free(ret);
exit(1);
}
*strp = ret;
return len;
}
#endif
|