Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_posix/sys/src/cmd/p.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include <bio.h>
4
 
5
#define	DEF	22	/* lines in chunk: 3*DEF == 66, #lines per nroff page */
6
 
7
Biobuf *cons;
8
Biobuf bout;
9
 
10
int pglen = DEF;
11
 
12
void printfile(int);
13
 
14
void
15
main(int argc, char *argv[])
16
{
17
	int n;
18
	int f;
19
 
20
	if((cons = Bopen("/dev/cons", OREAD)) == 0) {
21
		fprint(2, "p: can't open /dev/cons\n");
22
		exits("missing /dev/cons");
23
	}
24
	Binit(&bout, 1, OWRITE);
25
	n = 0;
26
	while(argc > 1) {
27
		--argc; argv++;
28
		if(*argv[0] == '-'){
29
			pglen = atoi(&argv[0][1]);
30
			if(pglen <= 0)
31
				pglen = DEF;
32
		} else {
33
			n++;
34
			f = open(argv[0], OREAD);
35
			if(f < 0){
36
				fprint(2, "p: can't open %s - %r\n", argv[0]);
37
				continue;
38
			}
39
			printfile(f);
40
			close(f);
41
		}
42
	}
43
	if(n == 0)
44
		printfile(0);
45
	exits(0);
46
}
47
 
48
void
49
printfile(int f)
50
{
51
	int i, j, n;
52
	char *s, *cmd;
53
	Biobuf *b;
54
 
55
	b = malloc(sizeof(Biobuf));
56
	Binit(b, f, OREAD);
57
	for(;;){
58
		for(i=1; i <= pglen; i++) {
59
			s = Brdline(b, '\n');
60
			if(s == 0){
61
				n = Blinelen(b);
62
				if(n > 0)	/* line too long for Brdline */
63
					for(j=0; j<n; j++)
64
						Bputc(&bout, Bgetc(b));
65
				else{		/* true EOF */
66
					free(b);
67
					return;
68
				}
69
			}else{
70
				Bwrite(&bout, s, Blinelen(b)-1);
71
				if(i < pglen)
72
					Bwrite(&bout, "\n", 1);
73
			}
74
		}
75
		Bflush(&bout);
76
	    getcmd:
77
		cmd = Brdline(cons, '\n');
78
		if(cmd == 0 || *cmd == 'q')
79
			exits(0);
80
		cmd[Blinelen(cons)-1] = 0;
81
		if(*cmd == '!'){
82
			if(fork() == 0){
83
				dup(Bfildes(cons), 0);
84
				execl("/bin/rc", "rc", "-c", cmd+1, nil);
85
			}
86
			waitpid();
87
			goto getcmd;
88
		}
89
	}
90
}