commit: 8749a3c6ddc198df4b332f35be033ef80336b2ab
parent 266415d38454b752ed2e1bee0c91f34ac2a05d38
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 17:04:58 +0200
cmd/id: handle unknown option
Diffstat:
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/cmd/id.c b/cmd/id.c
@@ -11,6 +11,7 @@
#include <sys/types.h> // uid_t
#include <unistd.h> // getuid, getgid, getopt, opt*, getgrouplist(FreeBSD, NetBSD)
+const char *argv0 = "id";
bool name_flag = false;
static int
@@ -202,7 +203,13 @@ main(int argc, char *argv[])
case 'r':
real_flag = true;
break;
+ case '?':
+ fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt);
+ usage();
+ free(groups);
+ return 1;
default:
+ fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c);
usage();
free(groups);
return 1;
@@ -270,7 +277,7 @@ main(int argc, char *argv[])
if(gr == NULL || gr->gr_name == NULL)
{
ret--;
- fprintf(stderr, "id: error: Cannot find name for group ID %u\n", gid);
+ fprintf(stderr, "%s: error: Cannot find name for group ID %u\n", argv0, gid);
printf("%u\n", gid);
}
else
@@ -297,7 +304,7 @@ main(int argc, char *argv[])
if(pw.pw_name == NULL)
{
ret--;
- fprintf(stderr, "id: error: Cannot find name for user ID %u\n", uid);
+ fprintf(stderr, "%s: error: Cannot find name for user ID %u\n", argv0, uid);
printf("%u\n", uid);
}
else
diff --git a/test-cmd/id.sh b/test-cmd/id.sh
@@ -36,4 +36,5 @@ t G_nobody '-G nobody' "$(id -G nobody)
t G_root '-G root' "$(id -G root)
"
-t --exit=1 badarg '-a' "${usage}"
+t --exit=1 badarg '-a' "id: error: Unhandled option '-a'
+${usage}"