From 96394d11b01b5613b635e4c5963a19e222e57afb Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Fri, 12 Jan 2001 18:30:54 +0000 Subject: [PATCH] ChangeLog, copy.c, gen_uuid.c, tst_uuid.c, uuid_time.c: uuid_time.c (main), tst_uuid.c (main): Fix gcc -Wall complaints. copy.c (uuid_copy): Change arguments to make it clear which argument is the source and which is the destination. gen_uuid.c (get_random_fd): Use gettimeofday to seed the PRNG, so we can take advantage of tv_usec to do (slightly) better at seeding it. --- lib/uuid/ChangeLog | 11 +++++++++++ lib/uuid/copy.c | 6 +++--- lib/uuid/gen_uuid.c | 17 ++++++++--------- lib/uuid/tst_uuid.c | 2 +- lib/uuid/uuid_time.c | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/uuid/ChangeLog b/lib/uuid/ChangeLog index 988e626..ac5255e 100644 --- a/lib/uuid/ChangeLog +++ b/lib/uuid/ChangeLog @@ -1,3 +1,14 @@ +2001-01-12 Theodore Ts'o + + * uuid_time.c (main), tst_uuid.c (main): Fix gcc -Wall complaints. + + * copy.c (uuid_copy): Change arguments to make it clear which + argument is the source and which is the destination. + + * gen_uuid.c (get_random_fd): Use gettimeofday to seed the PRNG, + so we can take advantage of tv_usec to do (slightly) + better at seeding it. + 2000-07-13 * Release of E2fsprogs 1.19 diff --git a/lib/uuid/copy.c b/lib/uuid/copy.c index fcb08ab..213a9e5 100644 --- a/lib/uuid/copy.c +++ b/lib/uuid/copy.c @@ -11,11 +11,11 @@ #include "uuidP.h" -void uuid_copy(uuid_t uu1, uuid_t uu2) +void uuid_copy(uuid_t dst, uuid_t src) { - unsigned char *cp1, *cp2; + unsigned char *cp1, *cp2; int i; - for (i=0, cp1 = uu1, cp2 = uu2; i < 16; i++) + for (i=0, cp1 = dst, cp2 = src; i < 16; i++) *cp1++ = *cp2++; } diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index be1dbbb..fd55114 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -43,17 +43,20 @@ static int get_random_fd() { - static int fd = -2; - int i; + struct timeval tv; + static int fd = -2; + int i; if (fd == -2) { + gettimeofday(&tv, 0); fd = open("/dev/urandom", O_RDONLY); if (fd == -1) fd = open("/dev/random", O_RDONLY | O_NONBLOCK); - srand((getpid() << 16) ^ getuid() ^ time(0)); + srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); } /* Crank the random number generator a few times */ - for (i = time(0) & 0x1F; i > 0; i--) + gettimeofday(&tv, 0); + for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) rand(); return fd; } @@ -85,14 +88,11 @@ static void get_random_bytes(void *buf, int nbytes) lose_counter = 0; } } - if (nbytes == 0) - return; /* XXX put something better here if no /dev/random! */ - for (i=0; i < nbytes; i++) + for (i = 0; i < nbytes; i++) *cp++ = rand() & 0xFF; return; - } /* @@ -268,4 +268,3 @@ void uuid_generate(uuid_t out) else uuid_generate_time(out); } - diff --git a/lib/uuid/tst_uuid.c b/lib/uuid/tst_uuid.c index b9fc5f6..dac52b5 100644 --- a/lib/uuid/tst_uuid.c +++ b/lib/uuid/tst_uuid.c @@ -85,7 +85,7 @@ main(int argc, char **argv) tv.tv_sec = 0; tv.tv_usec = 0; time_reg = uuid_time(buf, &tv); - printf("UUID time is: (%d, %d): %s\n", tv.tv_sec, tv.tv_usec, + printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, ctime(&time_reg)); uuid_parse(str, tst); if (!uuid_compare(buf, tst)) diff --git a/lib/uuid/uuid_time.c b/lib/uuid/uuid_time.c index f2fddfa..ee9ce26 100644 --- a/lib/uuid/uuid_time.c +++ b/lib/uuid/uuid_time.c @@ -131,7 +131,7 @@ main(int argc, char **argv) printf("Warning: not a time-based UUID, so UUID time " "decoding will likely not work!\n"); } - printf("UUID time is: (%d, %d): %s\n", tv.tv_sec, tv.tv_usec, + printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, ctime(&time_reg)); return 0; -- 1.8.3.1