-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
53 lines (47 loc) · 846 Bytes
/
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
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[]){
FILE* ptr;
if(argc==1){
printf("cat:invalid number of arguments.");
return 0;
}
ptr = fopen(argv[1],"r");
if(ptr==NULL){
printf("Error: The file doesn't exist.\n");
return 0;
}
char c;
if(argc==2){
while((c=fgetc(ptr))!=EOF){
printf("%c",c);
}
fclose(ptr);
}else if(argc==3){
if(strcmp(argv[2],"-E")==0){
while((c=fgetc(ptr))!=EOF){
if(c=='\n'){
printf("$");
}
printf("%c",c);
}
fclose(ptr);
}else if(strcmp(argv[2],"-n")==0){
int j = 1;
printf("%d ",j);
int mem=0;
while((c=fgetc(ptr))!=EOF){
if(mem==1){
mem = 0;
j++;
printf("%d ",j);
}
if(c=='\n'){
mem = 1;
}
printf("%c",c);
}
fclose(ptr);
}
}
}