From 727948e585cb133c32c8d42570e5524c58190307 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 4 Aug 2017 16:23:42 +0200 Subject: getopt32: factor out code to treat all args as options Working towards making getopt32() xmalloc-free function old new delta make_all_argv_opts - 58 +58 top_main 914 912 -2 getopt32 1517 1458 -59 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 0/2 up/down: 58/-61) Total: -3 bytes Signed-off-by: Denys Vlasenko --- libbb/getopt_allopts.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 libbb/getopt_allopts.c (limited to 'libbb/getopt_allopts.c') diff --git a/libbb/getopt_allopts.c b/libbb/getopt_allopts.c new file mode 100644 index 000000000..a67d2b70e --- /dev/null +++ b/libbb/getopt_allopts.c @@ -0,0 +1,27 @@ +/* vi: set sw=4 ts=4: */ +/* + * Copyright (C) 2017 Denys Vlasenko + * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. + */ +#include "libbb.h" + +//kbuild:lib-y += getopt_allopts.o + +void FAST_FUNC make_all_argv_opts(char **argv) +{ + /* Note: we skip argv[0] */ + while (*++argv) { + char *p; + + if (argv[0][0] == '-') + continue; + /* Neither top nor ps care if "" arg turns into "-" */ + /*if (argv[0][0] == '\0') + continue;*/ + p = xmalloc(strlen(*argv) + 2); + *p = '-'; + strcpy(p + 1, *argv); + *argv = p; + } +} -- cgit v1.2.3