xref: /inferno-os/appl/cmd/randpass.b (revision 2b69dba5038ffd0b59cf30a4c44bce549e5097f8)
1implement Randpass;
2
3include "sys.m";
4	sys: Sys;
5	stderr: ref Sys->FD;
6
7include "draw.m";
8
9include "ipints.m";
10	ipints: IPints;
11	IPint: import ipints;
12
13Randpass: module
14{
15	init: fn(nil: ref Draw->Context, nil: list of string);
16};
17
18init(nil: ref Draw->Context, args: list of string)
19{
20	sys = load Sys Sys->PATH;
21	ipints = load IPints IPints->PATH;
22
23	if(args != nil)
24		args = tl args;
25	pwlen := 16;
26	if(args != nil){
27		if(!isnumeric(hd args) || (pwlen = int hd args) <= 8 || pwlen > 256){
28			sys->fprint(sys->fildes(2), "Usage: randpass [password-length(<256, default=16)]\n");
29			raise "fail:usage";
30		}
31	}
32	sys->print("%s\n", IPint.random(pwlen*8).iptob64()[0: pwlen]);
33}
34
35isnumeric(s: string): int
36{
37	for(i := 0; i < len s; i++)
38		if(!(s[i]>='0' && s[i]<='9'))
39			return 0;
40	return i > 0;
41}
42