libfstools: fit: improve fit_volume_find string handling
[project/fstools.git] / libblkid-tiny / c.h
1 /*
2 * Fundamental C definitions.
3 */
4
5 #ifndef UTIL_LINUX_C_H
6 #define UTIL_LINUX_C_H
7
8 #include <limits.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include <assert.h>
19
20 #ifdef HAVE_ERR_H
21 # include <err.h>
22 #endif
23
24 /*
25 * Compiler-specific stuff
26 */
27 #ifndef __GNUC_PREREQ
28 # if defined __GNUC__ && defined __GNUC_MINOR__
29 # define __GNUC_PREREQ(maj, min) \
30 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
31 # else
32 # define __GNUC_PREREQ(maj, min) 0
33 # endif
34 #endif
35
36 #ifdef __GNUC__
37
38 /* &a[0] degrades to a pointer: a different type from an array */
39 # define __must_be_array(a) \
40 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
41
42 # define ignore_result(x) __extension__ ({ \
43 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
44 })
45
46 #else /* !__GNUC__ */
47 # define __must_be_array(a) 0
48 # define __attribute__(_arg_)
49 # define ignore_result(x) ((void) (x))
50 #endif /* !__GNUC__ */
51
52 /*
53 * Function attributes
54 */
55 #ifndef __ul_alloc_size
56 # if __GNUC_PREREQ (4, 3)
57 # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
58 # else
59 # define __ul_alloc_size(s)
60 # endif
61 #endif
62
63 #ifndef __ul_calloc_size
64 # if __GNUC_PREREQ (4, 3)
65 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
66 # else
67 # define __ul_calloc_size(n, s)
68 # endif
69 #endif
70
71 /*
72 * Force a compilation error if condition is true, but also produce a
73 * result (of value 0 and type size_t), so the expression can be used
74 * e.g. in a structure initializer (or wherever else comma expressions
75 * aren't permitted).
76 */
77 #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
78 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
79
80 #ifndef ARRAY_SIZE
81 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
82 #endif
83
84 #ifndef PATH_MAX
85 # define PATH_MAX 4096
86 #endif
87
88 #ifndef TRUE
89 # define TRUE 1
90 #endif
91
92 #ifndef FALSE
93 # define FALSE 0
94 #endif
95
96 #ifndef min
97 # define min(x, y) __extension__ ({ \
98 __typeof__(x) _min1 = (x); \
99 __typeof__(y) _min2 = (y); \
100 (void) (&_min1 == &_min2); \
101 _min1 < _min2 ? _min1 : _min2; })
102 #endif
103
104 #ifndef max
105 # define max(x, y) __extension__ ({ \
106 __typeof__(x) _max1 = (x); \
107 __typeof__(y) _max2 = (y); \
108 (void) (&_max1 == &_max2); \
109 _max1 > _max2 ? _max1 : _max2; })
110 #endif
111
112 #ifndef cmp_numbers
113 # define cmp_numbers(x, y) __extension__ ({ \
114 __typeof__(x) _a = (x); \
115 __typeof__(y) _b = (y); \
116 (void) (&_a == &_b); \
117 _a == _b ? 0 : _a > _b ? 1 : -1; })
118 #endif
119
120 #ifndef offsetof
121 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
122 #endif
123
124 #ifndef container_of
125 #define container_of(ptr, type, member) __extension__ ({ \
126 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
127 (type *)( (char *)__mptr - offsetof(type,member) );})
128 #endif
129
130 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
131 # ifdef HAVE___PROGNAME
132 extern char *__progname;
133 # define program_invocation_short_name __progname
134 # else
135 # ifdef HAVE_GETEXECNAME
136 # define program_invocation_short_name \
137 prog_inv_sh_nm_from_file(getexecname(), 0)
138 # else
139 # define program_invocation_short_name \
140 prog_inv_sh_nm_from_file(__FILE__, 1)
141 # endif
142 static char prog_inv_sh_nm_buf[256];
143 static inline char *
144 prog_inv_sh_nm_from_file(char *f, char stripext)
145 {
146 char *t;
147
148 if ((t = strrchr(f, '/')) != NULL)
149 t++;
150 else
151 t = f;
152
153 strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
154 prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
155
156 if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
157 *t = '\0';
158
159 return prog_inv_sh_nm_buf;
160 }
161 # endif
162 #endif
163
164
165 #ifndef HAVE_ERR_H
166 static inline void
167 errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
168 {
169 fprintf(stderr, "%s: ", program_invocation_short_name);
170 if (fmt != NULL) {
171 va_list argp;
172 va_start(argp, fmt);
173 vfprintf(stderr, fmt, argp);
174 va_end(argp);
175 if (adderr)
176 fprintf(stderr, ": ");
177 }
178 if (adderr)
179 fprintf(stderr, "%m");
180 fprintf(stderr, "\n");
181 if (doexit)
182 exit(excode);
183 }
184
185 #ifndef HAVE_ERR
186 # define err(E, FMT...) errmsg(1, E, 1, FMT)
187 #endif
188
189 #ifndef HAVE_ERRX
190 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
191 #endif
192
193 #ifndef HAVE_WARN
194 # define warn(FMT...) errmsg(0, 0, 1, FMT)
195 #endif
196
197 #ifndef HAVE_WARNX
198 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
199 #endif
200 #endif /* !HAVE_ERR_H */
201
202
203 static inline __attribute__((const)) int is_power_of_2(unsigned long num)
204 {
205 return (num != 0 && ((num & (num - 1)) == 0));
206 }
207
208 #ifndef HAVE_LOFF_T
209 typedef int64_t loff_t;
210 #endif
211
212 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
213 #include <sys/types.h>
214 #include <dirent.h>
215 static inline int dirfd(DIR *d)
216 {
217 return d->dd_fd;
218 }
219 #endif
220
221 /*
222 * Fallback defines for old versions of glibc
223 */
224 #include <fcntl.h>
225
226 #ifdef O_CLOEXEC
227 #define UL_CLOEXECSTR "e"
228 #else
229 #define UL_CLOEXECSTR ""
230 #endif
231
232 #ifndef O_CLOEXEC
233 #define O_CLOEXEC 0
234 #endif
235
236 #ifdef __FreeBSD_kernel__
237 #ifndef F_DUPFD_CLOEXEC
238 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
239 #endif
240 #endif
241
242
243 #ifndef AI_ADDRCONFIG
244 #define AI_ADDRCONFIG 0x0020
245 #endif
246
247 #ifndef IUTF8
248 #define IUTF8 0040000
249 #endif
250
251 #if 0
252 /*
253 * MAXHOSTNAMELEN replacement
254 */
255 static inline size_t get_hostname_max(void)
256 {
257 long len = sysconf(_SC_HOST_NAME_MAX);
258
259 if (0 < len)
260 return len;
261
262 #ifdef MAXHOSTNAMELEN
263 return MAXHOSTNAMELEN;
264 #elif HOST_NAME_MAX
265 return HOST_NAME_MAX;
266 #endif
267 return 64;
268 }
269
270 /*
271 * The usleep function was marked obsolete in POSIX.1-2001 and was removed
272 * in POSIX.1-2008. It was replaced with nanosleep() that provides more
273 * advantages (like no interaction with signals and other timer functions).
274 */
275 #include <time.h>
276
277 static inline int xusleep(useconds_t usec)
278 {
279 #ifdef HAVE_NANOSLEEP
280 struct timespec waittime = {
281 .tv_sec = usec / 1000000L,
282 .tv_nsec = (usec % 1000000L) * 1000
283 };
284 return nanosleep(&waittime, NULL);
285 #elif defined(HAVE_USLEEP)
286 return usleep(usec);
287 #else
288 # error "System with usleep() or nanosleep() required!"
289 #endif
290 }
291 #endif
292
293 /*
294 * Constant strings for usage() functions. For more info see
295 * Documentation/howto-usage-function.txt and disk-utils/delpart.c
296 */
297 #define USAGE_HEADER _("\nUsage:\n")
298 #define USAGE_OPTIONS _("\nOptions:\n")
299 #define USAGE_SEPARATOR "\n"
300 #define USAGE_HELP _(" -h, --help display this help and exit\n")
301 #define USAGE_VERSION _(" -V, --version output version information and exit\n")
302 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
303
304 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
305
306 /*
307 * scanf modifiers for "strings allocation"
308 */
309 #ifdef HAVE_SCANF_MS_MODIFIER
310 #define UL_SCNsA "%ms"
311 #elif defined(HAVE_SCANF_AS_MODIFIER)
312 #define UL_SCNsA "%as"
313 #endif
314
315 /*
316 * seek stuff
317 */
318 #ifndef SEEK_DATA
319 # define SEEK_DATA 3
320 #endif
321 #ifndef SEEK_HOLE
322 # define SEEK_HOLE 4
323 #endif
324
325
326 /*
327 * Macros to convert #define'itions to strings, for example
328 * #define XYXXY 42
329 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
330 */
331 #define stringify_value(s) stringify(s)
332 #define stringify(s) #s
333
334 /*
335 * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
336 * instrumentation shipped with Clang and GCC) to not instrument the
337 * annotated function. Furthermore, it will prevent the compiler from
338 * inlining the function because inlining currently breaks the blacklisting
339 * mechanism of AddressSanitizer.
340 */
341 #if defined(__has_feature)
342 # if __has_feature(address_sanitizer)
343 # define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
344 # else
345 # define UL_ASAN_BLACKLIST /* nothing */
346 # endif
347 #else
348 # define UL_ASAN_BLACKLIST /* nothing */
349 #endif
350
351 #endif /* UTIL_LINUX_C_H */