Blob


1 #define _XOPEN_SOURCE 700
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _FILE_OFFSET_BITS 64
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <ctype.h>
8 #include <wchar.h>
9 #include <wctype.h>
10 #include <string.h>
11 #include <sys/types.h> /* pid_t, ... */
12 #include <stdio.h>
13 #include <limits.h> /* PATH_MAX */
14 #include <locale.h> /* setlocale(), LC_ALL */
15 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
16 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
17 #include <libgen.h>
18 #include <sys/stat.h>
19 #include <fcntl.h> /* open() */
20 #include <sys/wait.h> /* waitpid() */
21 #include <signal.h> /* struct sigaction, sigaction() */
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <curses.h>
26 #include "config.h"
28 /* This signal is not defined by POSIX, but should be
29 present on all systems that have resizable terminals. */
30 #ifndef SIGWINCH
31 #define SIGWINCH 28
32 #endif
34 /* String buffers. */
35 #define BUFLEN PATH_MAX
36 static char BUF1[BUFLEN];
37 static char BUF2[BUFLEN];
38 static char INPUT[BUFLEN];
39 static char CLIPBOARD[BUFLEN];
40 static wchar_t WBUF[BUFLEN];
42 /* Paths to external programs. */
43 static char *user_shell;
44 static char *user_pager;
45 static char *user_editor;
46 static char *user_open;
48 /* Listing view parameters. */
49 #define HEIGHT (LINES-4)
50 #define STATUSPOS (COLS-16)
52 /* Listing view flags. */
53 #define SHOW_FILES 0x01u
54 #define SHOW_DIRS 0x02u
55 #define SHOW_HIDDEN 0x04u
57 /* Marks parameters. */
58 #define BULK_INIT 5
59 #define BULK_THRESH 256
61 /* Information associated to each entry in listing. */
62 typedef struct Row {
63 char *name;
64 off_t size;
65 mode_t mode;
66 int islink;
67 int marked;
68 } Row;
70 /* Dynamic array of marked entries. */
71 typedef struct Marks {
72 char dirpath[PATH_MAX];
73 int bulk;
74 int nentries;
75 char **entries;
76 } Marks;
78 /* Line editing state. */
79 typedef struct Edit {
80 wchar_t buffer[BUFLEN+1];
81 int left, right;
82 } Edit;
84 /* Each tab only stores the following information. */
85 typedef struct Tab {
86 int scroll;
87 int esel;
88 uint8_t flags;
89 char cwd[PATH_MAX];
90 } Tab;
92 typedef struct Prog {
93 off_t partial;
94 off_t total;
95 const char *msg;
96 } Prog;
98 /* Global state. */
99 static struct Rover {
100 int tab;
101 int nfiles;
102 Row *rows;
103 WINDOW *window;
104 Marks marks;
105 Edit edit;
106 int edit_scroll;
107 volatile sig_atomic_t pending_usr1;
108 volatile sig_atomic_t pending_winch;
109 Prog prog;
110 Tab tabs[10];
111 } rover;
113 /* Macros for accessing global state. */
114 #define ENAME(I) rover.rows[I].name
115 #define ESIZE(I) rover.rows[I].size
116 #define EMODE(I) rover.rows[I].mode
117 #define ISLINK(I) rover.rows[I].islink
118 #define MARKED(I) rover.rows[I].marked
119 #define SCROLL rover.tabs[rover.tab].scroll
120 #define ESEL rover.tabs[rover.tab].esel
121 #define FLAGS rover.tabs[rover.tab].flags
122 #define CWD rover.tabs[rover.tab].cwd
124 /* Helpers. */
125 #define MIN(A, B) ((A) < (B) ? (A) : (B))
126 #define MAX(A, B) ((A) > (B) ? (A) : (B))
127 #define ISDIR(E) (strchr((E), '/') != NULL)
129 /* Line Editing Macros. */
130 #define EDIT_FULL(E) ((E).left == (E).right)
131 #define EDIT_CAN_LEFT(E) ((E).left)
132 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
133 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
134 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
135 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
136 #define EDIT_BACKSPACE(E) (E).left--
137 #define EDIT_DELETE(E) (E).right++
138 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
140 typedef enum EditStat {CONTINUE, CONFIRM, CANCEL} EditStat;
141 typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK} Color;
142 typedef int (*PROCESS)(const char *path);
144 static void
145 init_marks(Marks *marks)
147 strcpy(marks->dirpath, "");
148 marks->bulk = BULK_INIT;
149 marks->nentries = 0;
150 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
153 /* Unmark all entries. */
154 static void
155 mark_none(Marks *marks)
157 int i;
159 strcpy(marks->dirpath, "");
160 for (i = 0; i < marks->bulk && marks->nentries; i++)
161 if (marks->entries[i]) {
162 free(marks->entries[i]);
163 marks->entries[i] = NULL;
164 marks->nentries--;
166 if (marks->bulk > BULK_THRESH) {
167 /* Reset bulk to free some memory. */
168 free(marks->entries);
169 marks->bulk = BULK_INIT;
170 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
174 static void
175 add_mark(Marks *marks, char *dirpath, char *entry)
177 int i;
179 if (!strcmp(marks->dirpath, dirpath)) {
180 /* Append mark to directory. */
181 if (marks->nentries == marks->bulk) {
182 /* Expand bulk to accomodate new entry. */
183 int extra = marks->bulk / 2;
184 marks->bulk += extra; /* bulk *= 1.5; */
185 marks->entries = realloc(marks->entries,
186 marks->bulk * sizeof *marks->entries);
187 memset(&marks->entries[marks->nentries], 0,
188 extra * sizeof *marks->entries);
189 i = marks->nentries;
190 } else {
191 /* Search for empty slot (there must be one). */
192 for (i = 0; i < marks->bulk; i++)
193 if (!marks->entries[i])
194 break;
196 } else {
197 /* Directory changed. Discard old marks. */
198 mark_none(marks);
199 strcpy(marks->dirpath, dirpath);
200 i = 0;
202 marks->entries[i] = malloc(strlen(entry) + 1);
203 strcpy(marks->entries[i], entry);
204 marks->nentries++;
207 static void
208 del_mark(Marks *marks, char *entry)
210 int i;
212 if (marks->nentries > 1) {
213 for (i = 0; i < marks->bulk; i++)
214 if (marks->entries[i] && !strcmp(marks->entries[i], entry))
215 break;
216 free(marks->entries[i]);
217 marks->entries[i] = NULL;
218 marks->nentries--;
219 } else
220 mark_none(marks);
223 static void
224 free_marks(Marks *marks)
226 int i;
228 for (i = 0; i < marks->bulk && marks->nentries; i++)
229 if (marks->entries[i]) {
230 free(marks->entries[i]);
231 marks->nentries--;
233 free(marks->entries);
236 static void
237 handle_usr1(int sig)
239 rover.pending_usr1 = 1;
242 static void
243 handle_winch(int sig)
245 rover.pending_winch = 1;
248 static void
249 enable_handlers()
251 struct sigaction sa;
253 memset(&sa, 0, sizeof (struct sigaction));
254 sa.sa_handler = handle_usr1;
255 sigaction(SIGUSR1, &sa, NULL);
256 sa.sa_handler = handle_winch;
257 sigaction(SIGWINCH, &sa, NULL);
260 static void
261 disable_handlers()
263 struct sigaction sa;
265 memset(&sa, 0, sizeof (struct sigaction));
266 sa.sa_handler = SIG_DFL;
267 sigaction(SIGUSR1, &sa, NULL);
268 sigaction(SIGWINCH, &sa, NULL);
271 static void reload();
272 static void update_view();
274 /* Handle any signals received since last call. */
275 static void
276 sync_signals()
278 if (rover.pending_usr1) {
279 /* SIGUSR1 received: refresh directory listing. */
280 reload();
281 rover.pending_usr1 = 0;
283 if (rover.pending_winch) {
284 /* SIGWINCH received: resize application accordingly. */
285 delwin(rover.window);
286 endwin();
287 refresh();
288 clear();
289 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
290 if (HEIGHT < rover.nfiles && SCROLL + HEIGHT > rover.nfiles)
291 SCROLL = ESEL - HEIGHT;
292 update_view();
293 rover.pending_winch = 0;
297 /* This function must be used in place of getch().
298 It handles signals while waiting for user input. */
299 static int
300 rover_getch()
302 int ch;
304 while ((ch = getch()) == ERR)
305 sync_signals();
306 return ch;
309 /* This function must be used in place of get_wch().
310 It handles signals while waiting for user input. */
311 static int
312 rover_get_wch(wint_t *wch)
314 wint_t ret;
316 while ((ret = get_wch(wch)) == (wint_t) ERR)
317 sync_signals();
318 return ret;
321 /* Get user programs from the environment. */
323 #define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
324 dst = getenv(#src);
326 static void
327 get_user_programs()
329 ROVER_ENV(user_shell, SHELL)
330 ROVER_ENV(user_pager, PAGER)
331 ROVER_ENV(user_editor, EDITOR)
332 ROVER_ENV(user_open, OPEN)
335 /* Do a fork-exec to external program (e.g. $EDITOR). */
336 static void
337 spawn(char **args)
339 pid_t pid;
340 int status;
342 setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
343 pid = fork();
344 if (pid > 0) {
345 /* fork() succeeded. */
346 disable_handlers();
347 endwin();
348 waitpid(pid, &status, 0);
349 enable_handlers();
350 kill(getpid(), SIGWINCH);
351 } else if (pid == 0) {
352 /* Child process. */
353 execvp(args[0], args);
357 static void
358 shell_escaped_cat(char *buf, char *str, size_t n)
360 char *p = buf + strlen(buf);
361 *p++ = '\'';
362 for (n--; n; n--, str++) {
363 switch (*str) {
364 case '\'':
365 if (n < 4)
366 goto done;
367 strcpy(p, "'\\''");
368 n -= 4;
369 p += 4;
370 break;
371 case '\0':
372 goto done;
373 default:
374 *p = *str;
375 p++;
378 done:
379 strncat(p, "'", n);
382 static int
383 open_with_env(char *program, char *path)
385 if (program) {
386 #ifdef RV_SHELL
387 strncpy(BUF1, program, BUFLEN - 1);
388 strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
389 shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
390 spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
391 #else
392 spawn((char *[]) {program, path, NULL});
393 #endif
394 return 1;
396 return 0;
399 /* Curses setup. */
400 static void
401 init_term()
403 setlocale(LC_ALL, "");
404 initscr();
405 cbreak(); /* Get one character at a time. */
406 timeout(100); /* For getch(). */
407 noecho();
408 nonl(); /* No NL->CR/NL on output. */
409 intrflush(stdscr, FALSE);
410 keypad(stdscr, TRUE);
411 curs_set(FALSE); /* Hide blinking cursor. */
412 if (has_colors()) {
413 short bg;
414 start_color();
415 #ifdef NCURSES_EXT_FUNCS
416 use_default_colors();
417 bg = -1;
418 #else
419 bg = COLOR_BLACK;
420 #endif
421 init_pair(RED, COLOR_RED, bg);
422 init_pair(GREEN, COLOR_GREEN, bg);
423 init_pair(YELLOW, COLOR_YELLOW, bg);
424 init_pair(BLUE, COLOR_BLUE, bg);
425 init_pair(CYAN, COLOR_CYAN, bg);
426 init_pair(MAGENTA, COLOR_MAGENTA, bg);
427 init_pair(WHITE, COLOR_WHITE, bg);
428 init_pair(BLACK, COLOR_BLACK, bg);
430 atexit((void (*)(void)) endwin);
431 enable_handlers();
434 /* Update the listing view. */
435 static void
436 update_view()
438 int i, j;
439 int numsize;
440 int ishidden;
441 int marking;
443 mvhline(0, 0, ' ', COLS);
444 attr_on(A_BOLD, NULL);
445 color_set(RVC_TABNUM, NULL);
446 mvaddch(0, COLS - 2, rover.tab + '0');
447 attr_off(A_BOLD, NULL);
448 if (rover.marks.nentries) {
449 numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
450 color_set(RVC_MARKS, NULL);
451 mvaddstr(0, COLS - 3 - numsize, BUF1);
452 } else
453 numsize = -1;
454 color_set(RVC_CWD, NULL);
455 mbstowcs(WBUF, CWD, PATH_MAX);
456 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
457 wcolor_set(rover.window, RVC_BORDER, NULL);
458 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
459 ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
460 /* Selection might not be visible, due to cursor wrapping or window
461 shrinking. In that case, the scroll must be moved to make it visible. */
462 if (rover.nfiles > HEIGHT) {
463 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
464 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
465 } else
466 SCROLL = 0;
467 marking = !strcmp(CWD, rover.marks.dirpath);
468 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
469 ishidden = ENAME(j)[0] == '.';
470 if (j == ESEL)
471 wattr_on(rover.window, A_REVERSE, NULL);
472 if (ISLINK(j))
473 wcolor_set(rover.window, RVC_LINK, NULL);
474 else if (ishidden)
475 wcolor_set(rover.window, RVC_HIDDEN, NULL);
476 else if (S_ISREG(EMODE(j))) {
477 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
478 wcolor_set(rover.window, RVC_EXEC, NULL);
479 else
480 wcolor_set(rover.window, RVC_REG, NULL);
481 } else if (S_ISDIR(EMODE(j)))
482 wcolor_set(rover.window, RVC_DIR, NULL);
483 else if (S_ISCHR(EMODE(j)))
484 wcolor_set(rover.window, RVC_CHR, NULL);
485 else if (S_ISBLK(EMODE(j)))
486 wcolor_set(rover.window, RVC_BLK, NULL);
487 else if (S_ISFIFO(EMODE(j)))
488 wcolor_set(rover.window, RVC_FIFO, NULL);
489 else if (S_ISSOCK(EMODE(j)))
490 wcolor_set(rover.window, RVC_SOCK, NULL);
491 if (S_ISDIR(EMODE(j))) {
492 mbstowcs(WBUF, ENAME(j), PATH_MAX);
493 if (ISLINK(j))
494 wcscat(WBUF, L"/");
495 } else {
496 char *suffix, *suffixes = "BKMGTPEZY";
497 off_t human_size = ESIZE(j) * 10;
498 int length = mbstowcs(NULL, ENAME(j), 0);
499 for (suffix = suffixes; human_size >= 10240; suffix++)
500 human_size = (human_size + 512) / 1024;
501 if (*suffix == 'B')
502 swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
503 (int) (COLS - length - 6),
504 (int) human_size / 10, *suffix);
505 else
506 swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
507 (int) (COLS - length - 8),
508 (int) human_size / 10, (int) human_size % 10, *suffix);
510 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
511 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
512 if (marking && MARKED(j)) {
513 wcolor_set(rover.window, RVC_MARKS, NULL);
514 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
515 } else
516 mvwaddch(rover.window, i + 1, 1, ' ');
517 if (j == ESEL)
518 wattr_off(rover.window, A_REVERSE, NULL);
520 for (; i < HEIGHT; i++)
521 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
522 if (rover.nfiles > HEIGHT) {
523 int center, height;
524 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
525 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
526 if (!height) height = 1;
527 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
528 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
530 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
531 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
532 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
533 if (!rover.nfiles)
534 strcpy(BUF2, "0/0");
535 else
536 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
537 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
538 color_set(RVC_STATUS, NULL);
539 mvaddstr(LINES - 1, STATUSPOS, BUF1);
540 wrefresh(rover.window);
543 /* Show a message on the status bar. */
544 static void
545 message(Color color, char *fmt, ...)
547 int len, pos;
548 va_list args;
550 va_start(args, fmt);
551 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
552 va_end(args);
553 len = strlen(BUF1);
554 pos = (STATUSPOS - len) / 2;
555 attr_on(A_BOLD, NULL);
556 color_set(color, NULL);
557 mvaddstr(LINES - 1, pos, BUF1);
558 color_set(DEFAULT, NULL);
559 attr_off(A_BOLD, NULL);
562 /* Clear message area, leaving only status info. */
563 static void
564 clear_message()
566 mvhline(LINES - 1, 0, ' ', STATUSPOS);
569 /* Comparison used to sort listing entries. */
570 static int
571 rowcmp(const void *a, const void *b)
573 int isdir1, isdir2, cmpdir;
574 const Row *r1 = a;
575 const Row *r2 = b;
576 isdir1 = S_ISDIR(r1->mode);
577 isdir2 = S_ISDIR(r2->mode);
578 cmpdir = isdir2 - isdir1;
579 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
582 /* Get all entries in current working directory. */
583 static int
584 ls(Row **rowsp, uint8_t flags)
586 DIR *dp;
587 struct dirent *ep;
588 struct stat statbuf;
589 Row *rows;
590 int i, n;
592 if(!(dp = opendir("."))) return -1;
593 n = -2; /* We don't want the entries "." and "..". */
594 while (readdir(dp)) n++;
595 rewinddir(dp);
596 rows = malloc(n * sizeof *rows);
597 i = 0;
598 while ((ep = readdir(dp))) {
599 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
600 continue;
601 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
602 continue;
603 lstat(ep->d_name, &statbuf);
604 rows[i].islink = S_ISLNK(statbuf.st_mode);
605 stat(ep->d_name, &statbuf);
606 if (S_ISDIR(statbuf.st_mode)) {
607 if (flags & SHOW_DIRS) {
608 rows[i].name = malloc(strlen(ep->d_name) + 2);
609 strcpy(rows[i].name, ep->d_name);
610 if (!rows[i].islink)
611 strcat(rows[i].name, "/");
612 rows[i].mode = statbuf.st_mode;
613 i++;
615 } else if (flags & SHOW_FILES) {
616 rows[i].name = malloc(strlen(ep->d_name) + 1);
617 strcpy(rows[i].name, ep->d_name);
618 rows[i].size = statbuf.st_size;
619 rows[i].mode = statbuf.st_mode;
620 i++;
623 n = i; /* Ignore unused space in array caused by filters. */
624 qsort(rows, n, sizeof (*rows), rowcmp);
625 closedir(dp);
626 *rowsp = rows;
627 return n;
630 static void
631 free_rows(Row **rowsp, int nfiles)
633 int i;
635 for (i = 0; i < nfiles; i++)
636 free((*rowsp)[i].name);
637 free(*rowsp);
638 *rowsp = NULL;
641 /* Change working directory to the path in CWD. */
642 static void
643 cd(int reset)
645 int i, j;
647 message(CYAN, "Loading \"%s\"...", CWD);
648 refresh();
649 if (chdir(CWD) == -1) {
650 getcwd(CWD, PATH_MAX-1);
651 if (CWD[strlen(CWD)-1] != '/')
652 strcat(CWD, "/");
653 goto done;
655 if (reset) ESEL = SCROLL = 0;
656 if (rover.nfiles)
657 free_rows(&rover.rows, rover.nfiles);
658 rover.nfiles = ls(&rover.rows, FLAGS);
659 if (!strcmp(CWD, rover.marks.dirpath)) {
660 for (i = 0; i < rover.nfiles; i++) {
661 for (j = 0; j < rover.marks.bulk; j++)
662 if (
663 rover.marks.entries[j] &&
664 !strcmp(rover.marks.entries[j], ENAME(i))
666 break;
667 MARKED(i) = j < rover.marks.bulk;
669 } else
670 for (i = 0; i < rover.nfiles; i++)
671 MARKED(i) = 0;
672 done:
673 clear_message();
674 update_view();
677 /* Select a target entry, if it is present. */
678 static void
679 try_to_sel(const char *target)
681 ESEL = 0;
682 if (!ISDIR(target))
683 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
684 ESEL++;
685 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
686 ESEL++;
689 /* Reload CWD, but try to keep selection. */
690 static void
691 reload()
693 if (rover.nfiles) {
694 strcpy(INPUT, ENAME(ESEL));
695 cd(0);
696 try_to_sel(INPUT);
697 update_view();
698 } else
699 cd(1);
702 static off_t
703 count_dir(const char *path)
705 DIR *dp;
706 struct dirent *ep;
707 struct stat statbuf;
708 char subpath[PATH_MAX];
709 off_t total;
711 if(!(dp = opendir(path))) return 0;
712 total = 0;
713 while ((ep = readdir(dp))) {
714 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
715 continue;
716 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
717 lstat(subpath, &statbuf);
718 if (S_ISDIR(statbuf.st_mode)) {
719 strcat(subpath, "/");
720 total += count_dir(subpath);
721 } else
722 total += statbuf.st_size;
724 closedir(dp);
725 return total;
728 static off_t
729 count_marked()
731 int i;
732 char *entry;
733 off_t total;
734 struct stat statbuf;
736 total = 0;
737 chdir(rover.marks.dirpath);
738 for (i = 0; i < rover.marks.bulk; i++) {
739 entry = rover.marks.entries[i];
740 if (entry) {
741 if (ISDIR(entry)) {
742 total += count_dir(entry);
743 } else {
744 lstat(entry, &statbuf);
745 total += statbuf.st_size;
749 chdir(CWD);
750 return total;
753 /* Recursively process a source directory using CWD as destination root.
754 For each node (i.e. directory), do the following:
755 1. call pre(destination);
756 2. call proc() on every child leaf (i.e. files);
757 3. recurse into every child node;
758 4. call pos(source).
759 E.g. to move directory /src/ (and all its contents) inside /dst/:
760 strcpy(CWD, "/dst/");
761 process_dir(adddir, movfile, deldir, "/src/"); */
762 static int
763 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
765 int ret;
766 DIR *dp;
767 struct dirent *ep;
768 struct stat statbuf;
769 char subpath[PATH_MAX];
771 ret = 0;
772 if (pre) {
773 char dstpath[PATH_MAX];
774 strcpy(dstpath, CWD);
775 strcat(dstpath, path + strlen(rover.marks.dirpath));
776 ret |= pre(dstpath);
778 if(!(dp = opendir(path))) return -1;
779 while ((ep = readdir(dp))) {
780 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
781 continue;
782 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
783 lstat(subpath, &statbuf);
784 if (S_ISDIR(statbuf.st_mode)) {
785 strcat(subpath, "/");
786 ret |= process_dir(pre, proc, pos, subpath);
787 } else
788 ret |= proc(subpath);
790 closedir(dp);
791 if (pos) ret |= pos(path);
792 return ret;
795 /* Process all marked entries using CWD as destination root.
796 All marked entries that are directories will be recursively processed.
797 See process_dir() for details on the parameters. */
798 static void
799 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
800 const char *msg_doing, const char *msg_done)
802 int i, ret;
803 char *entry;
804 char path[PATH_MAX];
806 clear_message();
807 message(CYAN, "%s...", msg_doing);
808 refresh();
809 rover.prog = (Prog) {0, count_marked(), msg_doing};
810 for (i = 0; i < rover.marks.bulk; i++) {
811 entry = rover.marks.entries[i];
812 if (entry) {
813 ret = 0;
814 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
815 if (ISDIR(entry)) {
816 if (!strncmp(path, CWD, strlen(path)))
817 ret = -1;
818 else
819 ret = process_dir(pre, proc, pos, path);
820 } else
821 ret = proc(path);
822 if (!ret) {
823 del_mark(&rover.marks, entry);
824 reload();
828 rover.prog.total = 0;
829 reload();
830 if (!rover.marks.nentries)
831 message(GREEN, "%s all marked entries.", msg_done);
832 else
833 message(RED, "Some errors occured while %s.", msg_doing);
834 RV_ALERT();
837 static void
838 update_progress(off_t delta)
840 int percent;
842 if (!rover.prog.total) return;
843 rover.prog.partial += delta;
844 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
845 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
846 refresh();
849 /* Wrappers for file operations. */
850 static int delfile(const char *path) {
851 int ret;
852 struct stat st;
854 ret = lstat(path, &st);
855 if (ret < 0) return ret;
856 update_progress(st.st_size);
857 return unlink(path);
859 static PROCESS deldir = rmdir;
860 static int addfile(const char *path) {
861 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
862 int ret;
864 ret = creat(path, 0644);
865 if (ret < 0) return ret;
866 return close(ret);
868 static int cpyfile(const char *srcpath) {
869 int src, dst, ret;
870 size_t size;
871 struct stat st;
872 char buf[BUFSIZ];
873 char dstpath[PATH_MAX];
875 strcpy(dstpath, CWD);
876 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
877 ret = lstat(srcpath, &st);
878 if (ret < 0) return ret;
879 if (S_ISLNK(st.st_mode)) {
880 ret = readlink(srcpath, BUF1, BUFLEN-1);
881 if (ret < 0) return ret;
882 BUF1[ret] = '\0';
883 ret = symlink(BUF1, dstpath);
884 } else {
885 ret = src = open(srcpath, O_RDONLY);
886 if (ret < 0) return ret;
887 ret = dst = creat(dstpath, st.st_mode);
888 if (ret < 0) return ret;
889 while ((size = read(src, buf, BUFSIZ)) > 0) {
890 write(dst, buf, size);
891 update_progress(size);
892 sync_signals();
894 close(src);
895 close(dst);
896 ret = 0;
898 return ret;
900 static int adddir(const char *path) {
901 int ret;
902 struct stat st;
904 ret = stat(CWD, &st);
905 if (ret < 0) return ret;
906 return mkdir(path, st.st_mode);
908 static int movfile(const char *srcpath) {
909 int ret;
910 struct stat st;
911 char dstpath[PATH_MAX];
913 strcpy(dstpath, CWD);
914 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
915 ret = rename(srcpath, dstpath);
916 if (ret == 0) {
917 ret = lstat(dstpath, &st);
918 if (ret < 0) return ret;
919 update_progress(st.st_size);
920 } else if (errno == EXDEV) {
921 ret = cpyfile(srcpath);
922 if (ret < 0) return ret;
923 ret = unlink(srcpath);
925 return ret;
928 static void
929 start_line_edit(const char *init_input)
931 curs_set(TRUE);
932 strncpy(INPUT, init_input, BUFLEN);
933 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
934 rover.edit.right = BUFLEN - 1;
935 rover.edit.buffer[BUFLEN] = L'\0';
936 rover.edit_scroll = 0;
939 /* Read input and change editing state accordingly. */
940 static EditStat
941 get_line_edit()
943 wchar_t eraser, killer, wch;
944 int ret, length;
946 ret = rover_get_wch((wint_t *) &wch);
947 erasewchar(&eraser);
948 killwchar(&killer);
949 if (ret == KEY_CODE_YES) {
950 if (wch == KEY_ENTER) {
951 curs_set(FALSE);
952 return CONFIRM;
953 } else if (wch == KEY_LEFT) {
954 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
955 } else if (wch == KEY_RIGHT) {
956 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
957 } else if (wch == KEY_UP) {
958 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
959 } else if (wch == KEY_DOWN) {
960 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
961 } else if (wch == KEY_BACKSPACE) {
962 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
963 } else if (wch == KEY_DC) {
964 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
966 } else {
967 if (wch == L'\r' || wch == L'\n') {
968 curs_set(FALSE);
969 return CONFIRM;
970 } else if (wch == L'\t') {
971 curs_set(FALSE);
972 return CANCEL;
973 } else if (wch == eraser) {
974 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
975 } else if (wch == killer) {
976 EDIT_CLEAR(rover.edit);
977 clear_message();
978 } else if (iswprint(wch)) {
979 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
982 /* Encode edit contents in INPUT. */
983 rover.edit.buffer[rover.edit.left] = L'\0';
984 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
985 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
986 BUFLEN-length);
987 return CONTINUE;
990 /* Update line input on the screen. */
991 static void
992 update_input(const char *prompt, Color color)
994 int plen, ilen, maxlen;
996 plen = strlen(prompt);
997 ilen = mbstowcs(NULL, INPUT, 0);
998 maxlen = STATUSPOS - plen - 2;
999 if (ilen - rover.edit_scroll < maxlen)
1000 rover.edit_scroll = MAX(ilen - maxlen, 0);
1001 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
1002 rover.edit_scroll = rover.edit.left - maxlen;
1003 else if (rover.edit.left < rover.edit_scroll)
1004 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
1005 color_set(RVC_PROMPT, NULL);
1006 mvaddstr(LINES - 1, 0, prompt);
1007 color_set(color, NULL);
1008 mbstowcs(WBUF, INPUT, COLS);
1009 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
1010 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
1011 color_set(DEFAULT, NULL);
1012 if (rover.edit_scroll)
1013 mvaddch(LINES - 1, plen - 1, '<');
1014 if (ilen > rover.edit_scroll + maxlen)
1015 mvaddch(LINES - 1, plen + maxlen, '>');
1016 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
1019 int
1020 main(int argc, char *argv[])
1022 int i, ch;
1023 char *program;
1024 char *entry;
1025 const char *key;
1026 const char *clip_path;
1027 DIR *d;
1028 EditStat edit_stat;
1029 FILE *save_cwd_file = NULL;
1030 FILE *save_marks_file = NULL;
1031 FILE *clip_file;
1033 if (argc >= 2) {
1034 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
1035 printf("rover %s\n", RV_VERSION);
1036 return 0;
1037 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1038 printf(
1039 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
1040 " Browse current directory or the ones specified.\n\n"
1041 " or: rover -h|--help\n"
1042 " Print this help message and exit.\n\n"
1043 " or: rover -v|--version\n"
1044 " Print program version and exit.\n\n"
1045 "See rover(1) for more information.\n"
1046 "Rover homepage: <https://github.com/lecram/rover>.\n"
1048 return 0;
1049 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
1050 if (argc > 2) {
1051 save_cwd_file = fopen(argv[2], "w");
1052 argc -= 2; argv += 2;
1053 } else {
1054 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1055 return 1;
1057 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
1058 if (argc > 2) {
1059 save_marks_file = fopen(argv[2], "a");
1060 argc -= 2; argv += 2;
1061 } else {
1062 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1063 return 1;
1067 get_user_programs();
1068 init_term();
1069 rover.nfiles = 0;
1070 for (i = 0; i < 10; i++) {
1071 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1072 rover.tabs[i].flags = RV_FLAGS;
1074 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1075 for (i = 1; i < argc && i < 10; i++) {
1076 if ((d = opendir(argv[i]))) {
1077 realpath(argv[i], rover.tabs[i].cwd);
1078 closedir(d);
1079 } else
1080 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1082 getcwd(rover.tabs[i].cwd, PATH_MAX);
1083 for (i++; i < 10; i++)
1084 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1085 for (i = 0; i < 10; i++)
1086 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1087 strcat(rover.tabs[i].cwd, "/");
1088 rover.tab = 1;
1089 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1090 init_marks(&rover.marks);
1091 cd(1);
1092 strcpy(CLIPBOARD, CWD);
1093 strcat(CLIPBOARD, ENAME(ESEL));
1094 while (1) {
1095 ch = rover_getch();
1096 key = keyname(ch);
1097 clear_message();
1098 if (!strcmp(key, RVK_QUIT)) break;
1099 else if (ch >= '0' && ch <= '9') {
1100 rover.tab = ch - '0';
1101 cd(0);
1102 } else if (!strcmp(key, RVK_HELP)) {
1103 spawn((char *[]) {"man", "rover", NULL});
1104 } else if (!strcmp(key, RVK_DOWN)) {
1105 if (!rover.nfiles) continue;
1106 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1107 update_view();
1108 } else if (!strcmp(key, RVK_UP)) {
1109 if (!rover.nfiles) continue;
1110 ESEL = MAX(ESEL - 1, 0);
1111 update_view();
1112 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1113 if (!rover.nfiles) continue;
1114 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1115 if (rover.nfiles > HEIGHT)
1116 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1117 update_view();
1118 } else if (!strcmp(key, RVK_JUMP_UP)) {
1119 if (!rover.nfiles) continue;
1120 ESEL = MAX(ESEL - RV_JUMP, 0);
1121 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1122 update_view();
1123 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1124 if (!rover.nfiles) continue;
1125 ESEL = 0;
1126 update_view();
1127 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1128 if (!rover.nfiles) continue;
1129 ESEL = rover.nfiles - 1;
1130 update_view();
1131 } else if (!strcmp(key, RVK_CD_DOWN)) {
1132 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1133 if (chdir(ENAME(ESEL)) == -1) {
1134 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1135 continue;
1137 strcat(CWD, ENAME(ESEL));
1138 cd(1);
1139 } else if (!strcmp(key, RVK_CD_UP)) {
1140 char *dirname, first;
1141 if (!strcmp(CWD, "/")) continue;
1142 CWD[strlen(CWD) - 1] = '\0';
1143 dirname = strrchr(CWD, '/') + 1;
1144 first = dirname[0];
1145 dirname[0] = '\0';
1146 cd(1);
1147 dirname[0] = first;
1148 dirname[strlen(dirname)] = '/';
1149 try_to_sel(dirname);
1150 dirname[0] = '\0';
1151 if (rover.nfiles > HEIGHT)
1152 SCROLL = ESEL - HEIGHT / 2;
1153 update_view();
1154 } else if (!strcmp(key, RVK_HOME)) {
1155 strcpy(CWD, getenv("HOME"));
1156 if (CWD[strlen(CWD) - 1] != '/')
1157 strcat(CWD, "/");
1158 cd(1);
1159 } else if (!strcmp(key, RVK_TARGET)) {
1160 char *bname, first;
1161 int is_dir = S_ISDIR(EMODE(ESEL));
1162 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1163 if (len == -1) continue;
1164 BUF1[len] = '\0';
1165 if (access(BUF1, F_OK) == -1) {
1166 char *msg;
1167 switch (errno) {
1168 case EACCES:
1169 msg = "Cannot access \"%s\".";
1170 break;
1171 case ENOENT:
1172 msg = "\"%s\" does not exist.";
1173 break;
1174 default:
1175 msg = "Cannot navigate to \"%s\".";
1177 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1178 message(RED, msg, BUF2);
1179 continue;
1181 realpath(BUF1, CWD);
1182 len = strlen(CWD);
1183 if (CWD[len - 1] == '/')
1184 CWD[len - 1] = '\0';
1185 bname = strrchr(CWD, '/') + 1;
1186 first = *bname;
1187 *bname = '\0';
1188 cd(1);
1189 *bname = first;
1190 if (is_dir)
1191 strcat(CWD, "/");
1192 try_to_sel(bname);
1193 *bname = '\0';
1194 update_view();
1195 } else if (!strcmp(key, RVK_COPY_PATH)) {
1196 clip_path = getenv("CLIP");
1197 if (!clip_path) goto copy_path_fail;
1198 clip_file = fopen(clip_path, "w");
1199 if (!clip_file) goto copy_path_fail;
1200 fprintf(clip_file, "%s%s\n", CWD, ENAME(ESEL));
1201 fclose(clip_file);
1202 goto copy_path_done;
1203 copy_path_fail:
1204 strcpy(CLIPBOARD, CWD);
1205 strcat(CLIPBOARD, ENAME(ESEL));
1206 copy_path_done:
1208 } else if (!strcmp(key, RVK_PASTE_PATH)) {
1209 clip_path = getenv("CLIP");
1210 if (!clip_path) goto paste_path_fail;
1211 clip_file = fopen(clip_path, "r");
1212 if (!clip_file) goto paste_path_fail;
1213 fscanf(clip_file, "%s\n", CLIPBOARD);
1214 fclose(clip_file);
1215 paste_path_fail:
1216 strcpy(BUF1, CLIPBOARD);
1217 strcpy(CWD, dirname(BUF1));
1218 if (strcmp(CWD, "/"))
1219 strcat(CWD, "/");
1220 cd(1);
1221 strcpy(BUF1, CLIPBOARD);
1222 try_to_sel(strstr(CLIPBOARD, basename(BUF1)));
1223 update_view();
1224 } else if (!strcmp(key, RVK_REFRESH)) {
1225 reload();
1226 } else if (!strcmp(key, RVK_SHELL)) {
1227 program = user_shell;
1228 if (program) {
1229 #ifdef RV_SHELL
1230 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1231 #else
1232 spawn((char *[]) {program, NULL});
1233 #endif
1234 reload();
1236 } else if (!strcmp(key, RVK_VIEW)) {
1237 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1238 if (open_with_env(user_pager, ENAME(ESEL)))
1239 cd(0);
1240 } else if (!strcmp(key, RVK_EDIT)) {
1241 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1242 if (open_with_env(user_editor, ENAME(ESEL)))
1243 cd(0);
1244 } else if (!strcmp(key, RVK_OPEN)) {
1245 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1246 if (open_with_env(user_open, ENAME(ESEL)))
1247 cd(0);
1248 } else if (!strcmp(key, RVK_SEARCH)) {
1249 int oldsel, oldscroll, length;
1250 if (!rover.nfiles) continue;
1251 oldsel = ESEL;
1252 oldscroll = SCROLL;
1253 start_line_edit("");
1254 update_input(RVP_SEARCH, RED);
1255 while ((edit_stat = get_line_edit()) == CONTINUE) {
1256 int sel;
1257 Color color = RED;
1258 length = strlen(INPUT);
1259 if (length) {
1260 for (sel = 0; sel < rover.nfiles; sel++)
1261 if (!strncmp(ENAME(sel), INPUT, length))
1262 break;
1263 if (sel < rover.nfiles) {
1264 color = GREEN;
1265 ESEL = sel;
1266 if (rover.nfiles > HEIGHT) {
1267 if (sel < 3)
1268 SCROLL = 0;
1269 else if (sel - 3 > rover.nfiles - HEIGHT)
1270 SCROLL = rover.nfiles - HEIGHT;
1271 else
1272 SCROLL = sel - 3;
1275 } else {
1276 ESEL = oldsel;
1277 SCROLL = oldscroll;
1279 update_view();
1280 update_input(RVP_SEARCH, color);
1282 if (edit_stat == CANCEL) {
1283 ESEL = oldsel;
1284 SCROLL = oldscroll;
1286 clear_message();
1287 update_view();
1288 } else if (!strcmp(key, RVK_TG_FILES)) {
1289 FLAGS ^= SHOW_FILES;
1290 reload();
1291 } else if (!strcmp(key, RVK_TG_DIRS)) {
1292 FLAGS ^= SHOW_DIRS;
1293 reload();
1294 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1295 FLAGS ^= SHOW_HIDDEN;
1296 reload();
1297 } else if (!strcmp(key, RVK_NEW_FILE)) {
1298 int ok = 0;
1299 start_line_edit("");
1300 update_input(RVP_NEW_FILE, RED);
1301 while ((edit_stat = get_line_edit()) == CONTINUE) {
1302 int length = strlen(INPUT);
1303 ok = length;
1304 for (i = 0; i < rover.nfiles; i++) {
1305 if (
1306 !strncmp(ENAME(i), INPUT, length) &&
1307 (!strcmp(ENAME(i) + length, "") ||
1308 !strcmp(ENAME(i) + length, "/"))
1309 ) {
1310 ok = 0;
1311 break;
1314 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1316 clear_message();
1317 if (edit_stat == CONFIRM) {
1318 if (ok) {
1319 if (addfile(INPUT) == 0) {
1320 cd(1);
1321 try_to_sel(INPUT);
1322 update_view();
1323 } else
1324 message(RED, "Could not create \"%s\".", INPUT);
1325 } else
1326 message(RED, "\"%s\" already exists.", INPUT);
1328 } else if (!strcmp(key, RVK_NEW_DIR)) {
1329 int ok = 0;
1330 start_line_edit("");
1331 update_input(RVP_NEW_DIR, RED);
1332 while ((edit_stat = get_line_edit()) == CONTINUE) {
1333 int length = strlen(INPUT);
1334 ok = length;
1335 for (i = 0; i < rover.nfiles; i++) {
1336 if (
1337 !strncmp(ENAME(i), INPUT, length) &&
1338 (!strcmp(ENAME(i) + length, "") ||
1339 !strcmp(ENAME(i) + length, "/"))
1340 ) {
1341 ok = 0;
1342 break;
1345 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1347 clear_message();
1348 if (edit_stat == CONFIRM) {
1349 if (ok) {
1350 if (adddir(INPUT) == 0) {
1351 cd(1);
1352 strcat(INPUT, "/");
1353 try_to_sel(INPUT);
1354 update_view();
1355 } else
1356 message(RED, "Could not create \"%s/\".", INPUT);
1357 } else
1358 message(RED, "\"%s\" already exists.", INPUT);
1360 } else if (!strcmp(key, RVK_RENAME)) {
1361 int ok = 0;
1362 char *last;
1363 int isdir;
1364 strcpy(INPUT, ENAME(ESEL));
1365 last = INPUT + strlen(INPUT) - 1;
1366 if ((isdir = *last == '/'))
1367 *last = '\0';
1368 start_line_edit(INPUT);
1369 update_input(RVP_RENAME, RED);
1370 while ((edit_stat = get_line_edit()) == CONTINUE) {
1371 int length = strlen(INPUT);
1372 ok = length;
1373 for (i = 0; i < rover.nfiles; i++)
1374 if (
1375 !strncmp(ENAME(i), INPUT, length) &&
1376 (!strcmp(ENAME(i) + length, "") ||
1377 !strcmp(ENAME(i) + length, "/"))
1378 ) {
1379 ok = 0;
1380 break;
1382 update_input(RVP_RENAME, ok ? GREEN : RED);
1384 clear_message();
1385 if (edit_stat == CONFIRM) {
1386 if (isdir)
1387 strcat(INPUT, "/");
1388 if (ok) {
1389 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1390 del_mark(&rover.marks, ENAME(ESEL));
1391 add_mark(&rover.marks, CWD, INPUT);
1393 cd(1);
1394 try_to_sel(INPUT);
1395 update_view();
1396 } else
1397 message(RED, "\"%s\" already exists.", INPUT);
1399 } else if (!strcmp(key, RVK_TG_EXEC)) {
1400 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1401 if (S_IXUSR & EMODE(ESEL))
1402 EMODE(ESEL) &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
1403 else
1404 EMODE(ESEL) |= S_IXUSR | S_IXGRP | S_IXOTH ;
1405 if (chmod(ENAME(ESEL), EMODE(ESEL))) {
1406 message(RED, "Failed to change mode of \"%s\".", ENAME(ESEL));
1407 } else {
1408 message(GREEN, "Changed mode of \"%s\".", ENAME(ESEL));
1409 update_view();
1411 } else if (!strcmp(key, RVK_DELETE)) {
1412 if (rover.nfiles) {
1413 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1414 if (rover_getch() == 'Y') {
1415 const char *name = ENAME(ESEL);
1416 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1417 reload();
1418 if (ret)
1419 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1420 } else
1421 clear_message();
1422 } else
1423 message(RED, "No entry selected for deletion.");
1424 } else if (!strcmp(key, RVK_TG_MARK)) {
1425 if (MARKED(ESEL))
1426 del_mark(&rover.marks, ENAME(ESEL));
1427 else
1428 add_mark(&rover.marks, CWD, ENAME(ESEL));
1429 MARKED(ESEL) = !MARKED(ESEL);
1430 ESEL = (ESEL + 1) % rover.nfiles;
1431 update_view();
1432 } else if (!strcmp(key, RVK_INVMARK)) {
1433 for (i = 0; i < rover.nfiles; i++) {
1434 if (MARKED(i))
1435 del_mark(&rover.marks, ENAME(i));
1436 else
1437 add_mark(&rover.marks, CWD, ENAME(i));
1438 MARKED(i) = !MARKED(i);
1440 update_view();
1441 } else if (!strcmp(key, RVK_MARKALL)) {
1442 for (i = 0; i < rover.nfiles; i++)
1443 if (!MARKED(i)) {
1444 add_mark(&rover.marks, CWD, ENAME(i));
1445 MARKED(i) = 1;
1447 update_view();
1448 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1449 if (rover.marks.nentries) {
1450 message(YELLOW, "Delete all marked entries? (Y/n)");
1451 if (rover_getch() == 'Y')
1452 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1453 else
1454 clear_message();
1455 } else
1456 message(RED, "No entries marked for deletion.");
1457 } else if (!strcmp(key, RVK_MARK_COPY)) {
1458 if (rover.marks.nentries)
1459 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1460 else
1461 message(RED, "No entries marked for copying.");
1462 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1463 if (rover.marks.nentries)
1464 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1465 else
1466 message(RED, "No entries marked for moving.");
1469 if (rover.nfiles)
1470 free_rows(&rover.rows, rover.nfiles);
1471 delwin(rover.window);
1472 if (save_cwd_file != NULL) {
1473 fputs(CWD, save_cwd_file);
1474 fclose(save_cwd_file);
1476 if (save_marks_file != NULL) {
1477 for (i = 0; i < rover.marks.bulk; i++) {
1478 entry = rover.marks.entries[i];
1479 if (entry)
1480 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1482 fclose(save_marks_file);
1484 free_marks(&rover.marks);
1485 return 0;