-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
234 lines (192 loc) · 5.82 KB
/
ls.c
File metadata and controls
234 lines (192 loc) · 5.82 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/sysmacros.h> // <-- ДЛЯ major() minor()
/* ---------- ТИП ФАЙЛА (первая буква в ls -l) ---------- */
void display_file_type(mode_t mode)
{
switch (mode & S_IFMT) {
case S_IFDIR: putchar('d'); break;
case S_IFCHR: putchar('c'); break;
case S_IFBLK: putchar('b'); break;
case S_IFREG: putchar('-'); break;
case S_IFLNK: putchar('l'); break;
case S_IFSOCK: putchar('s'); break;
default: putchar('?'); break;
}
}
/* ---------- ПРАВА rwxrwxrwx + спец-биты ---------- */
void display_permissions(mode_t mode)
{
const char *tbl = "rwxrwxrwx";
char buf[10];
int i;
for (i = 0; i < 9; i++) {
mode_t bit = 1 << (8 - i);
buf[i] = (mode & bit) ? tbl[i] : '-';
}
if (mode & S_ISUID) buf[2] = 's';
if (mode & S_ISGID) buf[5] = 's';
if (mode & S_ISVTX) buf[8] = 't';
buf[9] = '\0';
printf("%s ", buf);
}
/* ---------- Вывод одной строки в формате ls -l ---------- */
void print_long(const char *fullpath,
const char *name,
const struct stat *st,
int flag_g)
{
struct passwd *pw = getpwuid(st->st_uid);
struct group *gr = getgrgid(st->st_gid);
display_file_type(st->st_mode);
display_permissions(st->st_mode);
printf("%ld ", (long)st->st_nlink);
if (pw) printf("%s ", pw->pw_name);
else printf("%d ", st->st_uid);
if (flag_g) {
if (gr) printf("%s ", gr->gr_name);
else printf("%d ", st->st_gid);
}
/* устройства: печатаем major/minor */
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
printf("%u, %u ", major(st->st_rdev), minor(st->st_rdev));
} else {
printf("%ld ", (long)st->st_size);
}
printf("%s", name);
/* вывод цели символической ссылки */
if (S_ISLNK(st->st_mode)) {
char buf[PATH_MAX];
ssize_t n = readlink(fullpath, buf, sizeof(buf)-1);
if (n >= 0) {
buf[n] = '\0';
printf(" -> %s", buf);
}
}
putchar('\n');
}
/* ---------- Вывод обычного файла ---------- */
void print_entry(const char *fullpath,
const char *name,
int flag_l,
int flag_g)
{
if (!flag_l) {
puts(name);
return;
}
struct stat st;
if (lstat(fullpath, &st) == -1) {
perror(fullpath);
return;
}
print_long(fullpath, name, &st, flag_g);
}
/* ---------- ПРОТОТИП РЕКУРСИИ ---------- */
void list_dir(const char *path, int flag_l, int flag_R, int flag_g);
/* ---------- Определить: файл или каталог ---------- */
void list_path(const char *path,
int flag_l,
int flag_R,
int flag_g)
{
struct stat st;
if (lstat(path, &st) == -1) {
perror(path);
return;
}
if (S_ISDIR(st.st_mode))
list_dir(path, flag_l, flag_R, flag_g);
else
print_entry(path, path, flag_l, flag_g);
}
/* ---------- Обход каталога ---------- */
void list_dir(const char *path,
int flag_l,
int flag_R,
int flag_g)
{
DIR *dir = opendir(path);
if (!dir) {
perror(path);
return;
}
printf("%s:\n", path);
struct dirent *de;
/* --- ПЕРВЫЙ ПРОХОД: ПЕЧАТЬ --- */
while ((de = readdir(dir)) != NULL) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
char full[PATH_MAX];
snprintf(full, sizeof(full), "%s/%s", path, de->d_name);
print_entry(full, de->d_name, flag_l, flag_g);
}
closedir(dir);
/* --- РЕКУРСИЯ ТОЛЬКО ЕСЛИ -R --- */
if (!flag_R)
return;
dir = opendir(path);
if (!dir) {
perror(path);
return;
}
/* --- ВТОРОЙ ПРОХОД: ЗАХОД В ПОДКАТАЛОГИ --- */
while ((de = readdir(dir)) != NULL) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
char full[PATH_MAX];
snprintf(full, sizeof(full), "%s/%s", path, de->d_name);
struct stat st;
if (lstat(full, &st) == -1)
continue;
if (S_ISDIR(st.st_mode)) {
putchar('\n');
list_dir(full, flag_l, flag_R, flag_g);
}
}
closedir(dir);
}
/* ---------- main ---------- */
int main(int argc, char *argv[])
{
int flag_l = 0;
int flag_R = 0;
int flag_g = 0;
int i = 1;
/* разбор флагов: -l, -R, -g */
for (; i < argc; ++i) {
if (argv[i][0] != '-' || argv[i][1] == '\0')
break;
for (int j = 1; argv[i][j]; ++j) {
switch (argv[i][j]) {
case 'l': flag_l = 1; break;
case 'R': flag_R = 1; break;
case 'g': flag_g = 1; break;
default:
fprintf(stderr, "ls: unknown flag -%c\n", argv[i][j]);
exit(1);
}
}
}
/* если путей не было — ls текущего каталога "." */
if (i == argc) {
list_path(".", flag_l, flag_R, flag_g);
return 0;
}
/* иначе выводим по одному каждый путь */
for (; i < argc; ++i) {
list_path(argv[i], flag_l, flag_R, flag_g);
if (i + 1 < argc)
putchar('\n');
}
return 0;
}