Re: env: follow up on argv0 setting feature

2024-03-24 Thread Pádraig Brady

On 23/03/2024 20:24, Matheus Afonso Martins Moreira wrote:

Thank you!! Really happy to have this feature in upstream coreutils!

Would you mind giving me feedback on the patch I sent?
What can I do to make future contributions acceptable?
I'm not very familiar with the GNU development processes.
I just tried to follow the coding conventions.


Hi Matheus.
The main reason I didn't adjust your patch and
wait for feedback etc. is I wanted to get this in the release,
which is already now cut and in testing.
Also I'd reimplemented to support a NULL argv0
(but then went back on this after further consideration).

The main changes I did were to change what the option was called,
add texinfo documentation, and tests.
Also the logic after the execvp() was adjusted to use the
passed --argv0 value, rather than the command name.

I should have at least added you to the THANKS file,
which I'd done so now:
https://github.com/coreutils/coreutils/commit/e0cce6b62

thank you!
Pádraig



Re: env: follow up on argv0 setting feature

2024-03-21 Thread Pádraig Brady

On 20/03/2024 23:19, Pádraig Brady wrote:

On 13/03/2024 10:19, Matheus Afonso Martins Moreira wrote:

About a year ago, I posted an env feature request on this list:
the ability to set the value of argv[0].

https://lists.gnu.org/archive/html/coreutils/2023-03/msg2.html

I also sent a patch:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg3.html

After some discussion, I was informed it was under consideration:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg00012.html


We're still considering and will adjust as needed.


I waited a while and eventually sent an email about it:

https://lists.gnu.org/archive/html/coreutils/2023-08/msg00059.html

The maintainer noted that they expected this feature to be included
in the next release which would be focused on features:

https://lists.gnu.org/archive/html/coreutils/2023-08/msg00060.html


The next release will focus on new features, and this
will be considered. I expect this feature will be included.


Since then I've been tracking commits to the coreutils master branch
but it appears the feature has not landed yet.

What happened? Was it rejected?



Thanks for your patience.
I've attached an implementation for --argv0
which I intend to apply for the impending release.
Note this can accept empty and NULL values,
and so now gives env full control of the args it passes on.


In testing this on a newer Linux kernel I see that argv[0] == NULL
is converted to an empty string instead.
That was one of the proposals at https://lwn.net/Articles/882799/
and I see it was implemented on kernels >= 5.18:
https://github.com/torvalds/linux/commit/dcd46d89

So in retrospect it's probably not worth supporting a NULL argv0,
as newer kernels won't pass it.  Also with a NULL argv0,
the kernel puts the environ array directly after the first NULL,
and so will discard any other params passed to env,
which would be confusing to users.

Version 2 attached, now makes --argv0 require an argument.

thanks,
Pádraig.From 38653861b6e353e9f306d92e2e96179040e5b9d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= 
Date: Thu, 2 Mar 2023 11:56:18 -0300
Subject: [PATCH] env: add --argv0 to set the first argument passed to exec

Using the shell's exec -a feature can be awkward
so add support for setting argv0 to arbitrary values.
This gives env full control over the arguments it passes.

* src/env.c: Accept --argv0 and set argv[0] appropriately.
* tests/env/env.sh: Add test cases.
* doc/coreutils.texi (env invocation): Describe --argv0.
* NEWS: Mention the new feature.
---
 NEWS   |  3 +++
 doc/coreutils.texi |  7 +++
 src/env.c  | 31 ++-
 tests/env/env.sh   | 10 ++
 4 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index cb4762188..4110cafaf 100644
--- a/NEWS
+++ b/NEWS
@@ -92,6 +92,9 @@ GNU coreutils NEWS-*- outline -*-
   and the command exits with failure status if existing files.
   The -n,--no-clobber option is best avoided due to platform differences.
 
+  env now accepts the --argv0 option to override the zeroth argument
+  of the command being executed.
+
   mv now accepts an --exchange option, which causes the source and
   destination to be exchanged.  It should be combined with
   --no-target-directory (-T) if the destination is a directory.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index e36269588..5f7646039 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -17948,6 +17948,13 @@ Options must precede operands.
 
 @optNull
 
+@item -a @var{arg}
+@itemx --argv0=@var{arg}
+@opindex -a
+@opindex --argv0
+Override the the zeroth argument passed to the command being executed.
+Without this option a default value of @var{command} is used.
+
 @item -u @var{name}
 @itemx --unset=@var{name}
 @opindex -u
diff --git a/src/env.c b/src/env.c
index ed6628f8f..af876fa08 100644
--- a/src/env.c
+++ b/src/env.c
@@ -73,7 +73,7 @@ static bool report_signal_handling;
 /* The isspace characters in the C locale.  */
 #define C_ISSPACE_CHARS " \t\n\v\f\r"
 
-static char const shortopts[] = "+C:iS:u:v0" C_ISSPACE_CHARS;
+static char const shortopts[] = "+a:C:iS:u:v0" C_ISSPACE_CHARS;
 
 /* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
@@ -87,6 +87,7 @@ enum
 
 static struct option const longopts[] =
 {
+  {"argv0", required_argument, nullptr, 'a'},
   {"ignore-environment", no_argument, nullptr, 'i'},
   {"null", no_argument, nullptr, '0'},
   {"unset", required_argument, nullptr, 'u'},
@@ -118,6 +119,9 @@ Set each NAME to VALUE in the environment and run COMMAND.\n\
 
   emit_mandatory_arg_note ();
 
+  fputs (_("\
+  -a, --argv0=ARG  pass ARG as the zeroth argument of COMMAND\n\
+"), stdout);
   fputs (_("\
   -i, --ignore-environment  start with an empty environment\n\
   -0, --null   end each output line with 

Re: env: follow up on argv0 setting feature

2024-03-20 Thread Pádraig Brady

On 13/03/2024 10:19, Matheus Afonso Martins Moreira wrote:

About a year ago, I posted an env feature request on this list:
the ability to set the value of argv[0].

https://lists.gnu.org/archive/html/coreutils/2023-03/msg2.html

I also sent a patch:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg3.html

After some discussion, I was informed it was under consideration:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg00012.html


We're still considering and will adjust as needed.


I waited a while and eventually sent an email about it:

https://lists.gnu.org/archive/html/coreutils/2023-08/msg00059.html

The maintainer noted that they expected this feature to be included
in the next release which would be focused on features:

https://lists.gnu.org/archive/html/coreutils/2023-08/msg00060.html


The next release will focus on new features, and this
will be considered. I expect this feature will be included.


Since then I've been tracking commits to the coreutils master branch
but it appears the feature has not landed yet.

What happened? Was it rejected?



Thanks for your patience.
I've attached an implementation for --argv0
which I intend to apply for the impending release.
Note this can accept empty and NULL values,
and so now gives env full control of the args it passes on.

thanks,
Pádraig.From a2f4ef43c67450d13a12bc63c72f16c690670dfa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= 
Date: Thu, 2 Mar 2023 11:56:18 -0300
Subject: [PATCH] env: add --argv0 to set the first argument passed to exec

Using the shell's exec -a feature can be awkward
so add support for setting argv0 to arbitrary values.
This gives env full control over the arguments it passes.

* src/env.c: Accept --argv0 and set argv[0] appropriately.
* tests/env/env.sh: Add test cases.
* doc/coreutils.texi (env invocation): Describe --argv0.
* NEWS: Mention the new feature.
---
 NEWS   |  3 +++
 doc/coreutils.texi |  8 
 src/env.c  | 31 +++
 tests/env/env.sh   | 26 ++
 4 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index b3004273b..839582c16 100644
--- a/NEWS
+++ b/NEWS
@@ -92,6 +92,9 @@ GNU coreutils NEWS-*- outline -*-
   and the command exits with failure status if existing files.
   The -n,--no-clobber option is best avoided due to platform differences.
 
+  env now accepts the --argv0 option to override the zeroth argument
+  of the command being executed.
+
   od now supports printing IEEE half precision floating point with -t fH,
   or brain 16 bit floating point with -t fB, where supported by the compiler.
 
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index ec15a467f..766925623 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -17927,6 +17927,14 @@ Options must precede operands.
 
 @optNull
 
+@item --argv0[=@var{arg}]
+@opindex --argv0
+By default @var{command} is used as the zeroth argument passed to
+the command being executed, which can be overridden with this option.
+@var{arg} is optional, and set to the NULL is not specified.
+For example, @samp{--argv0=} would pass the empty string,
+while @samp{--argv0} would pass a NULL pointer.
+
 @item -u @var{name}
 @itemx --unset=@var{name}
 @opindex -u
diff --git a/src/env.c b/src/env.c
index ed6628f8f..dc08b2dba 100644
--- a/src/env.c
+++ b/src/env.c
@@ -79,7 +79,8 @@ static char const shortopts[] = "+C:iS:u:v0" C_ISSPACE_CHARS;
non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
 enum
 {
-  DEFAULT_SIGNAL_OPTION = CHAR_MAX + 1,
+  ARGV0_OPTION = CHAR_MAX + 1,
+  DEFAULT_SIGNAL_OPTION,
   IGNORE_SIGNAL_OPTION,
   BLOCK_SIGNAL_OPTION,
   LIST_SIGNAL_HANDLING_OPTION,
@@ -87,6 +88,7 @@ enum
 
 static struct option const longopts[] =
 {
+  {"argv0", optional_argument, nullptr, ARGV0_OPTION},
   {"ignore-environment", no_argument, nullptr, 'i'},
   {"null", no_argument, nullptr, '0'},
   {"unset", required_argument, nullptr, 'u'},
@@ -119,6 +121,9 @@ Set each NAME to VALUE in the environment and run COMMAND.\n\
   emit_mandatory_arg_note ();
 
   fputs (_("\
+  --argv0[=ARG]pass ARG as the zeroth argument of COMMAND\n\
+"), stdout);
+  fputs (_("\
   -i, --ignore-environment  start with an empty environment\n\
   -0, --null   end each output line with NUL, not newline\n\
   -u, --unset=NAME remove variable from the environment\n\
@@ -759,6 +764,7 @@ main (int argc, char **argv)
   bool ignore_environment = false;
   bool opt_nul_terminate_output = false;
   char const *newdir = nullptr;
+  char *argv0 = *argv;  /* Initialize to a distinct but unused value.  */
 
   initialize_main (, );
   set_program_name (argv[0]);
@@ -775,6 +781,9 @@ main (int argc, char **argv)
 {
   switch (optc)
 {
+case ARGV0_OPTION:
+  argv0 = optarg;
+  break;
 case 'i':
   ignore_environment = true;

Re: env: follow up on argv0 setting feature

2023-08-29 Thread Dragan Simic

On 2023-08-29 22:45, Rob Landley wrote:

On 8/25/23 08:18, Pádraig Brady wrote:

On 25/08/2023 03:48, Matheus Afonso Martins Moreira wrote:

About five months ago, I posted an env feature request on this list:
the ability to set the value of argv[0].

https://lists.gnu.org/archive/html/coreutils/2023-03/msg2.html

I also sent a patch:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg3.html

After some discussion, I was informed it was under consideration:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg00012.html


We're still considering and will adjust as needed.


Since then I've been tracking commits to the coreutils master branch.
I didn't see anything related to this feature in these past few 
months

so I thought I'd follow up here.

Has a decision been made?


We're currently about to release a version focusing on stability.
The next release will focus on new features, and this
will be considered. I expect this feature will be included.

thanks for the reminder,


And now that 9.4 is out I remind you of cut -DFO as well.


Chirp, chirp, "cut -m" is around as well. :)


Thanks,

Rob




Re: env: follow up on argv0 setting feature

2023-08-29 Thread Rob Landley
On 8/25/23 08:18, Pádraig Brady wrote:
> On 25/08/2023 03:48, Matheus Afonso Martins Moreira wrote:
>> About five months ago, I posted an env feature request on this list:
>> the ability to set the value of argv[0].
>> 
>> https://lists.gnu.org/archive/html/coreutils/2023-03/msg2.html
>> 
>> I also sent a patch:
>> 
>> https://lists.gnu.org/archive/html/coreutils/2023-03/msg3.html
>> 
>> After some discussion, I was informed it was under consideration:
>> 
>> https://lists.gnu.org/archive/html/coreutils/2023-03/msg00012.html
>> 
>>> We're still considering and will adjust as needed.
>> 
>> Since then I've been tracking commits to the coreutils master branch.
>> I didn't see anything related to this feature in these past few months
>> so I thought I'd follow up here.
>> 
>> Has a decision been made?
> 
> We're currently about to release a version focusing on stability.
> The next release will focus on new features, and this
> will be considered. I expect this feature will be included.
> 
> thanks for the reminder,

And now that 9.4 is out I remind you of cut -DFO as well.

Thanks,

Rob



Re: env: follow up on argv0 setting feature

2023-08-25 Thread Pádraig Brady

On 25/08/2023 03:48, Matheus Afonso Martins Moreira wrote:

About five months ago, I posted an env feature request on this list:
the ability to set the value of argv[0].

https://lists.gnu.org/archive/html/coreutils/2023-03/msg2.html

I also sent a patch:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg3.html

After some discussion, I was informed it was under consideration:

https://lists.gnu.org/archive/html/coreutils/2023-03/msg00012.html


We're still considering and will adjust as needed.


Since then I've been tracking commits to the coreutils master branch.
I didn't see anything related to this feature in these past few months
so I thought I'd follow up here.

Has a decision been made?


We're currently about to release a version focusing on stability.
The next release will focus on new features, and this
will be considered. I expect this feature will be included.

thanks for the reminder,

Pádraig