-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_ls.c
129 lines (127 loc) · 5.26 KB
/
execute_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include"variables.h"
#include"header.h"
// This is constructed by changing directories as opendir() was not working on parent directories. Ask why?
int execute_ls(char **args,int flag){
int num, one = 0, two = 0, i = 1, k = 0;
char **direc = malloc(1000*(sizeof(char*)));
while(args[i]){
if(args[i][0] == '-'){
for(int j = 1;j < strlen(args[i]); j++){
if(args[i][j] == 'l') one = 1;
else if(args[i][j] == 'a') two = 1;
else{
fprintf(stderr, "invalid option -- '%c'\n", args[i][j]);
return 1;
}
}
}
else if(args[i][0] == '~'){
direc[k++] = concat(HOME, &args[i][1]);
}
else{
direc[k++] = args[i]; // saving directoris for ls
}
i++;
}
direc[k] = NULL;
if(k == 0){direc[k] = "./";direc[++k] = NULL;}
int cnt = k, n;
if(one == 1 && two == 0) num = 1; // ls -l
else if(one == 1 && two == 1) num = 2; // ls -al
else if(one == 0 && two == 0) num = 3; // ls
else if(one == 0 && two == 1) num = 4; // ls -a
char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct stat fileStat;
struct tm *date_time;
char timestr[20], pwd[2000];
getcwd(pwd, sizeof(pwd));
DIR * d;
struct dirent *dir, **directories;
k = 0;
while(direc[k]){
if(cnt > 1)
printf("%s: \n", direc[k]);
int check = chdir(direc[k]);
n = scandir(".", &directories, NULL, alphasort);
if(n < 0 || check != 0){
perror("ls");
k++;
continue;
}
int i = 0;
while(i < n){
dir = directories[i];
i++;
if(stat(dir->d_name, &fileStat) < 0){
fprintf(stderr, "Error in reading file\n");
chdir(pwd);
return 1;
}
else
{
if((num == 1 || num == 3) && (dir->d_name)[0] == '.')
continue;
if(num == 1 || num == 2)
{
if (S_ISDIR(fileStat.st_mode)) printf("d");
else printf("-");
if (fileStat.st_mode & S_IRUSR) printf("r");
else printf("-");
if (fileStat.st_mode & S_IWUSR) printf("w");
else printf("-");
if (fileStat.st_mode & S_IXUSR) printf("x");
else printf("-");
if (fileStat.st_mode & S_IRGRP) printf("r");
else printf("-");
if (fileStat.st_mode & S_IWGRP) printf("w");
else printf("-");
if (fileStat.st_mode & S_IXGRP) printf("x");
else printf("-");
if (fileStat.st_mode & S_IROTH) printf("r");
else printf("-");
if (fileStat.st_mode & S_IWOTH) printf("w");
else printf("-");
if (fileStat.st_mode & S_IXOTH) printf("x");
else printf("-");
printf("\t");
printf("%lu\t",fileStat.st_nlink);
struct passwd * user = getpwuid(fileStat.st_uid);
struct group * group = getgrgid(fileStat.st_gid);
if (user != 0) printf("%s\t",user -> pw_name);
else printf("\t");
if (group != 0) printf("%s\t",group -> gr_name);
else printf("\t");
if (lstat(dir->d_name,&fileStat)==0 && S_ISLNK(fileStat.st_mode))
printf("%ld\t",fileStat.st_size);
else printf("%ld\t",fileStat.st_size);
date_time = localtime(&fileStat.st_ctime);
printf("%s %d\t",month[date_time -> tm_mon],date_time -> tm_mday);
strftime(timestr, 100, "%H:%M\t", localtime(&fileStat.st_ctime)); // Need to check if access time ot modified time a_time,c_time,a_time
printf("%s",timestr);
}
if (S_ISDIR(fileStat.st_mode)) printf(KBLU "%s " RESET,dir -> d_name);
else if (lstat(dir->d_name,&fileStat)==0 && S_ISLNK(fileStat.st_mode)) // Stat takes the file it refers to instead of the symbolic link
{
printf(KCYN "%s" RESET,dir -> d_name);
if (one==1)
{
char buff[1024];
ssize_t len = readlink(dir -> d_name, buff, sizeof(buff)-1);
if (len != -1)
{
buff[len] = '\0';
printf(" -> %s",buff);
}
}
}
else if (stat(dir -> d_name,&fileStat)==0 && ((fileStat.st_mode & S_IXUSR) || (fileStat.st_mode & S_IXGRP) || (fileStat.st_mode & S_IXOTH))) printf(KGRN "%s" RESET,dir -> d_name);
else printf("%s ", dir->d_name);
}
printf("\n");
}
k++;
if(cnt > 1)
printf("\n");
}
return 1;
}