-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
102 lines (101 loc) · 2.92 KB
/
Copy pathcat.c
File metadata and controls
102 lines (101 loc) · 2.92 KB
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
//cat 1.txt
//cat -n 1.txt
//cat -E 1.txt
int main(int argc, char* argv[]){
if (argc == 1){
printf("Filename not provided\n");
fflush(stdout);
return 0;
}
if (argc>2){
if(argv[1] == NULL){
printf("Filename not provided.\n");
fflush(stdout);
return 0;
}
if (strcmp(argv[1], "-n") == 0){
FILE* fop = fopen(argv[2], "r");
if(access(argv[2], F_OK) != 0){
printf("File '%s' does not exist!\n", argv[2]);fflush(stdout);
return 0;
}
// if (!fop){
// printf("Empty file created\n");
// printf("Exitting\n");
// return 0;
// }
int cur;
int linenum = 1;
printf("%d)", linenum);
fflush(stdout);
while((cur = getc(fop)) != EOF){
putchar(cur);
fflush(stdout);
if (cur == '\n'){
linenum = linenum + 1;
printf("%d)", linenum);
fflush(stdout);
}
}
fclose(fop);
return 0;
}
if (strcmp(argv[1], "-E") == 0){
FILE* fop = fopen(argv[2], "r");
if(access(argv[2], F_OK) != 0){
printf("File '%s' does not exist!\n", argv[2]);fflush(stdout);
return 0;
}
// if (!fop){
// printf("Empty file created\n");
// printf("Exitting\n");
// return 0;
// }
int cur;
//char* cur;
cur = getc(fop);
printf("$");
fflush(stdout);
while((cur = getc(fop)) != EOF){
putchar(cur);
fflush(stdout);
if (cur == '\n'){
printf("$");
fflush(stdout);
cur = getc(fop);
}
}
fclose(fop);
return 0;
}
for(int i = 1; i<=argc; i++){
FILE* fop = fopen(argv[i], "r");
if(access(argv[i], F_OK) != 0){
printf("File '%s' does not exist!\n", argv[i]);fflush(stdout);
return 0;
}
int cur;
while((cur = getc(fop)) != EOF){
putchar(cur);
fflush(stdout);
}
fclose(fop);
}
return 0;
}
else{
FILE* fop = fopen(argv[1], "r");
int cur;
//char* cur;
fflush(stdout);
while((cur = getc(fop)) != EOF){
putchar(cur);
fflush(stdout);
}
fclose(fop);
return 0;
}
}