-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
198 lines (182 loc) · 5.08 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include "utils.h"
#include "cli.h"
#include "colors.h"
typedef struct
{
char *name;
char *permissions;
ino_t inode;
unsigned long long size;
mode_t mode;
uid_t uid;
gid_t gid;
long last_modified;
} FileStats;
int cmp_file_size(const void *a, const void *b)
{
return ((FileStats *)b)->size - ((FileStats *)a)->size;
}
int cmp_file_name(const void *a, const void *b)
{
return strcasecmp(((FileStats *)a)->name, ((FileStats *)b)->name);
}
int cmp_file_date(const void *a, const void *b)
{
return ((FileStats *)b)->last_modified - ((FileStats *)a)->last_modified;
}
void print_name(FileStats *stats, int should_print_color)
{
switch (stats->mode & S_IFMT)
{
case S_IFBLK:
case S_IFCHR:
case S_IFIFO:
print_bold(should_print_color);
printf("%-30s", stats->name);
print_reset(should_print_color);
break;
case S_IFDIR:
print_cyan(should_print_color);
printf("%-30s", stats->name);
print_reset(should_print_color);
break;
case S_IFLNK:
print_blue(should_print_color);
printf("%-30s", stats->name);
print_reset(should_print_color);
break;
case S_IFREG:
printf("%-30s", stats->name);
break;
default:
printf("%-30s", stats->name);
break;
}
}
void print_stats(FileStats *stats, unsigned int size)
{
int should_print_color = isatty(fileno(stdout));
int should_print_total = isatty(fileno(stdin));
// messes with the output when piped
if (should_print_total)
{
printf("Total %d\n", size);
}
char buffer[10];
char s[100];
for (int i = 0; i < size; i++)
{
char *uname = get_user_name_from_uid(stats[i].uid);
char *gname = get_group_name_from_gid(stats[i].gid);
format_size(stats[i].size, buffer, sizeof(buffer));
struct tm *p = localtime(&stats[i].last_modified);
strftime(s, sizeof s, "%a %b %d %Y %H:%M", p);
printf("%-6s %-8s %-8s %-10s %-25s", stats[i].permissions, uname, gname, buffer, s);
print_name(&stats[i], should_print_color);
printf("\n");
}
}
int get_dir_item_count(char *path)
{
DIR *d;
struct dirent *dir;
d = opendir(path);
int i = 0;
if (d)
{
while ((dir = readdir(d)) != NULL)
{
i++;
}
closedir(d);
}
return i;
}
int command_ls(Cli_args *args, Arena *arena)
{
char *path = args->path;
bool sort_by_size = args->sort_by_size;
bool sort_by_name = args->sort_by_name;
struct stat sb;
if (lstat(path, &sb) == -1)
{
perror("stat");
return EXIT_FAILURE;
}
// handle case when ls is called on not-a-dir
if ((sb.st_mode & S_IFMT) != S_IFDIR)
{
FileStats *stat_list = (FileStats *)allocate(arena, sizeof(FileStats));
stat_list->gid = sb.st_gid;
stat_list->uid = sb.st_uid;
stat_list->size = sb.st_size;
stat_list->inode = sb.st_ino;
stat_list->mode = sb.st_mode;
stat_list->name = path;
stat_list->permissions = get_user_permissions(sb.st_mode, arena);
print_stats(stat_list, 1);
return EXIT_SUCCESS;
}
int i = 0;
int num_items = get_dir_item_count(path);
FileStats *stat_list = (FileStats *)allocate(arena, num_items * sizeof(FileStats));
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
stat_list[i].name = duplicate_string(dir->d_name, arena);
stat_list[i].inode = dir->d_ino;
char *full_path = join_paths(path, dir->d_name, arena);
if (lstat(full_path, &sb) == -1)
{
perror("stat");
return EXIT_FAILURE;
}
stat_list[i].permissions = get_user_permissions(sb.st_mode, arena);
stat_list[i].uid = sb.st_uid;
stat_list[i].gid = sb.st_gid;
stat_list[i].mode = sb.st_mode;
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
stat_list[i].last_modified = sb.st_mtimespec.tv_sec;
#else
stat_list[i].last_modified = sb.st_mtime;
#endif
if ((sb.st_mode & S_IFMT) == S_IFDIR && !check_if_parent_dir(dir->d_name))
{
unsigned long long s = get_dir_size(full_path, args->depth);
stat_list[i].size = s;
}
else
{
stat_list[i].size = sb.st_size;
}
i++;
}
closedir(d);
}
if (sort_by_size)
{
quick_sort(stat_list, sizeof(FileStats), 0, num_items - 1, cmp_file_size);
}
else if (sort_by_name)
{
quick_sort(stat_list, sizeof(FileStats), 0, num_items - 1, cmp_file_name);
}
else
{
quick_sort(stat_list, sizeof(FileStats), 0, num_items - 1, cmp_file_date);
}
print_stats(stat_list, num_items);
return EXIT_SUCCESS;
}