aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/read.c
blob: 56d80136d9fbc1a8c598c42f1c78267b55a7d6a9 (plain)
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
#include <u.h>
#include <libc.h>

int	multi;
int	nlines;
char	*status = nil;

int
line(int fd, char *file)
{
	char c;
	int m, n, nalloc;
	char *buf;

	nalloc = 0;
	buf = nil;
	for(m=0; ; ){
		n = read(fd, &c, 1);
		if(n < 0){
			fprint(2, "read: error reading %s: %r\n", file);
			exits("read error");
		}
		if(n == 0){
			if(m == 0)
				status = "eof";
			break;
		}
		if(m == nalloc){
			nalloc += 1024;
			buf = realloc(buf, nalloc);
			if(buf == nil){
				fprint(2, "read: malloc error: %r\n");
				exits("malloc");
			}
		}
		buf[m++] = c;
		if(c == '\n')
			break;
	}
	if(m > 0)
		write(1, buf, m);
	free(buf);
	return m;
}

void
lines(int fd, char *file)
{
	do{
		if(line(fd, file) == 0)
			break;
	}while(multi || --nlines>0);
}

void
main(int argc, char *argv[])
{
	int i, fd;
	char *s;

	ARGBEGIN{
	case 'm':
		multi = 1;
		break;
	case 'n':
		s = ARGF();
		if(s){
			nlines = atoi(s);
			break;
		}
		/* fall through */
	default:
		fprint(2, "usage: read [-m] [-n nlines] [files...]\n");
		exits("usage");
	}ARGEND

	if(argc == 0)
		lines(0, "<stdin>");
	else
		for(i=0; i<argc; i++){
			fd = open(argv[i], OREAD);
			if(fd < 0){
				fprint(2, "read: can't open %s: %r\n", argv[i]);
				exits("open");
			}
			lines(fd, argv[i]);
			close(fd);
		}

	exits(status);
}