xref: /inferno-os/os/boot/rpcg/sload.c (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1 /*
2  * send S records to rpcg
3  */
4 
5 #include <u.h>
6 #include <libc.h>
7 #include <bio.h>
8 
9 static	int	dbg;
10 static	char	buf[2048];
11 static	int	run=1;
12 static	void	stuffbym(char*, int, int);
13 static	void	getdot(void);
14 
15 void
main(int argc,char ** argv)16 main(int argc, char **argv)
17 {
18 	int n;
19 	char *l;
20 	Biobuf *f;
21 	static int p;
22 
23 	ARGBEGIN{
24 	case 'd': dbg++; break;
25 	case 'n': run=0; break;
26 	}ARGEND
27 
28 	f = Bopen(*argv? *argv: "k.mx", OREAD);
29 	if(f == 0) {
30 		fprint(2, "sload: cannot open k.mx: %r\n");
31 		exits("sload");
32 	}
33 	getdot();
34 	while((l = Brdline(f, '\n')) != 0) {
35 		l[Blinelen(f)-1] = '\r';
36 		stuffbym(l, Blinelen(f), 16);
37 		getdot();
38 		if(++p % 25 == 0)
39 			write(2, ".", 1);
40 	}
41 	exits(0);
42 }
43 
44 static void
stuffbym(char * l,int n,int m)45 stuffbym(char *l, int n, int m)
46 {
47 	int nr, ns;
48 
49 	while(n > 0) {
50 		ns = n;
51 		if(ns > m)
52 			ns = m;
53 		write(1, l, ns);
54 		l += ns;
55 		n -= ns;
56 	}
57 }
58 
59 static void
getdot(void)60 getdot(void)
61 {
62 	char c;
63 
64 	for(;;){
65 		if(read(0, &c, 1) != 1)
66 			exits("bang");
67 		write(2, &c, 1);
68 		if(c == '.')
69 			break;
70 	}
71 }
72