blob: 817d5e7356a95da2cb7df258e4e94c72e4e0b39d [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Copyright (c) Artem Bityutskiy, 2007, 2008
3 *
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.
8 *
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.
13 *
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.
17 */
18
19#ifndef __MTD_UTILS_COMMON_H__
20#define __MTD_UTILS_COMMON_H__
21
22#include <stdbool.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <ctype.h>
26#include <string.h>
27#include <fcntl.h>
28#include <errno.h>
29#include <features.h>
30#include <inttypes.h>
31
32#ifndef PROGRAM_NAME
33# error "You must define PROGRAM_NAME before including this header"
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifndef MIN /* some C lib headers define this for us */
41#define MIN(a, b) ((a) < (b) ? (a) : (b))
42#endif
43#ifndef MAX
44#define MAX(a, b) ((a) > (b) ? (a) : (b))
45#endif
46#define min(a, b) MIN(a, b) /* glue for linux kernel source */
47#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
48
49#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
50#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
51
52#define min_t(t,x,y) ({ \
53 typeof((x)) _x = (x); \
54 typeof((y)) _y = (y); \
55 (_x < _y) ? _x : _y; \
56})
57
58#define max_t(t,x,y) ({ \
59 typeof((x)) _x = (x); \
60 typeof((y)) _y = (y); \
61 (_x > _y) ? _x : _y; \
62})
63
64#ifndef O_CLOEXEC
65#define O_CLOEXEC 0
66#endif
67
68/* define a print format specifier for off_t */
69#ifdef __USE_FILE_OFFSET64
70#define PRIxoff_t PRIx64
71#define PRIdoff_t PRId64
72#else
73#define PRIxoff_t "l"PRIx32
74#define PRIdoff_t "l"PRId32
75#endif
76
77/* Verbose messages */
78#define bareverbose(verbose, fmt, ...) do { \
79 if (verbose) \
80 printf(fmt, ##__VA_ARGS__); \
81} while(0)
82#define verbose(verbose, fmt, ...) \
83 bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
84
85/* Normal messages */
86#define normsg_cont(fmt, ...) do { \
87 printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__); \
88} while(0)
89#define normsg(fmt, ...) do { \
90 normsg_cont(fmt "\n", ##__VA_ARGS__); \
91} while(0)
92
93/* Error messages */
94#define errmsg(fmt, ...) ({ \
95 fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
96 -1; \
97})
98#define errmsg_die(fmt, ...) do { \
99 exit(errmsg(fmt, ##__VA_ARGS__)); \
100} while(0)
101
102/* System error messages */
103#define sys_errmsg(fmt, ...) ({ \
104 int _err = errno; \
105 errmsg(fmt, ##__VA_ARGS__); \
106 fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
107 "", _err, strerror(_err)); \
108 -1; \
109})
110#define sys_errmsg_die(fmt, ...) do { \
111 exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
112} while(0)
113
114/* Warnings */
115#define warnmsg(fmt, ...) do { \
116 fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
117} while(0)
118
119/* uClibc versions before 0.9.34 and musl don't have rpmatch() */
120#if defined(__UCLIBC__) && \
121 (__UCLIBC_MAJOR__ == 0 && \
122 (__UCLIBC_MINOR__ < 9 || \
123 (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 34))) || \
124 !defined(__GLIBC__)
125#undef rpmatch
126#define rpmatch __rpmatch
127static inline int __rpmatch(const char *resp)
128{
129 return (resp[0] == 'y' || resp[0] == 'Y') ? 1 :
130 (resp[0] == 'n' || resp[0] == 'N') ? 0 : -1;
131}
132#endif
133
134/**
135 * prompt the user for confirmation
136 */
137static inline bool prompt(const char *msg, bool def)
138{
139 char *line = NULL;
140 size_t len;
141 bool ret = def;
142
143 do {
144 normsg_cont("%s (%c/%c) ", msg, def ? 'Y' : 'y', def ? 'n' : 'N');
145 fflush(stdout);
146
147 while (getline(&line, &len, stdin) == -1) {
148 printf("failed to read prompt; assuming '%s'\n",
149 def ? "yes" : "no");
150 break;
151 }
152
153 if (strcmp("\n", line) != 0) {
154 switch (rpmatch(line)) {
155 case 0: ret = false; break;
156 case 1: ret = true; break;
157 case -1:
158 puts("unknown response; please try again");
159 continue;
160 }
161 }
162 break;
163 } while (1);
164
165 free(line);
166
167 return ret;
168}
169
170static inline int is_power_of_2(unsigned long long n)
171{
172 return (n != 0 && ((n & (n - 1)) == 0));
173}
174
175/**
176 * simple_strtoX - convert a hex/dec/oct string into a number
177 * @snum: buffer to convert
178 * @error: set to 1 when buffer isn't fully consumed
179 *
180 * These functions are similar to the standard strtoX() functions, but they are
181 * a little bit easier to use if you want to convert full string of digits into
182 * the binary form. The typical usage:
183 *
184 * int error = 0;
185 * unsigned long num;
186 *
187 * num = simple_strtoul(str, &error);
188 * if (error || ... if needed, your check that num is not out of range ...)
189 * error_happened();
190 */
191#define simple_strtoX(func, type) \
192static inline type simple_##func(const char *snum, int *error) \
193{ \
194 char *endptr; \
195 type ret = func(snum, &endptr, 0); \
196 \
197 if (error && (!*snum || *endptr)) { \
198 errmsg("%s: unable to parse the number '%s'", #func, snum); \
199 *error = 1; \
200 } \
201 \
202 return ret; \
203}
204simple_strtoX(strtol, long int)
205simple_strtoX(strtoll, long long int)
206simple_strtoX(strtoul, unsigned long int)
207simple_strtoX(strtoull, unsigned long long int)
208
209/* Simple version-printing for utils */
210#define common_print_version() \
211do { \
212 printf("%s %s\n", PROGRAM_NAME, VERSION); \
213} while (0)
214
215#include "xalloc.h"
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif /* !__MTD_UTILS_COMMON_H__ */