zgenhostid: use argument path directly

Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12042
This commit is contained in:
наб 2021-05-16 14:26:42 +02:00 committed by Brian Behlendorf
parent e40ffed021
commit c1a64be6d4
1 changed files with 3 additions and 13 deletions

View File

@ -36,8 +36,6 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
static void usage(void);
static void static void
usage(void) usage(void)
{ {
@ -60,12 +58,11 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
/* default file path, can be optionally set by user */ /* default file path, can be optionally set by user */
char path[PATH_MAX] = "/etc/hostid"; const char *path = "/etc/hostid";
/* holds converted user input or lrand48() generated value */ /* holds converted user input or lrand48() generated value */
unsigned long input_i = 0; unsigned long input_i = 0;
int opt; int opt;
int pathlen;
int force_fwrite = 0; int force_fwrite = 0;
while ((opt = getopt_long(argc, argv, "fo:h?", 0, 0)) != -1) { while ((opt = getopt_long(argc, argv, "fo:h?", 0, 0)) != -1) {
switch (opt) { switch (opt) {
@ -73,14 +70,7 @@ main(int argc, char **argv)
force_fwrite = 1; force_fwrite = 1;
break; break;
case 'o': case 'o':
pathlen = snprintf(path, sizeof (path), "%s", optarg); path = optarg;
if (pathlen >= sizeof (path)) {
fprintf(stderr, "%s\n", strerror(EOVERFLOW));
exit(EXIT_FAILURE);
} else if (pathlen < 1) {
fprintf(stderr, "%s\n", strerror(EINVAL));
exit(EXIT_FAILURE);
}
break; break;
case 'h': case 'h':
case '?': case '?':
@ -118,7 +108,7 @@ main(int argc, char **argv)
if (force_fwrite == 0 && stat(path, &fstat) == 0 && if (force_fwrite == 0 && stat(path, &fstat) == 0 &&
S_ISREG(fstat.st_mode)) { S_ISREG(fstat.st_mode)) {
fprintf(stderr, "%s: %s\n", path, strerror(EEXIST)); fprintf(stderr, "%s: %s\n", path, strerror(EEXIST));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* /*