Index | Thread | Search

From:
Ingo Schwarze <schwarze@usta.de>
Subject:
Re: [PATCH] Make incorrect ftp(1) usage more obvious
To:
Petr Ročkai <m.v0adgys6@fixp.eu>
Cc:
tech@openbsd.org
Date:
Sat, 3 May 2025 19:36:32 +0200

Download raw body.

Thread
Hello,

Petr Rockai wrote on Sat, May 03, 2025 at 05:42:54PM +0200:
> On Fri, May 02, 2025 at 05:48:48PM +0200, Theo de Raadt wrote:

>> We follow original POSIX getopt(3) rules in every program, using libc code.
>> This applies to ALL commands.  No commands are different.

> This surprised me a bit, because:
> 
>     $ which grep
>     /usr/bin/grep
>     $ grep foo -i foo.txt
>     FOO
> 
> and even
> 
>     $ grep foo foo.txt -i
>     FOO

OUCH.

That's actually a bug (that i wasn't aware of).

This behaviour would be correct:

   $ grep foo foo.txt -i
  grep: -i: No such file or directory
   $ echo $?
  2

Note that our grep(1) manual does NOT even mention the enivronment
variable POSIXLY_CORRECT.  But even if it would mention the variable,
requiring POSIXLY_CORRECT to be set for getting correct behaviour
would still remain a violation.

We also need to be aware that requiring proper argument ordering
is *not* chicanery on the part of POSIX, but is actually useful.
It helps to reduce ambiguity in command line parsing and makes the
meaning of commands more easily predictable.

> Looking at the source, the difference seems to be getopt vs getopt_long
> (and there is a few other base programs that use getopt_long). POSIX
> doesn't seem to mention any --options for grep either.

No,  Every long option is a POSIX violation in the first place,
not only in grep(1), but in every program, no matter whether the
program is standardized or not.

So here is a patch that fixes the bug.
The patch survives /usr/src/regress/usr.bin/grep
but apart from that, it's not really tested yet.

Given how sloppy people who are used to Linux tend to be,
i worry that this might cause half the ports tree to crumble,
so i'm not sure whether i should really propose fixing this bug.

But if we decide to keep the bug, then we have to at least fix our
documentation, saying that our grep(1) intentionally violates POSIX
in this respect.  The manual should probably also say why we make
such a weird choice.

Yours,
  Ingo


Index: grep.c
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.c,v
diff -u -p -r1.67 grep.c
--- grep.c	26 Jun 2022 18:48:10 -0000	1.67
+++ grep.c	3 May 2025 17:10:54 -0000
@@ -125,9 +125,9 @@ usage(void)
 }
 
 #ifdef NOZ
-static const char optstr[] = "0123456789A:B:CEFGHILRUVabce:f:hilm:noqrsuvwxy";
+static const char optstr[] = "+0123456789A:B:CEFGHILRUVabce:f:hilm:noqrsuvwxy";
 #else
-static const char optstr[] = "0123456789A:B:CEFGHILRUVZabce:f:hilm:noqrsuvwxy";
+static const char optstr[] = "+0123456789A:B:CEFGHILRUVZabce:f:hilm:noqrsuvwxy";
 #endif
 
 static const struct option long_options[] =