-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
108 lines (98 loc) · 3.26 KB
/
ls.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
#include "header.h"
void ls(char **tokens){
int tno=0;
while(tokens[tno]!=NULL){
tno++;
}
char cwd[1000];
int hidden_flag=0;
int list_flag=0;
int i;
char** lsdirs = malloc(sizeof(char)*1000);
int dir=0;
for(i=1;i<tno;i++){
if( strcmp(tokens[i],"-al")==0 || strcmp(tokens[i],"-a")==0 || strcmp(tokens[i],"-la")==0 )
hidden_flag=1;
if( strcmp(tokens[i],"-al")==0 || strcmp(tokens[i],"-l")==0 || strcmp(tokens[i],"-la")==0 )
list_flag=1;
if( strcmp(tokens[i],"-al")!=0 && strcmp(tokens[i],"-l")!=0 && strcmp(tokens[i],"-la")!=0 && strcmp(tokens[i],"-a")!=0 ){
lsdirs[dir++]=tokens[i];
}
}
int n;
struct dirent **namelist;
// printf("%d\n",dir);
if(dir==0){
n = scandir(".", &namelist, NULL, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,namelist[n]->d_name);
if(hidden_flag==0 && namelist[n]->d_name[0]!='.'){
if(list_flag==1){
print_details(cwd);
printf("\t%s\n", namelist[n]->d_name);
}
else{
printf("%s\n", namelist[n]->d_name);
}
}
else if(hidden_flag==1){
if(list_flag==1){
print_details(cwd);
printf("\t%s\n", namelist[n]->d_name);
}
else{
printf("%s\n", namelist[n]->d_name);
}
}
free(namelist[n]);
}
free(namelist);
}
return;
}
while(dir>0)
{ // struct dirent **namelist;
dir--;
// printf("%s %d\n",lsdirs[0],dir);
n = scandir(lsdirs[dir], &namelist, NULL, alphasort);
if (n < 0)
perror("scandir");
else {
chdir(lsdirs[dir]);
printf("%s:\n",lsdirs[dir]);
while (n--) {
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,namelist[n]->d_name);
if(hidden_flag==0 && namelist[n]->d_name[0]!='.'){
if(list_flag==1){
print_details(cwd);
printf("\t%s\n", namelist[n]->d_name);
}
else{
printf("%s\n", namelist[n]->d_name);
}
}
else if(hidden_flag==1){
if(list_flag==1){
print_details(cwd);
printf("\t%s\n", namelist[n]->d_name);
}
else{
printf("%s\n", namelist[n]->d_name);
}
}
free(namelist[n]);
}
printf("\n");
free(namelist);
chdir(dir_before_ls);
}
// dir--;
}
}