dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Simplify procargs
@ 2025-08-03 16:15 Denys Vlasenko
  2025-08-31  8:45 ` Herbert Xu
  0 siblings, 1 reply; 2+ messages in thread
From: Denys Vlasenko @ 2025-08-03 16:15 UTC (permalink / raw)
  To: DASH shell mailing list

[-- Attachment #1: Type: text/plain, Size: 569 bytes --]

procargs(int argc, char **argv)

argc is used in just one place:
        if (argc > 0)
                xargv++;

Trivially replaceable by if(xargv[0] != NULL), so can avoid passing
this argument.

        char **xargv;
        xargv = argv;

xargv is always equal to argv, so why having a separate variable?

        const char *xminusc;
        xminusc = minusc;

Similar situation with xminusc being equal to minusc
during the range where it is live, they diverge here:

        if (xminusc) {
                minusc = *xargv++;

but after this, xminusc is not used.

[-- Attachment #2: 0001-Simplify-procargs.patch --]
[-- Type: text/x-patch, Size: 2054 bytes --]

From 7f36d8ed920f4dbb7623175df5faef2a09d84d4f Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <dvlasenk@redhat.com>
Date: Sun, 3 Aug 2025 18:13:01 +0200
Subject: [PATCH] Simplify procargs

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
---
 src/main.c    |  2 +-
 src/options.c | 12 ++++--------
 src/options.h |  2 +-
 3 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/main.c b/src/main.c
index 1e192f8..e9771fc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -149,7 +149,7 @@ main(int argc, char **argv)
 	rootpid = getpid();
 	init();
 	setstackmark(&smark);
-	login = procargs(argc, argv);
+	login = procargs(argv);
 	if (login) {
 		state = 1;
 		read_profile("/etc/profile");
diff --git a/src/options.c b/src/options.c
index c74e4fe..3e6c450 100644
--- a/src/options.c
+++ b/src/options.c
@@ -119,26 +119,22 @@ STATIC int getopts(char *, char *, char **);
  */
 
 int
-procargs(int argc, char **argv)
+procargs(char **xargv)
 {
 	int i;
-	const char *xminusc;
-	char **xargv;
 	int login;
 
-	xargv = argv;
 	login = xargv[0] && xargv[0][0] == '-';
 	arg0 = xargv[0];
-	if (argc > 0)
+	if (xargv[0])
 		xargv++;
 	for (i = 0; i < NOPTS; i++)
 		optlist[i] = 2;
 	argptr = xargv;
 	login |= options(1);
 	xargv = argptr;
-	xminusc = minusc;
 	if (*xargv == NULL) {
-		if (xminusc)
+		if (minusc)
 			sh_error("-c requires an argument");
 		sflag = 1;
 	}
@@ -153,7 +149,7 @@ procargs(int argc, char **argv)
 	debug = 1;
 #endif
 	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
-	if (xminusc) {
+	if (minusc) {
 		minusc = *xargv++;
 		if (*xargv)
 			goto setarg0;
diff --git a/src/options.h b/src/options.h
index f421316..0ad5535 100644
--- a/src/options.h
+++ b/src/options.h
@@ -76,7 +76,7 @@ extern char **argptr;		/* argument list for builtin commands */
 extern char *optionarg;		/* set by nextopt */
 extern char *optptr;		/* used by nextopt */
 
-int procargs(int, char **);
+int procargs(char **);
 void optschanged(void);
 void setparam(char **);
 void freeparam(volatile struct shparam *);
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: Simplify procargs
  2025-08-03 16:15 Simplify procargs Denys Vlasenko
@ 2025-08-31  8:45 ` Herbert Xu
  0 siblings, 0 replies; 2+ messages in thread
From: Herbert Xu @ 2025-08-31  8:45 UTC (permalink / raw)
  To: Denys Vlasenko; +Cc: dash

Denys Vlasenko <vda.linux@googlemail.com> wrote:
> 
> procargs(int argc, char **argv)
> 
> argc is used in just one place:
>        if (argc > 0)
>                xargv++;
> 
> Trivially replaceable by if(xargv[0] != NULL), so can avoid passing
> this argument.
> 
>        char **xargv;
>        xargv = argv;
> 
> xargv is always equal to argv, so why having a separate variable?
> 
>        const char *xminusc;
>        xminusc = minusc;
> 
> Similar situation with xminusc being equal to minusc
> during the range where it is live, they diverge here:
> 
>        if (xminusc) {
>                minusc = *xargv++;
> 
> but after this, xminusc is not used.

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-31  8:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-03 16:15 Simplify procargs Denys Vlasenko
2025-08-31  8:45 ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).