commit: e5d158506c7001999b47fe5b21e187f7e8b0f0e9
parent 195537341000ff43573d7722d18cd2eea55c892a
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 17:05:43 +0200
cmd/uniq: handle unknown option
Diffstat:
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/cmd/uniq.c b/cmd/uniq.c
@@ -21,6 +21,8 @@ enum uniq_mode
NO_REPEAT,
};
+const char *argv0 = "uniq";
+
int
main(int argc, char *argv[])
{
@@ -37,7 +39,7 @@ main(int argc, char *argv[])
case 'c':
if(mode != UNIQ)
{
- fprintf(stderr, "uniq: error: can only pass one of [-c|-d|-u]\n");
+ fprintf(stderr, "%s: error: can only pass one of [-c|-d|-u]\n", argv0);
return 1;
}
mode = COUNT;
@@ -45,7 +47,7 @@ main(int argc, char *argv[])
case 'd':
if(mode != UNIQ)
{
- fprintf(stderr, "uniq: error: can only pass one of [-c|-d|-u]\n");
+ fprintf(stderr, "%s: error: can only pass one of [-c|-d|-u]\n", argv0);
return 1;
}
mode = ONLY_REPEAT;
@@ -55,13 +57,16 @@ main(int argc, char *argv[])
field = strtoul(optarg, &endptr, 0);
if(errno != 0)
{
- fprintf(stderr, "uniq: error: Failed parsing '-f %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-f %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
if(endptr != NULL && endptr[0] != 0)
{
- fprintf(
- stderr, "uniq: error: Non-numeric characters passed to '-f %s': %s\n", optarg, endptr);
+ fprintf(stderr,
+ "%s: error: Non-numeric characters passed to '-f %s': %s\n",
+ argv0,
+ optarg,
+ endptr);
return 1;
}
break;
@@ -70,25 +75,35 @@ main(int argc, char *argv[])
shift = strtoul(optarg, &endptr, 0);
if(errno != 0)
{
- fprintf(stderr, "uniq: error: Failed parsing '-f %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-f %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
if(endptr != NULL && endptr[0] != 0)
{
- fprintf(
- stderr, "uniq: error: Non-numeric characters passed to '-f %s': %s\n", optarg, endptr);
+ fprintf(stderr,
+ "%s: error: Non-numeric characters passed to '-f %s': %s\n",
+ argv0,
+ optarg,
+ endptr);
return 1;
}
break;
case 'u':
if(mode != UNIQ)
{
- fprintf(stderr, "uniq: error: can only pass one of [-c|-d|-u]\n");
+ fprintf(stderr, "%s: error: can only pass one of [-c|-d|-u]\n", argv0);
return 1;
}
mode = NO_REPEAT;
break;
+ case ':':
+ fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt);
+ return 1;
+ case '?':
+ fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt);
+ return 1;
default:
+ fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c);
abort();
}
}