-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
99 lines (93 loc) · 2.21 KB
/
cat.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
int main(int argc, char** argv) {
int i, option_flag = 0, b_flag = 0, e_flag = 0, number_files = 0, line_count = 1;
char* file_names[100];
char* output_filename = NULL;
FILE* output_file;
if (!strcmp(argv[argc - 1], ">\n")) {
printf("Error in output redirection!!\n");
return 0;
}
for (i = 1; i < argc; i++) {
if (argv[i][strlen(argv[i]) - 1] == '\n')
argv[i][strlen(argv[i]) - 1] = '\0';
if (argv[i][0] == '-') {
option_flag = 1;
if (!strcmp(argv[i], "-b")) b_flag = 1;
else if (!strcmp(argv[i], "-e")) e_flag = 1;
else printf("Invalid option '%s'\n", argv[i]);
}
else if (!strcmp(argv[i], ">")) {
output_filename = argv[i + 1];
}
else if (strcmp(argv[i - 1], ">")) {
file_names[number_files++] = argv[i];
}
}
if (output_filename == NULL)
output_file = stdout;
else {
output_file = fopen(output_filename, "w");
if (output_file == NULL) {
output_file = stdout;
printf("File '%s' doesn't exist!!\n", output_filename);
}
}
if (number_files == 0) {
char ch, prev = '\n';
while ((ch = getchar()) != EOF) {
if (ch == '\n' && prev != '\n')
line_count++;
if (ch == '\n' && e_flag) {
fprintf(output_file, "$\n");
prev = ch;
continue;
}
if (prev == '\n' && b_flag) {
fprintf(output_file, "\t%d %c", line_count, ch);
prev = ch;
continue;
}
fprintf(output_file, "%c", ch);
prev = ch;
}
}
else {
for (i = 0; i < number_files; i++) {
char* file = file_names[i];
FILE* read_file = fopen(file, "r");
if (read_file == NULL) {
printf("File '%s' doesn't exist!!\n", file);
continue;
}
char ch, prev = '\n';
while ((ch = fgetc(read_file)) != EOF) {
if (ch == '\n' && prev != '\n')
line_count++;
if (ch == '\n' && e_flag) {
fprintf(output_file, "$\n");
prev = ch;
continue;
}
if (prev == '\n' && b_flag) {
fprintf(output_file, "\t%d %c", line_count, ch);
prev = ch;
continue;
}
fprintf(output_file, "%c", ch);
prev = ch;
}
fclose(read_file);
}
if (output_filename != NULL)
fclose(output_file);
}
return 0;
}