-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
328 lines (308 loc) · 9.27 KB
/
ls.c
File metadata and controls
328 lines (308 loc) · 9.27 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "ls.h"
#include "headers.h"
void print_file_data(char *file_name, char *file_path)
{
struct stat file_stats;
int stat_result = stat(file_path, &file_stats);
if (stat_result != 0)
{
char err_buf[1000];
sprintf(err_buf, "Error getting stats of %s", file_name);
perror(err_buf);
return;
}
char ch;
if (S_ISDIR(file_stats.st_mode))
ch = 'd';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IRUSR)
ch = 'r';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IWUSR)
ch = 'w';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IXUSR)
ch = 'x';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IRGRP)
ch = 'r';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IWGRP)
ch = 'w';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IXGRP)
ch = 'x';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IROTH)
ch = 'r';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IWOTH)
ch = 'w';
else
ch = '-';
printf("%c", ch);
if (file_stats.st_mode & S_IXOTH)
ch = 'x';
else
ch = '-';
printf("%c", ch);
printf(" ");
printf(" %3d", (int)file_stats.st_nlink);
struct passwd *pw = getpwuid(file_stats.st_uid);
if (pw == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting owner of %s\n", file_name);
perror(err_buf);
return;
}
printf(" %10s", pw->pw_name);
struct group *grp = getgrgid(file_stats.st_gid);
if (grp == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting groupid of %s\n", file_name);
perror(err_buf);
return;
}
printf(" %10s", grp->gr_name);
printf(" %10d", (int)file_stats.st_size);
struct tm *file_time = localtime(&file_stats.st_mtim.tv_sec);
if (file_time == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting time of %s\n", file_name);
perror(err_buf);
return;
}
int file_date = file_time->tm_mday;
int file_month = file_time->tm_mon;
int file_year = 1900 + file_time->tm_year;
int file_hour = file_time->tm_hour;
int file_min = file_time->tm_min;
int file_sec = file_time->tm_sec;
time_t raw_curr_time;
time(&raw_curr_time);
struct tm *curr_time_info = localtime(&raw_curr_time);
if (curr_time_info == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting current timr");
perror(err_buf);
return;
}
char time_array[100];
char month_array[12][20] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
if (1900 + curr_time_info->tm_year != file_year)
{
file_time = localtime(&file_stats.st_mtim.tv_sec);
if (file_time == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting time of %s\t", file_name);
perror(err_buf);
return;
}
strftime(time_array, sizeof(time_array), " %b %e %Y", file_time);
printf("%s", time_array);
}
else if (curr_time_info->tm_mon - file_month > 6)
{
file_time = localtime(&file_stats.st_mtim.tv_sec);
if (file_time == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting time of %s\t", file_name);
perror(err_buf);
return;
}
strftime(time_array, sizeof(time_array), " %b %e %Y", file_time);
printf("%s", time_array);
}
else if ((curr_time_info->tm_mon - file_month == 6) && (curr_time_info->tm_mday > file_date))
{
file_time = localtime(&file_stats.st_mtim.tv_sec);
if (file_time == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting time of %s\t", file_name);
perror(err_buf);
return;
}
strftime(time_array, sizeof(time_array), " %b %e %Y", file_time);
printf("%s", time_array);
}
else
{
file_time = localtime(&file_stats.st_mtim.tv_sec);
if (file_time == NULL)
{
char err_buf[1000];
sprintf(err_buf, "\rError getting time name of %s\t", file_name);
perror(err_buf);
return;
}
strftime(time_array, sizeof(time_array), " %b %e %H:%M", file_time);
printf("%s", time_array);
}
printf(" %s", file_name);
printf("\n");
}
void ls(char *home_dir, bool a_flag, bool l_flag, char **argument_list, int arg_length)
{
char *argument;
bool dir_newline_flag = false;
for (int y = 0; y < arg_length; y++)
{
bool free_flag = false;
argument = argument_list[y];
if (strlen(argument) == 0)
argument[0] = '.';
if (argument[0] == '~')
{
char *new_argument = calloc(1000, sizeof(char));
strcpy(new_argument, home_dir);
strcat(new_argument, argument + 1);
argument = new_argument;
free_flag = true;
}
//Testing if a file or directory
struct stat stats;
int stat_return = stat(argument, &stats);
if (stat_return != 0)
{
perror("Cannot open directory or file");
continue;
}
//Code for a directory
if (S_ISDIR(stats.st_mode))
{
if (arg_length > 1)
{
if (y > 0)
printf("\n");
dir_newline_flag = true;
printf("%s:\n", argument);
}
struct dirent *next_file;
DIR *dire = opendir(argument);
if (dire == NULL)
{
perror("Couldnt open directory");
}
char *file_name = calloc(1000, sizeof(char));
if (l_flag)
{
int total_siz = 0;
next_file = readdir(dire);
while ((next_file != NULL))
{
strcpy(file_name, next_file->d_name);
if (!a_flag && file_name[0] == '.')
{
next_file = readdir(dire);
continue;
}
if (!l_flag)
{
next_file = readdir(dire);
continue;
}
char file_path[10000];
strcpy(file_path, argument);
strcat(file_path, "/");
strcat(file_path, file_name);
struct stat file_stats;
int stat_result = stat(file_path, &file_stats);
if (stat_result == -1)
{
char err_buf[1000];
sprintf(err_buf, "Error reading %s", file_name);
perror(err_buf);
return;
}
total_siz += file_stats.st_blocks;
next_file = readdir(dire);
}
printf("total %d\n", total_siz / 2);
}
if (closedir(dire) < 0)
{
perror("Error closing directory");
return;
}
dire = opendir(argument);
if (dire == NULL)
{
perror("Couldnt open directory");
}
next_file = readdir(dire);
while ((next_file != NULL))
{
strcpy(file_name, next_file->d_name);
if (!a_flag && file_name[0] == '.')
{
next_file = readdir(dire);
continue;
}
if (!l_flag)
{
printf("%s\n", next_file->d_name);
next_file = readdir(dire);
continue;
}
char file_path[10000];
strcpy(file_path, argument);
strcat(file_path, "/");
strcat(file_path, file_name);
print_file_data(file_name, file_path);
next_file = readdir(dire);
}
if (free_flag)
free(argument);
free(file_name);
if (closedir(dire) < 0)
{
perror("Error closing directory");
return;
}
continue;
}
//Code for a file
if (dir_newline_flag)
{
printf("\n");
}
int length = strlen(argument);
int break_index = 0;
for (int x = 0; x < length; x++)
if (argument[x] == '/')
break_index = x + 1;
char *file_name = (char *)calloc(1000, sizeof(char));
strcpy(file_name, argument + break_index);
if (l_flag)
print_file_data(argument, argument);
else
printf("%s\n", argument);
free(file_name);
if (free_flag)
free(argument);
continue;
}
}