Re: enforce zero options

2016-10-12 Thread Joerg Sonnenberger
On Wed, Oct 12, 2016 at 03:53:17PM +0200, Jan Stary wrote:
> I don't get it: why do we need to handle --
> in utils which take no options and no arguments?

Are you sure they will never handle options in the future?

Joerg



Re: enforce zero options

2016-10-12 Thread Jan Stary
On Oct 12 15:00:23, j...@wxcvbn.org wrote:
> Jan Stary  writes:
> 
> > Some programs in bin/ and usr.bin/ use the following idiom
> > to make sure that there are no options present:
> >  
> > while ((ch = getopt(argc, argv, "")) != -1)
> > switch (ch) {
> > case '?':
> > default:
> > usage();
> > /* NOTREACHED */
> > }
> >
> > if (argc != optind) {
> > usage();
> > /* NOTREACHED */
> > }
> >  
> > Why is this better then simply checking that (argc == 1)?
> 
> getopt(3) handles --.  Using getopt(3) everywhere is good for
> consistency.

I don't get it: why do we need to handle --
in utils which take no options and no arguments?

e.g. logname(1) is supposed to be launched just like "logname".
Does logname.c do the above just to handle "logname --" ?

Jan



Re: enforce zero options

2016-10-12 Thread Jeremie Courreges-Anglas
Jan Stary  writes:

> Some programs in bin/ and usr.bin/ use the following idiom
> to make sure that there are no options present:
>  
>   while ((ch = getopt(argc, argv, "")) != -1)
>   switch (ch) {
>   case '?':
>   default:
>   usage();
>   /* NOTREACHED */
>   }
>
>   if (argc != optind) {
>   usage();
>   /* NOTREACHED */
>   }
>  
> Why is this better then simply checking that (argc == 1)?

getopt(3) handles --.  Using getopt(3) everywhere is good for
consistency.

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



enforce zero options

2016-10-12 Thread Jan Stary
Some programs in bin/ and usr.bin/ use the following idiom
to make sure that there are no options present:
 
while ((ch = getopt(argc, argv, "")) != -1)
switch (ch) {
case '?':
default:
usage();
/* NOTREACHED */
}

if (argc != optind) {
usage();
/* NOTREACHED */
}
 
Why is this better then simply checking that (argc == 1)?

Below is a diff to logname as an example.
(Remove the pointless locale while there.)

Jan


Index: logname.c
===
RCS file: /cvs/src/usr.bin/logname/logname.c,v
retrieving revision 1.9
diff -u -p -r1.9 logname.c
--- logname.c   9 Oct 2015 01:37:08 -   1.9
+++ logname.c   12 Oct 2016 08:38:49 -
@@ -32,7 +32,6 @@
 
 #include 
 #include 
-#include 
 #include 
 #include 
 
@@ -44,23 +43,11 @@ main(int argc, char *argv[])
int ch;
char *p;
 
-   setlocale(LC_ALL, "");
-
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
 
-   while ((ch = getopt(argc, argv, "")) != -1)
-   switch (ch) {
-   case '?':
-   default:
-   usage();
-   /* NOTREACHED */
-   }
-
-   if (argc != optind) {
+   if (argc != 1)
usage();
-   /* NOTREACHED */
-   }
 
if ((p = getlogin()) == NULL)
err(1, NULL);