2 c53df816 2023-02-08 op * fm.c was written by Omar Polo <op@omarpolo.com>, based on the work
3 c53df816 2023-02-08 op * of Marcel Rodrigues, and is placed in the public domain. The author
4 c53df816 2023-02-08 op * hereby disclaims copyright to this source code.
7 2b2f7365 2021-11-26 op /* needed for some ncurses stuff */
8 c9f42136 2021-11-25 op #define _XOPEN_SOURCE_EXTENDED
9 c9f42136 2021-11-25 op #define _FILE_OFFSET_BITS 64
11 8ada055d 2021-11-26 op #include <sys/stat.h>
12 8ada055d 2021-11-26 op #include <sys/types.h>
13 8ada055d 2021-11-26 op #include <sys/wait.h>
15 01a2cff6 2021-12-12 op #include <assert.h>
16 8ada055d 2021-11-26 op #include <ctype.h>
17 8ada055d 2021-11-26 op #include <curses.h>
18 8ada055d 2021-11-26 op #include <dirent.h>
19 cc9de337 2021-11-26 op #include <err.h>
20 8ada055d 2021-11-26 op #include <errno.h>
21 8ada055d 2021-11-26 op #include <fcntl.h>
22 cc9de337 2021-11-26 op #include <getopt.h>
23 8ada055d 2021-11-26 op #include <libgen.h>
24 8ada055d 2021-11-26 op #include <limits.h>
25 8ada055d 2021-11-26 op #include <locale.h>
26 8ada055d 2021-11-26 op #include <signal.h>
27 8ada055d 2021-11-26 op #include <stdarg.h>
28 c9f42136 2021-11-25 op #include <stdint.h>
29 8ada055d 2021-11-26 op #include <stdio.h>
30 8ada055d 2021-11-26 op #include <stdlib.h>
31 8ada055d 2021-11-26 op #include <string.h>
32 8ada055d 2021-11-26 op #include <unistd.h>
33 c9f42136 2021-11-25 op #include <wchar.h>
34 c9f42136 2021-11-25 op #include <wctype.h>
36 e03d1219 2021-12-13 op #ifndef FM_SHELL
37 e03d1219 2021-12-13 op #define FM_SHELL "/bin/sh"
40 63fe4ed0 2021-12-13 op #ifndef FM_PAGER
41 63fe4ed0 2021-12-13 op #define FM_PAGER "/usr/bin/less"
45 d244a655 2021-12-13 op #define FM_ED "/bin/ed"
48 2c5122bc 2021-12-13 op #ifndef FM_OPENER
49 2c5122bc 2021-12-13 op #define FM_OPENER "xdg-open"
52 c9f42136 2021-11-25 op #include "config.h"
54 cc9de337 2021-11-26 op struct option opts[] = {
55 cc9de337 2021-11-26 op {"help", no_argument, NULL, 'h'},
56 cc9de337 2021-11-26 op {"version", no_argument, NULL, 'v'},
57 cc9de337 2021-11-26 op {NULL, 0, NULL, 0}
60 01a2cff6 2021-12-12 op static char clipboard[PATH_MAX];
62 c9f42136 2021-11-25 op /* String buffers. */
63 c9f42136 2021-11-25 op #define BUFLEN PATH_MAX
64 c9f42136 2021-11-25 op static char BUF1[BUFLEN];
65 c9f42136 2021-11-25 op static char BUF2[BUFLEN];
66 c9f42136 2021-11-25 op static char INPUT[BUFLEN];
67 c9f42136 2021-11-25 op static wchar_t WBUF[BUFLEN];
69 c9f42136 2021-11-25 op /* Paths to external programs. */
70 c9f42136 2021-11-25 op static char *user_shell;
71 c9f42136 2021-11-25 op static char *user_pager;
72 c9f42136 2021-11-25 op static char *user_editor;
73 c9f42136 2021-11-25 op static char *user_open;
75 c9f42136 2021-11-25 op /* Listing view parameters. */
76 c9f42136 2021-11-25 op #define HEIGHT (LINES-4)
77 c9f42136 2021-11-25 op #define STATUSPOS (COLS-16)
79 c9f42136 2021-11-25 op /* Listing view flags. */
80 c9f42136 2021-11-25 op #define SHOW_FILES 0x01u
81 c9f42136 2021-11-25 op #define SHOW_DIRS 0x02u
82 c9f42136 2021-11-25 op #define SHOW_HIDDEN 0x04u
84 c9f42136 2021-11-25 op /* Marks parameters. */
85 c9f42136 2021-11-25 op #define BULK_INIT 5
86 c9f42136 2021-11-25 op #define BULK_THRESH 256
88 c9f42136 2021-11-25 op /* Information associated to each entry in listing. */
97 c9f42136 2021-11-25 op /* Dynamic array of marked entries. */
98 3a4fbb34 2021-11-26 op struct marks {
99 c66e2de7 2021-11-25 op char dirpath[PATH_MAX];
101 c66e2de7 2021-11-25 op int nentries;
102 c66e2de7 2021-11-25 op char **entries;
105 c9f42136 2021-11-25 op /* Line editing state. */
106 3a4fbb34 2021-11-26 op struct edit {
107 c66e2de7 2021-11-25 op wchar_t buffer[BUFLEN + 1];
108 c66e2de7 2021-11-25 op int left, right;
111 c9f42136 2021-11-25 op /* Each tab only stores the following information. */
115 c66e2de7 2021-11-25 op uint8_t flags;
116 c66e2de7 2021-11-25 op char cwd[PATH_MAX];
119 3a4fbb34 2021-11-26 op struct prog {
120 c66e2de7 2021-11-25 op off_t partial;
122 c66e2de7 2021-11-25 op const char *msg;
125 c9f42136 2021-11-25 op /* Global state. */
126 c3e1e821 2021-11-26 op static struct state {
129 3a4fbb34 2021-11-26 op struct row *rows;
130 c66e2de7 2021-11-25 op WINDOW *window;
131 3a4fbb34 2021-11-26 op struct marks marks;
132 3a4fbb34 2021-11-26 op struct edit edit;
133 c66e2de7 2021-11-25 op int edit_scroll;
134 c66e2de7 2021-11-25 op volatile sig_atomic_t pending_usr1;
135 c66e2de7 2021-11-25 op volatile sig_atomic_t pending_winch;
136 3a4fbb34 2021-11-26 op struct prog prog;
137 3a4fbb34 2021-11-26 op struct tab tabs[10];
140 c9f42136 2021-11-25 op /* Macros for accessing global state. */
141 c3e1e821 2021-11-26 op #define ENAME(I) fm.rows[I].name
142 c3e1e821 2021-11-26 op #define ESIZE(I) fm.rows[I].size
143 c3e1e821 2021-11-26 op #define EMODE(I) fm.rows[I].mode
144 c3e1e821 2021-11-26 op #define ISLINK(I) fm.rows[I].islink
145 c3e1e821 2021-11-26 op #define MARKED(I) fm.rows[I].marked
146 c3e1e821 2021-11-26 op #define SCROLL fm.tabs[fm.tab].scroll
147 c3e1e821 2021-11-26 op #define ESEL fm.tabs[fm.tab].esel
148 c3e1e821 2021-11-26 op #define FLAGS fm.tabs[fm.tab].flags
149 c3e1e821 2021-11-26 op #define CWD fm.tabs[fm.tab].cwd
151 c9f42136 2021-11-25 op /* Helpers. */
152 c9f42136 2021-11-25 op #define MIN(A, B) ((A) < (B) ? (A) : (B))
153 c9f42136 2021-11-25 op #define MAX(A, B) ((A) > (B) ? (A) : (B))
154 c9f42136 2021-11-25 op #define ISDIR(E) (strchr((E), '/') != NULL)
155 c71d4135 2021-12-01 op #define CTRL(x) ((x) & 0x1f)
156 126b027d 2021-12-01 op #define nitems(a) (sizeof(a)/sizeof(a[0]))
158 c9f42136 2021-11-25 op /* Line Editing Macros. */
159 c9f42136 2021-11-25 op #define EDIT_FULL(E) ((E).left == (E).right)
160 c9f42136 2021-11-25 op #define EDIT_CAN_LEFT(E) ((E).left)
161 c9f42136 2021-11-25 op #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
162 c9f42136 2021-11-25 op #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
163 c9f42136 2021-11-25 op #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
164 c9f42136 2021-11-25 op #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
165 c9f42136 2021-11-25 op #define EDIT_BACKSPACE(E) (E).left--
166 c9f42136 2021-11-25 op #define EDIT_DELETE(E) (E).right++
167 c9f42136 2021-11-25 op #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
169 f92b5d94 2021-11-26 op enum editstate { CONTINUE, CONFIRM, CANCEL };
170 f92b5d94 2021-11-26 op enum color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK };
172 c9f42136 2021-11-25 op typedef int (*PROCESS)(const char *path);
174 dbbe06a3 2021-11-26 op #ifndef __dead
175 dbbe06a3 2021-11-26 op #define __dead __attribute__((noreturn))
178 85f3c2a1 2021-12-01 op static inline __dead void
179 dbbe06a3 2021-11-26 op quit(const char *reason)
181 dbbe06a3 2021-11-26 op int saved_errno;
183 dbbe06a3 2021-11-26 op saved_errno = errno;
186 dbbe06a3 2021-11-26 op fflush(stderr);
187 dbbe06a3 2021-11-26 op errno = saved_errno;
188 dbbe06a3 2021-11-26 op err(1, "%s", reason);
191 dbbe06a3 2021-11-26 op static inline void *
192 dbbe06a3 2021-11-26 op xcalloc(size_t nmemb, size_t size)
196 dbbe06a3 2021-11-26 op if ((d = calloc(nmemb, size)) == NULL)
197 dbbe06a3 2021-11-26 op quit("calloc");
201 dbbe06a3 2021-11-26 op static inline void *
202 dbbe06a3 2021-11-26 op xrealloc(void *p, size_t size)
206 d80d006a 2021-12-01 op if ((d = realloc(p, size)) == NULL)
207 dbbe06a3 2021-11-26 op quit("realloc");
211 acf1203a 2023-02-08 op static inline int
212 acf1203a 2023-02-08 op xasprintf(char **ret, const char *fmt, ...)
217 acf1203a 2023-02-08 op va_start(ap, fmt);
218 acf1203a 2023-02-08 op r = vasprintf(ret, fmt, ap);
222 acf1203a 2023-02-08 op quit("asprintf");
226 acf1203a 2023-02-08 op static inline char *
227 acf1203a 2023-02-08 op xstrdup(char *str)
231 acf1203a 2023-02-08 op if ((s = strdup(str)) == NULL)
232 acf1203a 2023-02-08 op quit("strdup");
237 3a4fbb34 2021-11-26 op init_marks(struct marks *marks)
239 acf1203a 2023-02-08 op strlcpy(marks->dirpath, "", sizeof(marks->dirpath));
240 c66e2de7 2021-11-25 op marks->bulk = BULK_INIT;
241 c66e2de7 2021-11-25 op marks->nentries = 0;
242 06e3ae5b 2021-11-26 op marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
245 c9f42136 2021-11-25 op /* Unmark all entries. */
247 3a4fbb34 2021-11-26 op mark_none(struct marks *marks)
251 acf1203a 2023-02-08 op strlcpy(marks->dirpath, "", sizeof(marks->dirpath));
252 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk && marks->nentries; i++)
253 c66e2de7 2021-11-25 op if (marks->entries[i]) {
254 c66e2de7 2021-11-25 op free(marks->entries[i]);
255 c66e2de7 2021-11-25 op marks->entries[i] = NULL;
256 c66e2de7 2021-11-25 op marks->nentries--;
258 c66e2de7 2021-11-25 op if (marks->bulk > BULK_THRESH) {
259 c66e2de7 2021-11-25 op /* Reset bulk to free some memory. */
260 c66e2de7 2021-11-25 op free(marks->entries);
261 c66e2de7 2021-11-25 op marks->bulk = BULK_INIT;
262 06e3ae5b 2021-11-26 op marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
267 3a4fbb34 2021-11-26 op add_mark(struct marks *marks, char *dirpath, char *entry)
271 c66e2de7 2021-11-25 op if (!strcmp(marks->dirpath, dirpath)) {
272 c66e2de7 2021-11-25 op /* Append mark to directory. */
273 c66e2de7 2021-11-25 op if (marks->nentries == marks->bulk) {
274 c66e2de7 2021-11-25 op /* Expand bulk to accomodate new entry. */
275 c66e2de7 2021-11-25 op int extra = marks->bulk / 2;
276 c66e2de7 2021-11-25 op marks->bulk += extra; /* bulk *= 1.5; */
277 dbbe06a3 2021-11-26 op marks->entries = xrealloc(marks->entries,
278 06e3ae5b 2021-11-26 op marks->bulk * sizeof(*marks->entries));
279 c66e2de7 2021-11-25 op memset(&marks->entries[marks->nentries], 0,
280 06e3ae5b 2021-11-26 op extra * sizeof(*marks->entries));
281 c66e2de7 2021-11-25 op i = marks->nentries;
283 c66e2de7 2021-11-25 op /* Search for empty slot (there must be one). */
284 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk; i++)
285 c66e2de7 2021-11-25 op if (!marks->entries[i])
289 c66e2de7 2021-11-25 op /* Directory changed. Discard old marks. */
290 c66e2de7 2021-11-25 op mark_none(marks);
291 acf1203a 2023-02-08 op strlcpy(marks->dirpath, dirpath, sizeof(marks->dirpath));
294 acf1203a 2023-02-08 op marks->entries[i] = xstrdup(entry);
295 c66e2de7 2021-11-25 op marks->nentries++;
299 3a4fbb34 2021-11-26 op del_mark(struct marks *marks, char *entry)
303 c66e2de7 2021-11-25 op if (marks->nentries > 1) {
304 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk; i++)
305 c66e2de7 2021-11-25 op if (marks->entries[i] &&
306 c66e2de7 2021-11-25 op !strcmp(marks->entries[i], entry))
308 c66e2de7 2021-11-25 op free(marks->entries[i]);
309 c66e2de7 2021-11-25 op marks->entries[i] = NULL;
310 c66e2de7 2021-11-25 op marks->nentries--;
312 c66e2de7 2021-11-25 op mark_none(marks);
316 3a4fbb34 2021-11-26 op free_marks(struct marks *marks)
320 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk && marks->nentries; i++)
321 c66e2de7 2021-11-25 op if (marks->entries[i]) {
322 c66e2de7 2021-11-25 op free(marks->entries[i]);
323 c66e2de7 2021-11-25 op marks->nentries--;
325 c66e2de7 2021-11-25 op free(marks->entries);
329 c9f42136 2021-11-25 op handle_usr1(int sig)
331 c3e1e821 2021-11-26 op fm.pending_usr1 = 1;
335 c9f42136 2021-11-25 op handle_winch(int sig)
337 c3e1e821 2021-11-26 op fm.pending_winch = 1;
341 ae787901 2021-12-01 op enable_handlers(void)
343 c66e2de7 2021-11-25 op struct sigaction sa;
345 c66e2de7 2021-11-25 op memset(&sa, 0, sizeof(struct sigaction));
346 c66e2de7 2021-11-25 op sa.sa_handler = handle_usr1;
347 c66e2de7 2021-11-25 op sigaction(SIGUSR1, &sa, NULL);
348 c66e2de7 2021-11-25 op sa.sa_handler = handle_winch;
349 c66e2de7 2021-11-25 op sigaction(SIGWINCH, &sa, NULL);
353 ae787901 2021-12-01 op disable_handlers(void)
355 c66e2de7 2021-11-25 op struct sigaction sa;
357 c66e2de7 2021-11-25 op memset(&sa, 0, sizeof(struct sigaction));
358 c66e2de7 2021-11-25 op sa.sa_handler = SIG_DFL;
359 c66e2de7 2021-11-25 op sigaction(SIGUSR1, &sa, NULL);
360 c66e2de7 2021-11-25 op sigaction(SIGWINCH, &sa, NULL);
363 ae787901 2021-12-01 op static void reload(void);
364 ae787901 2021-12-01 op static void update_view(void);
366 c9f42136 2021-11-25 op /* Handle any signals received since last call. */
368 ae787901 2021-12-01 op sync_signals(void)
370 c3e1e821 2021-11-26 op if (fm.pending_usr1) {
371 c66e2de7 2021-11-25 op /* SIGUSR1 received: refresh directory listing. */
373 c3e1e821 2021-11-26 op fm.pending_usr1 = 0;
375 c3e1e821 2021-11-26 op if (fm.pending_winch) {
376 c66e2de7 2021-11-25 op /* SIGWINCH received: resize application accordingly. */
377 c3e1e821 2021-11-26 op delwin(fm.window);
381 c3e1e821 2021-11-26 op fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
382 c3e1e821 2021-11-26 op if (HEIGHT < fm.nfiles && SCROLL + HEIGHT > fm.nfiles)
383 c66e2de7 2021-11-25 op SCROLL = ESEL - HEIGHT;
384 c66e2de7 2021-11-25 op update_view();
385 c3e1e821 2021-11-26 op fm.pending_winch = 0;
390 c66e2de7 2021-11-25 op * This function must be used in place of getch(). It handles signals
391 c66e2de7 2021-11-25 op * while waiting for user input.
398 c66e2de7 2021-11-25 op while ((ch = getch()) == ERR)
399 c66e2de7 2021-11-25 op sync_signals();
404 c66e2de7 2021-11-25 op * This function must be used in place of get_wch(). It handles
405 c66e2de7 2021-11-25 op * signals while waiting for user input.
408 c3e1e821 2021-11-26 op fm_get_wch(wint_t *wch)
412 c66e2de7 2021-11-25 op while ((ret = get_wch(wch)) == (wint_t)ERR)
413 c66e2de7 2021-11-25 op sync_signals();
417 c9f42136 2021-11-25 op /* Get user programs from the environment. */
419 c3e1e821 2021-11-26 op #define FM_ENV(dst, src) if ((dst = getenv("FM_" #src)) == NULL) \
420 c66e2de7 2021-11-25 op dst = getenv(#src);
423 c9f42136 2021-11-25 op get_user_programs()
425 c3e1e821 2021-11-26 op FM_ENV(user_shell, SHELL);
426 c3e1e821 2021-11-26 op FM_ENV(user_pager, PAGER);
427 c3e1e821 2021-11-26 op FM_ENV(user_editor, VISUAL);
428 c66e2de7 2021-11-25 op if (!user_editor)
429 c3e1e821 2021-11-26 op FM_ENV(user_editor, EDITOR);
430 c3e1e821 2021-11-26 op FM_ENV(user_open, OPEN);
433 c9f42136 2021-11-25 op /* Do a fork-exec to external program (e.g. $EDITOR). */
435 126b027d 2021-12-01 op spawn(const char *argv0, ...)
440 126b027d 2021-12-01 op const char *argv[16], *last;
443 bdf4854f 2021-12-01 op memset(argv, 0, sizeof(argv));
445 126b027d 2021-12-01 op va_start(ap, argv0);
446 126b027d 2021-12-01 op argv[0] = argv0;
447 126b027d 2021-12-01 op for (i = 1; i < nitems(argv); ++i) {
448 126b027d 2021-12-01 op last = va_arg(ap, const char *);
449 126b027d 2021-12-01 op if (last == NULL)
451 126b027d 2021-12-01 op argv[i] = last;
455 126b027d 2021-12-01 op if (last != NULL)
458 126b027d 2021-12-01 op disable_handlers();
461 126b027d 2021-12-01 op switch (pid = fork()) {
463 126b027d 2021-12-01 op quit("fork");
464 126b027d 2021-12-01 op case 0: /* child */
465 126b027d 2021-12-01 op setenv("RVSEL", fm.nfiles ? ENAME(ESEL) : "", 1);
466 126b027d 2021-12-01 op execvp(argv[0], (char *const *)argv);
467 126b027d 2021-12-01 op quit("execvp");
469 c66e2de7 2021-11-25 op waitpid(pid, &status, 0);
470 c66e2de7 2021-11-25 op enable_handlers();
471 c66e2de7 2021-11-25 op kill(getpid(), SIGWINCH);
476 c9f42136 2021-11-25 op shell_escaped_cat(char *buf, char *str, size_t n)
478 c66e2de7 2021-11-25 op char *p = buf + strlen(buf);
480 c66e2de7 2021-11-25 op for (n--; n; n--, str++) {
481 c66e2de7 2021-11-25 op switch (*str) {
485 acf1203a 2023-02-08 op strlcpy(p, "'\\''", n);
497 c66e2de7 2021-11-25 op strncat(p, "'", n);
501 c9f42136 2021-11-25 op open_with_env(char *program, char *path)
503 c66e2de7 2021-11-25 op if (program) {
504 c9f42136 2021-11-25 op #ifdef RV_SHELL
505 c66e2de7 2021-11-25 op strncpy(BUF1, program, BUFLEN - 1);
506 c66e2de7 2021-11-25 op strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
507 c66e2de7 2021-11-25 op shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
508 126b027d 2021-12-01 op spawn(RV_SHELL, "-c", BUF1, NULL );
510 126b027d 2021-12-01 op spawn(program, path, NULL);
517 c9f42136 2021-11-25 op /* Curses setup. */
521 c66e2de7 2021-11-25 op setlocale(LC_ALL, "");
524 c66e2de7 2021-11-25 op timeout(100); /* For getch(). */
526 c66e2de7 2021-11-25 op nonl(); /* No NL->CR/NL on output. */
527 c66e2de7 2021-11-25 op intrflush(stdscr, FALSE);
528 c66e2de7 2021-11-25 op keypad(stdscr, TRUE);
529 c66e2de7 2021-11-25 op curs_set(FALSE); /* Hide blinking cursor. */
530 c66e2de7 2021-11-25 op if (has_colors()) {
532 c66e2de7 2021-11-25 op start_color();
533 c9f42136 2021-11-25 op #ifdef NCURSES_EXT_FUNCS
534 c66e2de7 2021-11-25 op use_default_colors();
537 c66e2de7 2021-11-25 op bg = COLOR_BLACK;
539 c66e2de7 2021-11-25 op init_pair(RED, COLOR_RED, bg);
540 c66e2de7 2021-11-25 op init_pair(GREEN, COLOR_GREEN, bg);
541 c66e2de7 2021-11-25 op init_pair(YELLOW, COLOR_YELLOW, bg);
542 c66e2de7 2021-11-25 op init_pair(BLUE, COLOR_BLUE, bg);
543 c66e2de7 2021-11-25 op init_pair(CYAN, COLOR_CYAN, bg);
544 c66e2de7 2021-11-25 op init_pair(MAGENTA, COLOR_MAGENTA, bg);
545 c66e2de7 2021-11-25 op init_pair(WHITE, COLOR_WHITE, bg);
546 c66e2de7 2021-11-25 op init_pair(BLACK, COLOR_BLACK, bg);
548 c66e2de7 2021-11-25 op atexit((void (*)(void))endwin);
549 c66e2de7 2021-11-25 op enable_handlers();
552 c9f42136 2021-11-25 op /* Update the listing view. */
554 c9f42136 2021-11-25 op update_view()
558 c66e2de7 2021-11-25 op int ishidden;
561 c66e2de7 2021-11-25 op mvhline(0, 0, ' ', COLS);
562 c66e2de7 2021-11-25 op attr_on(A_BOLD, NULL);
563 c66e2de7 2021-11-25 op color_set(RVC_TABNUM, NULL);
564 c3e1e821 2021-11-26 op mvaddch(0, COLS - 2, fm.tab + '0');
565 c66e2de7 2021-11-25 op attr_off(A_BOLD, NULL);
566 c3e1e821 2021-11-26 op if (fm.marks.nentries) {
567 c3e1e821 2021-11-26 op numsize = snprintf(BUF1, BUFLEN, "%d", fm.marks.nentries);
568 c66e2de7 2021-11-25 op color_set(RVC_MARKS, NULL);
569 c66e2de7 2021-11-25 op mvaddstr(0, COLS - 3 - numsize, BUF1);
571 c66e2de7 2021-11-25 op numsize = -1;
572 c66e2de7 2021-11-25 op color_set(RVC_CWD, NULL);
573 c66e2de7 2021-11-25 op mbstowcs(WBUF, CWD, PATH_MAX);
574 c66e2de7 2021-11-25 op mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
575 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_BORDER, NULL);
576 c3e1e821 2021-11-26 op wborder(fm.window, 0, 0, 0, 0, 0, 0, 0, 0);
577 c3e1e821 2021-11-26 op ESEL = MAX(MIN(ESEL, fm.nfiles - 1), 0);
580 c66e2de7 2021-11-25 op * Selection might not be visible, due to cursor wrapping or
581 c66e2de7 2021-11-25 op * window shrinking. In that case, the scroll must be moved to
582 c66e2de7 2021-11-25 op * make it visible.
584 c3e1e821 2021-11-26 op if (fm.nfiles > HEIGHT) {
585 c66e2de7 2021-11-25 op SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
586 c3e1e821 2021-11-26 op SCROLL = MIN(MAX(SCROLL, 0), fm.nfiles - HEIGHT);
589 c3e1e821 2021-11-26 op marking = !strcmp(CWD, fm.marks.dirpath);
590 c3e1e821 2021-11-26 op for (i = 0, j = SCROLL; i < HEIGHT && j < fm.nfiles; i++, j++) {
591 c66e2de7 2021-11-25 op ishidden = ENAME(j)[0] == '.';
592 c66e2de7 2021-11-25 op if (j == ESEL)
593 c3e1e821 2021-11-26 op wattr_on(fm.window, A_REVERSE, NULL);
594 c66e2de7 2021-11-25 op if (ISLINK(j))
595 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_LINK, NULL);
596 c66e2de7 2021-11-25 op else if (ishidden)
597 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_HIDDEN, NULL);
598 c66e2de7 2021-11-25 op else if (S_ISREG(EMODE(j))) {
599 c66e2de7 2021-11-25 op if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
600 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_EXEC, NULL);
602 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_REG, NULL);
603 c66e2de7 2021-11-25 op } else if (S_ISDIR(EMODE(j)))
604 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_DIR, NULL);
605 c66e2de7 2021-11-25 op else if (S_ISCHR(EMODE(j)))
606 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_CHR, NULL);
607 c66e2de7 2021-11-25 op else if (S_ISBLK(EMODE(j)))
608 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_BLK, NULL);
609 c66e2de7 2021-11-25 op else if (S_ISFIFO(EMODE(j)))
610 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_FIFO, NULL);
611 c66e2de7 2021-11-25 op else if (S_ISSOCK(EMODE(j)))
612 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_SOCK, NULL);
613 c66e2de7 2021-11-25 op if (S_ISDIR(EMODE(j))) {
614 c66e2de7 2021-11-25 op mbstowcs(WBUF, ENAME(j), PATH_MAX);
615 c66e2de7 2021-11-25 op if (ISLINK(j))
616 acf1203a 2023-02-08 op wcslcat(WBUF, L"/", sizeof(WBUF));
618 61ac2a4b 2021-12-01 op const char *suffix, *suffixes = "BKMGTPEZY";
619 c66e2de7 2021-11-25 op off_t human_size = ESIZE(j) * 10;
620 c66e2de7 2021-11-25 op int length = mbstowcs(WBUF, ENAME(j), PATH_MAX);
621 c66e2de7 2021-11-25 op int namecols = wcswidth(WBUF, length);
622 c66e2de7 2021-11-25 op for (suffix = suffixes; human_size >= 10240; suffix++)
623 c66e2de7 2021-11-25 op human_size = (human_size + 512) / 1024;
624 c66e2de7 2021-11-25 op if (*suffix == 'B')
625 c66e2de7 2021-11-25 op swprintf(WBUF + length, PATH_MAX - length,
627 c66e2de7 2021-11-25 op (int)(COLS - namecols - 6),
628 c66e2de7 2021-11-25 op (int)human_size / 10, *suffix);
630 c66e2de7 2021-11-25 op swprintf(WBUF + length, PATH_MAX - length,
631 c66e2de7 2021-11-25 op L"%*d.%d %c",
632 c66e2de7 2021-11-25 op (int)(COLS - namecols - 8),
633 c66e2de7 2021-11-25 op (int)human_size / 10,
634 c66e2de7 2021-11-25 op (int)human_size % 10, *suffix);
636 c3e1e821 2021-11-26 op mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
637 c3e1e821 2021-11-26 op mvwaddnwstr(fm.window, i + 1, 2, WBUF, COLS - 4);
638 c66e2de7 2021-11-25 op if (marking && MARKED(j)) {
639 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_MARKS, NULL);
640 c3e1e821 2021-11-26 op mvwaddch(fm.window, i + 1, 1, RVS_MARK);
642 c3e1e821 2021-11-26 op mvwaddch(fm.window, i + 1, 1, ' ');
643 c66e2de7 2021-11-25 op if (j == ESEL)
644 c3e1e821 2021-11-26 op wattr_off(fm.window, A_REVERSE, NULL);
646 c66e2de7 2021-11-25 op for (; i < HEIGHT; i++)
647 c3e1e821 2021-11-26 op mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
648 c3e1e821 2021-11-26 op if (fm.nfiles > HEIGHT) {
649 c66e2de7 2021-11-25 op int center, height;
650 c3e1e821 2021-11-26 op center = (SCROLL + HEIGHT / 2) * HEIGHT / fm.nfiles;
651 c3e1e821 2021-11-26 op height = (HEIGHT - 1) * HEIGHT / fm.nfiles;
654 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_SCROLLBAR, NULL);
655 c3e1e821 2021-11-26 op mvwvline(fm.window, center - height/2 + 1, COLS - 1,
656 c66e2de7 2021-11-25 op RVS_SCROLLBAR, height);
658 c66e2de7 2021-11-25 op BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
659 c66e2de7 2021-11-25 op BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
660 c66e2de7 2021-11-25 op BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
661 c3e1e821 2021-11-26 op if (!fm.nfiles)
662 acf1203a 2023-02-08 op strlcpy(BUF2, "0/0", sizeof(BUF2));
664 c3e1e821 2021-11-26 op snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, fm.nfiles);
665 c66e2de7 2021-11-25 op snprintf(BUF1 + 3, BUFLEN - 3, "%12s", BUF2);
666 c66e2de7 2021-11-25 op color_set(RVC_STATUS, NULL);
667 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, STATUSPOS, BUF1);
668 c3e1e821 2021-11-26 op wrefresh(fm.window);
671 c9f42136 2021-11-25 op /* Show a message on the status bar. */
672 afd4e08d 2021-12-01 op static void __attribute__((format(printf, 2, 3)))
673 21c41be6 2021-12-01 op message(enum color c, const char *fmt, ...)
675 c66e2de7 2021-11-25 op int len, pos;
676 c66e2de7 2021-11-25 op va_list args;
678 c66e2de7 2021-11-25 op va_start(args, fmt);
679 c66e2de7 2021-11-25 op vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
680 c66e2de7 2021-11-25 op va_end(args);
681 c66e2de7 2021-11-25 op len = strlen(BUF1);
682 c66e2de7 2021-11-25 op pos = (STATUSPOS - len) / 2;
683 c66e2de7 2021-11-25 op attr_on(A_BOLD, NULL);
684 f92b5d94 2021-11-26 op color_set(c, NULL);
685 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, pos, BUF1);
686 c66e2de7 2021-11-25 op color_set(DEFAULT, NULL);
687 c66e2de7 2021-11-25 op attr_off(A_BOLD, NULL);
690 c9f42136 2021-11-25 op /* Clear message area, leaving only status info. */
692 c9f42136 2021-11-25 op clear_message()
694 c66e2de7 2021-11-25 op mvhline(LINES - 1, 0, ' ', STATUSPOS);
697 c9f42136 2021-11-25 op /* Comparison used to sort listing entries. */
699 c9f42136 2021-11-25 op rowcmp(const void *a, const void *b)
701 c66e2de7 2021-11-25 op int isdir1, isdir2, cmpdir;
702 3a4fbb34 2021-11-26 op const struct row *r1 = a;
703 3a4fbb34 2021-11-26 op const struct row *r2 = b;
704 c66e2de7 2021-11-25 op isdir1 = S_ISDIR(r1->mode);
705 c66e2de7 2021-11-25 op isdir2 = S_ISDIR(r2->mode);
706 c66e2de7 2021-11-25 op cmpdir = isdir2 - isdir1;
707 c66e2de7 2021-11-25 op return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
710 c9f42136 2021-11-25 op /* Get all entries in current working directory. */
712 3a4fbb34 2021-11-26 op ls(struct row **rowsp, uint8_t flags)
715 c66e2de7 2021-11-25 op struct dirent *ep;
716 c66e2de7 2021-11-25 op struct stat statbuf;
717 3a4fbb34 2021-11-26 op struct row *rows;
720 c66e2de7 2021-11-25 op if (!(dp = opendir(".")))
722 c66e2de7 2021-11-25 op n = -2; /* We don't want the entries "." and "..". */
723 c66e2de7 2021-11-25 op while (readdir(dp))
725 c66e2de7 2021-11-25 op if (n == 0) {
726 c66e2de7 2021-11-25 op closedir(dp);
729 c66e2de7 2021-11-25 op rewinddir(dp);
730 1cd87bff 2023-02-08 op rows = xcalloc(n, sizeof(*rows));
732 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
733 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
735 c66e2de7 2021-11-25 op if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
737 c66e2de7 2021-11-25 op lstat(ep->d_name, &statbuf);
738 c66e2de7 2021-11-25 op rows[i].islink = S_ISLNK(statbuf.st_mode);
739 c66e2de7 2021-11-25 op stat(ep->d_name, &statbuf);
740 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
741 c66e2de7 2021-11-25 op if (flags & SHOW_DIRS) {
742 acf1203a 2023-02-08 op xasprintf(&rows[i].name, "%s%s",
743 acf1203a 2023-02-08 op ep->d_name, rows[i].islink ? "" : "/");
744 c66e2de7 2021-11-25 op rows[i].mode = statbuf.st_mode;
747 c66e2de7 2021-11-25 op } else if (flags & SHOW_FILES) {
748 acf1203a 2023-02-08 op rows[i].name = xstrdup(ep->d_name);
749 c66e2de7 2021-11-25 op rows[i].size = statbuf.st_size;
750 c66e2de7 2021-11-25 op rows[i].mode = statbuf.st_mode;
754 c66e2de7 2021-11-25 op n = i; /* Ignore unused space in array caused by filters. */
755 c66e2de7 2021-11-25 op qsort(rows, n, sizeof(*rows), rowcmp);
756 c66e2de7 2021-11-25 op closedir(dp);
757 c66e2de7 2021-11-25 op *rowsp = rows;
762 3a4fbb34 2021-11-26 op free_rows(struct row **rowsp, int nfiles)
766 c66e2de7 2021-11-25 op for (i = 0; i < nfiles; i++)
767 c66e2de7 2021-11-25 op free((*rowsp)[i].name);
768 c66e2de7 2021-11-25 op free(*rowsp);
769 c66e2de7 2021-11-25 op *rowsp = NULL;
772 c9f42136 2021-11-25 op /* Change working directory to the path in CWD. */
774 c9f42136 2021-11-25 op cd(int reset)
778 c66e2de7 2021-11-25 op message(CYAN, "Loading \"%s\"...", CWD);
780 c66e2de7 2021-11-25 op if (chdir(CWD) == -1) {
781 c66e2de7 2021-11-25 op getcwd(CWD, PATH_MAX - 1);
782 c66e2de7 2021-11-25 op if (CWD[strlen(CWD) - 1] != '/')
783 acf1203a 2023-02-08 op strlcat(CWD, "/", sizeof(CWD));
787 c66e2de7 2021-11-25 op ESEL = SCROLL = 0;
788 c3e1e821 2021-11-26 op if (fm.nfiles)
789 c3e1e821 2021-11-26 op free_rows(&fm.rows, fm.nfiles);
790 c3e1e821 2021-11-26 op fm.nfiles = ls(&fm.rows, FLAGS);
791 c3e1e821 2021-11-26 op if (!strcmp(CWD, fm.marks.dirpath)) {
792 c3e1e821 2021-11-26 op for (i = 0; i < fm.nfiles; i++) {
793 c3e1e821 2021-11-26 op for (j = 0; j < fm.marks.bulk; j++)
794 c3e1e821 2021-11-26 op if (fm.marks.entries[j] &&
795 c3e1e821 2021-11-26 op !strcmp(fm.marks.entries[j], ENAME(i)))
797 c3e1e821 2021-11-26 op MARKED(i) = j < fm.marks.bulk;
800 c3e1e821 2021-11-26 op for (i = 0; i < fm.nfiles; i++)
801 c66e2de7 2021-11-25 op MARKED(i) = 0;
803 c66e2de7 2021-11-25 op clear_message();
804 c66e2de7 2021-11-25 op update_view();
807 c9f42136 2021-11-25 op /* Select a target entry, if it is present. */
809 c9f42136 2021-11-25 op try_to_sel(const char *target)
812 c66e2de7 2021-11-25 op if (!ISDIR(target))
813 c3e1e821 2021-11-26 op while ((ESEL + 1) < fm.nfiles && S_ISDIR(EMODE(ESEL)))
815 c3e1e821 2021-11-26 op while ((ESEL + 1) < fm.nfiles && strcoll(ENAME(ESEL), target) < 0)
819 c9f42136 2021-11-25 op /* Reload CWD, but try to keep selection. */
823 c3e1e821 2021-11-26 op if (fm.nfiles) {
824 acf1203a 2023-02-08 op strlcpy(INPUT, ENAME(ESEL), sizeof(INPUT));
826 c66e2de7 2021-11-25 op try_to_sel(INPUT);
827 c66e2de7 2021-11-25 op update_view();
833 c9f42136 2021-11-25 op count_dir(const char *path)
836 c66e2de7 2021-11-25 op struct dirent *ep;
837 c66e2de7 2021-11-25 op struct stat statbuf;
838 c66e2de7 2021-11-25 op char subpath[PATH_MAX];
841 c66e2de7 2021-11-25 op if (!(dp = opendir(path)))
844 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
845 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
847 c66e2de7 2021-11-25 op snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
848 c66e2de7 2021-11-25 op lstat(subpath, &statbuf);
849 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
850 acf1203a 2023-02-08 op strlcat(subpath, "/", sizeof(subpath));
851 c66e2de7 2021-11-25 op total += count_dir(subpath);
853 c66e2de7 2021-11-25 op total += statbuf.st_size;
855 c66e2de7 2021-11-25 op closedir(dp);
856 c66e2de7 2021-11-25 op return total;
860 c9f42136 2021-11-25 op count_marked()
865 c66e2de7 2021-11-25 op struct stat statbuf;
868 c3e1e821 2021-11-26 op chdir(fm.marks.dirpath);
869 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
870 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
872 c66e2de7 2021-11-25 op if (ISDIR(entry)) {
873 c66e2de7 2021-11-25 op total += count_dir(entry);
875 c66e2de7 2021-11-25 op lstat(entry, &statbuf);
876 c66e2de7 2021-11-25 op total += statbuf.st_size;
881 c66e2de7 2021-11-25 op return total;
885 c66e2de7 2021-11-25 op * Recursively process a source directory using CWD as destination
886 c66e2de7 2021-11-25 op * root. For each node (i.e. directory), do the following:
888 c66e2de7 2021-11-25 op * 1. call pre(destination);
889 c66e2de7 2021-11-25 op * 2. call proc() on every child leaf (i.e. files);
890 c66e2de7 2021-11-25 op * 3. recurse into every child node;
891 c66e2de7 2021-11-25 op * 4. call pos(source).
893 c66e2de7 2021-11-25 op * E.g. to move directory /src/ (and all its contents) inside /dst/:
894 acf1203a 2023-02-08 op * strlcpy(CWD, "/dst/", sizeof(CWD));
895 c66e2de7 2021-11-25 op * process_dir(adddir, movfile, deldir, "/src/");
898 c9f42136 2021-11-25 op process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
902 c66e2de7 2021-11-25 op struct dirent *ep;
903 c66e2de7 2021-11-25 op struct stat statbuf;
904 c66e2de7 2021-11-25 op char subpath[PATH_MAX];
908 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
909 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
910 acf1203a 2023-02-08 op strlcat(dstpath, path + strlen(fm.marks.dirpath),
911 acf1203a 2023-02-08 op sizeof(dstpath));
912 c66e2de7 2021-11-25 op ret |= pre(dstpath);
914 c66e2de7 2021-11-25 op if (!(dp = opendir(path)))
916 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
917 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
919 c66e2de7 2021-11-25 op snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
920 c66e2de7 2021-11-25 op lstat(subpath, &statbuf);
921 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
922 c66e2de7 2021-11-25 op strcat(subpath, "/");
923 c66e2de7 2021-11-25 op ret |= process_dir(pre, proc, pos, subpath);
925 c66e2de7 2021-11-25 op ret |= proc(subpath);
927 c66e2de7 2021-11-25 op closedir(dp);
929 c66e2de7 2021-11-25 op ret |= pos(path);
934 c66e2de7 2021-11-25 op * Process all marked entries using CWD as destination root. All
935 c66e2de7 2021-11-25 op * marked entries that are directories will be recursively processed.
936 c66e2de7 2021-11-25 op * See process_dir() for details on the parameters.
939 c66e2de7 2021-11-25 op process_marked(PROCESS pre, PROCESS proc, PROCESS pos, const char *msg_doing,
940 c66e2de7 2021-11-25 op const char *msg_done)
944 c66e2de7 2021-11-25 op char path[PATH_MAX];
946 c66e2de7 2021-11-25 op clear_message();
947 c66e2de7 2021-11-25 op message(CYAN, "%s...", msg_doing);
949 c3e1e821 2021-11-26 op fm.prog = (struct prog){0, count_marked(), msg_doing};
950 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
951 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
954 c3e1e821 2021-11-26 op snprintf(path, PATH_MAX, "%s%s", fm.marks.dirpath,
956 c66e2de7 2021-11-25 op if (ISDIR(entry)) {
957 c66e2de7 2021-11-25 op if (!strncmp(path, CWD, strlen(path)))
960 c66e2de7 2021-11-25 op ret = process_dir(pre, proc, pos, path);
962 c66e2de7 2021-11-25 op ret = proc(path);
964 c3e1e821 2021-11-26 op del_mark(&fm.marks, entry);
969 c3e1e821 2021-11-26 op fm.prog.total = 0;
971 c3e1e821 2021-11-26 op if (!fm.marks.nentries)
972 c66e2de7 2021-11-25 op message(GREEN, "%s all marked entries.", msg_done);
974 c66e2de7 2021-11-25 op message(RED, "Some errors occured while %s.", msg_doing);
979 c9f42136 2021-11-25 op update_progress(off_t delta)
983 c3e1e821 2021-11-26 op if (!fm.prog.total)
985 c3e1e821 2021-11-26 op fm.prog.partial += delta;
986 c3e1e821 2021-11-26 op percent = (int)(fm.prog.partial * 100 / fm.prog.total);
987 c3e1e821 2021-11-26 op message(CYAN, "%s...%d%%", fm.prog.msg, percent);
991 c9f42136 2021-11-25 op /* Wrappers for file operations. */
993 c66e2de7 2021-11-25 op delfile(const char *path)
996 c66e2de7 2021-11-25 op struct stat st;
998 c66e2de7 2021-11-25 op ret = lstat(path, &st);
1001 c66e2de7 2021-11-25 op update_progress(st.st_size);
1002 c66e2de7 2021-11-25 op return unlink(path);
1006 c66e2de7 2021-11-25 op addfile(const char *path)
1008 c66e2de7 2021-11-25 op /* Using creat(2) because mknod(2) doesn't seem to be portable. */
1011 c66e2de7 2021-11-25 op ret = creat(path, 0644);
1012 c66e2de7 2021-11-25 op if (ret < 0)
1014 c66e2de7 2021-11-25 op return close(ret);
1018 c66e2de7 2021-11-25 op cpyfile(const char *srcpath)
1020 c66e2de7 2021-11-25 op int src, dst, ret;
1021 c66e2de7 2021-11-25 op size_t size;
1022 c66e2de7 2021-11-25 op struct stat st;
1023 c66e2de7 2021-11-25 op char buf[BUFSIZ];
1024 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
1026 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
1027 acf1203a 2023-02-08 op strlcat(dstpath, srcpath + strlen(fm.marks.dirpath), sizeof(dstpath));
1028 c66e2de7 2021-11-25 op ret = lstat(srcpath, &st);
1029 c66e2de7 2021-11-25 op if (ret < 0)
1031 c66e2de7 2021-11-25 op if (S_ISLNK(st.st_mode)) {
1032 c66e2de7 2021-11-25 op ret = readlink(srcpath, BUF1, BUFLEN - 1);
1033 c66e2de7 2021-11-25 op if (ret < 0)
1035 c66e2de7 2021-11-25 op BUF1[ret] = '\0';
1036 c66e2de7 2021-11-25 op ret = symlink(BUF1, dstpath);
1038 c66e2de7 2021-11-25 op ret = src = open(srcpath, O_RDONLY);
1039 c66e2de7 2021-11-25 op if (ret < 0)
1041 c66e2de7 2021-11-25 op ret = dst = creat(dstpath, st.st_mode);
1042 c66e2de7 2021-11-25 op if (ret < 0)
1044 c66e2de7 2021-11-25 op while ((size = read(src, buf, BUFSIZ)) > 0) {
1045 c66e2de7 2021-11-25 op write(dst, buf, size);
1046 c66e2de7 2021-11-25 op update_progress(size);
1047 c66e2de7 2021-11-25 op sync_signals();
1057 c66e2de7 2021-11-25 op adddir(const char *path)
1060 c66e2de7 2021-11-25 op struct stat st;
1062 c66e2de7 2021-11-25 op ret = stat(CWD, &st);
1063 c66e2de7 2021-11-25 op if (ret < 0)
1065 c66e2de7 2021-11-25 op return mkdir(path, st.st_mode);
1069 c66e2de7 2021-11-25 op movfile(const char *srcpath)
1072 c66e2de7 2021-11-25 op struct stat st;
1073 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
1075 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
1076 acf1203a 2023-02-08 op strlcat(dstpath, srcpath + strlen(fm.marks.dirpath), sizeof(dstpath));
1077 c66e2de7 2021-11-25 op ret = rename(srcpath, dstpath);
1078 c66e2de7 2021-11-25 op if (ret == 0) {
1079 c66e2de7 2021-11-25 op ret = lstat(dstpath, &st);
1080 c66e2de7 2021-11-25 op if (ret < 0)
1082 c66e2de7 2021-11-25 op update_progress(st.st_size);
1083 c66e2de7 2021-11-25 op } else if (errno == EXDEV) {
1084 c66e2de7 2021-11-25 op ret = cpyfile(srcpath);
1085 c66e2de7 2021-11-25 op if (ret < 0)
1087 c66e2de7 2021-11-25 op ret = unlink(srcpath);
1093 c9f42136 2021-11-25 op start_line_edit(const char *init_input)
1095 c66e2de7 2021-11-25 op curs_set(TRUE);
1096 c66e2de7 2021-11-25 op strncpy(INPUT, init_input, BUFLEN);
1097 c3e1e821 2021-11-26 op fm.edit.left = mbstowcs(fm.edit.buffer, init_input, BUFLEN);
1098 c3e1e821 2021-11-26 op fm.edit.right = BUFLEN - 1;
1099 c3e1e821 2021-11-26 op fm.edit.buffer[BUFLEN] = L'\0';
1100 c3e1e821 2021-11-26 op fm.edit_scroll = 0;
1103 c9f42136 2021-11-25 op /* Read input and change editing state accordingly. */
1104 f92b5d94 2021-11-26 op static enum editstate
1105 c9f42136 2021-11-25 op get_line_edit()
1107 c66e2de7 2021-11-25 op wchar_t eraser, killer, wch;
1108 c66e2de7 2021-11-25 op int ret, length;
1110 c3e1e821 2021-11-26 op ret = fm_get_wch((wint_t *)&wch);
1111 c66e2de7 2021-11-25 op erasewchar(&eraser);
1112 c66e2de7 2021-11-25 op killwchar(&killer);
1113 c66e2de7 2021-11-25 op if (ret == KEY_CODE_YES) {
1114 c66e2de7 2021-11-25 op if (wch == KEY_ENTER) {
1115 c66e2de7 2021-11-25 op curs_set(FALSE);
1116 c66e2de7 2021-11-25 op return CONFIRM;
1117 c66e2de7 2021-11-25 op } else if (wch == KEY_LEFT) {
1118 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1119 c3e1e821 2021-11-26 op EDIT_LEFT(fm.edit);
1120 c66e2de7 2021-11-25 op } else if (wch == KEY_RIGHT) {
1121 c3e1e821 2021-11-26 op if (EDIT_CAN_RIGHT(fm.edit))
1122 c3e1e821 2021-11-26 op EDIT_RIGHT(fm.edit);
1123 c66e2de7 2021-11-25 op } else if (wch == KEY_UP) {
1124 c3e1e821 2021-11-26 op while (EDIT_CAN_LEFT(fm.edit))
1125 c3e1e821 2021-11-26 op EDIT_LEFT(fm.edit);
1126 c66e2de7 2021-11-25 op } else if (wch == KEY_DOWN) {
1127 c3e1e821 2021-11-26 op while (EDIT_CAN_RIGHT(fm.edit))
1128 c3e1e821 2021-11-26 op EDIT_RIGHT(fm.edit);
1129 c66e2de7 2021-11-25 op } else if (wch == KEY_BACKSPACE) {
1130 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1131 c3e1e821 2021-11-26 op EDIT_BACKSPACE(fm.edit);
1132 c66e2de7 2021-11-25 op } else if (wch == KEY_DC) {
1133 c3e1e821 2021-11-26 op if (EDIT_CAN_RIGHT(fm.edit))
1134 c3e1e821 2021-11-26 op EDIT_DELETE(fm.edit);
1137 c66e2de7 2021-11-25 op if (wch == L'\r' || wch == L'\n') {
1138 c66e2de7 2021-11-25 op curs_set(FALSE);
1139 c66e2de7 2021-11-25 op return CONFIRM;
1140 c66e2de7 2021-11-25 op } else if (wch == L'\t') {
1141 c66e2de7 2021-11-25 op curs_set(FALSE);
1142 c66e2de7 2021-11-25 op return CANCEL;
1143 c66e2de7 2021-11-25 op } else if (wch == eraser) {
1144 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1145 c3e1e821 2021-11-26 op EDIT_BACKSPACE(fm.edit);
1146 c66e2de7 2021-11-25 op } else if (wch == killer) {
1147 c3e1e821 2021-11-26 op EDIT_CLEAR(fm.edit);
1148 c66e2de7 2021-11-25 op clear_message();
1149 c66e2de7 2021-11-25 op } else if (iswprint(wch)) {
1150 c3e1e821 2021-11-26 op if (!EDIT_FULL(fm.edit))
1151 c3e1e821 2021-11-26 op EDIT_INSERT(fm.edit, wch);
1154 c66e2de7 2021-11-25 op /* Encode edit contents in INPUT. */
1155 c3e1e821 2021-11-26 op fm.edit.buffer[fm.edit.left] = L'\0';
1156 c3e1e821 2021-11-26 op length = wcstombs(INPUT, fm.edit.buffer, BUFLEN);
1157 c3e1e821 2021-11-26 op wcstombs(&INPUT[length], &fm.edit.buffer[fm.edit.right + 1],
1158 c66e2de7 2021-11-25 op BUFLEN - length);
1159 c66e2de7 2021-11-25 op return CONTINUE;
1162 c9f42136 2021-11-25 op /* Update line input on the screen. */
1164 f92b5d94 2021-11-26 op update_input(const char *prompt, enum color c)
1166 c66e2de7 2021-11-25 op int plen, ilen, maxlen;
1168 c66e2de7 2021-11-25 op plen = strlen(prompt);
1169 c66e2de7 2021-11-25 op ilen = mbstowcs(NULL, INPUT, 0);
1170 c66e2de7 2021-11-25 op maxlen = STATUSPOS - plen - 2;
1171 c3e1e821 2021-11-26 op if (ilen - fm.edit_scroll < maxlen)
1172 c3e1e821 2021-11-26 op fm.edit_scroll = MAX(ilen - maxlen, 0);
1173 c3e1e821 2021-11-26 op else if (fm.edit.left > fm.edit_scroll + maxlen - 1)
1174 c3e1e821 2021-11-26 op fm.edit_scroll = fm.edit.left - maxlen;
1175 c3e1e821 2021-11-26 op else if (fm.edit.left < fm.edit_scroll)
1176 c3e1e821 2021-11-26 op fm.edit_scroll = MAX(fm.edit.left - maxlen, 0);
1177 c66e2de7 2021-11-25 op color_set(RVC_PROMPT, NULL);
1178 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, 0, prompt);
1179 f92b5d94 2021-11-26 op color_set(c, NULL);
1180 c66e2de7 2021-11-25 op mbstowcs(WBUF, INPUT, COLS);
1181 c3e1e821 2021-11-26 op mvaddnwstr(LINES - 1, plen, &WBUF[fm.edit_scroll], maxlen);
1182 c3e1e821 2021-11-26 op mvaddch(LINES - 1, plen + MIN(ilen - fm.edit_scroll, maxlen + 1),
1184 c66e2de7 2021-11-25 op color_set(DEFAULT, NULL);
1185 c3e1e821 2021-11-26 op if (fm.edit_scroll)
1186 c66e2de7 2021-11-25 op mvaddch(LINES - 1, plen - 1, '<');
1187 c3e1e821 2021-11-26 op if (ilen > fm.edit_scroll + maxlen)
1188 c66e2de7 2021-11-25 op mvaddch(LINES - 1, plen + maxlen, '>');
1189 c3e1e821 2021-11-26 op move(LINES - 1, plen + fm.edit.left - fm.edit_scroll);
1193 a1bd5162 2021-12-01 op cmd_down(void)
1195 a1bd5162 2021-12-01 op if (fm.nfiles)
1196 a1bd5162 2021-12-01 op ESEL = MIN(ESEL + 1, fm.nfiles - 1);
1200 a1bd5162 2021-12-01 op cmd_up(void)
1202 a1bd5162 2021-12-01 op if (fm.nfiles)
1203 a1bd5162 2021-12-01 op ESEL = MAX(ESEL - 1, 0);
1207 a1bd5162 2021-12-01 op cmd_scroll_down(void)
1209 a1bd5162 2021-12-01 op if (!fm.nfiles)
1211 a1bd5162 2021-12-01 op ESEL = MIN(ESEL + HEIGHT, fm.nfiles - 1);
1212 a1bd5162 2021-12-01 op if (fm.nfiles > HEIGHT)
1213 a1bd5162 2021-12-01 op SCROLL = MIN(SCROLL + HEIGHT, fm.nfiles - HEIGHT);
1217 a1bd5162 2021-12-01 op cmd_scroll_up(void)
1219 a1bd5162 2021-12-01 op if (!fm.nfiles)
1221 a1bd5162 2021-12-01 op ESEL = MAX(ESEL - HEIGHT, 0);
1222 a1bd5162 2021-12-01 op SCROLL = MAX(SCROLL - HEIGHT, 0);
1226 a1bd5162 2021-12-01 op cmd_man(void)
1228 a1bd5162 2021-12-01 op spawn("man", "fm", NULL);
1232 6d283064 2021-12-01 op cmd_jump_top(void)
1234 6d283064 2021-12-01 op if (fm.nfiles)
1239 6d283064 2021-12-01 op cmd_jump_bottom(void)
1241 6d283064 2021-12-01 op if (fm.nfiles)
1242 6d283064 2021-12-01 op ESEL = fm.nfiles - 1;
1246 6adcf849 2021-12-01 op cmd_cd_down(void)
1248 6adcf849 2021-12-01 op if (!fm.nfiles || !S_ISDIR(EMODE(ESEL)))
1250 6adcf849 2021-12-01 op if (chdir(ENAME(ESEL)) == -1) {
1251 6adcf849 2021-12-01 op message(RED, "cd: %s: %s", ENAME(ESEL), strerror(errno));
1254 6adcf849 2021-12-01 op strlcat(CWD, ENAME(ESEL), sizeof(CWD));
1259 6adcf849 2021-12-01 op cmd_cd_up(void)
1261 6adcf849 2021-12-01 op char *dirname, first;
1263 6adcf849 2021-12-01 op if (!strcmp(CWD, "/"))
1266 6adcf849 2021-12-01 op /* dirname(3) basically */
1267 6adcf849 2021-12-01 op dirname = strrchr(CWD, '/');
1268 6adcf849 2021-12-01 op *dirname-- = '\0';
1269 6adcf849 2021-12-01 op dirname = strrchr(CWD, '/') + 1;
1271 6adcf849 2021-12-01 op first = dirname[0];
1272 6adcf849 2021-12-01 op dirname[0] = '\0';
1274 6adcf849 2021-12-01 op dirname[0] = first;
1275 6adcf849 2021-12-01 op strlcat(dirname, "/", sizeof(dirname));
1276 6adcf849 2021-12-01 op try_to_sel(dirname);
1277 6adcf849 2021-12-01 op dirname[0] = '\0';
1278 6adcf849 2021-12-01 op if (fm.nfiles > HEIGHT)
1279 6adcf849 2021-12-01 op SCROLL = ESEL - HEIGHT / 2;
1283 6adcf849 2021-12-01 op cmd_home(void)
1285 6adcf849 2021-12-01 op const char *home;
1287 6adcf849 2021-12-01 op if ((home = getenv("HOME")) == NULL) {
1288 6adcf849 2021-12-01 op message(RED, "HOME is not defined!");
1292 6adcf849 2021-12-01 op strlcpy(CWD, home, sizeof(CWD));
1293 6adcf849 2021-12-01 op if (*CWD != '\0' && CWD[strlen(CWD) - 1] != '/')
1294 6adcf849 2021-12-01 op strlcat(CWD, "/", sizeof(CWD));
1299 01a2cff6 2021-12-12 op cmd_copy_path(void)
1301 01a2cff6 2021-12-12 op const char *path;
1304 01a2cff6 2021-12-12 op if ((path = getenv("CLIP")) == NULL ||
1305 01a2cff6 2021-12-12 op (f = fopen(path, "w")) == NULL) {
1306 01a2cff6 2021-12-12 op /* use internal clipboard */
1307 01a2cff6 2021-12-12 op strlcpy(clipboard, CWD, sizeof(clipboard));
1308 01a2cff6 2021-12-12 op strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1312 01a2cff6 2021-12-12 op fprintf(f, "%s%s", CWD, ENAME(ESEL));
1313 01a2cff6 2021-12-12 op fputc('\0', f);
1318 01a2cff6 2021-12-12 op cmd_paste_path(void)
1321 01a2cff6 2021-12-12 op const char *path;
1324 01a2cff6 2021-12-12 op char p[PATH_MAX];
1326 01a2cff6 2021-12-12 op if ((path = getenv("CLIP")) != NULL &&
1327 01a2cff6 2021-12-12 op (f = fopen(path, "w")) != NULL) {
1328 01a2cff6 2021-12-12 op r = fread(clipboard, 1, sizeof(clipboard)-1, f);
1330 01a2cff6 2021-12-12 op if (r == -1 || r == 0)
1332 01a2cff6 2021-12-12 op if ((nl = memmem(clipboard, r, "", 1)) == NULL)
1336 01a2cff6 2021-12-12 op strlcpy(p, clipboard, sizeof(p));
1337 01a2cff6 2021-12-12 op strlcpy(CWD, clipboard, sizeof(CWD));
1338 01a2cff6 2021-12-12 op if ((nl = strrchr(CWD, '/')) != NULL) {
1340 01a2cff6 2021-12-12 op nl = strrchr(p, '/');
1341 01a2cff6 2021-12-12 op assert(nl != NULL);
1343 01a2cff6 2021-12-12 op if (strcmp(CWD, "/") != 0)
1344 01a2cff6 2021-12-12 op strlcat(CWD, "/", sizeof(CWD));
1346 01a2cff6 2021-12-12 op try_to_sel(nl+1);
1350 01a2cff6 2021-12-12 op memset(clipboard, 0, sizeof(clipboard));
1354 38d7b472 2021-12-13 op cmd_reload(void)
1360 e03d1219 2021-12-13 op cmd_shell(void)
1362 e03d1219 2021-12-13 op const char *shell;
1364 e03d1219 2021-12-13 op if ((shell = getenv("SHELL")) == NULL)
1365 e03d1219 2021-12-13 op shell = FM_SHELL;
1366 e03d1219 2021-12-13 op spawn(shell, NULL);
1370 63fe4ed0 2021-12-13 op cmd_view(void)
1372 63fe4ed0 2021-12-13 op const char *pager;
1374 63fe4ed0 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1377 63fe4ed0 2021-12-13 op if ((pager = getenv("PAGER")) == NULL)
1378 63fe4ed0 2021-12-13 op pager = FM_PAGER;
1379 63fe4ed0 2021-12-13 op spawn(pager, ENAME(ESEL), NULL);
1383 d244a655 2021-12-13 op cmd_edit(void)
1385 d244a655 2021-12-13 op const char *editor;
1387 d244a655 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1390 d244a655 2021-12-13 op if ((editor = getenv("VISUAL")) == NULL ||
1391 d244a655 2021-12-13 op (editor = getenv("EDITOR")) == NULL)
1392 d244a655 2021-12-13 op editor = FM_ED;
1394 d244a655 2021-12-13 op spawn(editor, ENAME(ESEL), NULL);
1398 2c5122bc 2021-12-13 op cmd_open(void)
1400 2c5122bc 2021-12-13 op const char *opener;
1402 2c5122bc 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1405 2c5122bc 2021-12-13 op if ((opener = getenv("OPENER")) == NULL)
1406 2c5122bc 2021-12-13 op opener = FM_OPENER;
1408 2c5122bc 2021-12-13 op spawn(opener, ENAME(ESEL), NULL);
1412 2c5122bc 2021-12-13 op cmd_mark(void)
1414 2c5122bc 2021-12-13 op if (MARKED(ESEL))
1415 2c5122bc 2021-12-13 op del_mark(&fm.marks, ENAME(ESEL));
1417 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1419 2c5122bc 2021-12-13 op MARKED(ESEL) = !MARKED(ESEL);
1420 2c5122bc 2021-12-13 op ESEL = (ESEL + 1) % fm.nfiles;
1424 2c5122bc 2021-12-13 op cmd_toggle_mark(void)
1428 2c5122bc 2021-12-13 op for (i = 0; i < fm.nfiles; ++i) {
1429 2c5122bc 2021-12-13 op if (MARKED(i))
1430 2c5122bc 2021-12-13 op del_mark(&fm.marks, ENAME(i));
1432 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1433 2c5122bc 2021-12-13 op MARKED(i) = !MARKED(i);
1438 2c5122bc 2021-12-13 op cmd_mark_all(void)
1442 2c5122bc 2021-12-13 op for (i = 0; i < fm.nfiles; ++i)
1443 2c5122bc 2021-12-13 op if (!MARKED(i)) {
1444 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1445 2c5122bc 2021-12-13 op MARKED(i) = 1;
1452 a1bd5162 2021-12-01 op int meta, ch, c;
1453 a1bd5162 2021-12-01 op struct binding {
1455 a1bd5162 2021-12-01 op #define K_META 1
1456 a1bd5162 2021-12-01 op #define K_CTRL 2
1457 a1bd5162 2021-12-01 op int chflags;
1458 a1bd5162 2021-12-01 op void (*fn)(void);
1459 a1bd5162 2021-12-01 op #define X_UPDV 1
1460 a1bd5162 2021-12-01 op #define X_QUIT 2
1462 a1bd5162 2021-12-01 op } bindings[] = {
1463 6d283064 2021-12-01 op {'<', K_META, cmd_jump_top, X_UPDV},
1464 6d283064 2021-12-01 op {'>', K_META, cmd_jump_bottom, X_UPDV},
1465 a1bd5162 2021-12-01 op {'?', 0, cmd_man, 0},
1466 6d283064 2021-12-01 op {'G', 0, cmd_jump_bottom, X_UPDV},
1467 6adcf849 2021-12-01 op {'H', 0, cmd_home, X_UPDV},
1468 a1bd5162 2021-12-01 op {'J', 0, cmd_scroll_down, X_UPDV},
1469 a1bd5162 2021-12-01 op {'K', 0, cmd_scroll_up, X_UPDV},
1470 2c5122bc 2021-12-13 op {'M', 0, cmd_mark_all, X_UPDV},
1471 584ee48a 2021-12-13 op {'P', 0, cmd_paste_path, X_UPDV},
1472 a1bd5162 2021-12-01 op {'V', K_CTRL, cmd_scroll_down, X_UPDV},
1473 584ee48a 2021-12-13 op {'Y', 0, cmd_copy_path, X_UPDV},
1474 584ee48a 2021-12-13 op {'^', 0, cmd_cd_up, X_UPDV},
1475 584ee48a 2021-12-13 op {'b', 0, cmd_cd_up, X_UPDV},
1476 d244a655 2021-12-13 op {'e', 0, cmd_edit, X_UPDV},
1477 584ee48a 2021-12-13 op {'f', 0, cmd_cd_down, X_UPDV},
1478 6d283064 2021-12-01 op {'g', 0, cmd_jump_top, X_UPDV},
1479 a1bd5162 2021-12-01 op {'g', K_CTRL, NULL, X_UPDV},
1480 584ee48a 2021-12-13 op {'h', 0, cmd_cd_up, X_UPDV},
1481 a1bd5162 2021-12-01 op {'j', 0, cmd_down, X_UPDV},
1482 a1bd5162 2021-12-01 op {'k', 0, cmd_up, X_UPDV},
1483 584ee48a 2021-12-13 op {'l', 0, cmd_cd_down, X_UPDV},
1484 584ee48a 2021-12-13 op {'l', K_CTRL, cmd_reload, X_UPDV},
1485 a1bd5162 2021-12-01 op {'n', 0, cmd_down, X_UPDV},
1486 a1bd5162 2021-12-01 op {'n', K_CTRL, cmd_down, X_UPDV},
1487 2c5122bc 2021-12-13 op {'m', 0, cmd_mark, X_UPDV},
1488 e03d1219 2021-12-13 op {'m', K_CTRL, cmd_shell, X_UPDV},
1489 2c5122bc 2021-12-13 op {'o', 0, cmd_open, X_UPDV},
1490 a1bd5162 2021-12-01 op {'p', 0, cmd_up, X_UPDV},
1491 a1bd5162 2021-12-01 op {'p', K_CTRL, cmd_up, X_UPDV},
1492 a1bd5162 2021-12-01 op {'q', 0, NULL, X_QUIT},
1493 2c5122bc 2021-12-13 op {'t', 0, cmd_toggle_mark, X_UPDV},
1494 63fe4ed0 2021-12-13 op {'v', 0, cmd_view, X_UPDV},
1495 a1bd5162 2021-12-01 op {'v', K_META, cmd_scroll_up, X_UPDV},
1496 1a7d56bf 2021-12-12 op {KEY_DOWN, 0, cmd_scroll_down, X_UPDV},
1497 a1bd5162 2021-12-01 op {KEY_NPAGE, 0, cmd_scroll_down, X_UPDV},
1498 a1bd5162 2021-12-01 op {KEY_PPAGE, 0, cmd_scroll_up, X_UPDV},
1499 a1bd5162 2021-12-01 op {KEY_RESIZE, 0, NULL, X_UPDV},
1500 a1bd5162 2021-12-01 op {KEY_RESIZE, K_META, NULL, X_UPDV},
1501 1a7d56bf 2021-12-12 op {KEY_UP, 0, cmd_scroll_up, X_UPDV},
1508 a1bd5162 2021-12-01 op ch = fm_getch();
1509 a1bd5162 2021-12-01 op if (ch == '\e') {
1511 a1bd5162 2021-12-01 op if ((ch = fm_getch()) == '\e') {
1517 a1bd5162 2021-12-01 op clear_message();
1519 a1bd5162 2021-12-01 op for (i = 0; i < nitems(bindings); ++i) {
1520 a1bd5162 2021-12-01 op b = &bindings[i];
1522 a1bd5162 2021-12-01 op if (b->chflags & K_CTRL)
1523 a1bd5162 2021-12-01 op c = CTRL(c);
1524 a1bd5162 2021-12-01 op if ((!meta && b->chflags & K_META) || ch != c)
1527 a1bd5162 2021-12-01 op if (b->flags & X_QUIT)
1529 a1bd5162 2021-12-01 op if (b->fn != NULL)
1531 a1bd5162 2021-12-01 op if (b->flags & X_UPDV)
1532 a1bd5162 2021-12-01 op update_view();
1537 a1bd5162 2021-12-01 op message(RED, "%s%s is undefined",
1538 a1bd5162 2021-12-01 op meta ? "M-": "", keyname(ch));
1543 d248a263 2023-02-08 op static __dead void
1546 d248a263 2023-02-08 op fprintf(stderr, "usage: %s [-hv] [-d file] [-m file] [dirs ...]\n",
1547 d248a263 2023-02-08 op getprogname());
1548 d248a263 2023-02-08 op fprintf(stderr, "version: %s\n", RV_VERSION);
1553 c9f42136 2021-11-25 op main(int argc, char *argv[])
1556 c66e2de7 2021-11-25 op char *entry;
1558 c66e2de7 2021-11-25 op FILE *save_cwd_file = NULL;
1559 c66e2de7 2021-11-25 op FILE *save_marks_file = NULL;
1561 5d347732 2023-02-08 op if (pledge("stdio rpath wpath cpath tty proc exec", NULL) == -1)
1562 5d347732 2023-02-08 op err(1, "pledge");
1564 cc9de337 2021-11-26 op while ((ch = getopt_long(argc, argv, "d:hm:v", opts, NULL)) != -1) {
1565 cc9de337 2021-11-26 op switch (ch) {
1567 cc9de337 2021-11-26 op if ((save_cwd_file = fopen(optarg, "w")) == NULL)
1568 cc9de337 2021-11-26 op err(1, "open %s", optarg);
1574 cc9de337 2021-11-26 op if ((save_marks_file = fopen(optarg, "a")) == NULL)
1575 cc9de337 2021-11-26 op err(1, "open %s", optarg);
1578 cc9de337 2021-11-26 op printf("version: fm %s\n", RV_VERSION);
1585 c66e2de7 2021-11-25 op get_user_programs();
1586 c66e2de7 2021-11-25 op init_term();
1587 c3e1e821 2021-11-26 op fm.nfiles = 0;
1588 c66e2de7 2021-11-25 op for (i = 0; i < 10; i++) {
1589 c3e1e821 2021-11-26 op fm.tabs[i].esel = fm.tabs[i].scroll = 0;
1590 c3e1e821 2021-11-26 op fm.tabs[i].flags = RV_FLAGS;
1592 acf1203a 2023-02-08 op strlcpy(fm.tabs[0].cwd, getenv("HOME"), sizeof(fm.tabs[0].cwd));
1593 c66e2de7 2021-11-25 op for (i = 1; i < argc && i < 10; i++) {
1594 c66e2de7 2021-11-25 op if ((d = opendir(argv[i]))) {
1595 c3e1e821 2021-11-26 op realpath(argv[i], fm.tabs[i].cwd);
1596 c66e2de7 2021-11-25 op closedir(d);
1598 acf1203a 2023-02-08 op strlcpy(fm.tabs[i].cwd, fm.tabs[0].cwd,
1599 acf1203a 2023-02-08 op sizeof(fm.tabs[i].cwd));
1602 c3e1e821 2021-11-26 op getcwd(fm.tabs[i].cwd, PATH_MAX);
1603 c66e2de7 2021-11-25 op for (i++; i < 10; i++)
1604 acf1203a 2023-02-08 op strlcpy(fm.tabs[i].cwd, fm.tabs[i - 1].cwd,
1605 acf1203a 2023-02-08 op sizeof(fm.tabs[i].cwd));
1606 c66e2de7 2021-11-25 op for (i = 0; i < 10; i++)
1607 c3e1e821 2021-11-26 op if (fm.tabs[i].cwd[strlen(fm.tabs[i].cwd) - 1] != '/')
1608 acf1203a 2023-02-08 op strlcat(fm.tabs[i].cwd, "/", sizeof(fm.tabs[i].cwd));
1610 c3e1e821 2021-11-26 op fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1611 c3e1e821 2021-11-26 op init_marks(&fm.marks);
1613 01a2cff6 2021-12-12 op strlcpy(clipboard, CWD, sizeof(clipboard));
1614 c3e1e821 2021-11-26 op if (fm.nfiles > 0)
1615 01a2cff6 2021-12-12 op strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1619 c3e1e821 2021-11-26 op if (fm.nfiles)
1620 c3e1e821 2021-11-26 op free_rows(&fm.rows, fm.nfiles);
1621 c3e1e821 2021-11-26 op delwin(fm.window);
1622 c66e2de7 2021-11-25 op if (save_cwd_file != NULL) {
1623 c66e2de7 2021-11-25 op fputs(CWD, save_cwd_file);
1624 c66e2de7 2021-11-25 op fclose(save_cwd_file);
1626 c66e2de7 2021-11-25 op if (save_marks_file != NULL) {
1627 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
1628 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
1630 c66e2de7 2021-11-25 op fprintf(save_marks_file, "%s%s\n",
1631 c3e1e821 2021-11-26 op fm.marks.dirpath, entry);
1633 c66e2de7 2021-11-25 op fclose(save_marks_file);
1635 c3e1e821 2021-11-26 op free_marks(&fm.marks);