Get elapsed time in a proper way

This commit is contained in:
wiire-a 2017-12-22 12:04:11 +01:00
parent 58e04d5aa0
commit 801f1b1605
2 changed files with 28 additions and 8 deletions

View File

@ -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);

View File

@ -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 */