summaryrefslogtreecommitdiff
path: root/udflabel/options.c
diff options
context:
space:
mode:
Diffstat (limited to 'udflabel/options.c')
-rw-r--r--udflabel/options.c55
1 files changed, 14 insertions, 41 deletions
diff --git a/udflabel/options.c b/udflabel/options.c
index 347461f..8220785 100644
--- a/udflabel/options.c
+++ b/udflabel/options.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 Pali Rohár <pali.rohar@gmail.com>
+ * Copyright (C) 2017-2018 Pali Rohár <pali.rohar@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <time.h>
#include <fcntl.h>
@@ -89,45 +90,20 @@ static void usage(void)
exit(1);
}
-static unsigned long int strtoul_safe(const char *str, int base, int *failed)
-{
- char *endptr = NULL;
- unsigned long int ret;
- errno = 0;
- ret = strtoul(str, &endptr, base);
- *failed = (!*str || *endptr || errno) ? 1 : 0;
- return ret;
-}
-
-static void get_random_bytes(void *buffer, size_t count)
-{
- int fd;
- size_t i;
-
- fd = open("/dev/urandom", O_RDONLY);
- if (fd >= 0)
- {
- if (read(fd, buffer, count) == (ssize_t)count)
- {
- close(fd);
- return;
- }
- close(fd);
- }
-
- for (i = 0; i < count; ++i)
- ((uint8_t *)buffer)[i] = rand() % 0xFF;
-}
-
static void process_uuid_arg(const char *arg, char *new_uuid)
{
- unsigned long int rnd;
int i;
+ time_t cur_time;
+ uint32_t uuid_time;
if (strcmp(arg, "random") == 0)
{
- get_random_bytes(&rnd, sizeof(rnd));
- snprintf(new_uuid, 17, "%08lx%08lx", ((unsigned long int)time(NULL)) % 0xFFFFFFFF, rnd % 0xFFFFFFFF);
+ cur_time = time(NULL);
+ if (cur_time != (time_t)-1 && cur_time >= 0)
+ uuid_time = cur_time & 0xFFFFFFFF;
+ else
+ uuid_time = randu32();
+ snprintf(new_uuid, 17, "%08lx%08lx", (unsigned long int)uuid_time, (unsigned long int)randu32());
return;
}
@@ -181,7 +157,6 @@ static void process_vid_lvid_arg(struct udf_disc *disc, int option, const char *
void parse_args(int argc, char *argv[], struct udf_disc *disc, char **filename, int *force, dstring *new_lvid, dstring *new_vid, dstring *new_fsid, dstring *new_fullvsid, char *new_uuid, dstring *new_vsid)
{
- unsigned long int value;
int failed;
int ret;
size_t len;
@@ -196,22 +171,20 @@ void parse_args(int argc, char *argv[], struct udf_disc *disc, char **filename,
break;
case OPT_BLK_SIZE:
case 'b':
- value = strtoul_safe(optarg, 0, &failed);
- if (failed || value < 512 || value > 32768 || (value & (value - 1)))
+ disc->blocksize = strtou32(optarg, 0, &failed);
+ if (failed || disc->blocksize < 512 || disc->blocksize > 32768 || (disc->blocksize & (disc->blocksize - 1)))
{
fprintf(stderr, "%s: Error: Invalid value for option --blocksize\n", appname);
exit(1);
}
- disc->blocksize = value;
break;
case OPT_VAT_BLOCK:
- value = strtoul_safe(optarg, 0, &failed);
- if (failed || value > UINT32_MAX)
+ disc->vat_block = strtou32(optarg, 0, &failed);
+ if (failed)
{
fprintf(stderr, "%s: Error: Invalid value for option --vatblock\n", appname);
exit(1);
}
- disc->vat_block = value;
break;
case OPT_FORCE:
*force = 1;