Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * prng.c |
michael@0 | 3 | * |
michael@0 | 4 | * Command-line pseudo-random number generator |
michael@0 | 5 | * |
michael@0 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <limits.h> |
michael@0 | 13 | #include <time.h> |
michael@0 | 14 | |
michael@0 | 15 | #ifdef __OS2__ |
michael@0 | 16 | #include <types.h> |
michael@0 | 17 | #include <process.h> |
michael@0 | 18 | #else |
michael@0 | 19 | #include <unistd.h> |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | #include "bbs_rand.h" |
michael@0 | 23 | |
michael@0 | 24 | int main(int argc, char *argv[]) |
michael@0 | 25 | { |
michael@0 | 26 | unsigned char *seed; |
michael@0 | 27 | unsigned int ix, num = 1; |
michael@0 | 28 | pid_t pid; |
michael@0 | 29 | |
michael@0 | 30 | if(argc > 1) { |
michael@0 | 31 | num = atoi(argv[1]); |
michael@0 | 32 | if(num <= 0) |
michael@0 | 33 | num = 1; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | pid = getpid(); |
michael@0 | 37 | srand(time(NULL) * (unsigned int)pid); |
michael@0 | 38 | |
michael@0 | 39 | /* Not a perfect seed, but not bad */ |
michael@0 | 40 | seed = malloc(bbs_seed_size); |
michael@0 | 41 | for(ix = 0; ix < bbs_seed_size; ix++) { |
michael@0 | 42 | seed[ix] = rand() % UCHAR_MAX; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | bbs_srand(seed, bbs_seed_size); |
michael@0 | 46 | memset(seed, 0, bbs_seed_size); |
michael@0 | 47 | free(seed); |
michael@0 | 48 | |
michael@0 | 49 | while(num-- > 0) { |
michael@0 | 50 | ix = bbs_rand(); |
michael@0 | 51 | |
michael@0 | 52 | printf("%u\n", ix); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | return 0; |
michael@0 | 56 | |
michael@0 | 57 | } |