-
Notifications
You must be signed in to change notification settings - Fork 1
/
wc.c
73 lines (56 loc) · 1.26 KB
/
wc.c
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
#include "myshell.h"
int wc(int fd);
int main(int argc, char** argv){
char* options = argv[0];
char** paths = argv+1;
int numOfPaths = argc-1;
if(argc==0){
printf("Enter file name to perform action\n");
return -1;
}
for(int i=0;i<numOfPaths;i++){
int fd = open(paths[i],O_RDONLY);
if(fd<0)
return -1;
wc(fd);
}
}
int wc(int fd){
char* buf = malloc(sizeof(char)*BUFFERSIZE2048);
int bytesRead;
while (( bytesRead = read(fd, buf, BUFFERSIZE2048)) > 0){
//printf("Number of bytes read = %d\n",bytesRead);
}
//printf("%s\n",buf);
int lineCount = 0;
int wordCount = 0;
int byteCount = 0;
for(int i=0;i<strlen(buf);i++){
if(buf[i]=='\n')
lineCount++;
byteCount++;
}
printf("%d\t",lineCount);
//calculate lineCount //consequent new lines will be calculated as 1 newline
char* token = strtok(buf, "\n");
char* lines[lineCount];
int i=0;
while(token!=NULL){
lines[i] = malloc(sizeof(char)*(strlen(token)+1));
strcpy(lines[i],token);
i++;
token = strtok(NULL,"\n");
}
lineCount = i;
for(int i=0;i<lineCount;i++){
char* token = strtok(lines[i]," \t");
while(token!=NULL){
wordCount++;
token = strtok(NULL," \t");
}
}
//TODO Free data
printf("%d\t",wordCount);
printf("%d\n",byteCount);
return 0;
}