-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.c
82 lines (78 loc) · 1.69 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
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
if(strcmp(argv[1],"-n")==0){//to print the line number
struct stat st={0};
if(stat(argv[2],&st)==-1){//checks if such dirtectory does not exist
printf("%s\n","File does not exist" );
}
else{
FILE *f = fopen(argv[2],"r");
char line[1000];
int count=1;
while(fgets(line,sizeof line,f)!=NULL){
fprintf(stdout, "%d %s",count,line);
count++;
}
perror(argv[2]);
fclose(f);
}
}
else if(strcmp(argv[1],"-E")==0){//to print $ at the begining of each line
struct stat st={0};
if(stat(argv[2],&st)==-1){//checks if such dirtectory does not exist
printf("%s\n","File does not exist" );
}
else{
FILE *f = fopen(argv[2],"r");
char line[1000];
while(fgets(line,sizeof line,f)!=NULL){
fprintf(stdout, "$%s",line);
}
perror(argv[2]);
fclose(f);
}
}
else{//default
struct stat st={0};
if(stat(argv[1],&st)==-1){//checks if such dirtectory does not exist
printf("%s\n","File does not exist" );
}
else{
FILE *f = fopen(argv[1],"r");
char line[1000];
while(fgets(line,sizeof line,f)!=NULL){
fprintf(stdout, "%s",line);
}
perror(argv[1]);
fclose(f);
}
}
return 0;
}
/*DIR *dir;
struct dirent **file;
char cwd[1000];
getcwd(cwd,sizeof cwd);
dir=opendir(cwd);
if(dir!=NULL){
//printf("%s\n","hiii" );
int size=scandir(cwd,&file,0,alphasort);
int i=0;
while(i<size){
char *line=file[i]->d_name;
char a=line[0];
char b=line[1];
printf("%s ",line );//print all files including . and ..
free(file[i]);
i++;
}
free(file);
}
else{
printf("%s\n","ooops" );
}*/