output_buffer: only realloc once, and memset just what we need
[fio.git] / lib / 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>
33
34#ifndef PROGRAM_NAME
35# error "You must define PROGRAM_NAME before including this header"
36#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#ifndef MIN /* some C lib headers define this for us */
43#define MIN(a, b) ((a) < (b) ? (a) : (b))
44#endif
45#ifndef MAX
46#define MAX(a, b) ((a) > (b) ? (a) : (b))
47#endif
48#define min(a, b) MIN(a, b) /* glue for linux kernel source */
49#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
50
51#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
52#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
53
54#define min_t(t,x,y) ({ \
55 typeof((x)) _x = (x); \
56 typeof((y)) _y = (y); \
57 (_x < _y) ? _x : _y; \
58})
59
60#define max_t(t,x,y) ({ \
61 typeof((x)) _x = (x); \
62 typeof((y)) _y = (y); \
63 (_x > _y) ? _x : _y; \
64})
65
66#ifndef O_CLOEXEC
67#define O_CLOEXEC 0
68#endif
69
70/* define a print format specifier for off_t */
71#ifdef __USE_FILE_OFFSET64
72#define PRIxoff_t PRIx64
73#define PRIdoff_t PRId64
74#else
75#define PRIxoff_t "l"PRIx32
76#define PRIdoff_t "l"PRId32
77#endif
78
79/* Verbose messages */
80#define bareverbose(verbose, fmt, ...) do { \
81 if (verbose) \
82 printf(fmt, ##__VA_ARGS__); \
83} while(0)
84#define verbose(verbose, fmt, ...) \
85 bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
86
87/* Normal messages */
88#define normsg_cont(fmt, ...) do { \
89 printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__); \
90} while(0)
91#define normsg(fmt, ...) do { \
92 normsg_cont(fmt "\n", ##__VA_ARGS__); \
93} while(0)
94
95/* Error messages */
96#define errmsg(fmt, ...) ({ \
97 fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
98 -1; \
99})
100#define errmsg_die(fmt, ...) do { \
101 exit(errmsg(fmt, ##__VA_ARGS__)); \
102} while(0)
103
104/* System error messages */
105#define sys_errmsg(fmt, ...) ({ \
106 int _err = errno; \
107 errmsg(fmt, ##__VA_ARGS__); \
108 fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
109 "", _err, strerror(_err)); \
110 -1; \
111})
112#define sys_errmsg_die(fmt, ...) do { \
113 exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
114} while(0)
115
116/* Warnings */
117#define warnmsg(fmt, ...) do { \
118 fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
119} while(0)
120
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))
126#undef rpmatch
127#define rpmatch __rpmatch
128static inline int __rpmatch(const char *resp)
129{
130 return (resp[0] == 'y' || resp[0] == 'Y') ? 1 :
131 (resp[0] == 'n' || resp[0] == 'N') ? 0 : -1;
132}
133#endif
134#endif
135
136/**
137 * prompt the user for confirmation
138 */
139static inline bool prompt(const char *msg, bool def)
140{
141 char *line = NULL;
142 size_t len;
143 bool ret = def;
144
145 do {
146 normsg_cont("%s (%c/%c) ", msg, def ? 'Y' : 'y', def ? 'n' : 'N');
147 fflush(stdout);
148
149 while (getline(&line, &len, stdin) == -1) {
150 printf("failed to read prompt; assuming '%s'\n",
151 def ? "yes" : "no");
152 break;
153 }
154
155 if (strcmp("\n", line) != 0) {
156 switch (rpmatch(line)) {
157 case 0: ret = false; break;
158 case 1: ret = true; break;
159 case -1:
160 puts("unknown response; please try again");
161 continue;
162 }
163 }
164 break;
165 } while (1);
166
167 free(line);
168
169 return ret;
170}
171
172static inline int is_power_of_2(unsigned long long n)
173{
174 return (n != 0 && ((n & (n - 1)) == 0));
175}
176
177/**
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
181 *
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:
185 *
186 * int error = 0;
187 * unsigned long num;
188 *
189 * num = simple_strtoul(str, &error);
190 * if (error || ... if needed, your check that num is not out of range ...)
191 * error_happened();
192 */
193#define simple_strtoX(func, type) \
194static inline type simple_##func(const char *snum, int *error) \
195{ \
196 char *endptr; \
197 type ret = func(snum, &endptr, 0); \
198 \
199 if (error && (!*snum || *endptr)) { \
200 errmsg("%s: unable to parse the number '%s'", #func, snum); \
201 *error = 1; \
202 } \
203 \
204 return ret; \
205}
206simple_strtoX(strtol, long int)
207simple_strtoX(strtoll, long long int)
208simple_strtoX(strtoul, unsigned long int)
209simple_strtoX(strtoull, unsigned long long int)
210
211/* Simple version-printing for utils */
212#define common_print_version() \
213do { \
214 printf("%s %s\n", PROGRAM_NAME, VERSION); \
215} while (0)
216
217#include "libmtd_xalloc.h"
218
219#ifdef __cplusplus
220}
221#endif
222
223#endif /* !__MTD_UTILS_COMMON_H__ */