-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ void add_entropy(char data[8]) | |
|
||
close(rnd); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,73 +34,76 @@ | |
/* Command line parsing */ | ||
|
||
typedef struct options_s { | ||
char * args[1]; char * sleep_i; int verbose; | ||
char * args[1]; char * sleep_i; int verbose; | ||
} options; | ||
|
||
const char * argp_program_version = "rng-usleep 0.2"; | ||
const char * argp_program_bug_address = "<[email protected]>"; | ||
static char argp_arg[] = "[LENGTH]"; | ||
static char argp_doc[] = | ||
"Generate random data from the practical time variations of usleep(3). " | ||
"If LENGTH is omitted, will generate until terminated. "; | ||
"Generate random data from the practical time variations of usleep(3). " | ||
"If LENGTH is omitted, will generate until terminated. "; | ||
static struct argp_option optinfo[] = { | ||
{ "verbose", 'v', 0, 0, "Provide extra information." }, | ||
{ "interval", 'i', "TIME", 0, | ||
"Time in microseconds to sleep for (default 40). On top of this, there " | ||
"is processing overhead, typically in the order of a few dozen us. " }, | ||
{ 0 } | ||
{ "verbose", 'v', 0, 0, "Provide extra information." }, | ||
{ "interval", 'i', "TIME", 0, | ||
"Time in microseconds to sleep for (default 40). On top of this, there " | ||
"is processing overhead, typically in the order of a few dozen us. " }, | ||
{ 0 } | ||
}; | ||
|
||
static error_t parse_opt(int key, char * arg, struct argp_state * state){ | ||
options * opts = state->input; | ||
|
||
switch (key) { | ||
case ARGP_KEY_ARG: | ||
if (state->arg_num >= 1) { | ||
argp_usage(state); | ||
} | ||
opts->args[state->arg_num] = arg; | ||
break; | ||
case ARGP_KEY_END: | ||
if (state->arg_num < 0) { | ||
argp_usage(state); | ||
} | ||
break; | ||
case 'i': | ||
opts->sleep_i = arg; | ||
break; | ||
case 'v': | ||
opts->verbose = 1; | ||
break; | ||
default: | ||
return ARGP_ERR_UNKNOWN; | ||
} /* switch */ | ||
|
||
return 0; | ||
static error_t parse_opt(int key, char * arg, struct argp_state * state) | ||
{ | ||
options * opts = state->input; | ||
|
||
switch (key) { | ||
case ARGP_KEY_ARG: | ||
if (state->arg_num >= 1) { | ||
argp_usage(state); | ||
} | ||
opts->args[state->arg_num] = arg; | ||
break; | ||
case ARGP_KEY_END: | ||
if (state->arg_num < 0) { | ||
argp_usage(state); | ||
} | ||
break; | ||
case 'i': | ||
opts->sleep_i = arg; | ||
break; | ||
case 'v': | ||
opts->verbose = 1; | ||
break; | ||
default: | ||
return ARGP_ERR_UNKNOWN; | ||
} /* switch */ | ||
|
||
return 0; | ||
} /* parse_opt */ | ||
|
||
static struct argp argp = { optinfo, parse_opt, argp_arg, argp_doc }; | ||
|
||
int main(int argc, char * argv[]){ | ||
int main(int argc, char * argv[]) | ||
{ | ||
|
||
options opts = { { NULL }, NULL, 0 }; | ||
options opts = { { NULL }, NULL, 0 }; | ||
|
||
argp_parse(&argp, argc, argv, 0, 0, &opts); | ||
argp_parse(&argp, argc, argv, 0, 0, &opts); | ||
|
||
int length = 0, sleep_i = 40; | ||
TO_INT(opts.args[0], length, "LENGTH"); | ||
TO_INT(opts.sleep_i, sleep_i, "TIME"); | ||
int length = 0, sleep_i = 40; | ||
TO_INT(opts.args[0], length, "LENGTH"); | ||
TO_INT(opts.sleep_i, sleep_i, "TIME"); | ||
|
||
char * len_str, * pad_str; | ||
len_str = (length) ? opts.args[0] : ""; | ||
pad_str = (length) ? " " : ""; | ||
char * len_str, * pad_str; | ||
len_str = (length) ? opts.args[0] : ""; | ||
pad_str = (length) ? " " : ""; | ||
|
||
fprintf(stderr, "generating %s%srandom bytes @ %d+c us/bit\n", | ||
len_str, pad_str, sleep_i); | ||
fprintf(stderr, "generating %s%srandom bytes @ %d+c us/bit\n", | ||
len_str, pad_str, sleep_i); | ||
|
||
if (daemon(-1, -1) == -1) | ||
printf("%s\n", "failed to become daemon process"); | ||
if (daemon(-1, -1) == -1) | ||
printf("%s\n", "failed to become daemon process"); | ||
|
||
generate_random(length, sleep_i); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
generate_random(length, sleep_i); | ||
return EXIT_SUCCESS; | ||
} |