diff --git a/README b/README index 031c459..66db61f 100644 --- a/README +++ b/README @@ -12,6 +12,7 @@ Usage: macgen [OPTIONS] -u, --uppercase Print hex characters in uppercase instead of lowercase. -n, --no-newline Do not output the trailing newline. -r, --repeat=NUM Generate NUM MAC addresses. + -s, --seed=NUMBER Set a specific random seed. -v, --version Print version and exit. -h, --help Print this help message. diff --git a/configure.ac b/configure.ac index 578fdfc..e2b4855 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([macgen], [1.0.1], [info@ultramarin.se]) +AC_INIT([macgen], [1.0.2], [info@ultramarin.se]) AM_INIT_AUTOMAKE([-Wall -Werror foreign dist-bzip2]) AC_PROG_CC diff --git a/src/appargs.cpp b/src/appargs.cpp index acc982a..8eb4745 100644 --- a/src/appargs.cpp +++ b/src/appargs.cpp @@ -17,6 +17,8 @@ * along with this program. If not, see . */ #include +#include +#include #include #include @@ -46,6 +48,7 @@ namespace macgen { << " -u, --uppercase Print hex characters in uppercase instead of lowercase." << std::endl << " -n, --no-newline Do not output the trailing newline." << std::endl << " -r, --repeat=NUM Generate NUM MAC addresses." << std::endl + << " -s, --seed=NUMBER Set a specific random seed." << std::endl << " -v, --version Print version and exit." << std::endl << " -h, --help Print this help message." << std::endl << std::endl; @@ -70,11 +73,16 @@ namespace macgen { { "uppercase", no_argument, 0, 'u'}, { "no-newline", no_argument, 0, 'n'}, { "repeat", required_argument, 0, 'r'}, + { "seed", required_argument, 0, 's'}, { "version", no_argument, 0, 'v'}, { "help", no_argument, 0, 'h'}, { 0, 0, 0, 0} }; - const char* arg_format = "mcdunr:vh"; + const char* arg_format = "mcdunr:s:vh"; + + // Set default random seed, milliseconds since epoch + auto now = std::chrono::system_clock().now (); + seed = std::chrono::duration_cast(now.time_since_epoch()).count (); while (1) { int c = getopt_long (argc, argv, arg_format, long_options, NULL); @@ -97,10 +105,24 @@ namespace macgen { no_newline = true; break; case 'r': - repeat = atoi (optarg); - if (repeat == 0) { - std::cerr << "Invalid repeat value: " << optarg << std::endl << std::endl; - print_usage_and_exit (std::cerr, 1); + try { + repeat = std::stoi (optarg); + }catch (...) { + repeat = -1; + } + if (repeat < 1) { + std::cerr << "Error: Invalid repeat value: " << optarg + << " (use option '-h' for help)" << std::endl; + exit (1); + } + break; + case 's': + try { + seed = (unsigned long) std::stol (optarg, nullptr, 0); + }catch(...) { + std::cerr << "Error: Invalid seed value: " << optarg + << " (use option '-h' for help)" << std::endl; + exit (1); } break; case 'v': diff --git a/src/appargs.hpp b/src/appargs.hpp index 7f3848b..0ba0319 100644 --- a/src/appargs.hpp +++ b/src/appargs.hpp @@ -28,6 +28,7 @@ namespace macgen { bool uppercase; bool no_newline; int repeat; + unsigned long seed; appargs (int argc, char* argv[]); }; diff --git a/src/main.cpp b/src/main.cpp index 0175f98..f65f8e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,9 @@ int main (int argc, char* argv[]) std::default_random_engine re (rd()); std::uniform_int_distribution random_value (1, 65535); + // Set random seed + re.seed (args.seed); + std::cout << std::hex; if (args.uppercase) std::cout << std::uppercase;