trivial simplification in /usr/bin/users

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

trivial simplification in /usr/bin/users

Han Boetes
Hi,

At the moment users accepts arguments without a dash, but errors
out on arguments with dash.

/usr/src/usr.bin/users% users -foo
users: unknown option -- f
usage: users
/usr/src/usr.bin/users% users foo  
han

After applying the patch users errors out on all arguments, which
simplifies the code and provides better error reporting.

/usr/src/usr.bin/users% ./users foo
usage: users
/usr/src/usr.bin/users% ./users -foo
usage: users


Index: users.c
===================================================================
RCS file: /cvs/src/usr.bin/users/users.c,v
retrieving revision 1.8
diff -u -p -r1.8 users.c
--- users.c 16 Oct 2003 16:57:37 -0000 1.8
+++ users.c 14 Mar 2006 23:25:12 -0000
@@ -64,15 +64,10 @@ main(int argc, char *argv[])
  struct utmp utmp;
  int ch;
 
- while ((ch = getopt(argc, argv, "")) != -1)
- switch(ch) {
- case '?':
- default:
- (void)fprintf(stderr, "usage: users\n");
- exit(1);
- }
- argc -= optind;
- argv += optind;
+ if (argc != 1) {
+ (void)fprintf(stderr, "usage: users\n");
+ exit(1);
+ }
 
  if (!freopen(_PATH_UTMP, "r", stdin)) {
  err(1, "can't open %s", _PATH_UTMP);



# Han

Reply | Threaded
Open this post in threaded view
|

Re: trivial simplification in /usr/bin/users

Theo de Raadt
- while ((ch = getopt(argc, argv, "")) != -1)
- switch(ch) {
- case '?':
- default:
- (void)fprintf(stderr, "usage: users\n");
- exit(1);
- }
- argc -= optind;
- argv += optind;

No.  In a POSIX world, all programs should use getopt() and thus
-options if possible.  A few old ones may be backwards compatible,
like dd and ps and tar for instance.