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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Imported from mtd-utils by dehrenberg */
21 #ifndef __MTD_UTILS_COMMON_H__
22 #define __MTD_UTILS_COMMON_H__
35 # error "You must define PROGRAM_NAME before including this header"
42 #ifndef MIN /* some C lib headers define this for us */
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define MAX(a, b) ((a) > (b) ? (a) : (b))
48 #define min(a, b) MIN(a, b) /* glue for linux kernel source */
49 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
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 #if defined(__UCLIBC__)
122 /* uClibc versions before 0.9.34 don't have rpmatch() */
123 #if __UCLIBC_MAJOR__ == 0 && \
124 (__UCLIBC_MINOR__ < 9 || \
125 (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 34))
127 #define rpmatch __rpmatch
128 static inline int __rpmatch(const char *resp)
130 return (resp[0] == 'y' || resp[0] == 'Y') ? 1 :
131 (resp[0] == 'n' || resp[0] == 'N') ? 0 : -1;
137 * prompt the user for confirmation
139 static inline bool prompt(const char *msg, bool def)
146 normsg_cont("%s (%c/%c) ", msg, def ? 'Y' : 'y', def ? 'n' : 'N');
149 while (getline(&line, &len, stdin) == -1) {
150 printf("failed to read prompt; assuming '%s'\n",
155 if (strcmp("\n", line) != 0) {
156 switch (rpmatch(line)) {
157 case 0: ret = false; break;
158 case 1: ret = true; break;
160 puts("unknown response; please try again");
172 static inline int is_power_of_2(unsigned long long n)
174 return (n != 0 && ((n & (n - 1)) == 0));
178 * simple_strtoX - convert a hex/dec/oct string into a number
179 * @snum: buffer to convert
180 * @error: set to 1 when buffer isn't fully consumed
182 * These functions are similar to the standard strtoX() functions, but they are
183 * a little bit easier to use if you want to convert full string of digits into
184 * the binary form. The typical usage:
189 * num = simple_strtoul(str, &error);
190 * if (error || ... if needed, your check that num is not out of range ...)
193 #define simple_strtoX(func, type) \
194 static inline type simple_##func(const char *snum, int *error) \
197 type ret = func(snum, &endptr, 0); \
199 if (error && (!*snum || *endptr)) { \
200 errmsg("%s: unable to parse the number '%s'", #func, snum); \
206 simple_strtoX(strtol, long int)
207 simple_strtoX(strtoll, long long int)
208 simple_strtoX(strtoul, unsigned long int)
209 simple_strtoX(strtoull, unsigned long long int)
211 /* Simple version-printing for utils */
212 #define common_print_version() \
214 printf("%s %s\n", PROGRAM_NAME, VERSION); \
217 #include "libmtd_xalloc.h"
223 #endif /* !__MTD_UTILS_COMMON_H__ */