Support running in loop mode

Allow the program to run in loop mode, where it will get random
data from the card at regular interval. The loop can be infinite,
or limited to a specified number of iterations.
master
Damien Goutte-Gattat 8 years ago
parent 4a22aa58f5
commit 1e54de0357
  1. 41
      src/scdrand.c

@ -41,6 +41,8 @@
#define MAX_RANDOM_BYTES 256
#define DEFAULT_RANDOM_BYTES 8
#define DEFAULT_LOOP_ITERATIONS 1
#define DEFAULT_LOOP_INTERVAL 10
static void
usage(int status)
@ -55,6 +57,13 @@ specified, the default is %d bytes.\n\n", DEFAULT_RANDOM_BYTES);
-v, --version Display the version message.\n\
");
printf("\
-l, --loop Loop indefinitely.\n\
-L, --max-loop N Loop N times (default: %d).\n\
-i, --interval N Sleep N seconds between iterations\n\
(default: %d seconds).\n\
\n", DEFAULT_LOOP_ITERATIONS, DEFAULT_LOOP_INTERVAL);
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
@ -319,21 +328,26 @@ get_uinteger_or_die(const char *arg)
int
main(int argc, char **argv)
{
int c, fd, n;
unsigned nbytes;
int c, fd, n, loop;
unsigned nbytes, interval;
assuan_context_t ctx;
unsigned char random_buffer[MAX_RANDOM_BYTES];
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "loop", 0, NULL, 'l' },
{ "max-loop", 1, NULL, 'L' },
{ "interval", 1, NULL, 'i' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
nbytes = DEFAULT_RANDOM_BYTES;
loop = DEFAULT_LOOP_ITERATIONS;
interval = DEFAULT_LOOP_INTERVAL;
while ( (c = getopt_long(argc, argv, "hv", options, NULL)) != -1 ) {
while ( (c = getopt_long(argc, argv, "hvlL:i:", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
@ -346,6 +360,18 @@ main(int argc, char **argv)
case 'v':
info();
break;
case 'l':
loop = -1;
break;
case 'L':
loop = get_uinteger_or_die(optarg);
break;
case 'i':
interval = get_uinteger_or_die(optarg);
break;
}
}
@ -367,8 +393,13 @@ main(int argc, char **argv)
assuan_set_gpg_err_source(GPG_ERR_SOURCE_USER_1);
ctx = connect_to_scdaemon();
n = get_challenge(ctx, random_buffer, nbytes);
write(fd, random_buffer, n);
while ( loop == -1 || loop-- > 0 ) {
n = get_challenge(ctx, random_buffer, nbytes);
write(fd, random_buffer, n);
if ( loop != 0 )
sleep(interval);
}
assuan_release(ctx);
close(fd);

Loading…
Cancel
Save