-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.c
153 lines (109 loc) · 2.13 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
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
153
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc,char *argv[]){
if (argc==2){
DIR *lol;
struct dirent *travel;
lol = opendir(".");
if(strcmp(argv[1],"*")==0){
DIR *lol;
struct dirent *travel;
lol = opendir(".");
if(lol!=NULL){
travel = readdir(lol);
while(travel!=NULL){
FILE *cp;
cp = fopen((*travel).d_name,"r");
int num;
num = fgetc(cp);
while(num!=-1){
printf("%c",num);
num = fgetc(cp);
}
travel = readdir(lol);
}
}
}
else{
FILE *cp;
cp = fopen(argv[1],"r");
struct stat lol1;
int existence = stat(argv[1],&lol1);
if(existence==0){
if(S_ISREG(lol1.st_mode)){
if(lol!=NULL){
travel = readdir(lol);
while(travel!=NULL){
if(strcmp((*travel).d_name,argv[1])==0){
int num;
num = fgetc(cp);
while(num!=-1){
printf("%c",num);
num = fgetc(cp);
}
break;
}
travel = readdir(lol);
}
}
}
else{
printf("Cannot cat directory %s\n",argv[1] );
}
}
else{
printf("File %s doesn't exist\n",argv[1] );
}
}
}
if(argc==3){
if(strcmp(argv[1],"-n")==0){
DIR *lol;
struct dirent *travel;
lol = opendir(".");
FILE *cp;
cp = fopen(argv[2],"r");
struct stat lol1;
int existence = stat(argv[2],&lol1);
if(existence==0){
if(S_ISREG(lol1.st_mode)){
if(lol!=NULL){
travel = readdir(lol);
while(travel!=NULL){
if(strcmp((*travel).d_name,argv[2])==0){
int num;
num = fgetc(cp);
int count = 1;
printf("%d ",count);
while(num!=-1){
if(num=='\n'){
printf("\n");
count++;
printf("%d ",count);
}
else{
printf("%c",num);
}
num = fgetc(cp);
}
break;
}
travel = readdir(lol);
}
}
}
else{
printf("Cannot cat directory %s\n",argv[2] );
}
}
else{
printf("File %s doesn't exist\n",argv[2] );
}
}
}
}