From 865a5a50501f3fd0cf5ed28500d3e6e45a6456de Mon Sep 17 00:00:00 2001 From: Christian Franke 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 --- 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