2 * Copyright (c) Artem Bityutskiy, 2007, 2008
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /* Imported from mtd-utils by dehrenberg */
21 #ifndef __MTD_UTILS_COMMON_H__
22 #define __MTD_UTILS_COMMON_H__
33 #include <sys/sysmacros.h>
36 # error "You must define PROGRAM_NAME before including this header"
43 #ifndef MIN /* some C lib headers define this for us */
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
47 #define MAX(a, b) ((a) > (b) ? (a) : (b))
49 #define min(a, b) MIN(a, b) /* glue for linux kernel source */
51 #define ALIGN(x,a) __ALIGN_MASK(x,(__typeof__(x))(a)-1)
52 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
54 #define min_t(t,x,y) ({ \
55 __typeof__((x)) _x = (x); \
56 __typeof__((y)) _y = (y); \
57 (_x < _y) ? _x : _y; \
60 #define max_t(t,x,y) ({ \
61 __typeof__((x)) _x = (x); \
62 __typeof__((y)) _y = (y); \
63 (_x > _y) ? _x : _y; \
70 /* define a print format specifier for off_t */
71 #ifdef __USE_FILE_OFFSET64
72 #define PRIxoff_t PRIx64
73 #define PRIdoff_t PRId64
75 #define PRIxoff_t "l"PRIx32
76 #define PRIdoff_t "l"PRId32
79 /* Verbose messages */
80 #define bareverbose(verbose, fmt, ...) do { \
82 printf(fmt, ##__VA_ARGS__); \
84 #define verbose(verbose, fmt, ...) \
85 bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
88 #define normsg_cont(fmt, ...) do { \
89 printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__); \
91 #define normsg(fmt, ...) do { \
92 normsg_cont(fmt "\n", ##__VA_ARGS__); \
96 #define errmsg(fmt, ...) ({ \
97 fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
100 #define errmsg_die(fmt, ...) do { \
101 exit(errmsg(fmt, ##__VA_ARGS__)); \
104 /* System error messages */
105 #define sys_errmsg(fmt, ...) ({ \
107 errmsg(fmt, ##__VA_ARGS__); \
108 fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
109 "", _err, strerror(_err)); \
112 #define sys_errmsg_die(fmt, ...) do { \
113 exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
117 #define warnmsg(fmt, ...) do { \
118 fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
121 static inline int is_power_of_2(unsigned long long n)
123 return (n != 0 && ((n & (n - 1)) == 0));
127 * simple_strtoX - convert a hex/dec/oct string into a number
128 * @snum: buffer to convert
129 * @error: set to 1 when buffer isn't fully consumed
131 * These functions are similar to the standard strtoX() functions, but they are
132 * a little bit easier to use if you want to convert full string of digits into
133 * the binary form. The typical usage:
138 * num = simple_strtoul(str, &error);
139 * if (error || ... if needed, your check that num is not out of range ...)
142 #define simple_strtoX(func, type) \
143 static inline type simple_##func(const char *snum, int *error) \
146 type ret = func(snum, &endptr, 0); \
148 if (error && (!*snum || *endptr)) { \
149 errmsg("%s: unable to parse the number '%s'", #func, snum); \
155 simple_strtoX(strtol, long int)
156 simple_strtoX(strtoll, long long int)
157 simple_strtoX(strtoul, unsigned long int)
158 simple_strtoX(strtoull, unsigned long long int)
160 /* Simple version-printing for utils */
161 #define common_print_version() \
163 printf("%s %s\n", PROGRAM_NAME, VERSION); \
166 #include "libmtd_xalloc.h"
172 #endif /* !__MTD_UTILS_COMMON_H__ */