-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathls.c
142 lines (131 loc) · 4.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "headers.h"
// function to check if a file is hidden or not
int is_hidden(char *file)
{
if(file[0] == '.')
return 1;
else
return 0;
}
// function to long list the files in the directory
void l_list(char *dir, char *file)
{
char *path = (char *)malloc(sizeof(char) * (strlen(dir) + strlen(file) + 5));
strcpy(path, dir);
strcat(path, "/");
strcat(path, file);
struct stat file_stat;
if(stat(path, &file_stat) < 0)
{
perror(DFLT "File error");
strcpy(emoji,":'(");
}
else
{
char perm[20]="";
struct group *gr;
gr = getgrgid(file_stat.st_gid);
struct passwd *pw;
pw = getpwuid(file_stat.st_uid);
if (S_ISDIR(file_stat.st_mode) ? strcat(perm, "d") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IRUSR) ? strcat(perm, "r") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IWUSR) ? strcat(perm, "w") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IXUSR) ? strcat(perm, "x") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IRGRP) ? strcat(perm, "r") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IWGRP) ? strcat(perm, "w") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IXGRP) ? strcat(perm, "x") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IROTH) ? strcat(perm, "r") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IWOTH) ? strcat(perm, "w") : strcat(perm, "-"));
if ((file_stat.st_mode & S_IXOTH) ? strcat(perm, "x") : strcat(perm, "-"));
char *time = (char *)malloc(sizeof(char) * 50);
strftime(time, 50, "%b %d %H:%M", localtime( &file_stat.st_mtime));
if(perm[0] == 'd')
printf(DFLT "%s %ld %s\t%s\t%ld\t%s\t" BLUE_BOLD "%s" DFLT "\n", perm, file_stat.st_nlink, pw->pw_name,gr->gr_name, file_stat.st_size, time, file);
else if(perm[3] == 'x')
printf(DFLT "%s %ld %s\t%s\t%ld\t%s\t" GREEN_BOLD "%s" DFLT "\n", perm, file_stat.st_nlink, pw->pw_name,gr->gr_name, file_stat.st_size, time, file);
else
printf(DFLT "%s %ld %s\t%s\t%ld\t%s\t%s\n", perm, file_stat.st_nlink, pw->pw_name,gr->gr_name, file_stat.st_size, time, file);
}
free(path);
}
// function to list files in the directory according to the flags
void ls(char* dir, int a_flag, int l_flag)
{
struct dirent **files;
int num = scandir(dir, &files, NULL, alphasort);
if(num < 0) {
perror(DFLT "Directory error");
strcpy(emoji,":'(");
return;
}
for(int i = 0; i < num; i++) {
if(a_flag == 1 || (a_flag == 0 && !is_hidden(files[i]->d_name))) {
if(l_flag) {
l_list(dir, files[i]->d_name);
}
else {
if(files[i]->d_type == DT_DIR) {
printf(BLUE_BOLD "%s\n" DFLT, files[i]->d_name);
}
else {
printf(GREEN_BOLD "%s\n" DFLT,files[i]->d_name);
}
}
}
free(files[i]);
}
free(files);
}
// checking flags in ls
void check_ls(char *token)
{
int l_flag = 0;
int a_flag = 0;
int flag = 0;
// handling extra spaces, tabs etc. in input command
char delim[] = " \n\t\r";
char** direcs = (char**) malloc(20 * sizeof(char*));
char** p_direcs = (char**) malloc(20 * sizeof(char*));
int k = 0;
token = strtok(NULL, delim);
if(token == NULL)
{
ls(".",0,0);
flag = 1;
}
else {
while(token != NULL)
{
if(!strcmp(token,"-l")) {
l_flag = 1;
}
else if(!strcmp(token,"-a")) {
a_flag = 1;
}
else if(!strcmp(token,"-al") || !strcmp(token,"-la")) {
l_flag = 1;
a_flag = 1;
}
else
{
char *abs_path = get_abs_path(token);
direcs[k] = abs_path;
p_direcs[k] = abs_path;
k++;
flag = 1;
}
token = strtok(NULL, delim);
}
for(ll i = 0; i < k; i++)
{
if(k > 1) printf(DFLT "%s:\n",direcs[i]);
ls(p_direcs[i], a_flag, l_flag);
if(k > 1) printf("\n");
}
}
// if only flags mentioned, list files of cwd appropriately
if(!flag)
{
ls(".", a_flag, l_flag);
}
}