-
Notifications
You must be signed in to change notification settings - Fork 0
/
fm.c
320 lines (294 loc) · 6.75 KB
/
fm.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
/* see LICENSE file for copyright and license details. */
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <limits.h>
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <regex.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ncurses.h>
#include <lib0.h>
enum { NONE = 0, QUIT = 1, LOAD = 2, DRAW = 4, DOTS = 8 };
union arg {
int i;
bool b;
const void *v;
};
struct assoc {
char *re;
char *bin;
};
struct entry {
char name[NAME_MAX];
char perms[11];
char size[10];
char owner[10];
char group[10];
bool isdir;
bool ismarked;
};
struct key {
const char *key;
void (*fn)(const union arg *);
const union arg arg;
};
static int cmp(const void *, const void *);
static void curse(void);
static void del(const union arg *);
static void dot(const union arg *);
static void draw(void);
static void input(void);
static void load(void);
static void mark(const union arg *);
static void nav(const union arg *);
static void quit(const union arg *);
static void run(void);
static void setup(void);
static void spawn(const char *[]);
static void step(const union arg *);
static void touch(const union arg *);
static void with(const union arg *);
static char cwd[PATH_MAX], msg[BUFSIZ];
static int cur, nents;
static unsigned int flags = LOAD;
static struct entry *ents;
#include "config.h"
int cmp(const void *a, const void *b) {
int r;
if ((r = ((struct entry *)b)->isdir - ((struct entry *)a)->isdir))
return r;
return strcoll(((struct entry *)a)->name, ((struct entry *)b)->name);
}
void curse(void) {
endwin();
cbreak();
noecho();
keypad(stdscr, 1);
curs_set(0);
refresh();
flags |= DRAW;
}
void del(const union arg *arg) {
#define DELETE(NAME) \
spawn((const char *[]){"rm", "-rf", (NAME), NULL});
if (!nents)
return;
strcpy(msg, "delete? (Y/n)");
draw();
flags |= DRAW;
if (getch() != 'Y')
return;
flags |= LOAD;
if (!arg->b) {
DELETE(ents[cur].name);
return;
}
for (int i = 0; i < nents; i++)
if (ents[i].ismarked)
DELETE(ents[i].name);
}
void dot(const union arg *arg) {
USED(arg), flags = (flags ^ DOTS) | LOAD;
}
void draw(void) {
int i, beg, end, nl, max;
struct entry *p;
erase();
attr_on(A_BOLD, NULL);
printw("%s\n", cwd);
attr_off(A_BOLD, NULL);
nl = MIN(LINES - 4, nents);
if (cur < nl / 2) {
beg = 0;
end = nl;
} else if (cur >= nents - nl / 2) {
beg = nents - nl;
end = nents;
} else {
beg = cur - nl / 2;
end = cur + nl / 2 + (nl & 1);
}
for (max = 0, i = 0; i < nents; i++)
max = MAX(max, strlen(ents[i].name));
max = CAP(max, 0, COLS);
for (i = beg; i < end; i++) {
if (i == cur)
attr_on(A_REVERSE, NULL);
if ((p = &ents[i])->isdir)
attr_on(A_BOLD, NULL);
printw("%c %s%*s \n", p->ismarked ? '*' : ' ',
p->name, max - strlen(p->name), "");
attr_off(A_BOLD | A_REVERSE, NULL);
}
if (msg[0] != '\0') {
printw("%s\n", msg);
}
else if (nents) {
p = &ents[cur];
printw("%s:%s %s %s\n", p->owner, p->group, p->size, p->perms);
}
refresh();
flags &= ~DRAW;
}
void input(void) {
int c, i;
if ((c = getch()) == KEY_RESIZE)
curse();
for (i = 0; i < (int)LEN(keys); i++)
if (!strcmp(keys[i].key, keyname(c)))
keys[i].fn(&(keys[i].arg));
}
void load(void) {
static char units[] = {'B','K','M','G','T'};
struct passwd *pw;
struct group *gr;
float sz;
regex_t re;
DIR *d;
struct dirent *dp;
struct stat sb;
int i;
free(ents);
cur = 0, nents = 0, ents = NULL;
getcwd(cwd, PATH_MAX);
regcomp(&re, flags & DOTS ? "." : "^[^.]",
REG_NOSUB|REG_EXTENDED|REG_ICASE);
d = opendir(cwd);
while ((dp = readdir(d))) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
if (regexec(&re, dp->d_name, 0, NULL, 0))
continue;
stat(dp->d_name, &sb);
ents = realloc(ents, (nents + 1) * sizeof(struct entry));
strcpy(ents[nents].name, dp->d_name);
ents[nents].isdir = S_ISDIR(sb.st_mode);
ents[nents].ismarked = 0;
pw = getpwuid(sb.st_uid);
strcpy(ents[nents].owner, pw->pw_name);
gr = getgrgid(sb.st_gid);
strcpy(ents[nents].group, gr->gr_name);
for (sz = (float)sb.st_size, i = 0; sz > 1024 && i < 4; i++)
sz /= 1024;
sprintf(ents[nents].size, "%.3g%c", sz, units[i]);
ents[nents].perms[0] = S_ISDIR(sb.st_mode) ? 'd' : '-';
ents[nents].perms[1] = (sb.st_mode & S_IRUSR) ? 'r' : '-';
ents[nents].perms[2] = (sb.st_mode & S_IWUSR) ? 'w' : '-';
ents[nents].perms[3] = (sb.st_mode & S_IXUSR) ? 'x' : '-';
ents[nents].perms[4] = (sb.st_mode & S_IRGRP) ? 'r' : '-';
ents[nents].perms[5] = (sb.st_mode & S_IWGRP) ? 'w' : '-';
ents[nents].perms[6] = (sb.st_mode & S_IXGRP) ? 'x' : '-';
ents[nents].perms[7] = (sb.st_mode & S_IROTH) ? 'r' : '-';
ents[nents].perms[8] = (sb.st_mode & S_IWOTH) ? 'w' : '-';
ents[nents].perms[9] = (sb.st_mode & S_IXOTH) ? 'x' : '-';
ents[nents].perms[10] = '\0';
nents++;
}
closedir(d);
qsort(ents, nents, sizeof(struct entry), cmp);
flags = (flags & ~LOAD) | DRAW;
}
void quit(const union arg *arg) {
USED(arg), flags |= QUIT;
}
void mark(const union arg *arg) {
if (!nents)
return;
ents[cur].ismarked = !ents[cur].ismarked;
flags |= DRAW;
USED(arg);
}
void nav(const union arg *arg) {
chdir(arg->i == 0 ? "." : arg->i < 0 ? ".." : ents[cur].name);
flags |= LOAD;
}
void run(void) {
while (!(flags & QUIT)) {
msg[0] = '\0';
if (flags & LOAD)
load();
if (flags & DRAW)
draw();
input();
}
}
void setup(void) {
setlocale(LC_ALL, "");
atexit((void (*)(void))endwin);
initscr();
curse();
}
void spawn(const char *arg[]) {
pid_t pid;
int r;
pid = fork();
if (pid > 0) {
endwin();
waitpid(pid, &r, 0);
curse();
} else if (pid == 0) {
execvp((const char *)arg[0], (char * const *)arg);
die("execvp %s failed\n", arg[0]);
}
}
void step(const union arg *arg) {
cur = CAP(cur + arg->i, 0, nents - 1), flags |= DRAW;
}
void touch(const union arg *arg) {
char buf[100] = {0}, *s = buf;
int c;
do {
sprintf(msg, "NAME: %s", buf);
draw();
c = getch();
if (c == '\n') {
goto out;
}
else if (c == KEY_BACKSPACE) {
if (s > buf)
*--s = 0;
}
else if (isprint(c)) {
if (s - buf < (ptrdiff_t)sizeof buf - 1)
*s++ = c, *s = 0;
}
else {
goto out;
}
} while (1);
out:
flags |= DRAW;
if (buf[0] == '\0')
return;
spawn((const char*[]) {arg->b ? "mkdir" : "touch", buf, NULL});
flags |= LOAD;
}
void with(const union arg *arg) {
char *bin, cmd[PATH_MAX];
regex_t re;
size_t i;
for (bin = NULL, i = 0; i < LEN(assocs); i++) {
if (regcomp(&re, assocs[i].re,
REG_NOSUB|REG_EXTENDED|REG_ICASE))
continue;
if (!regexec(&re, ents[cur].name, 0, NULL, 0)) {
bin = assocs[i].bin;
break;
}
}
snprintf(cmd, PATH_MAX, "%s \"%s\"", bin, ents[cur].name);
spawn((const char*[]){"sh", "-c", cmd, NULL});
USED(arg);
}
int main(void) {
setup();
run();
return 0;
}