-
Notifications
You must be signed in to change notification settings - Fork 3
/
ls.c
324 lines (249 loc) · 8.5 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
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
#include "main.h"
bool listAll = false, listLong = false, dirPresent = false;
void printTotal(DIR *d, struct dirent *dir, char path[]) {
long long total = 0;
while ((dir = readdir(d)) != NULL)
{
char myfile[2048];
strcpy(myfile, path);
strcat(myfile, "/");
strcat(myfile, dir->d_name);
// If there is no '.' then don't consider in the total.
if (dir->d_name[0] == '.' && !listAll) {
continue;
}
struct stat direc = {0};
if (stat(myfile, &direc) == 0)
total += direc.st_blocks;
}
printf("total %lld\n", total / 2);
rewinddir(d);
return;
}
//extract the file's type and permissions from the file's stats and formats the string to encode it
void getPermissions(struct stat sb, char* permissionString){
//determine the filetype and add the corresponding character
permissionString[0] = '-';
if(S_ISDIR(sb.st_mode)){
permissionString[0] = 'd';
}
else if(S_ISCHR(sb.st_mode)){
permissionString[0] = 'r';
}
else if(S_ISBLK(sb.st_mode)){
permissionString[0] = 'b';
}
else if(S_ISFIFO(sb.st_mode)){
permissionString[0] = 'p';
}
else if(S_ISLNK(sb.st_mode)){
permissionString[0] = 'l';
}
else if(S_ISSOCK(sb.st_mode)){
permissionString[0] = 's';
}
//check each permission and set the appropriate character
permissionString[1] = (sb.st_mode & S_IRUSR) == S_IRUSR ? 'r': '-';
permissionString[2] = (sb.st_mode & S_IWUSR) == S_IWUSR ? 'w': '-';
permissionString[3] = (sb.st_mode & S_IXUSR) == S_IXUSR ? 'x': '-';
permissionString[4] = (sb.st_mode & S_IRGRP) == S_IRGRP ? 'r': '-';
permissionString[5] = (sb.st_mode & S_IWGRP) == S_IWGRP ? 'w': '-';
permissionString[6] = (sb.st_mode & S_IXGRP) == S_IXGRP ? 'x': '-';
permissionString[7] = (sb.st_mode & S_IROTH) == S_IROTH ? 'r': '-';
permissionString[8] = (sb.st_mode & S_IWOTH) == S_IWOTH ? 'w': '-';
permissionString[9] = (sb.st_mode & S_IXOTH) == S_IXOTH ? 'x': '-';
permissionString[10] = '\0';
return;
}
//list a file's info (Print the filename if listLong is false)
void listFile(char* dirname, char* filename, bool listLong){
struct stat sb;
char fullPath[2 * MAX_STR_LENGTH];
// Stores the permissions
char permissionString[11];
// Stores the Username and the groupnames
char username[MAX_STR_LENGTH], groupname[MAX_STR_LENGTH];
// Stores the time when the file was created
char timestring[MAX_TIME_LENGTH];
// check if the -l option was used, show full paths
if(listLong == true){
strcpy(fullPath, dirname);
strcat(fullPath, "/");
strcat(fullPath, filename);
if(stat(fullPath, &sb) == -1){
perror(filename);
return;
}
getPermissions(sb, permissionString);
getUserName(sb, username);
getGroupName(sb, groupname);
getModTime(sb, timestring);
// 10i -> To give a padding of 10 from left
printf("%s %i %s %s %10i %s %s\n", permissionString, (int)sb.st_nlink, username, groupname, (int)sb.st_size, timestring, filename);
}
else{
printf("%s\n",filename);
}
}
//extracts the last modified time from the file stats
void getModTime(struct stat sb, char* timestring){
struct tm *timestamp;
struct tm *currentTime;
time_t currentTimeRaw;
int currentYear, fileYear;
//get the current year
time(¤tTimeRaw);
currentTime = localtime(¤tTimeRaw);
currentYear = (int)currentTime->tm_year;
timestamp = localtime((time_t*)&sb.st_mtim); //time the file was last modified
fileYear = (int)timestamp->tm_year; //year the file was last modified
//if the file was last modified during the current year, show the time, else show the year
if(currentYear == fileYear){
strftime(timestring, MAX_TIME_LENGTH, "%b %d %R", timestamp);
}
else{
strftime(timestring, MAX_TIME_LENGTH, "%b %d %Y", timestamp);
}
}
//returns the group name of the file's owner from the stats
void getGroupName(struct stat sb, char* groupname){
struct group *groupInfo;
groupInfo = getgrgid(sb.st_gid);
strncpy(groupname, groupInfo->gr_name, MAX_STR_LENGTH);
}
//returns the username of the file's owner from the file's stats
void getUserName(struct stat sb, char* username){
struct passwd *userInfo;
userInfo = getpwuid(sb.st_uid);
strncpy(username, userInfo->pw_name, MAX_STR_LENGTH);
}
// Returns the count of '-' in the command arguments.
int getHiphenCount(long long int argc, char* argv[]) {
int hiphenCount=0;
//check the number of option arguments (starting with '-')
for(int i = 1; i < argc; i++){
if(argv[i][0] == '-'){
hiphenCount++;
}
}
return hiphenCount;
}
void updateLsBools(long long int argc, char* argv[]) {
listAll = false, listLong = false, dirPresent = false;
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-l"))
listLong = 1;
else if (!strcmp(argv[i], "-a"))
listAll = 1;
else if (!strcmp(argv[i], "-al") || !strcmp(argv[i], "-la"))
{
listLong = 1;
listAll = 1;
}
else if (strcmp(argv[i], ""))
dirPresent = true;
}
}
//reads a directory to read each file it contains
void listDir(char* dirname, bool listAll, bool listLong){
struct dirent **dirEntry;
DIR* dirp;
char* filename;
ll x = 0, count = scandir(dirname, &dirEntry, NULL, alphasort);
if (count < 0)
perror("Error: no such file or directory exists\n");
else {
// Print Total only in case of (ls -l) OR (ls -al) OR (ls -la)
if (listLong) {
struct dirent *dir;
dirp = opendir(dirname);
printTotal(dirp, dir, dirname);
closedir(dirp);
}
while (x < count)
{
filename = dirEntry[x] -> d_name;
if (filename[0] == '.')
{
if(listAll){
listFile(dirname, filename, listLong);
}
x++;
continue;
}
else
{
listFile(dirname, filename, listLong);
x++;
}
}
}
return;
}
void ls(long long int argc, char* argv[]){
// Stores information about the files
struct stat sb;
// Stores the count of '-'
int hiphenCount = getHiphenCount(argc, argv);
char filename[MAX_STR_LENGTH];
// This function update all the bools of the ls function
updateLsBools(argc, argv);
//if there are no arguments other than '-,
// then list what's in the current directory, i.e '.'
if(argc - hiphenCount == 1){
listDir(".", listAll, listLong);
}
// If the last character ends at '~', then call ls on the pseudoHome.
else{
if (strcmp(argv[argc - 1], "~") == 0) {
strcpy(filename, pseudoHome);
// Check if the file exists or not.
if(stat(filename, &sb) == -1) {
perror(filename);
}
else{
if((sb.st_mode & S_IFMT) == S_IFDIR){
if(argc - hiphenCount > 2){
printf("%s:\n", filename);
}
listDir(filename, listAll, listLong);
printf("\n");
}
// Else print the filename
else{
listFile(".", filename, listLong);
}
}
}
else {
//go through each command line argument
for(int i = 1; i < argc; i++){
strncpy(filename, argv[i], MAX_STR_LENGTH);
//exclude option arguments
if(filename[0] == '-') {
continue;
}
//Throw error if filename is invalid
if(stat(filename, &sb) == -1) {
perror(filename);
}
else{
//if the file is a directory then read it's contents and list the files inside
//otherwise just list the file
if((sb.st_mode & S_IFMT) == S_IFDIR){
if(argc - hiphenCount > 2){
printf("%s:\n", filename);
}
listDir(filename, listAll, listLong);
printf("\n");
}
// Else print the filename
else{
listFile(".", filename, listLong);
}
}
}
}
}
}