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 wchar_t WBUF[BUFLEN];
41 /* Paths to external programs. */
42 static char *user_shell;
43 static char *user_pager;
44 static char *user_editor;
45 static char *user_open;
47 /* Listing view parameters. */
48 #define HEIGHT (LINES-4)
49 #define STATUSPOS (COLS-16)
51 /* Listing view flags. */
52 #define SHOW_FILES 0x01u
53 #define SHOW_DIRS 0x02u
54 #define SHOW_HIDDEN 0x04u
56 /* Marks parameters. */
57 #define BULK_INIT 5
58 #define BULK_THRESH 256
60 /* Information associated to each entry in listing. */
61 typedef struct Row {
62 char *name;
63 off_t size;
64 mode_t mode;
65 int islink;
66 int marked;
67 } Row;
69 /* Dynamic array of marked entries. */
70 typedef struct Marks {
71 char dirpath[PATH_MAX];
72 int bulk;
73 int nentries;
74 char **entries;
75 } Marks;
77 /* Line editing state. */
78 typedef struct Edit {
79 wchar_t buffer[BUFLEN+1];
80 int left, right;
81 } Edit;
83 /* Each tab only stores the following information. */
84 typedef struct Tab {
85 int scroll;
86 int esel;
87 uint8_t flags;
88 char cwd[PATH_MAX];
89 } Tab;
91 typedef struct Prog {
92 off_t partial;
93 off_t total;
94 const char *msg;
95 } Prog;
97 /* Global state. */
98 static struct Rover {
99 int tab;
100 int nfiles;
101 Row *rows;
102 WINDOW *window;
103 Marks marks;
104 Edit edit;
105 int edit_scroll;
106 volatile sig_atomic_t pending_usr1;
107 volatile sig_atomic_t pending_winch;
108 Prog prog;
109 Tab tabs[10];
110 } rover;
112 /* Macros for accessing global state. */
113 #define ENAME(I) rover.rows[I].name
114 #define ESIZE(I) rover.rows[I].size
115 #define EMODE(I) rover.rows[I].mode
116 #define ISLINK(I) rover.rows[I].islink
117 #define MARKED(I) rover.rows[I].marked
118 #define SCROLL rover.tabs[rover.tab].scroll
119 #define ESEL rover.tabs[rover.tab].esel
120 #define FLAGS rover.tabs[rover.tab].flags
121 #define CWD rover.tabs[rover.tab].cwd
123 /* Helpers. */
124 #define MIN(A, B) ((A) < (B) ? (A) : (B))
125 #define MAX(A, B) ((A) > (B) ? (A) : (B))
126 #define ISDIR(E) (strchr((E), '/') != NULL)
128 /* Line Editing Macros. */
129 #define EDIT_FULL(E) ((E).left == (E).right)
130 #define EDIT_CAN_LEFT(E) ((E).left)
131 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
132 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
133 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
134 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
135 #define EDIT_BACKSPACE(E) (E).left--
136 #define EDIT_DELETE(E) (E).right++
137 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
139 typedef enum EditStat {CONTINUE, CONFIRM, CANCEL} EditStat;
140 typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK} Color;
141 typedef int (*PROCESS)(const char *path);
143 static void
144 init_marks(Marks *marks)
146 strcpy(marks->dirpath, "");
147 marks->bulk = BULK_INIT;
148 marks->nentries = 0;
149 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
152 /* Unmark all entries. */
153 static void
154 mark_none(Marks *marks)
156 int i;
158 strcpy(marks->dirpath, "");
159 for (i = 0; i < marks->bulk && marks->nentries; i++)
160 if (marks->entries[i]) {
161 free(marks->entries[i]);
162 marks->entries[i] = NULL;
163 marks->nentries--;
165 if (marks->bulk > BULK_THRESH) {
166 /* Reset bulk to free some memory. */
167 free(marks->entries);
168 marks->bulk = BULK_INIT;
169 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
173 static void
174 add_mark(Marks *marks, char *dirpath, char *entry)
176 int i;
178 if (!strcmp(marks->dirpath, dirpath)) {
179 /* Append mark to directory. */
180 if (marks->nentries == marks->bulk) {
181 /* Expand bulk to accomodate new entry. */
182 int extra = marks->bulk / 2;
183 marks->bulk += extra; /* bulk *= 1.5; */
184 marks->entries = realloc(marks->entries,
185 marks->bulk * sizeof *marks->entries);
186 memset(&marks->entries[marks->nentries], 0,
187 extra * sizeof *marks->entries);
188 i = marks->nentries;
189 } else {
190 /* Search for empty slot (there must be one). */
191 for (i = 0; i < marks->bulk; i++)
192 if (!marks->entries[i])
193 break;
195 } else {
196 /* Directory changed. Discard old marks. */
197 mark_none(marks);
198 strcpy(marks->dirpath, dirpath);
199 i = 0;
201 marks->entries[i] = malloc(strlen(entry) + 1);
202 strcpy(marks->entries[i], entry);
203 marks->nentries++;
206 static void
207 del_mark(Marks *marks, char *entry)
209 int i;
211 if (marks->nentries > 1) {
212 for (i = 0; i < marks->bulk; i++)
213 if (marks->entries[i] && !strcmp(marks->entries[i], entry))
214 break;
215 free(marks->entries[i]);
216 marks->entries[i] = NULL;
217 marks->nentries--;
218 } else
219 mark_none(marks);
222 static void
223 free_marks(Marks *marks)
225 int i;
227 for (i = 0; i < marks->bulk && marks->nentries; i++)
228 if (marks->entries[i]) {
229 free(marks->entries[i]);
230 marks->nentries--;
232 free(marks->entries);
235 static void
236 handle_usr1(int sig)
238 rover.pending_usr1 = 1;
241 static void
242 handle_winch(int sig)
244 rover.pending_winch = 1;
247 static void
248 enable_handlers()
250 struct sigaction sa;
252 memset(&sa, 0, sizeof (struct sigaction));
253 sa.sa_handler = handle_usr1;
254 sigaction(SIGUSR1, &sa, NULL);
255 sa.sa_handler = handle_winch;
256 sigaction(SIGWINCH, &sa, NULL);
259 static void
260 disable_handlers()
262 struct sigaction sa;
264 memset(&sa, 0, sizeof (struct sigaction));
265 sa.sa_handler = SIG_DFL;
266 sigaction(SIGUSR1, &sa, NULL);
267 sigaction(SIGWINCH, &sa, NULL);
270 static void reload();
271 static void update_view();
273 /* Handle any signals received since last call. */
274 static void
275 sync_signals()
277 if (rover.pending_usr1) {
278 /* SIGUSR1 received: refresh directory listing. */
279 reload();
280 rover.pending_usr1 = 0;
282 if (rover.pending_winch) {
283 /* SIGWINCH received: resize application accordingly. */
284 delwin(rover.window);
285 endwin();
286 refresh();
287 clear();
288 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
289 if (HEIGHT < rover.nfiles && SCROLL + HEIGHT > rover.nfiles)
290 SCROLL = ESEL - HEIGHT;
291 update_view();
292 rover.pending_winch = 0;
296 /* This function must be used in place of getch().
297 It handles signals while waiting for user input. */
298 static int
299 rover_getch()
301 int ch;
303 while ((ch = getch()) == ERR)
304 sync_signals();
305 return ch;
308 /* This function must be used in place of get_wch().
309 It handles signals while waiting for user input. */
310 static int
311 rover_get_wch(wint_t *wch)
313 wint_t ret;
315 while ((ret = get_wch(wch)) == (wint_t) ERR)
316 sync_signals();
317 return ret;
320 /* Get user programs from the environment. */
322 #define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
323 dst = getenv(#src);
325 static void
326 get_user_programs()
328 ROVER_ENV(user_shell, SHELL)
329 ROVER_ENV(user_pager, PAGER)
330 ROVER_ENV(user_editor, EDITOR)
331 ROVER_ENV(user_open, OPEN)
334 /* Do a fork-exec to external program (e.g. $EDITOR). */
335 static void
336 spawn(char **args)
338 pid_t pid;
339 int status;
341 setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
342 pid = fork();
343 if (pid > 0) {
344 /* fork() succeeded. */
345 disable_handlers();
346 endwin();
347 waitpid(pid, &status, 0);
348 enable_handlers();
349 kill(getpid(), SIGWINCH);
350 } else if (pid == 0) {
351 /* Child process. */
352 execvp(args[0], args);
356 static void
357 shell_escaped_cat(char *buf, char *str, size_t n)
359 char *p = buf + strlen(buf);
360 *p++ = '\'';
361 for (n--; n; n--, str++) {
362 switch (*str) {
363 case '\'':
364 if (n < 4)
365 goto done;
366 strcpy(p, "'\\''");
367 n -= 4;
368 p += 4;
369 break;
370 case '\0':
371 goto done;
372 default:
373 *p = *str;
374 p++;
377 done:
378 strncat(p, "'", n);
381 static int
382 open_with_env(char *program, char *path)
384 if (program) {
385 #ifdef RV_SHELL
386 strncpy(BUF1, program, BUFLEN - 1);
387 strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
388 shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
389 spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
390 #else
391 spawn((char *[]) {program, path, NULL});
392 #endif
393 return 1;
395 return 0;
398 /* Curses setup. */
399 static void
400 init_term()
402 setlocale(LC_ALL, "");
403 initscr();
404 cbreak(); /* Get one character at a time. */
405 timeout(100); /* For getch(). */
406 noecho();
407 nonl(); /* No NL->CR/NL on output. */
408 intrflush(stdscr, FALSE);
409 keypad(stdscr, TRUE);
410 curs_set(FALSE); /* Hide blinking cursor. */
411 if (has_colors()) {
412 short bg;
413 start_color();
414 #ifdef NCURSES_EXT_FUNCS
415 use_default_colors();
416 bg = -1;
417 #else
418 bg = COLOR_BLACK;
419 #endif
420 init_pair(RED, COLOR_RED, bg);
421 init_pair(GREEN, COLOR_GREEN, bg);
422 init_pair(YELLOW, COLOR_YELLOW, bg);
423 init_pair(BLUE, COLOR_BLUE, bg);
424 init_pair(CYAN, COLOR_CYAN, bg);
425 init_pair(MAGENTA, COLOR_MAGENTA, bg);
426 init_pair(WHITE, COLOR_WHITE, bg);
427 init_pair(BLACK, COLOR_BLACK, bg);
429 atexit((void (*)(void)) endwin);
430 enable_handlers();
433 /* Update the listing view. */
434 static void
435 update_view()
437 int i, j;
438 int numsize;
439 int ishidden;
440 int marking;
442 mvhline(0, 0, ' ', COLS);
443 attr_on(A_BOLD, NULL);
444 color_set(RVC_TABNUM, NULL);
445 mvaddch(0, COLS - 2, rover.tab + '0');
446 attr_off(A_BOLD, NULL);
447 if (rover.marks.nentries) {
448 numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
449 color_set(RVC_MARKS, NULL);
450 mvaddstr(0, COLS - 3 - numsize, BUF1);
451 } else
452 numsize = -1;
453 color_set(RVC_CWD, NULL);
454 mbstowcs(WBUF, CWD, PATH_MAX);
455 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
456 wcolor_set(rover.window, RVC_BORDER, NULL);
457 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
458 ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
459 /* Selection might not be visible, due to cursor wrapping or window
460 shrinking. In that case, the scroll must be moved to make it visible. */
461 if (rover.nfiles > HEIGHT) {
462 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
463 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
464 } else
465 SCROLL = 0;
466 marking = !strcmp(CWD, rover.marks.dirpath);
467 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
468 ishidden = ENAME(j)[0] == '.';
469 if (j == ESEL)
470 wattr_on(rover.window, A_REVERSE, NULL);
471 if (ISLINK(j))
472 wcolor_set(rover.window, RVC_LINK, NULL);
473 else if (ishidden)
474 wcolor_set(rover.window, RVC_HIDDEN, NULL);
475 else if (S_ISREG(EMODE(j))) {
476 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
477 wcolor_set(rover.window, RVC_EXEC, NULL);
478 else
479 wcolor_set(rover.window, RVC_REG, NULL);
480 } else if (S_ISDIR(EMODE(j)))
481 wcolor_set(rover.window, RVC_DIR, NULL);
482 else if (S_ISCHR(EMODE(j)))
483 wcolor_set(rover.window, RVC_CHR, NULL);
484 else if (S_ISBLK(EMODE(j)))
485 wcolor_set(rover.window, RVC_BLK, NULL);
486 else if (S_ISFIFO(EMODE(j)))
487 wcolor_set(rover.window, RVC_FIFO, NULL);
488 else if (S_ISSOCK(EMODE(j)))
489 wcolor_set(rover.window, RVC_SOCK, NULL);
490 if (S_ISDIR(EMODE(j))) {
491 mbstowcs(WBUF, ENAME(j), PATH_MAX);
492 if (ISLINK(j))
493 wcscat(WBUF, L"/");
494 } else {
495 char *suffix, *suffixes = "BKMGTPEZY";
496 off_t human_size = ESIZE(j) * 10;
497 int length = mbstowcs(NULL, ENAME(j), 0);
498 for (suffix = suffixes; human_size >= 10240; suffix++)
499 human_size = (human_size + 512) / 1024;
500 if (*suffix == 'B')
501 swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
502 (int) (COLS - length - 6),
503 (int) human_size / 10, *suffix);
504 else
505 swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
506 (int) (COLS - length - 8),
507 (int) human_size / 10, (int) human_size % 10, *suffix);
509 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
510 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
511 if (marking && MARKED(j)) {
512 wcolor_set(rover.window, RVC_MARKS, NULL);
513 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
514 } else
515 mvwaddch(rover.window, i + 1, 1, ' ');
516 if (j == ESEL)
517 wattr_off(rover.window, A_REVERSE, NULL);
519 for (; i < HEIGHT; i++)
520 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
521 if (rover.nfiles > HEIGHT) {
522 int center, height;
523 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
524 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
525 if (!height) height = 1;
526 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
527 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
529 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
530 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
531 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
532 if (!rover.nfiles)
533 strcpy(BUF2, "0/0");
534 else
535 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
536 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
537 color_set(RVC_STATUS, NULL);
538 mvaddstr(LINES - 1, STATUSPOS, BUF1);
539 wrefresh(rover.window);
542 /* Show a message on the status bar. */
543 static void
544 message(Color color, char *fmt, ...)
546 int len, pos;
547 va_list args;
549 va_start(args, fmt);
550 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
551 va_end(args);
552 len = strlen(BUF1);
553 pos = (STATUSPOS - len) / 2;
554 attr_on(A_BOLD, NULL);
555 color_set(color, NULL);
556 mvaddstr(LINES - 1, pos, BUF1);
557 color_set(DEFAULT, NULL);
558 attr_off(A_BOLD, NULL);
561 /* Clear message area, leaving only status info. */
562 static void
563 clear_message()
565 mvhline(LINES - 1, 0, ' ', STATUSPOS);
568 /* Comparison used to sort listing entries. */
569 static int
570 rowcmp(const void *a, const void *b)
572 int isdir1, isdir2, cmpdir;
573 const Row *r1 = a;
574 const Row *r2 = b;
575 isdir1 = S_ISDIR(r1->mode);
576 isdir2 = S_ISDIR(r2->mode);
577 cmpdir = isdir2 - isdir1;
578 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
581 /* Get all entries in current working directory. */
582 static int
583 ls(Row **rowsp, uint8_t flags)
585 DIR *dp;
586 struct dirent *ep;
587 struct stat statbuf;
588 Row *rows;
589 int i, n;
591 if(!(dp = opendir("."))) return -1;
592 n = -2; /* We don't want the entries "." and "..". */
593 while (readdir(dp)) n++;
594 rewinddir(dp);
595 rows = malloc(n * sizeof *rows);
596 i = 0;
597 while ((ep = readdir(dp))) {
598 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
599 continue;
600 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
601 continue;
602 lstat(ep->d_name, &statbuf);
603 rows[i].islink = S_ISLNK(statbuf.st_mode);
604 stat(ep->d_name, &statbuf);
605 if (S_ISDIR(statbuf.st_mode)) {
606 if (flags & SHOW_DIRS) {
607 rows[i].name = malloc(strlen(ep->d_name) + 2);
608 strcpy(rows[i].name, ep->d_name);
609 if (!rows[i].islink)
610 strcat(rows[i].name, "/");
611 rows[i].mode = statbuf.st_mode;
612 i++;
614 } else if (flags & SHOW_FILES) {
615 rows[i].name = malloc(strlen(ep->d_name) + 1);
616 strcpy(rows[i].name, ep->d_name);
617 rows[i].size = statbuf.st_size;
618 rows[i].mode = statbuf.st_mode;
619 i++;
622 n = i; /* Ignore unused space in array caused by filters. */
623 qsort(rows, n, sizeof (*rows), rowcmp);
624 closedir(dp);
625 *rowsp = rows;
626 return n;
629 static void
630 free_rows(Row **rowsp, int nfiles)
632 int i;
634 for (i = 0; i < nfiles; i++)
635 free((*rowsp)[i].name);
636 free(*rowsp);
637 *rowsp = NULL;
640 /* Change working directory to the path in CWD. */
641 static void
642 cd(int reset)
644 int i, j;
646 message(CYAN, "Loading \"%s\"...", CWD);
647 refresh();
648 if (reset) ESEL = SCROLL = 0;
649 chdir(CWD);
650 if (rover.nfiles)
651 free_rows(&rover.rows, rover.nfiles);
652 rover.nfiles = ls(&rover.rows, FLAGS);
653 if (!strcmp(CWD, rover.marks.dirpath)) {
654 for (i = 0; i < rover.nfiles; i++) {
655 for (j = 0; j < rover.marks.bulk; j++)
656 if (
657 rover.marks.entries[j] &&
658 !strcmp(rover.marks.entries[j], ENAME(i))
660 break;
661 MARKED(i) = j < rover.marks.bulk;
663 } else
664 for (i = 0; i < rover.nfiles; i++)
665 MARKED(i) = 0;
666 clear_message();
667 update_view();
670 /* Select a target entry, if it is present. */
671 static void
672 try_to_sel(const char *target)
674 ESEL = 0;
675 if (!ISDIR(target))
676 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
677 ESEL++;
678 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
679 ESEL++;
682 /* Reload CWD, but try to keep selection. */
683 static void
684 reload()
686 if (rover.nfiles) {
687 strcpy(INPUT, ENAME(ESEL));
688 cd(0);
689 try_to_sel(INPUT);
690 update_view();
691 } else
692 cd(1);
695 static off_t
696 count_dir(const char *path)
698 DIR *dp;
699 struct dirent *ep;
700 struct stat statbuf;
701 char subpath[PATH_MAX];
702 off_t total;
704 if(!(dp = opendir(path))) return 0;
705 total = 0;
706 while ((ep = readdir(dp))) {
707 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
708 continue;
709 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
710 lstat(subpath, &statbuf);
711 if (S_ISDIR(statbuf.st_mode)) {
712 strcat(subpath, "/");
713 total += count_dir(subpath);
714 } else
715 total += statbuf.st_size;
717 closedir(dp);
718 return total;
721 static off_t
722 count_marked()
724 int i;
725 char *entry;
726 off_t total;
727 struct stat statbuf;
729 total = 0;
730 chdir(rover.marks.dirpath);
731 for (i = 0; i < rover.marks.bulk; i++) {
732 entry = rover.marks.entries[i];
733 if (entry) {
734 if (ISDIR(entry)) {
735 total += count_dir(entry);
736 } else {
737 lstat(entry, &statbuf);
738 total += statbuf.st_size;
742 chdir(CWD);
743 return total;
746 /* Recursively process a source directory using CWD as destination root.
747 For each node (i.e. directory), do the following:
748 1. call pre(destination);
749 2. call proc() on every child leaf (i.e. files);
750 3. recurse into every child node;
751 4. call pos(source).
752 E.g. to move directory /src/ (and all its contents) inside /dst/:
753 strcpy(CWD, "/dst/");
754 process_dir(adddir, movfile, deldir, "/src/"); */
755 static int
756 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
758 int ret;
759 DIR *dp;
760 struct dirent *ep;
761 struct stat statbuf;
762 char subpath[PATH_MAX];
764 ret = 0;
765 if (pre) {
766 char dstpath[PATH_MAX];
767 strcpy(dstpath, CWD);
768 strcat(dstpath, path + strlen(rover.marks.dirpath));
769 ret |= pre(dstpath);
771 if(!(dp = opendir(path))) return -1;
772 while ((ep = readdir(dp))) {
773 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
774 continue;
775 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
776 lstat(subpath, &statbuf);
777 if (S_ISDIR(statbuf.st_mode)) {
778 strcat(subpath, "/");
779 ret |= process_dir(pre, proc, pos, subpath);
780 } else
781 ret |= proc(subpath);
783 closedir(dp);
784 if (pos) ret |= pos(path);
785 return ret;
788 /* Process all marked entries using CWD as destination root.
789 All marked entries that are directories will be recursively processed.
790 See process_dir() for details on the parameters. */
791 static void
792 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
793 const char *msg_doing, const char *msg_done)
795 int i, ret;
796 char *entry;
797 char path[PATH_MAX];
799 clear_message();
800 message(CYAN, "%s...", msg_doing);
801 refresh();
802 rover.prog = (Prog) {0, count_marked(), msg_doing};
803 for (i = 0; i < rover.marks.bulk; i++) {
804 entry = rover.marks.entries[i];
805 if (entry) {
806 ret = 0;
807 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
808 if (ISDIR(entry)) {
809 if (!strncmp(path, CWD, strlen(path)))
810 ret = -1;
811 else
812 ret = process_dir(pre, proc, pos, path);
813 } else
814 ret = proc(path);
815 if (!ret) {
816 del_mark(&rover.marks, entry);
817 reload();
821 rover.prog.total = 0;
822 reload();
823 if (!rover.marks.nentries)
824 message(GREEN, "%s all marked entries.", msg_done);
825 else
826 message(RED, "Some errors occured while %s.", msg_doing);
827 RV_ALERT();
830 static void
831 update_progress(off_t delta)
833 int percent;
835 if (!rover.prog.total) return;
836 rover.prog.partial += delta;
837 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
838 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
839 refresh();
842 /* Wrappers for file operations. */
843 static int delfile(const char *path) {
844 int ret;
845 struct stat st;
847 ret = lstat(path, &st);
848 if (ret < 0) return ret;
849 update_progress(st.st_size);
850 return unlink(path);
852 static PROCESS deldir = rmdir;
853 static int addfile(const char *path) {
854 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
855 int ret;
857 ret = creat(path, 0644);
858 if (ret < 0) return ret;
859 return close(ret);
861 static int cpyfile(const char *srcpath) {
862 int src, dst, ret;
863 size_t size;
864 struct stat st;
865 char buf[BUFSIZ];
866 char dstpath[PATH_MAX];
868 strcpy(dstpath, CWD);
869 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
870 ret = lstat(srcpath, &st);
871 if (ret < 0) return ret;
872 if (S_ISLNK(st.st_mode)) {
873 ret = readlink(srcpath, BUF1, BUFLEN-1);
874 if (ret < 0) return ret;
875 BUF1[ret] = '\0';
876 ret = symlink(BUF1, dstpath);
877 } else {
878 ret = src = open(srcpath, O_RDONLY);
879 if (ret < 0) return ret;
880 ret = dst = creat(dstpath, st.st_mode);
881 if (ret < 0) return ret;
882 while ((size = read(src, buf, BUFSIZ)) > 0) {
883 write(dst, buf, size);
884 update_progress(size);
885 sync_signals();
887 close(src);
888 close(dst);
889 ret = 0;
891 return ret;
893 static int adddir(const char *path) {
894 int ret;
895 struct stat st;
897 ret = stat(CWD, &st);
898 if (ret < 0) return ret;
899 return mkdir(path, st.st_mode);
901 static int movfile(const char *srcpath) {
902 int ret;
903 struct stat st;
904 char dstpath[PATH_MAX];
906 strcpy(dstpath, CWD);
907 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
908 ret = rename(srcpath, dstpath);
909 if (ret == 0) {
910 ret = lstat(dstpath, &st);
911 if (ret < 0) return ret;
912 update_progress(st.st_size);
913 } else if (errno == EXDEV) {
914 ret = cpyfile(srcpath);
915 if (ret < 0) return ret;
916 ret = unlink(srcpath);
918 return ret;
921 static void
922 start_line_edit(const char *init_input)
924 curs_set(TRUE);
925 strncpy(INPUT, init_input, BUFLEN);
926 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
927 rover.edit.right = BUFLEN - 1;
928 rover.edit.buffer[BUFLEN] = L'\0';
929 rover.edit_scroll = 0;
932 /* Read input and change editing state accordingly. */
933 static EditStat
934 get_line_edit()
936 wchar_t eraser, killer, wch;
937 int ret, length;
939 ret = rover_get_wch((wint_t *) &wch);
940 erasewchar(&eraser);
941 killwchar(&killer);
942 if (ret == KEY_CODE_YES) {
943 if (wch == KEY_ENTER) {
944 curs_set(FALSE);
945 return CONFIRM;
946 } else if (wch == KEY_LEFT) {
947 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
948 } else if (wch == KEY_RIGHT) {
949 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
950 } else if (wch == KEY_UP) {
951 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
952 } else if (wch == KEY_DOWN) {
953 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
954 } else if (wch == KEY_BACKSPACE) {
955 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
956 } else if (wch == KEY_DC) {
957 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
959 } else {
960 if (wch == L'\r' || wch == L'\n') {
961 curs_set(FALSE);
962 return CONFIRM;
963 } else if (wch == L'\t') {
964 curs_set(FALSE);
965 return CANCEL;
966 } else if (wch == eraser) {
967 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
968 } else if (wch == killer) {
969 EDIT_CLEAR(rover.edit);
970 clear_message();
971 } else if (iswprint(wch)) {
972 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
975 /* Encode edit contents in INPUT. */
976 rover.edit.buffer[rover.edit.left] = L'\0';
977 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
978 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
979 BUFLEN-length);
980 return CONTINUE;
983 /* Update line input on the screen. */
984 static void
985 update_input(const char *prompt, Color color)
987 int plen, ilen, maxlen;
989 plen = strlen(prompt);
990 ilen = mbstowcs(NULL, INPUT, 0);
991 maxlen = STATUSPOS - plen - 2;
992 if (ilen - rover.edit_scroll < maxlen)
993 rover.edit_scroll = MAX(ilen - maxlen, 0);
994 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
995 rover.edit_scroll = rover.edit.left - maxlen;
996 else if (rover.edit.left < rover.edit_scroll)
997 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
998 color_set(RVC_PROMPT, NULL);
999 mvaddstr(LINES - 1, 0, prompt);
1000 color_set(color, NULL);
1001 mbstowcs(WBUF, INPUT, COLS);
1002 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
1003 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
1004 color_set(DEFAULT, NULL);
1005 if (rover.edit_scroll)
1006 mvaddch(LINES - 1, plen - 1, '<');
1007 if (ilen > rover.edit_scroll + maxlen)
1008 mvaddch(LINES - 1, plen + maxlen, '>');
1009 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
1012 int
1013 main(int argc, char *argv[])
1015 int i, ch;
1016 char *program;
1017 char *entry;
1018 const char *key;
1019 DIR *d;
1020 EditStat edit_stat;
1021 FILE *save_cwd_file = NULL;
1022 FILE *save_marks_file = NULL;
1024 if (argc >= 2) {
1025 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
1026 printf("rover %s\n", RV_VERSION);
1027 return 0;
1028 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1029 printf(
1030 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
1031 " Browse current directory or the ones specified.\n\n"
1032 " or: rover -h|--help\n"
1033 " Print this help message and exit.\n\n"
1034 " or: rover -v|--version\n"
1035 " Print program version and exit.\n\n"
1036 "See rover(1) for more information.\n"
1037 "Rover homepage: <https://github.com/lecram/rover>.\n"
1039 return 0;
1040 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
1041 if (argc > 2) {
1042 save_cwd_file = fopen(argv[2], "w");
1043 argc -= 2; argv += 2;
1044 } else {
1045 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1046 return 1;
1048 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
1049 if (argc > 2) {
1050 save_marks_file = fopen(argv[2], "a");
1051 argc -= 2; argv += 2;
1052 } else {
1053 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1054 return 1;
1058 get_user_programs();
1059 init_term();
1060 rover.nfiles = 0;
1061 for (i = 0; i < 10; i++) {
1062 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1063 rover.tabs[i].flags = RV_FLAGS;
1065 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1066 for (i = 1; i < argc && i < 10; i++) {
1067 if ((d = opendir(argv[i]))) {
1068 realpath(argv[i], rover.tabs[i].cwd);
1069 closedir(d);
1070 } else
1071 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1073 getcwd(rover.tabs[i].cwd, PATH_MAX);
1074 for (i++; i < 10; i++)
1075 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1076 for (i = 0; i < 10; i++)
1077 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1078 strcat(rover.tabs[i].cwd, "/");
1079 rover.tab = 1;
1080 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1081 init_marks(&rover.marks);
1082 cd(1);
1083 while (1) {
1084 ch = rover_getch();
1085 key = keyname(ch);
1086 clear_message();
1087 if (!strcmp(key, RVK_QUIT)) break;
1088 else if (ch >= '0' && ch <= '9') {
1089 rover.tab = ch - '0';
1090 cd(0);
1091 } else if (!strcmp(key, RVK_HELP)) {
1092 spawn((char *[]) {"man", "rover", NULL});
1093 } else if (!strcmp(key, RVK_DOWN)) {
1094 if (!rover.nfiles) continue;
1095 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1096 update_view();
1097 } else if (!strcmp(key, RVK_UP)) {
1098 if (!rover.nfiles) continue;
1099 ESEL = MAX(ESEL - 1, 0);
1100 update_view();
1101 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1102 if (!rover.nfiles) continue;
1103 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1104 if (rover.nfiles > HEIGHT)
1105 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1106 update_view();
1107 } else if (!strcmp(key, RVK_JUMP_UP)) {
1108 if (!rover.nfiles) continue;
1109 ESEL = MAX(ESEL - RV_JUMP, 0);
1110 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1111 update_view();
1112 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1113 if (!rover.nfiles) continue;
1114 ESEL = 0;
1115 update_view();
1116 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1117 if (!rover.nfiles) continue;
1118 ESEL = rover.nfiles - 1;
1119 update_view();
1120 } else if (!strcmp(key, RVK_CD_DOWN)) {
1121 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1122 if (chdir(ENAME(ESEL)) == -1) {
1123 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1124 continue;
1126 strcat(CWD, ENAME(ESEL));
1127 cd(1);
1128 } else if (!strcmp(key, RVK_CD_UP)) {
1129 char *dirname, first;
1130 if (!strcmp(CWD, "/")) continue;
1131 CWD[strlen(CWD) - 1] = '\0';
1132 dirname = strrchr(CWD, '/') + 1;
1133 first = dirname[0];
1134 dirname[0] = '\0';
1135 cd(1);
1136 dirname[0] = first;
1137 dirname[strlen(dirname)] = '/';
1138 try_to_sel(dirname);
1139 dirname[0] = '\0';
1140 if (rover.nfiles > HEIGHT)
1141 SCROLL = ESEL - HEIGHT / 2;
1142 update_view();
1143 } else if (!strcmp(key, RVK_HOME)) {
1144 strcpy(CWD, getenv("HOME"));
1145 if (CWD[strlen(CWD) - 1] != '/')
1146 strcat(CWD, "/");
1147 cd(1);
1148 } else if (!strcmp(key, RVK_TARGET)) {
1149 char *bname, first;
1150 int is_dir = S_ISDIR(EMODE(ESEL));
1151 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1152 if (len == -1) continue;
1153 BUF1[len] = '\0';
1154 if (access(BUF1, F_OK) == -1) {
1155 char *msg;
1156 switch (errno) {
1157 case EACCES:
1158 msg = "Cannot access \"%s\".";
1159 break;
1160 case ENOENT:
1161 msg = "\"%s\" does not exist.";
1162 break;
1163 default:
1164 msg = "Cannot navigate to \"%s\".";
1166 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1167 message(RED, msg, BUF2);
1168 continue;
1170 realpath(BUF1, CWD);
1171 len = strlen(CWD);
1172 if (CWD[len - 1] == '/')
1173 CWD[len - 1] = '\0';
1174 bname = strrchr(CWD, '/') + 1;
1175 first = *bname;
1176 *bname = '\0';
1177 cd(1);
1178 *bname = first;
1179 if (is_dir)
1180 strcat(CWD, "/");
1181 try_to_sel(bname);
1182 *bname = '\0';
1183 update_view();
1184 } else if (!strcmp(key, RVK_REFRESH)) {
1185 reload();
1186 } else if (!strcmp(key, RVK_SHELL)) {
1187 program = user_shell;
1188 if (program) {
1189 #ifdef RV_SHELL
1190 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1191 #else
1192 spawn((char *[]) {program, NULL});
1193 #endif
1194 reload();
1196 } else if (!strcmp(key, RVK_VIEW)) {
1197 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1198 if (open_with_env(user_pager, ENAME(ESEL)))
1199 cd(0);
1200 } else if (!strcmp(key, RVK_EDIT)) {
1201 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1202 if (open_with_env(user_editor, ENAME(ESEL)))
1203 cd(0);
1204 } else if (!strcmp(key, RVK_OPEN)) {
1205 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1206 if (open_with_env(user_open, ENAME(ESEL)))
1207 cd(0);
1208 } else if (!strcmp(key, RVK_SEARCH)) {
1209 int oldsel, oldscroll, length;
1210 if (!rover.nfiles) continue;
1211 oldsel = ESEL;
1212 oldscroll = SCROLL;
1213 start_line_edit("");
1214 update_input(RVP_SEARCH, RED);
1215 while ((edit_stat = get_line_edit()) == CONTINUE) {
1216 int sel;
1217 Color color = RED;
1218 length = strlen(INPUT);
1219 if (length) {
1220 for (sel = 0; sel < rover.nfiles; sel++)
1221 if (!strncmp(ENAME(sel), INPUT, length))
1222 break;
1223 if (sel < rover.nfiles) {
1224 color = GREEN;
1225 ESEL = sel;
1226 if (rover.nfiles > HEIGHT) {
1227 if (sel < 3)
1228 SCROLL = 0;
1229 else if (sel - 3 > rover.nfiles - HEIGHT)
1230 SCROLL = rover.nfiles - HEIGHT;
1231 else
1232 SCROLL = sel - 3;
1235 } else {
1236 ESEL = oldsel;
1237 SCROLL = oldscroll;
1239 update_view();
1240 update_input(RVP_SEARCH, color);
1242 if (edit_stat == CANCEL) {
1243 ESEL = oldsel;
1244 SCROLL = oldscroll;
1246 clear_message();
1247 update_view();
1248 } else if (!strcmp(key, RVK_TG_FILES)) {
1249 FLAGS ^= SHOW_FILES;
1250 reload();
1251 } else if (!strcmp(key, RVK_TG_DIRS)) {
1252 FLAGS ^= SHOW_DIRS;
1253 reload();
1254 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1255 FLAGS ^= SHOW_HIDDEN;
1256 reload();
1257 } else if (!strcmp(key, RVK_NEW_FILE)) {
1258 int ok = 0;
1259 start_line_edit("");
1260 update_input(RVP_NEW_FILE, RED);
1261 while ((edit_stat = get_line_edit()) == CONTINUE) {
1262 int length = strlen(INPUT);
1263 ok = length;
1264 for (i = 0; i < rover.nfiles; i++) {
1265 if (
1266 !strncmp(ENAME(i), INPUT, length) &&
1267 (!strcmp(ENAME(i) + length, "") ||
1268 !strcmp(ENAME(i) + length, "/"))
1269 ) {
1270 ok = 0;
1271 break;
1274 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1276 clear_message();
1277 if (edit_stat == CONFIRM) {
1278 if (ok) {
1279 if (addfile(INPUT) == 0) {
1280 cd(1);
1281 try_to_sel(INPUT);
1282 update_view();
1283 } else
1284 message(RED, "Could not create \"%s\".", INPUT);
1285 } else
1286 message(RED, "\"%s\" already exists.", INPUT);
1288 } else if (!strcmp(key, RVK_NEW_DIR)) {
1289 int ok = 0;
1290 start_line_edit("");
1291 update_input(RVP_NEW_DIR, RED);
1292 while ((edit_stat = get_line_edit()) == CONTINUE) {
1293 int length = strlen(INPUT);
1294 ok = length;
1295 for (i = 0; i < rover.nfiles; i++) {
1296 if (
1297 !strncmp(ENAME(i), INPUT, length) &&
1298 (!strcmp(ENAME(i) + length, "") ||
1299 !strcmp(ENAME(i) + length, "/"))
1300 ) {
1301 ok = 0;
1302 break;
1305 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1307 clear_message();
1308 if (edit_stat == CONFIRM) {
1309 if (ok) {
1310 if (adddir(INPUT) == 0) {
1311 cd(1);
1312 strcat(INPUT, "/");
1313 try_to_sel(INPUT);
1314 update_view();
1315 } else
1316 message(RED, "Could not create \"%s/\".", INPUT);
1317 } else
1318 message(RED, "\"%s\" already exists.", INPUT);
1320 } else if (!strcmp(key, RVK_RENAME)) {
1321 int ok = 0;
1322 char *last;
1323 int isdir;
1324 strcpy(INPUT, ENAME(ESEL));
1325 last = INPUT + strlen(INPUT) - 1;
1326 if ((isdir = *last == '/'))
1327 *last = '\0';
1328 start_line_edit(INPUT);
1329 update_input(RVP_RENAME, RED);
1330 while ((edit_stat = get_line_edit()) == CONTINUE) {
1331 int length = strlen(INPUT);
1332 ok = length;
1333 for (i = 0; i < rover.nfiles; i++)
1334 if (
1335 !strncmp(ENAME(i), INPUT, length) &&
1336 (!strcmp(ENAME(i) + length, "") ||
1337 !strcmp(ENAME(i) + length, "/"))
1338 ) {
1339 ok = 0;
1340 break;
1342 update_input(RVP_RENAME, ok ? GREEN : RED);
1344 clear_message();
1345 if (edit_stat == CONFIRM) {
1346 if (isdir)
1347 strcat(INPUT, "/");
1348 if (ok) {
1349 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1350 del_mark(&rover.marks, ENAME(ESEL));
1351 add_mark(&rover.marks, CWD, INPUT);
1353 cd(1);
1354 try_to_sel(INPUT);
1355 update_view();
1356 } else
1357 message(RED, "\"%s\" already exists.", INPUT);
1359 } else if (!strcmp(key, RVK_DELETE)) {
1360 if (rover.nfiles) {
1361 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1362 if (rover_getch() == 'Y') {
1363 const char *name = ENAME(ESEL);
1364 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1365 reload();
1366 if (ret)
1367 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1368 } else
1369 clear_message();
1370 } else
1371 message(RED, "No entry selected for deletion.");
1372 } else if (!strcmp(key, RVK_TG_MARK)) {
1373 if (MARKED(ESEL))
1374 del_mark(&rover.marks, ENAME(ESEL));
1375 else
1376 add_mark(&rover.marks, CWD, ENAME(ESEL));
1377 MARKED(ESEL) = !MARKED(ESEL);
1378 ESEL = (ESEL + 1) % rover.nfiles;
1379 update_view();
1380 } else if (!strcmp(key, RVK_INVMARK)) {
1381 for (i = 0; i < rover.nfiles; i++) {
1382 if (MARKED(i))
1383 del_mark(&rover.marks, ENAME(i));
1384 else
1385 add_mark(&rover.marks, CWD, ENAME(i));
1386 MARKED(i) = !MARKED(i);
1388 update_view();
1389 } else if (!strcmp(key, RVK_MARKALL)) {
1390 for (i = 0; i < rover.nfiles; i++)
1391 if (!MARKED(i)) {
1392 add_mark(&rover.marks, CWD, ENAME(i));
1393 MARKED(i) = 1;
1395 update_view();
1396 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1397 if (rover.marks.nentries) {
1398 message(YELLOW, "Delete all marked entries? (Y/n)");
1399 if (rover_getch() == 'Y')
1400 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1401 else
1402 clear_message();
1403 } else
1404 message(RED, "No entries marked for deletion.");
1405 } else if (!strcmp(key, RVK_MARK_COPY)) {
1406 if (rover.marks.nentries)
1407 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1408 else
1409 message(RED, "No entries marked for copying.");
1410 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1411 if (rover.marks.nentries)
1412 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1413 else
1414 message(RED, "No entries marked for moving.");
1417 if (rover.nfiles)
1418 free_rows(&rover.rows, rover.nfiles);
1419 delwin(rover.window);
1420 if (save_cwd_file != NULL) {
1421 fputs(CWD, save_cwd_file);
1422 fclose(save_cwd_file);
1424 if (save_marks_file != NULL) {
1425 for (i = 0; i < rover.marks.bulk; i++) {
1426 entry = rover.marks.entries[i];
1427 if (entry)
1428 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1430 fclose(save_marks_file);
1432 free_marks(&rover.marks);
1433 return 0;