This source file includes following definitions.
- wc
- main
1 #include "kernel/types.h"
2 #include "kernel/stat.h"
3 #include "kernel/fcntl.h"
4 #include "user/user.h"
5
6 char buf[512];
7
8 void
9 wc(int fd, char *name)
10 {
11 int i, n;
12 int l, w, c, inword;
13
14 l = w = c = 0;
15 inword = 0;
16 while((n = read(fd, buf, sizeof(buf))) > 0){
17 for(i=0; i<n; i++){
18 c++;
19 if(buf[i] == '\n')
20 l++;
21 if(strchr(" \r\t\n\v", buf[i]))
22 inword = 0;
23 else if(!inword){
24 w++;
25 inword = 1;
26 }
27 }
28 }
29 if(n < 0){
30 printf("wc: read error\n");
31 exit(1);
32 }
33 printf("%d %d %d %s\n", l, w, c, name);
34 }
35
36 int
37 main(int argc, char *argv[])
38 {
39 int fd, i;
40
41 if(argc <= 1){
42 wc(0, "");
43 exit(0);
44 }
45
46 for(i = 1; i < argc; i++){
47 if((fd = open(argv[i], O_RDONLY)) < 0){
48 printf("wc: cannot open %s\n", argv[i]);
49 exit(1);
50 }
51 wc(fd, argv[i]);
52 close(fd);
53 }
54 exit(0);
55 }