From d1e85b9c4688c52a5c97dfe097fb932f0c7c3bbd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 9 Jul 2017 02:13:00 -0500 Subject: dd cleanup: binary search is overkill, eliminate struct pair, simplify strstarteq, use local variable "conv" instead of toys.optargs. --- toys/pending/dd.c | 67 +++++++++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/toys/pending/dd.c b/toys/pending/dd.c index 2160e723..aebc90e8 100644 --- a/toys/pending/dd.c +++ b/toys/pending/dd.c @@ -7,7 +7,7 @@ * * todo: ctrl-c doesn't work, the read() is restarting. -USE_DD(NEWTOY(dd, NULL, TOYFLAG_USR|TOYFLAG_BIN)) +USE_DD(NEWTOY(dd, 0, TOYFLAG_USR|TOYFLAG_BIN)) config DD bool "dd" @@ -52,22 +52,10 @@ GLOBALS( } in, out; ); -#define C_SYNC 0x0100 -#define C_FSYNC 0x0200 -#define C_NOERROR 0x0400 -#define C_NOTRUNC 0x0800 - -struct pair { - char *name; - unsigned val; -}; - -static struct pair clist[] = { - { "fsync", C_FSYNC }, - { "noerror", C_NOERROR }, - { "notrunc", C_NOTRUNC }, - { "sync", C_SYNC }, -}; +#define C_FSYNC 1 +#define C_NOERROR 2 +#define C_NOTRUNC 4 +#define C_SYNC 8 static void status() { @@ -112,24 +100,14 @@ int strstarteq(char **a, char *b) { char *aa = *a; - if (!strstart(&aa, b)) return 0; - if (*aa != '=') return 0; - *a = ++aa; - - return 1; -} - -static int comp(const void *a, const void *b) //const to shut compiler up -{ - return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name); + return strstart(&aa, b) && *aa == '=' && (*a = aa+1); } void dd_main() { - struct pair *res, key; char **args; unsigned long long bs = 0; - int trunc = O_TRUNC; + int trunc = O_TRUNC, conv = 0; TT.show_xfer = TT.show_records = 1; TT.c_count = ULLONG_MAX; @@ -154,13 +132,14 @@ void dd_main() else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0; else error_exit("unknown status '%s'", arg); } else if (strstarteq(&arg, "conv")) { - while (arg) { - key.name = strsep(&arg, ","); - if (!(res = bsearch(&key, clist, ARRAY_LEN(clist), - sizeof(struct pair), comp))) - error_exit("unknown conversion %s", key.name); - - toys.optflags |= res->val; + char *ss, *convs[] = {"fsync", "noerror", "notrunc", "sync"}; + int i, len; + + while ((ss = comma_iterate(&arg, &len))) { + for (i = 0; i