-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail.c
152 lines (133 loc) · 3.21 KB
/
tail.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#define DEAFULT 10
#define ILLEGAL_OPTION -99
#define ERROR_CODE -999
char buf[512];
int linesNumberToPrint(char* argvs[]) {
char** argv = argvs;
int i = 1;
char number[100];
if(argv[1][0] == '-') {
for(i=1; i<strlen(argv[1]);i++) {
if(((int)(argv[1][i]))>=48 && ((int)(argv[1][i]))<=57) {
number[i-1] = argv[1][i];
}
else {
return ILLEGAL_OPTION; // Illegal Option
}
}
int linesToDisplay = (atoi)(number);
return linesToDisplay;
}
else {
for(i=0; i<strlen(argv[1]);i++) {
if(((int)(argv[1][i]))>=48 && ((int)(argv[1][i]))<=57) {
number[i] = argv[1][i];
}
else {
return ERROR_CODE;
}
}
return ERROR_CODE;
}
}
void tail(int fd, int linesToPrint)
{
int i, n;
int linesInFile = 0;
int linesReadInTempFile = 0;
int linesToPrintFn = linesToPrint;
int lineIndexPrintStart;
int tempFile = open("tempTailFile",O_CREATE | O_RDWR);
// Writing in temporaryFile
while((n = read(fd, buf, sizeof(buf))) > 0){
write(tempFile, buf, n);
for(i=0; i<n; i++){
if(buf[i] == '\n')
linesInFile++;
}
}
close(tempFile);
lineIndexPrintStart = linesInFile - linesToPrintFn;
//Open the file again to read the lines
tempFile = open("tempTailFile", O_RDONLY);
while((n = read(tempFile, buf, sizeof(buf))) > 0) {
for(i=0; i<n; i++) {
if(linesReadInTempFile >= lineIndexPrintStart) {
printf(1, "%c", buf[i]);
}
if(buf[i] == '\n') {
linesReadInTempFile++;
}
}
}
// To delete the temporary file
unlink("tempTailFile");
}
int main(int argc, char *argv[]) {
int fd, numberOfLines;
//If user is entering input from the command line
if(argc <= 1) {
tail(0,DEAFULT);
exit();
}
else if(argc == 2) {
numberOfLines = linesNumberToPrint(argv);
fd = open(argv[1], O_RDONLY);
// for handling case like
//tail README
if(fd >= 0) {
tail(fd, DEAFULT);
close(fd);
exit();
}
// When file can't be opened and the paramater after
// tail starts with '-' and it's an invalid number
// eg cat README | tail -68ty
else if ((fd < 0) && (numberOfLines == ILLEGAL_OPTION)) {
printf(1, "Illegal Option\n");
exit();
}
// When file can't be opened and the paramater
// eg cat README | tail 68
else if(fd < 0 && numberOfLines == ERROR_CODE) {
printf(1, "tail: cannot open %s\n", argv[1]);
exit();
}
// For handling cases like cat README | tail -9
else if((fd < 0) && ((numberOfLines%2 == 0) || (numberOfLines%2 == 1))) {
tail (0,numberOfLines);
exit();
}
// When tail can't open a file
else if (fd < 0) {
printf(1, "tail: cannot open %s\n", argv[1]);
}
}
else if(argc == 3) {
numberOfLines = linesNumberToPrint(argv);
if((fd = open(argv[2], O_RDONLY)) < 0){
printf(1, "tail: cannot open %s\n", argv[2]);
exit();
}
// If the number is -99 which means the paramater
// after tail is not correct so we will output
// Illegal Option error
if(numberOfLines == ILLEGAL_OPTION) {
printf(1, "Illegal option\n");
exit();
}
numberOfLines = ((numberOfLines == ERROR_CODE) ? DEAFULT : numberOfLines);
tail(fd, numberOfLines);
close(fd);
exit();
}
else {
printf(1, "tail: cannot open %s\n", argv[2]);
exit();
}
exit();
}