summaryrefslogtreecommitdiff
blob: b73fbff8c7a2ac356f764d2aa79b3a362b3082b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
From fb187b4c710714a56d74af8a50adccd6118445e7 Mon Sep 17 00:00:00 2001
From: zwpwjwtz <zwpwjwtz@126.com>
Date: Tue, 29 Mar 2016 15:36:04 +0800
Subject: [PATCH] Replace parse_args() with argp_parse() from argp.h

---
 f3read.c  | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 f3write.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 utils.c   |  92 ----------------------------------------------------
 utils.h   |   5 ---
 4 files changed, 200 insertions(+), 113 deletions(-)

diff --git a/f3read.c b/f3read.c
index 2dc6942..683232d 100644
--- a/f3read.c
+++ b/f3read.c
@@ -12,8 +12,100 @@
 #include <unistd.h>
 #include <err.h>
 #include <sys/time.h>
+#include <limits.h>
+#include <argp.h>
 
 #include "utils.h"
+#include "version.h"
+
+/* Argp's global variables. */
+const char *argp_program_version = "F3 Read " F3_STR_VERSION;
+
+/* Arguments. */
+static char adoc[] = "<PATH>";
+
+static char doc[] = "F3 Read -- test real flash memory capacity\n"
+    "Copyright (C) 2010 Digirati Internet LTDA.\n"
+	"This is free software; see the source for copying conditions.\n";
+
+static struct argp_option options[] = {
+	{"start-at",		's',	"NUM",		0,
+		"Disk type of the partition table",			0},
+	{"end-at",		'e',	"NUM",		0,
+		"Type of the file system of the partition",		0},
+	{"progress",			'p',	NULL,		0,
+		"Show progress of the operation (default)",				0},
+	{ 0 }
+};
+
+struct args {
+	long        start_at;
+	long        end_at;
+	int	    show_progress;
+	const char  *dev_path;
+};
+
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
+{
+	struct args *args = state->input;
+	char *endptr;
+
+	switch (key) {
+	case 's':
+	    args->start_at = strtol(arg, &endptr, 10);
+	    if (*endptr != '\0')
+            argp_error(state, "Option --start-at must be a number");
+		break;
+
+	case 'e':
+	    args->end_at = strtol(arg, &endptr, 10);
+	    if (*endptr != '\0')
+            argp_error(state, "Option --end-at must be a number");
+		break;
+
+	case 'p':
+		args->show_progress = 1;
+		break;
+
+	case ARGP_KEY_INIT:
+		args->dev_path = NULL;
+		args->start_at = 0;
+		args->end_at = LONG_MAX;
+		args->show_progress = 0;
+		break;
+
+	case ARGP_KEY_ARG:
+		if (args->dev_path)
+			argp_error(state,
+				"Wrong number of arguments; only one is allowed");
+		args->dev_path = arg;
+		break;
+
+	case ARGP_KEY_END:
+		if (!args->dev_path)
+			argp_error(state,
+				"The disk path was not specified");
+
+        if (args->start_at < 0)
+			argp_error(state,
+				"Option --start-at must be greater than 0");
+
+		if (args->end_at < 0)
+			argp_error(state,
+				"Option --end-at must be greater than 0");
+
+		if (args->start_at > args->end_at)
+			argp_error(state,
+				"Option --start-at must be less or equal to option --end-at");
+		break;
+
+	default:
+		return ARGP_ERR_UNKNOWN;
+	}
+	return 0;
+}
+
+static struct argp argp = {options, parse_opt, adoc, doc, NULL, NULL, NULL};
 
 static inline void update_dt(struct timeval *dt, const struct timeval *t1,
 	const struct timeval *t2)
@@ -228,20 +320,21 @@ static void iterate_files(const char *path, const long *files,
 
 int main(int argc, char **argv)
 {
-	long start_at, end_at;
-	const char *path;
 	const long *files;
-	int progress;
+	struct args args;
 	int rc;
-
-	rc = parse_args("read", argc, argv, &start_at, &end_at, &path);
+	rc = argp_parse(&argp, argc, argv, 0, NULL, &args);
 	if (rc)
 		return rc;
 
-	files = ls_my_files(path, start_at, end_at);
 	/* If stdout isn't a terminal, supress progress. */
-	progress = isatty(STDOUT_FILENO);
-	iterate_files(path, files, start_at, end_at, progress);
+	if (!args.show_progress)
+        args.show_progress = isatty(STDOUT_FILENO);
+
+	files = ls_my_files(args.dev_path, args.start_at, args.end_at);
+	/* If stdout isn't a terminal, supress progress. */
+
+	iterate_files(args.dev_path, files, args.start_at, args.end_at, args.show_progress);
 	free((void *)files);
 	return 0;
 }
diff --git a/f3write.c b/f3write.c
index b964059..bfef27e 100644
--- a/f3write.c
+++ b/f3write.c
@@ -15,8 +15,99 @@
 #include <unistd.h>
 #include <err.h>
 #include <math.h>
+#include <argp.h>
 
 #include "utils.h"
+#include "version.h"
+
+/* Argp's global variables. */
+const char *argp_program_version = "F3 Write " F3_STR_VERSION;
+
+/* Arguments. */
+static char adoc[] = "<PATH>";
+
+static char doc[] = "F3 Write -- test real flash memory capacity\n"
+    "Copyright (C) 2010 Digirati Internet LTDA.\n"
+	"This is free software; see the source for copying conditions.\n";
+
+static struct argp_option options[] = {
+	{"start-at",		's',	"NUM",		0,
+		"Disk type of the partition table",			0},
+	{"end-at",		'e',	"NUM",		0,
+		"Type of the file system of the partition",		0},
+	{"progress",			'p',	NULL,		0,
+		"Show progress of the operation (default)",				0},
+	{ 0 }
+};
+
+struct args {
+	long        start_at;
+	long        end_at;
+	int	    show_progress;
+	const char  *dev_path;
+};
+
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
+{
+	struct args *args = state->input;
+	char *endptr;
+
+	switch (key) {
+	case 's':
+	    args->start_at = strtol(arg, &endptr, 10);
+	    if (*endptr != '\0')
+            argp_error(state, "Option --start-at must be a number");
+		break;
+
+	case 'e':
+	    args->end_at = strtol(arg, &endptr, 10);
+	    if (*endptr != '\0')
+            argp_error(state, "Option --end-at must be a number");
+		break;
+
+	case 'p':
+		args->show_progress = 1;
+		break;
+
+	case ARGP_KEY_INIT:
+		args->dev_path = NULL;
+		args->start_at = 0;
+		args->end_at = LONG_MAX;
+		args->show_progress = 0;
+		break;
+
+	case ARGP_KEY_ARG:
+		if (args->dev_path)
+			argp_error(state,
+				"Wrong number of arguments; only one is allowed");
+		args->dev_path = arg;
+		break;
+
+	case ARGP_KEY_END:
+		if (!args->dev_path)
+			argp_error(state,
+				"The disk path was not specified");
+
+        if (args->start_at < 0)
+			argp_error(state,
+				"Option --start-at must be greater than 0");
+
+		if (args->end_at < 0)
+			argp_error(state,
+				"Option --end-at must be greater than 0");
+
+		if (args->start_at > args->end_at)
+			argp_error(state,
+				"Option --start-at must be less or equal to option --end-at");
+		break;
+
+	default:
+		return ARGP_ERR_UNKNOWN;
+	}
+	return 0;
+}
+
+static struct argp argp = {options, parse_opt, adoc, doc, NULL, NULL, NULL};
 
 static uint64_t fill_buffer(void *buf, size_t size, uint64_t offset)
 {
@@ -464,17 +555,17 @@ static void unlink_old_files(const char *path, long start_at, long end_at)
 
 int main(int argc, char **argv)
 {
-	long start_at, end_at;
-	const char *path;
-	int progress;
+	struct args args;
 	int rc;
-
-	rc = parse_args("write", argc, argv, &start_at, &end_at, &path);
+	rc = argp_parse(&argp, argc, argv, 0, NULL, &args);
 	if (rc)
 		return rc;
 
-	unlink_old_files(path, start_at, end_at);
+	unlink_old_files(args.dev_path, args.start_at, args.end_at);
+
 	/* If stdout isn't a terminal, supress progress. */
-	progress = isatty(STDOUT_FILENO);
-	return fill_fs(path, start_at, end_at, progress);
+	if (!args.show_progress)
+        args.show_progress = isatty(STDOUT_FILENO);
+
+	return fill_fs(args.dev_path, args.start_at, args.end_at, args.show_progress);
 }
diff --git a/utils.c b/utils.c
index a30ea3b..6aaceff 100644
--- a/utils.c
+++ b/utils.c
@@ -13,13 +13,11 @@
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
-#include <limits.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <errno.h>
 #include <err.h>
 
-#include "version.h"
 #include "utils.h"
 
 const char *adjust_unit(double *ptr_bytes)
@@ -60,87 +58,6 @@ char *full_fn_from_number(const char **filename, const char *path, long num)
 	return str;
 }
 
-/* Parse @param and return the start-at parameter.
- * The string must be of the format "--start-at=NUM"; otherwise it returns -1.
- */
-#define START_AT_TEXT "--start-at="
-#define   END_AT_TEXT   "--end-at="
-
-static inline int is_param(const char *text, const char *param)
-{
-	return !strncmp(param, text, strlen(text));
-}
-
-static long parse_long_param(const char *param)
-{
-	char *endptr;
-	long value;
-
-	/* Skip text. */
-	while (*param != '=') {
-		if (*param == '\0')
-			return -1;
-		param++;
-	}
-	param++; /* Skip '='. */
-
-	value = strtol(param, &endptr, 10);
-	if (*endptr != '\0')
-		return -1;
-
-	return (value <= 0 || value == LONG_MAX) ? -1 : value - 1;
-}
-
-static int parse_param(const char *param, long *pstart_at, long *pend_at)
-{
-	if (is_param(START_AT_TEXT, param))
-		*pstart_at = parse_long_param(param);
-	else if (is_param(END_AT_TEXT, param))
-		*pend_at = parse_long_param(param);
-	else
-		return 1;
-	return 0;
-}
-
-int parse_args(const char *name, int argc, char **argv,
-	long *pstart_at, long *pend_at, const char **ppath)
-{
-	*pstart_at = 0;
-	*pend_at = LONG_MAX - 1;
-
-	switch (argc) {
-	case 2:
-		*ppath = argv[1];
-		break;
-
-	case 3:
-		if (parse_param(argv[1], pstart_at, pend_at))
-			goto error;
-		*ppath = argv[2];
-		break;
-
-	case 4:
-		if (parse_param(argv[1], pstart_at, pend_at))
-			goto error;
-		if (parse_param(argv[2], pstart_at, pend_at))
-			goto error;
-		*ppath = argv[3];
-		break;
-
-	default:
-		goto error;
-	}
-
-	if (*pstart_at >= 0 && *pend_at >= 0 && *pstart_at <= *pend_at)
-		return 0;
-
-error:
-	print_header(stderr, name);
-	fprintf(stderr, "Usage: f3%s [%sNUM] [%sNUM] <PATH>\n",
-		name, START_AT_TEXT, END_AT_TEXT);
-	return 1;
-}
-
 static long number_from_filename(const char *filename)
 {
 	const char *p;
@@ -218,15 +135,6 @@ const long *ls_my_files(const char *path, long start_at, long end_at)
 	return ret;
 }
 
-void print_header(FILE *f, const char *name)
-{
-	fprintf(f,
-	"F3 %s " F3_STR_VERSION "\n"
-	"Copyright (C) 2010 Digirati Internet LTDA.\n"
-	"This is free software; see the source for copying conditions.\n"
-	"\n", name);
-}
-
 #if __APPLE__ && __MACH__
 
 /* This function is a _rough_ approximation of fdatasync(2). */
diff --git a/utils.h b/utils.h
index 6b7c92f..4fdbce2 100644
--- a/utils.h
+++ b/utils.h
@@ -22,13 +22,8 @@ static inline long delay_ms(const struct timeval *t1, const struct timeval *t2)
 		(t2->tv_usec - t1->tv_usec) / 1000;
 }
 
-int parse_args(const char *name, int argc, char **argv,
-	long *pstart_at, long *pend_at, const char **ppath);
-
 const long *ls_my_files(const char *path, long start_at, long end_at);
 
-void print_header(FILE *f, const char *name);
-
 static inline uint64_t random_number(uint64_t prv_number)
 {
 	return prv_number * 4294967311ULL + 17;