-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.c
More file actions
executable file
·113 lines (104 loc) · 2.47 KB
/
rand.c
File metadata and controls
executable file
·113 lines (104 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <getopt.h>
char *usage = "rand: simple random integer generator using /dev/random\nrand [-hdxrun] [[min] max]\n-h\tThis help\n-d/-x\tOutput in decimal (default) or hexadecimal\n-u/-r\tUse /dev/urandom (default) or /dev/random\n-n num\tOutput num numbers (default 1)\nmin\tMinimum number (default 0)\nmax\tMaximum number (default UINT_MAX)\n";
void die(char *msg)
{
fprintf(stderr, "%s", msg);
exit(1);
}
int main(int argc, char **argv)
{
// Important variables
unsigned int min = 0, max = UINT_MAX;
int n = 1;
int base = 10, range = 0;
char *randpath = "/dev/urandom";
// Read arguments
int opt;
while ((opt = getopt(argc, argv, "dhn:rux")) != -1)
{
if (opt == 'h') die(usage);
else if (opt == 'r') randpath = "/dev/random";
else if (opt == 'u') randpath = "/dev/urandom";
else if (opt == 'n')
{
errno = 0;
n = strtol(optarg, NULL, 10);
if (errno) die(usage);
}
else if (opt == 'd') base = 10;
else if (opt == 'x') base = 16;
else die(usage);
}
int optcnt = optind;
if (optcnt < argc - 2)
{
fprintf(stderr, "Bad argument list.\n");
die(usage);
}
else if (optcnt == argc - 1)
{
range = 1;
errno = 0;
max = strtol(argv[optcnt], NULL, 10);
if (errno) die(usage);
}
else if (optcnt == argc - 2)
{
range = 1;
errno = 0;
min = strtol(argv[optcnt], NULL, 10);
max = strtol(argv[optcnt + 1], NULL, 10);
if (errno) die(usage);
}
else if (optcnt == argc);
else die("Internal error 1");
if (min > max)
{
int t = min;
min = max;
max = t;
}
// The trivial case
if (min == max)
{
for (int i = 0; i < n; i++) printf("%u\n", min);
return 0;
}
// Open the random file
errno = 0;
FILE *randfile = fopen(randpath, "r");
if (randfile == NULL)
{
perror("Could not open random file for reading");
return 1;
}
// Get and print random data
int nchars = sizeof(unsigned int) / sizeof(char);
unsigned char c[nchars];
unsigned int rand;
int cnt = 0;
while (cnt < n)
{
rand = 0;
for (int j = 0; j < nchars; j++)
{
int t = fgetc(randfile);
if (t == -1) die("EOF");
c[j] = (unsigned char) (t & 0xFF);
}
for (int j = 0; j < nchars; j++) rand |= c[j] << (j * sizeof(char) * 8);
if (range) rand = rand % (max - min + 1) + min;
if (base == 10) printf("%u\n", rand);
else if (base == 16) printf("%x\n", rand);
if (n > 0) cnt++;
}
return 0;
}