Raw
1 /*
2 * Simple random data generator used to create reproducible test files.
3 * This is inspired from POSIX.1-2001 implementation example for rand().
4 * Copyright (C) 2007 by Nicolas Pitre, licensed under the GPL version 2.
5 */
6
7 #include "test-tool.h"
8 #include "git-compat-util.h"
9 #include "parse.h"
10
11 int cmd__genrandom(int argc, const char **argv)
12 {
13 unsigned long count, next = 0;
14 unsigned char *c;
15
16 if (argc < 2 || argc > 3) {
17 fprintf(stderr, "usage: %s <seed_string> [<size>]\n", argv[0]);
18 return 1;
19 }
20
21 c = (unsigned char *) argv[1];
22 do {
23 next = next * 11 + *c;
24 } while (*c++);
25
26 count = ULONG_MAX;
27 if (argc == 3 && !git_parse_ulong(argv[2], &count))
28 return error_errno("cannot parse argument '%s'", argv[2]);
29
30 while (count--) {
31 next = next * 1103515245 + 12345;
32 if (putchar((next >> 16) & 0xff) == EOF)
33 return -1;
34 }
35
36 return 0;
37 }