-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
91 lines (82 loc) · 1.68 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
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
char cwd[1000];
void callGetcwd(){
getcwd(cwd,sizeof(cwd));
if(cwd==NULL){
printf("getcwd() error\n");
}
}
int main(int argc, char const* arg[]){
char* token=arg[0];
char stri[105];
strcpy(stri,arg[1]);
if(strcmp(token,"-n")==0){
char* vari=strtok(stri," ");
if(strcmp(vari,"cat")==0){
vari=strtok(NULL," ");
if(strcmp(vari,"-n")==0){
vari=strtok(NULL," ");
}
}
struct stat st={0}; //initalize status of file
int line=1;
callGetcwd();
strcat(cwd,"/");
strcat(cwd,vari);
if(stat(cwd,&st)==-1){
printf("File does not exist\n");
}
FILE *file=fopen(cwd,"r");
printf("%s/n",cwd);
if(file!=NULL){
char output[150];
while((fgets(output,sizeof(output),file))!=NULL){
printf("%d ",line);
line+=1;
fputs(output,stdout);
}
}
if(file==NULL){
printf("Error in opening file\n");
}
}
else if(strcmp(token,"-E")==0){
struct stat st={0}; //initalize status of file
char* vari=strtok(stri," ");
if(strcmp(vari,"cat")==0){
vari=strtok(NULL," ");
if(strcmp(vari,"-E")==0){
vari=strtok(NULL," ");
}
}
callGetcwd();
strcat(cwd,"/");
strcat(cwd,vari);
if(stat(cwd,&st)==-1){
printf("File does not exist\n");
}
FILE *file=fopen(cwd,"r");
if(file!=NULL){
char output[150];
while(fgets(output,sizeof(output),file)!=NULL){
strcat(output,"$");
fputs(output,stdout);
}
}
if(file==NULL){
printf("Error in opening file\n");
}
}
else if(strcmp(token,"--help")==0){
printf("Help in cat Command\n");
}
else{
printf("Command not supported\n");
}
}