From 4aca87515a5083ae0e31ce3177189fd43b6d05ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 3 Jan 2015 13:58:15 +0100 Subject: patch to Vanilla Tomato 1.28 --- release/src/router/busybox/shell/match.c | 140 +++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 release/src/router/busybox/shell/match.c (limited to 'release/src/router/busybox/shell/match.c') diff --git a/release/src/router/busybox/shell/match.c b/release/src/router/busybox/shell/match.c new file mode 100644 index 00000000..47038d66 --- /dev/null +++ b/release/src/router/busybox/shell/match.c @@ -0,0 +1,140 @@ +/* + * ##/%% variable matching code ripped out of ash shell for code sharing + * + * Copyright (c) 1989, 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Copyright (c) 1997-2005 Herbert Xu + * was re-ported from NetBSD and debianized. + * + * This code is derived from software contributed to Berkeley by + * Kenneth Almquist. + * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. + * + * Original BSD copyright notice is retained at the end of this file. + */ +#ifdef STANDALONE +# include +# include +# include +# include +# include +#else +# include "libbb.h" +#endif +#include +#include "match.h" + +#define pmatch(a, b) !fnmatch((a), (b), 0) + +char *scanleft(char *string, char *pattern, bool zero) +{ + char c; + char *loc = string; + + do { + int match; + const char *s; + + c = *loc; + if (zero) { + *loc = '\0'; + s = string; + } else + s = loc; + match = pmatch(pattern, s); + *loc = c; + + if (match) + return loc; + + loc++; + } while (c); + + return NULL; +} + +char *scanright(char *string, char *pattern, bool zero) +{ + char c; + char *loc = string + strlen(string); + + while (loc >= string) { + int match; + const char *s; + + c = *loc; + if (zero) { + *loc = '\0'; + s = string; + } else + s = loc; + match = pmatch(pattern, s); + *loc = c; + + if (match) + return loc; + + loc--; + } + + return NULL; +} + +#ifdef STANDALONE +int main(int argc, char *argv[]) +{ + char *string; + char *op; + char *pattern; + bool zero; + char *loc; + + int i; + + if (argc == 1) { + puts( + "Usage: match [test...]\n\n" + "Where a is the form: \n" + "This is to test the shell ${var#val} expression type.\n\n" + "e.g. `match 'abc#a*'` -> bc" + ); + return 1; + } + + for (i = 1; i < argc; ++i) { + size_t off; + scan_t scan; + + printf("'%s': ", argv[i]); + + string = strdup(argv[i]); + off = strcspn(string, "#%"); + if (!off) { + printf("invalid format\n"); + free(string); + continue; + } + op = string + off; + scan = pick_scan(op[0], op[1], &zero); + pattern = op + 1; + if (op[0] == op[1]) + op[1] = '\0', ++pattern; + op[0] = '\0'; + + loc = scan(string, pattern, zero); + + if (zero) { + printf("'%s'\n", loc); + } else { + *loc = '\0'; + printf("'%s'\n", string); + } + + free(string); + } + + return 0; +} +#endif -- cgit v1.2.3-54-g00ecf