Skip to content

Commit

Permalink
Added option -s,--seed=NUMBER to set a specific random seed.
Browse files Browse the repository at this point in the history
  • Loading branch information
alfmep committed Dec 9, 2021
1 parent d5a60a4 commit 8727d83
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 27 additions & 5 deletions src/appargs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <chrono>
#include <string>
#include <getopt.h>
#include <appargs.hpp>

Expand Down Expand Up @@ -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;
Expand All @@ -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<std::chrono::milliseconds>(now.time_since_epoch()).count ();

while (1) {
int c = getopt_long (argc, argv, arg_format, long_options, NULL);
Expand All @@ -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':
Expand Down
1 change: 1 addition & 0 deletions src/appargs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace macgen {
bool uppercase;
bool no_newline;
int repeat;
unsigned long seed;

appargs (int argc, char* argv[]);
};
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ int main (int argc, char* argv[])
std::default_random_engine re (rd());
std::uniform_int_distribution<uint16_t> random_value (1, 65535);

// Set random seed
re.seed (args.seed);

std::cout << std::hex;
if (args.uppercase)
std::cout << std::uppercase;
Expand Down

0 comments on commit 8727d83

Please sign in to comment.