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__
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 */
50 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
52 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
53 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
55 #define min_t(t,x,y) ({ \
56 typeof((x)) _x = (x); \
57 typeof((y)) _y = (y); \
58 (_x < _y) ? _x : _y; \
61 #define max_t(t,x,y) ({ \
62 typeof((x)) _x = (x); \
63 typeof((y)) _y = (y); \
64 (_x > _y) ? _x : _y; \
71 /* define a print format specifier for off_t */
72 #ifdef __USE_FILE_OFFSET64
73 #define PRIxoff_t PRIx64
74 #define PRIdoff_t PRId64
76 #define PRIxoff_t "l"PRIx32
77 #define PRIdoff_t "l"PRId32
80 /* Verbose messages */
81 #define bareverbose(verbose, fmt, ...) do { \
83 printf(fmt, ##__VA_ARGS__); \
85 #define verbose(verbose, fmt, ...) \
86 bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
89 #define normsg_cont(fmt, ...) do { \
90 printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__); \
92 #define normsg(fmt, ...) do { \
93 normsg_cont(fmt "\n", ##__VA_ARGS__); \
97 #define errmsg(fmt, ...) ({ \
98 fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
101 #define errmsg_die(fmt, ...) do { \
102 exit(errmsg(fmt, ##__VA_ARGS__)); \
105 /* System error messages */
106 #define sys_errmsg(fmt, ...) ({ \
108 errmsg(fmt, ##__VA_ARGS__); \
109 fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
110 "", _err, strerror(_err)); \
113 #define sys_errmsg_die(fmt, ...) do { \
114 exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
118 #define warnmsg(fmt, ...) do { \
119 fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
122 #if defined(__UCLIBC__)
123 /* uClibc versions before 0.9.34 don't have rpmatch() */
124 #if __UCLIBC_MAJOR__ == 0 && \
125 (__UCLIBC_MINOR__ < 9 || \
126 (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 34))
128 #define rpmatch __rpmatch
129 static inline int __rpmatch(const char *resp)
131 return (resp[0] == 'y' || resp[0] == 'Y') ? 1 :
132 (resp[0] == 'n' || resp[0] == 'N') ? 0 : -1;
138 * prompt the user for confirmation
140 static inline bool prompt(const char *msg, bool def)
147 normsg_cont("%s (%c/%c) ", msg, def ? 'Y' : 'y', def ? 'n' : 'N');
150 while (getline(&line, &len, stdin) == -1) {
151 printf("failed to read prompt; assuming '%s'\n",
156 if (strcmp("\n", line) != 0) {
157 switch (rpmatch(line)) {
158 case 0: ret = false; break;
159 case 1: ret = true; break;
161 puts("unknown response; please try again");
173 static inline int is_power_of_2(unsigned long long n)
175 return (n != 0 && ((n & (n - 1)) == 0));
179 * simple_strtoX - convert a hex/dec/oct string into a number
180 * @snum: buffer to convert
181 * @error: set to 1 when buffer isn't fully consumed
183 * These functions are similar to the standard strtoX() functions, but they are
184 * a little bit easier to use if you want to convert full string of digits into
185 * the binary form. The typical usage:
190 * num = simple_strtoul(str, &error);
191 * if (error || ... if needed, your check that num is not out of range ...)
194 #define simple_strtoX(func, type) \
195 static inline type simple_##func(const char *snum, int *error) \
198 type ret = func(snum, &endptr, 0); \
200 if (error && (!*snum || *endptr)) { \
201 errmsg("%s: unable to parse the number '%s'", #func, snum); \
207 simple_strtoX(strtol, long int)
208 simple_strtoX(strtoll, long long int)
209 simple_strtoX(strtoul, unsigned long int)
210 simple_strtoX(strtoull, unsigned long long int)
212 /* Simple version-printing for utils */
213 #define common_print_version() \
215 printf("%s %s\n", PROGRAM_NAME, VERSION); \
218 #include "libmtd_xalloc.h"
224 #endif /* !__MTD_UTILS_COMMON_H__ */