This possibly improves the usability of chattr for some typical use cases:

Command         : Old  : New behavior
================================================
chattr -h       : help : help
chattr -h FILE  : help : chattr -- -h -- FILE
chattr -hs FILE : help : chattr -- -h -s -- FILE
chattr -sh FILE : fail : chattr -- -s -h -- FILE
chattr -ar FILE : fail : chattr -- -a -r -- FILE

Unrelated: there a two trivial block-copied-but-not-changed issues:

$ egrep 'ACL|--r' chattr.c
          "Get POSIX ACL information\n"
      "  -R, --recursive     recursively list attributes of directories and their \n"

Regards,
Christian

From 865a5a50501f3fd0cf5ed28500d3e6e45a6456de Mon Sep 17 00:00:00 2001
From: Christian Franke <[email protected]>
Date: Wed, 19 May 2021 16:24:47 +0200
Subject: [PATCH] Cygwin: utils: chattr: Improve option parsing.

Interpret '-h' as '--help' only if last argument.
Allow multiple characters in first '-mode' argument.

Signed-off-by: Christian Franke <[email protected]>
---
 winsup/utils/chattr.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/winsup/utils/chattr.c b/winsup/utils/chattr.c
index 98f693aab..f6ce340b4 100644
--- a/winsup/utils/chattr.c
+++ b/winsup/utils/chattr.c
@@ -271,7 +271,7 @@ int
 main (int argc, char **argv)
 {
   int c, ret = 0;
-  int lastoptind = 0;
+  int lastoptind = 1;
   char *opt;
 
   opterr = 0;
@@ -295,15 +295,16 @@ main (int argc, char **argv)
          print_version ();
          return 0;
          break;
+       case 'h':
+         /* Print help if -h is last argument or --help is used,
+            otherwise interpret -h as 'remove hidden attribute'.  */
+         if (optind >= argc || (optind > lastoptind && argv[optind-1][1] == 
'-'))
+           usage (stdout);
+         /*FALLTHRU*/
        default:
          if (optind > lastoptind)
-           {
-             --optind;
-             goto next;
-           }
-         /*FALLTHRU*/
-       case 'h':
-         usage (c == 'h' ? stdout : stderr);
+           --optind;
+         goto next;
        }
     }
 next:
-- 
2.31.1

Reply via email to