Repository navigation
Expand file tree
/
Copy pathpeek.c
More file actions
244 lines (213 loc) · 5.9 KB
/
Copy pathpeek.c
File metadata and controls
244 lines (213 loc) · 5.9 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "headers.h"
#include "tools.h"
int compare(const void *a, const void *b)
{
return strcmp(*(const char **)a, *(const char **)b);
}
typedef struct
{
const char *directory;
int showHidden;
int showDetails;
} ListOptions;
void applyColor(struct stat file_stat)
{
if (S_ISDIR(file_stat.st_mode))
{
printf(BLUE_COLOR);
}
else if (file_stat.st_mode & S_IXUSR)
{
printf(GREEN_COLOR);
}
}
void listWithOptions(ListOptions options)
{
DIR *dir = opendir(options.directory);
if (dir == NULL)
{
printError("opendir error");
return;
}
struct dirent *entry;
size_t num_entries = 0;
while ((entry = readdir(dir)) != NULL)
{
if (options.showHidden || entry->d_name[0] != '.')
{
num_entries++;
}
}
rewinddir(dir);
char **entries = (char **)malloc(num_entries * sizeof(char *));
if (entries == NULL)
{
printError("memory allocation error");
closedir(dir);
return;
}
size_t idx = 0;
while ((entry = readdir(dir)) != NULL)
{
if (options.showHidden || entry->d_name[0] != '.')
{
entries[idx] = strdup(entry->d_name);
idx++;
}
}
qsort(entries, num_entries, sizeof(char *), compare);
for (size_t i = 0; i < num_entries; i++)
{
struct stat file_stat;
char filepath[DEF_SIZE];
snprintf(filepath, sizeof(filepath), "%s/%s", options.directory, entries[i]);
if (stat(filepath, &file_stat) == 0)
{
applyColor(file_stat);
}
printf("%s%s\n", entries[i], RESET_COLOR);
free(entries[i]);
}
free(entries);
closedir(dir);
}
void listDirectoryHidden(const char *directory)
{
ListOptions options = {directory, 1, 0};
listWithOptions(options);
}
void listDirectory(const char *directory)
{
ListOptions options = {directory, 0, 0};
listWithOptions(options);
}
void listDetailedDirectory(const char *directory)
{
DIR *dir = opendir(directory);
if (dir == NULL)
{
printError("opendir error");
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
char *filename = entry->d_name;
char filepath[DEF_SIZE];
snprintf(filepath, sizeof(filepath), "%s/%s", directory, filename);
struct stat file_stat;
if (stat(filepath, &file_stat) < 0)
{
printError("stat error");
closedir(dir);
return;
}
// File type and permissions
printf((file_stat.st_mode & S_IRUSR) ? "r" : "-");
printf((file_stat.st_mode & S_IWUSR) ? "w" : "-");
printf((file_stat.st_mode & S_IXUSR) ? "x" : "-");
printf((file_stat.st_mode & S_IRGRP) ? "r" : "-");
printf((file_stat.st_mode & S_IWGRP) ? "w" : "-");
printf((file_stat.st_mode & S_IXGRP) ? "x" : "-");
printf((file_stat.st_mode & S_IROTH) ? "r" : "-");
printf((file_stat.st_mode & S_IWOTH) ? "w" : "-");
printf((file_stat.st_mode & S_IXOTH) ? "x" : "-");
printf(RESET_COLOR); // Reset color
printf(" ");
// Other details
printf("%ld ", file_stat.st_nlink);
struct passwd *pw = getpwuid(file_stat.st_uid);
if (pw == NULL)
{
printError("getpwuid error");
closedir(dir);
return;
}
printf("%s ", pw->pw_name);
struct group *gr = getgrgid(file_stat.st_gid);
if (gr == NULL)
{
printError("getgrgid error");
closedir(dir);
return;
}
printf("%s ", gr->gr_name);
printf("%6ld ", file_stat.st_size);
// Format time
struct tm *timeinfo = localtime(&file_stat.st_mtime);
char time_buffer[80];
strftime(time_buffer, sizeof(time_buffer), "%b %d %H:%M", timeinfo);
printf("%s ", time_buffer);
// File name with color for directories and executables
if (S_ISDIR(file_stat.st_mode))
{
printf(BLUE_COLOR "%s" RESET_COLOR, filename);
}
else if (file_stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
{
printf(GREEN_COLOR "%s" RESET_COLOR, filename);
}
else
{
printf("%s", filename);
}
printf("\n");
}
if (closedir(dir) != 0)
{
printError("closedir error");
}
}
void peek(Command *cmd)
{
int showAll = 0;
int showDetails = 0;
char *directory = NULL;
for (int i = 1; i < cmd->argc; i++)
{
if (strcmp(cmd->argv[i], "-a") == 0)
{
showAll = 1;
}
else if (strcmp(cmd->argv[i], "-l") == 0 || strcmp(cmd->argv[i], "-la") == 0 || strcmp(cmd->argv[i], "-al") == 0)
{
showDetails = 1;
}
else
{
directory = cmd->argv[i];
}
}
if (directory == NULL)
{
// peek at current working directory
char current_directory[DEF_SIZE];
if (getcwd(current_directory, sizeof(current_directory)) == NULL)
{
printError("getcwd error");
return;
}
if (showDetails)
listDetailedDirectory(current_directory);
else if (showAll)
listDirectoryHidden(current_directory);
else
listDirectory(current_directory);
}
else
{
if (directory[0] == '~' && (directory[2] == '/' || directory[2] == '\0'))
{
// handling ~/dir case
char expanded_path[DEF_SIZE];
snprintf(expanded_path, sizeof(expanded_path), "%s%s", global_home, directory + 1);
directory = expanded_path;
}
if (showDetails)
listDetailedDirectory(directory);
else if (showAll)
listDirectoryHidden(directory);
else
listDirectory(directory);
}
}