-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_ls.c
More file actions
148 lines (117 loc) · 3.17 KB
/
command_ls.c
File metadata and controls
148 lines (117 loc) · 3.17 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
#include "shell.h"
#include "utils.h"
#include "command_ls.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void print_ls_perms(mode_t bits){
char file_perms[] = "----------";
if (S_ISDIR(bits))
file_perms[0] = 'd';
else if (S_ISLNK(bits))
file_perms[0] = 'l';
else if (S_ISBLK(bits))
file_perms[0] = 'b';
else if (S_ISCHR(bits))
file_perms[0] = 'c';
else if (S_ISSOCK(bits))
file_perms[0] = 's';
else if (S_ISFIFO(bits))
file_perms[0] = 'f';
else if (S_ISREG(bits))
file_perms[0] = '-';
else
file_perms[0] = '?';
if (bits & S_IRUSR)
file_perms[1] = 'r';
if (bits & S_IWUSR)
file_perms[2] = 'w';
if (bits & S_IXUSR)
file_perms[3] = 'x';
if (bits & S_IRGRP)
file_perms[4] = 'r';
if (bits & S_IWGRP)
file_perms[5] = 'w';
if (bits & S_IXGRP)
file_perms[6] = 'x';
if (bits & S_IROTH)
file_perms[7] = 'r';
if (bits & S_IWOTH)
file_perms[8] = 'w';
if (bits & S_IXOTH)
file_perms[9] = 'x';
printf("%s ", file_perms);
}
void ls(char* str){
int l = strlen(str);
int has_l = 0;
int has_a = 0;
char* path;
// strcpy(parsed_string, str);
// printf("blah: %s\n", str);
for(int i= 0; i < l; i++){
if((str[i] == '-') && (i > 0)){
str[i] = ' ';
if(str[i-1] == ' '){
do {
if(str[i] == 'a'){
has_a = 1;
str[i] = ' ';
}
else if(str[i] == 'l'){
has_l = 1;
str[i] = ' ';
}
i++;
} while((str[i] != ' ') && (i < l));
}
}
}
path = trim_str(str);
if(strlen(path) < 1)
path = ".";
if(!(strcmp(path, "~")))
strcpy(path, root_dir);
DIR *mydir = opendir(path);
struct dirent *myfile;
struct stat mystat;
long int file_size;
char* file_name;
struct passwd *pwd;
struct group *grp;
char* file_time;
while((myfile = readdir(mydir)) != NULL){
stat(myfile->d_name, &mystat);
pwd = getpwuid(mystat.st_uid);
grp = getgrgid(mystat.st_gid);
file_name = myfile->d_name;
if(!has_a)
if(file_name[0] == '.'){
continue;
}
if(!has_l){
printf(" %s\t", file_name);
continue;
}
print_ls_perms(mystat.st_mode);
printf("%3lu ", mystat.st_nlink);
if(pwd)
printf("%10s ", pwd->pw_name);
else
// printf("%10ld ", (long)mystat.st_uid);
printf(" shared");
if(grp)
printf("%10s ", grp->gr_name);
else
printf(" shared");
printf("%6ld ", mystat.st_size);
file_time = asctime(localtime(&(mystat.st_mtime)));
file_time[strlen(file_time) - 1] = 0;
printf(" %s ", file_time);
printf(" %s\n", file_name);
}
printf("\n");
}