-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
182 lines (146 loc) · 3.77 KB
/
Copy pathls.c
File metadata and controls
182 lines (146 loc) · 3.77 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
#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <grp.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
int show_all = 0;
int show_long = 0;
struct stat *get_file_stat(const char *dir, const char *name, struct stat *sp) {
char fullpath[4096];
snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, name);
if (lstat(fullpath, sp) < 0) {
perror("lstat");
return NULL;
}
return sp;
}
long get_dir_meta(const char *path, unsigned long *max_size_len,
unsigned long *total_size) {
DIR *dir = opendir(path);
if (!dir) {
perror("opendir");
return -1;
}
errno = 0;
long items = 0;
struct dirent *entry;
while ((entry = readdir(dir))) {
if (!show_all && entry->d_name[0] == '.')
continue;
struct stat sp;
if (!get_file_stat(path, entry->d_name, &sp))
continue;
unsigned long len = 1;
for (off_t s = sp.st_size; s >= 10; s /= 10)
len++;
*max_size_len = len > *max_size_len ? len : *max_size_len;
*total_size += sp.st_size;
items++;
}
if (errno != 0) {
closedir(dir);
perror("readdir");
return -1;
}
closedir(dir);
return items;
}
void mode_string(mode_t mode, char *str) {
if (S_ISDIR(mode))
str[0] = 'd';
else if (S_ISLNK(mode))
str[0] = 'l';
else if (S_ISBLK(mode))
str[0] = 'b';
else if (S_ISCHR(mode))
str[0] = 'c';
else if (S_ISFIFO(mode))
str[0] = 'p';
else if (S_ISSOCK(mode))
str[0] = 's';
else
str[0] = '-';
str[1] = (mode & S_IRUSR) ? 'r' : '-';
str[2] = (mode & S_IWUSR) ? 'w' : '-';
str[3] = (mode & S_IXUSR) ? 'x' : '-';
str[4] = (mode & S_IRGRP) ? 'r' : '-';
str[5] = (mode & S_IWGRP) ? 'w' : '-';
str[6] = (mode & S_IXGRP) ? 'x' : '-';
str[7] = (mode & S_IROTH) ? 'r' : '-';
str[8] = (mode & S_IWOTH) ? 'w' : '-';
str[9] = (mode & S_IXOTH) ? 'x' : '-';
str[10] = '\0';
}
void print_long(const char *dir, const char *name, const int max_size_len) {
struct stat sp;
if (!get_file_stat(dir, name, &sp))
return;
char modes[11];
mode_string(sp.st_mode, modes);
struct passwd *pwd = getpwuid(sp.st_uid);
struct group *grp = getgrgid(sp.st_gid);
const char *user = pwd ? pwd->pw_name : "???";
const char *group = grp ? grp->gr_name : "???";
time_t now_t = time(NULL);
struct tm now = *localtime(&now_t);
char timebuf[32];
struct tm tp = *localtime(&sp.st_mtim.tv_sec);
if (tp.tm_year == now.tm_year) {
strftime(timebuf, sizeof(timebuf), "%b %e %H:%M", &tp);
} else {
strftime(timebuf, sizeof(timebuf), "%b %e %Y", &tp);
}
printf("%s %jd %s %s %*ju %s %s\n", modes, (uintmax_t)sp.st_nlink, user,
group, max_size_len, (uintmax_t)sp.st_size, timebuf, name);
}
int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "al")) != -1) {
switch (opt) {
case 'a':
show_all = 1;
break;
case 'l':
show_long = 1;
break;
default:
fprintf(stderr, "Usage: %s [-al] [path]\n", argv[0]);
return 1;
}
}
const char *path = (optind < argc) ? argv[optind] : ".";
unsigned long max_size_len = 0, total_size = 0;
long items = 0;
if (show_long) {
if ((items = get_dir_meta(path, &max_size_len, &total_size)) == -1) {
return 1;
}
printf("items %lu total %lu\n", items, total_size);
}
DIR *dir = opendir(path);
if (!dir) {
perror("opendir");
return 1;
}
errno = 0;
struct dirent *entry;
while ((entry = readdir(dir))) {
if (!show_all && entry->d_name[0] == '.')
continue;
if (show_long)
print_long(path, entry->d_name, max_size_len);
else
printf("%s\n", entry->d_name);
}
if (errno != 0) {
perror("readdir");
return 1;
}
closedir(dir);
return 0;
}