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#define FOR_pwgen
31#include "toys.h"
32
33GLOBALS(
34 char *r;
35)
36
37void pwgen_main(void)
38{
39 int length = 8, count, ii, jj, c, rand = 0, x = 0;
40 unsigned xx = 80, yy = 24;
41 char randbuf[16];
42
43 if (isatty(1)) terminal_size(&xx, &yy);
44 else toys.optflags |= FLAG_1;
45
46 if (toys.optc && (length = atolx(*toys.optargs))>sizeof(toybuf))
47 error_exit("bad length");
48 if (toys.optc>1) count = atolx(toys.optargs[1]);
49 else count = FLAG(1) ? 1 : (xx/(length+1))*(yy-1);
50
51 for (jj = 0; jj<count; jj++) {
52 for (ii = 0; ii<length;) {
53
54 if (!rand) xgetrandom(randbuf, rand = sizeof(randbuf), 0);
55 c = 33+randbuf[--rand]%93;
56 if (FLAG(s)) randbuf[rand] = 0;
57
58 if (c>='A' && c<='Z') {
59 if (FLAG(A)) continue;
60
61 else c |= (0x80&randbuf[rand])>>2;
62 }
63 if (FLAG(0) && c>='0' && c<='9') continue;
64 if (FLAG(B) && strchr("0O1lI8B5S2ZD'`.,", c)) continue;
65 if (FLAG(v) && strchr("aeiou", tolower(c))) continue;
66 if (!FLAG(y) || (0x80&randbuf[rand]))
67 if (c<'0' || (c>'9' && c<'A') || (c>'Z' && c<'a') || c>'z') continue;
68 if (TT.r && strchr(TT.r, c)) continue;
69
70 toybuf[ii++] = c;
71 }
72 if (FLAG(1) || (x += length+1)+length>=xx) x = 0;
73 xprintf("%.*s%c", length, toybuf, x ? ' ' : '\n');
74 }
75 if (x) xputc('\n');
76}
77