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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/* See LICENSE file for copyright and license details. */
#ifdef __linux__
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#endif
#ifdef _WIN32
#ifndef _CRT_RAND_S
#define _CRT_RAND_S
#endif
#endif
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/random.h>
#endif
#include "arg.h"
#include "config.h"
char *argv0;
static unsigned char rbuf[256];
static size_t rpos = sizeof rbuf;
static void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-v] [-n n] [-l l]\n", argv0 ? argv0 : "rg");
exit(1);
}
static int
fillrand(unsigned char *buf, size_t n)
{
#ifdef _WIN32
size_t i;
unsigned int r;
int k;
for (i = 0; i < n; ) {
if (rand_s(&r) != 0)
return -1;
for (k = 0; k < 4 && i < n; k++)
buf[i++] = (unsigned char)(r >> (8 * k));
}
return 0;
#else
size_t off;
ssize_t r;
off = 0;
while (off < n) {
r = getrandom(buf + off, n - off, 0);
if (r < 0) {
if (errno == EINTR)
continue;
return -1;
}
if (r == 0)
return -1;
off += (size_t)r;
}
return 0;
#endif
}
static unsigned char
randbyte(void)
{
if (rpos >= sizeof rbuf) {
if (fillrand(rbuf, sizeof rbuf) < 0)
die("rg: rng:");
rpos = 0;
}
return rbuf[rpos++];
}
static uint32_t
randu32(void)
{
return ((uint32_t)randbyte() << 24) |
((uint32_t)randbyte() << 16) |
((uint32_t)randbyte() << 8) |
(uint32_t)randbyte();
}
static uint32_t
randbelow(uint32_t n)
{
uint32_t x, min;
if (n <= 1)
return 0;
/* reject [0, 2^32 % n) so x % n is unbiased */
min = (uint32_t)(-n) % n;
do {
x = randu32();
} while (x < min);
return x % n;
}
static int
eint(const char *s)
{
char *end;
long v;
if (s == NULL || s[0] == '\0')
usage();
errno = 0;
v = strtol(s, &end, 10);
if (errno || end == s || *end != '\0' || v < INT_MIN || v > INT_MAX)
die("rg: invalid number '%s'", s);
return (int)v;
}
int
main(int argc, char *argv[])
{
int i, j, n, l, alen;
n = defn;
l = defl;
ARGBEGIN {
case 'n':
n = eint(EARGF(usage()));
break;
case 'l':
l = eint(EARGF(usage()));
break;
case 'v':
puts("rg-" VERSION);
return 0;
default:
usage();
} ARGEND
if (argc)
usage();
if (n < minn || n > maxn)
die("rg: n must be %d..%d", minn, maxn);
if (l < minl || l > maxl)
die("rg: l must be %d..%d", minl, maxl);
alen = (int)strlen(alphabet);
if (alen < 1)
die("rg: empty alphabet");
for (i = 0; i < n; i++) {
for (j = 0; j < l; j++)
putchar(alphabet[randbelow((uint32_t)alen)]);
putchar('\n');
}
return 0;
}
|