block: fix possible NULL pointer dereference in mount_device()
[project/ubox.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 <stdarg.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #ifdef HAVE_ERR_H
18 # include <err.h>
19 #endif
20
21 /*
22 * Compiler specific stuff
23 */
24 #ifndef __GNUC_PREREQ
25 # if defined __GNUC__ && defined __GNUC_MINOR__
26 # define __GNUC_PREREQ(maj, min) \
27 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
28 # else
29 # define __GNUC_PREREQ(maj, min) 0
30 # endif
31 #endif
32
33 #ifdef __GNUC__
34
35 /* &a[0] degrades to a pointer: a different type from an array */
36 # define __must_be_array(a) \
37 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
38
39 # define ignore_result(x) ({ \
40 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
41 })
42
43 #else /* !__GNUC__ */
44 # define __must_be_array(a) 0
45 # define __attribute__(_arg_)
46 # define ignore_result(x) ((void) (x))
47 #endif /* !__GNUC__ */
48
49 /*
50 * Function attributes
51 */
52 #ifndef __ul_alloc_size
53 # if __GNUC_PREREQ (4, 3)
54 # define __ul_alloc_size(s) __attribute__((alloc_size(s)))
55 # else
56 # define __ul_alloc_size(s)
57 # endif
58 #endif
59
60 #ifndef __ul_calloc_size
61 # if __GNUC_PREREQ (4, 3)
62 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s)))
63 # else
64 # define __ul_calloc_size(n, s)
65 # endif
66 #endif
67
68 /* Force a compilation error if condition is true, but also produce a
69 * result (of value 0 and type size_t), so the expression can be used
70 * e.g. in a structure initializer (or where-ever else comma expressions
71 * aren't permitted).
72 */
73 #define UL_BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
74 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
75
76 #ifndef ARRAY_SIZE
77 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
78 #endif
79
80 #ifndef PATH_MAX
81 # define PATH_MAX 4096
82 #endif
83
84 #ifndef TRUE
85 # define TRUE 1
86 #endif
87
88 #ifndef FALSE
89 # define FALSE 0
90 #endif
91
92 #ifndef min
93 # define min(x, y) ({ \
94 __typeof__(x) _min1 = (x); \
95 __typeof__(y) _min2 = (y); \
96 (void) (&_min1 == &_min2); \
97 _min1 < _min2 ? _min1 : _min2; })
98 #endif
99
100 #ifndef max
101 # define max(x, y) ({ \
102 __typeof__(x) _max1 = (x); \
103 __typeof__(y) _max2 = (y); \
104 (void) (&_max1 == &_max2); \
105 _max1 > _max2 ? _max1 : _max2; })
106 #endif
107
108 #ifndef offsetof
109 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
110 #endif
111
112 #ifndef container_of
113 #define container_of(ptr, type, member) ({ \
114 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
115 (type *)( (char *)__mptr - offsetof(type,member) );})
116 #endif
117
118 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
119 # ifdef HAVE___PROGNAME
120 extern char *__progname;
121 # define program_invocation_short_name __progname
122 # else
123 # ifdef HAVE_GETEXECNAME
124 # define program_invocation_short_name \
125 prog_inv_sh_nm_from_file(getexecname(), 0)
126 # else
127 # define program_invocation_short_name \
128 prog_inv_sh_nm_from_file(__FILE__, 1)
129 # endif
130 static char prog_inv_sh_nm_buf[256];
131 static inline char *
132 prog_inv_sh_nm_from_file(char *f, char stripext)
133 {
134 char *t;
135
136 if ((t = strrchr(f, '/')) != NULL)
137 t++;
138 else
139 t = f;
140
141 strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
142 prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
143
144 if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
145 *t = '\0';
146
147 return prog_inv_sh_nm_buf;
148 }
149 # endif
150 #endif
151
152
153 #ifndef HAVE_ERR_H
154 static inline void
155 errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
156 {
157 fprintf(stderr, "%s: ", program_invocation_short_name);
158 if (fmt != NULL) {
159 va_list argp;
160 va_start(argp, fmt);
161 vfprintf(stderr, fmt, argp);
162 va_end(argp);
163 if (adderr)
164 fprintf(stderr, ": ");
165 }
166 if (adderr)
167 fprintf(stderr, "%m");
168 fprintf(stderr, "\n");
169 if (doexit)
170 exit(excode);
171 }
172
173 #ifndef HAVE_ERR
174 # define err(E, FMT...) errmsg(1, E, 1, FMT)
175 #endif
176
177 #ifndef HAVE_ERRX
178 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
179 #endif
180
181 #ifndef HAVE_WARN
182 # define warn(FMT...) errmsg(0, 0, 1, FMT)
183 #endif
184
185 #ifndef HAVE_WARNX
186 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
187 #endif
188 #endif /* !HAVE_ERR_H */
189
190
191 static inline __attribute__((const)) int is_power_of_2(unsigned long num)
192 {
193 return (num != 0 && ((num & (num - 1)) == 0));
194 }
195
196 #ifndef HAVE_LOFF_T
197 typedef int64_t loff_t;
198 #endif
199
200 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
201 #include <sys/types.h>
202 #include <dirent.h>
203 static inline int dirfd(DIR *d)
204 {
205 return d->dd_fd;
206 }
207 #endif
208
209 /*
210 * Fallback defines for old versions of glibc
211 */
212 #include <fcntl.h>
213 #ifndef O_CLOEXEC
214 #define O_CLOEXEC 0
215 #endif
216
217 #ifndef AI_ADDRCONFIG
218 #define AI_ADDRCONFIG 0x0020
219 #endif
220
221 #ifndef IUTF8
222 #define IUTF8 0040000
223 #endif
224
225 /*
226 * Constant strings for usage() functions. For more info see
227 * Documentation/howto-usage-function.txt and sys-utils/arch.c
228 */
229 #define USAGE_HEADER _("\nUsage:\n")
230 #define USAGE_OPTIONS _("\nOptions:\n")
231 #define USAGE_SEPARATOR _("\n")
232 #define USAGE_HELP _(" -h, --help display this help and exit\n")
233 #define USAGE_VERSION _(" -V, --version output version information and exit\n")
234 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
235
236 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
237
238 /*
239 * scanf modifiers for "strings allocation"
240 */
241 #ifdef HAVE_SCANF_MS_MODIFIER
242 #define UL_SCNsA "%ms"
243 #elif defined(HAVE_SCANF_AS_MODIFIER)
244 #define UL_SCNsA "%as"
245 #endif
246
247 #endif /* UTIL_LINUX_C_H */