-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
167 lines (163 loc) · 5.31 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
#include "header.h"
void long_ls(char *file, int flag)
{
DIR* dir = opendir(file);
struct dirent* c;
if(dir) {
struct stat name;
if (stat(file, &name) == -1) {
perror("long_ls");
}
else {
while((c = readdir(dir)) != NULL) {
struct stat p;
if(!stat(c->d_name, &p)) {
if (flag == 1)
{
// struct stat name;
// if (stat(file, &name) == -1) {
// perror("long_ls");
// }
printf((p.st_mode & S_IFDIR)? "d":"-");
printf((p.st_mode & S_IRUSR)? "r":"-");
printf((p.st_mode & S_IWUSR)? "w":"-");
printf((p.st_mode & S_IXUSR)? "x":"-");
printf((p.st_mode & S_IRGRP)? "r":"-");
printf((p.st_mode & S_IWGRP)? "w":"-");
printf((p.st_mode & S_IXGRP)? "x":"-");
printf((p.st_mode & S_IROTH)? "r":"-");
printf((p.st_mode & S_IWOTH)? "w":"-");
printf((p.st_mode & S_IXOTH)? "x":"-");
printf("\t");
printf("%ld", p.st_size);
printf("\t");
printf("%s\t", ctime(&p.st_atime));
printf("%s\n", c->d_name);
}
else {
if(c->d_name[0] != '.')
{
// struct stat name;
// if (stat(file, &name) == -1) {
// perror("long_ls");
// }
printf((p.st_mode & S_IFDIR)? "d":"-");
printf((p.st_mode & S_IRUSR)? "r":"-");
printf((p.st_mode & S_IWUSR)? "w":"-");
printf((p.st_mode & S_IXUSR)? "x":"-");
printf((p.st_mode & S_IRGRP)? "r":"-");
printf((p.st_mode & S_IWGRP)? "w":"-");
printf((p.st_mode & S_IXGRP)? "x":"-");
printf((p.st_mode & S_IROTH)? "r":"-");
printf((p.st_mode & S_IWOTH)? "w":"-");
printf((p.st_mode & S_IXOTH)? "x":"-");
printf("\t");
printf("%ld", p.st_size);
printf("\t");
printf("%s\t", ctime(&p.st_atime));
printf("%s\n", c->d_name);
}
}
}
}
}
}
else {
perror("directory not found");
}
}
void norm_ls(char *file, int flag)
{
DIR* dir = opendir(file);
struct dirent* c;
if(dir) {
while((c = readdir(dir)) != NULL) {
if (flag == 1) printf("%s\n",c->d_name);
else {
if(c->d_name[0] != '.')
printf("%s\n",c->d_name);
}
}
}
else {
perror("directory not found");
}
}
void ls (char *line)
{
char *token = line;
int hidden = 0, ll = 0, dir = 0, dir2 = 0, i=0, j=0;
char **n_dir = malloc(sizeof(char *) * 20000);
n_dir[i] = CWD;
// hidden = 1 -> display hidden
// all = 1 -> use long display
// dir = 0 -> pwd, dir = -1 -> dirname(pwd), dir = 1 -> home, dir = 2 -> accept directory name
token = strtok(NULL, " \"\n\t\r");
if (token == NULL)
{
hidden = 0; ll = 0; dir = 0;
}
else
{
while (token != NULL)
{
// if (strcmp(token, ".") == 0) {
// dir = 0;
// n_dir[i++] = CWD;
// }
// else if (strcmp(token, "..") == 0) {
// dir = -1;
// n_dir[i++] = dirname(CWD);
// }
if (strcmp(token, "~") == 0) {
dir = 1;
n_dir[i++] = HOME;
}
else if (strcmp(token, "-l") == 0) {
ll = 1;
}
else if (strcmp(token, "-a") == 0) {
hidden = 1;
}
else if((strcmp(token, "-la") == 0) || strcmp(token, "-al") == 0) {
ll = 1; hidden =1;
}
else {
dir2 = 2;
n_dir[i++] = token;
// strcpy(n_dir, token);
// printf("%s :\n", n_dir);
// if (ll == 1) {
// long_ls(n_dir, hidden);
// }
// else {
// norm_ls(n_dir, hidden);
// }
}
token = strtok(NULL, " \"\n\t\r");
}
}
if(i==0){
printf("%s :\n", n_dir[i]);
if (ll == 1) {
long_ls(n_dir[i], hidden);
}
else {
norm_ls(n_dir[i], hidden);
}
printf("\n");
}
else {
while(j<i) {
printf("%s :\n", n_dir[j]);
if (ll == 1) {
long_ls(n_dir[j], hidden);
}
else {
norm_ls(n_dir[j], hidden);
}
printf("\n");
j++;
}
}
}