From 801f1b160521079fae304c87a08be7f943bf1ebe Mon Sep 17 00:00:00 2001 From: wiire-a Date: Fri, 22 Dec 2017 12:04:11 +0100 Subject: [PATCH] Get elapsed time in a proper way --- src/pixiewps.c | 11 ++++++----- src/utils.h | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/pixiewps.c b/src/pixiewps.c index 2015d58..206ca94 100644 --- a/src/pixiewps.c +++ b/src/pixiewps.c @@ -806,8 +806,9 @@ usage_err: pfound = crack(wps, wps->pin); } + struct timeval diff; gettimeofday(&t_end, 0); - unsigned long ms_elapsed = get_elapsed_ms(&t_start, &t_end); + timeval_subtract(&diff, &t_end, &t_start); printf("\n Pixiewps %s\n", SHORT_VERSION); if (wps->verbosity > 1) { @@ -859,8 +860,7 @@ usage_err: else { printf("\n [-] WPA-PSK not found!"); } - - printf("\n\n [*] Time taken: %lu s %lu ms\n\n", ms_elapsed / 1000, ms_elapsed % 1000); + printf("\n\n [*] Time taken: %lu s %lu ms\n\n", diff.tv_sec, diff.tv_usec / 1000); if (decrypted5) { free(decrypted5); @@ -1378,8 +1378,9 @@ usage_err: k++; } + struct timeval diff; gettimeofday(&t_end, 0); - unsigned long ms_elapsed = get_elapsed_ms(&t_start, &t_end); + timeval_subtract(&diff, &t_end, &t_start); k--; @@ -1454,7 +1455,7 @@ usage_err: else { printf("\n [-] WPS pin not found!"); } - printf("\n\n [*] Time taken: %lu s %lu ms\n\n", ms_elapsed / 1000, ms_elapsed % 1000); + printf("\n\n [*] Time taken: %lu s %lu ms\n\n", diff.tv_sec, diff.tv_usec / 1000); if (wps->warning) { printf("%s", wps->warning); diff --git a/src/utils.h b/src/utils.h index 97f3388..7ac8303 100644 --- a/src/utils.h +++ b/src/utils.h @@ -230,10 +230,29 @@ unsigned int get_unix_datetime(char *s, time_t *datetime) return 0; } -/* Return the difference of time between the two in milliseconds */ -unsigned long get_elapsed_ms(struct timeval *start, struct timeval *end) +/* Subtract the ‘struct timeval’ values X and Y + Return 1 if the difference is negative, otherwise 0 + Reference: https://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html */ +int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) { - return (((end->tv_sec - start->tv_sec) * 1000000 + (end->tv_usec - start->tv_usec)) / 1000); + /* Perform the carry for the later subtraction by updating y */ + if (x->tv_usec < y->tv_usec) { + const int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } + if (x->tv_usec - y->tv_usec > 1000000) { + const int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } + + /* Compute the time remaining to wait, tv_usec is certainly positive */ + result->tv_sec = x->tv_sec - y->tv_sec; + result->tv_usec = x->tv_usec - y->tv_usec; + + /* Return 1 if result is negative */ + return x->tv_sec < y->tv_sec; } /* Convert an unsigned integer to a char array without termination */