Re: [PATCH 6/7] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.
On Tue, Jun 24, 2008 at 05:18:56PM +0000, Linus Torvalds wrote:
Show 20 quoted lines
>
>
> On Tue, 24 Jun 2008, Pierre Habouzit wrote:
> >
> > This way, argv[0] isn't clobbered, to the cost of maybe not having a
> > resulting NULL terminated argv array.
>
> Umm. I think it's much easier to do by always having
>
> ctx->out = argv;
>
> and then just initializing cpix to 0 or 1:
>
> ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
>
> because now parse_options_end() doesn't need to play games any more. It
> doesn't need to care about PARSE_OPT_KEEP_ARGV0, it can just do exactly
> what it always used to do, because "ctx->cpidx + ctx->argc" automatically
> does the right thing (it is both the return value _and_ the index that you
> should fill with NULL.
It indeed is now a trivial patch:
-----8<-----
Subject: [PATCH] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.
This way, argv[0] isn't clobbered.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
parse-options.c | 1 +
parse-options.h | 1 +
2 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 60a11e8..51a44e3 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -248,6 +248,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
ctx->argc = argc - 1;
ctx->argv = argv + 1;
ctx->out = argv;
+ ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
ctx->flags = flags;
}
diff --git a/parse-options.h b/parse-options.h
index b391bb6..6299632 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -20,6 +20,7 @@ enum parse_opt_type {
enum parse_opt_flags {
PARSE_OPT_KEEP_DASHDASH = 1,
PARSE_OPT_STOP_AT_NON_OPTION = 2,
+ PARSE_OPT_KEEP_ARGV0 = 4,
};
enum parse_opt_option_flags {
--
1.5.6.120.g3adb8.dirty