commit: 8c6ea09ea6fb549e7701b5f11a6f73dd0d2e84d7
parent b04ba07147b2f0487de6136471e026e4d04496c3
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 16:50:34 +0200
cmd/mktemp: gracefully handle unknown options
Diffstat:
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/cmd/mktemp.c b/cmd/mktemp.c
@@ -11,6 +11,8 @@
#include <string.h> // strerror
#include <unistd.h> // getopt
+const char *argv0 = "mktemp";
+
int
main(int argc, char *argv[])
{
@@ -36,7 +38,11 @@ main(int argc, char *argv[])
if(tmpdir == NULL) tmpdir = getenv("TMPDIR");
if(tmpdir == NULL) tmpdir = "/tmp";
break;
+ case '?':
+ fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
+ return 1;
default:
+ fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c);
abort();
}
}
@@ -50,7 +56,7 @@ main(int argc, char *argv[])
}
else if(argc > 1)
{
- fprintf(stderr, "mktemp: error: Only one template argument is supported, got %d\n", argc);
+ fprintf(stderr, "%s: error: Only one template argument is supported, got %d\n", argv0, argc);
return 1;
}
@@ -59,7 +65,8 @@ main(int argc, char *argv[])
if(chdir(tmpdir) < 0)
{
fprintf(stderr,
- "mktemp: error: Failed changing directory into tmpdir '%s': %s\n",
+ "%s: error: Failed changing directory into tmpdir '%s': %s\n",
+ argv0,
tmpdir,
strerror(errno));
return 1;
@@ -74,7 +81,8 @@ main(int argc, char *argv[])
if(dir == NULL)
{
if(!o_quiet)
- fprintf(stderr, "mktemp: error: Failed creating random directory: %s\n", strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed creating random directory: %s\n", argv0, strerror(errno));
return 1;
}
@@ -88,7 +96,7 @@ main(int argc, char *argv[])
if(fd < 0)
{
if(!o_quiet)
- fprintf(stderr, "mktemp: error: Failed creating random file: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed creating random file: %s\n", argv0, strerror(errno));
return 1;
}
@@ -97,8 +105,7 @@ main(int argc, char *argv[])
if(close(fd) < 0)
{
- if(!o_quiet)
- fprintf(stderr, "mktemp: error: Failed closing file descriptor: %s\n", strerror(errno));
+ if(!o_quiet) fprintf(stderr, "%s: error: Failed closing file: %s\n", argv0, strerror(errno));
return 1;
}