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