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, VISUAL)
332 if (!user_editor)
333 ROVER_ENV(user_editor, EDITOR)
334 ROVER_ENV(user_open, OPEN)
337 /* Do a fork-exec to external program (e.g. $EDITOR). */
338 static void
339 spawn(char **args)
341 pid_t pid;
342 int status;
344 setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
345 pid = fork();
346 if (pid > 0) {
347 /* fork() succeeded. */
348 disable_handlers();
349 endwin();
350 waitpid(pid, &status, 0);
351 enable_handlers();
352 kill(getpid(), SIGWINCH);
353 } else if (pid == 0) {
354 /* Child process. */
355 execvp(args[0], args);
359 static void
360 shell_escaped_cat(char *buf, char *str, size_t n)
362 char *p = buf + strlen(buf);
363 *p++ = '\'';
364 for (n--; n; n--, str++) {
365 switch (*str) {
366 case '\'':
367 if (n < 4)
368 goto done;
369 strcpy(p, "'\\''");
370 n -= 4;
371 p += 4;
372 break;
373 case '\0':
374 goto done;
375 default:
376 *p = *str;
377 p++;
380 done:
381 strncat(p, "'", n);
384 static int
385 open_with_env(char *program, char *path)
387 if (program) {
388 #ifdef RV_SHELL
389 strncpy(BUF1, program, BUFLEN - 1);
390 strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
391 shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
392 spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
393 #else
394 spawn((char *[]) {program, path, NULL});
395 #endif
396 return 1;
398 return 0;
401 /* Curses setup. */
402 static void
403 init_term()
405 setlocale(LC_ALL, "");
406 initscr();
407 cbreak(); /* Get one character at a time. */
408 timeout(100); /* For getch(). */
409 noecho();
410 nonl(); /* No NL->CR/NL on output. */
411 intrflush(stdscr, FALSE);
412 keypad(stdscr, TRUE);
413 curs_set(FALSE); /* Hide blinking cursor. */
414 if (has_colors()) {
415 short bg;
416 start_color();
417 #ifdef NCURSES_EXT_FUNCS
418 use_default_colors();
419 bg = -1;
420 #else
421 bg = COLOR_BLACK;
422 #endif
423 init_pair(RED, COLOR_RED, bg);
424 init_pair(GREEN, COLOR_GREEN, bg);
425 init_pair(YELLOW, COLOR_YELLOW, bg);
426 init_pair(BLUE, COLOR_BLUE, bg);
427 init_pair(CYAN, COLOR_CYAN, bg);
428 init_pair(MAGENTA, COLOR_MAGENTA, bg);
429 init_pair(WHITE, COLOR_WHITE, bg);
430 init_pair(BLACK, COLOR_BLACK, bg);
432 atexit((void (*)(void)) endwin);
433 enable_handlers();
436 /* Update the listing view. */
437 static void
438 update_view()
440 int i, j;
441 int numsize;
442 int ishidden;
443 int marking;
445 mvhline(0, 0, ' ', COLS);
446 attr_on(A_BOLD, NULL);
447 color_set(RVC_TABNUM, NULL);
448 mvaddch(0, COLS - 2, rover.tab + '0');
449 attr_off(A_BOLD, NULL);
450 if (rover.marks.nentries) {
451 numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
452 color_set(RVC_MARKS, NULL);
453 mvaddstr(0, COLS - 3 - numsize, BUF1);
454 } else
455 numsize = -1;
456 color_set(RVC_CWD, NULL);
457 mbstowcs(WBUF, CWD, PATH_MAX);
458 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
459 wcolor_set(rover.window, RVC_BORDER, NULL);
460 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
461 ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
462 /* Selection might not be visible, due to cursor wrapping or window
463 shrinking. In that case, the scroll must be moved to make it visible. */
464 if (rover.nfiles > HEIGHT) {
465 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
466 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
467 } else
468 SCROLL = 0;
469 marking = !strcmp(CWD, rover.marks.dirpath);
470 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
471 ishidden = ENAME(j)[0] == '.';
472 if (j == ESEL)
473 wattr_on(rover.window, A_REVERSE, NULL);
474 if (ISLINK(j))
475 wcolor_set(rover.window, RVC_LINK, NULL);
476 else if (ishidden)
477 wcolor_set(rover.window, RVC_HIDDEN, NULL);
478 else if (S_ISREG(EMODE(j))) {
479 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
480 wcolor_set(rover.window, RVC_EXEC, NULL);
481 else
482 wcolor_set(rover.window, RVC_REG, NULL);
483 } else if (S_ISDIR(EMODE(j)))
484 wcolor_set(rover.window, RVC_DIR, NULL);
485 else if (S_ISCHR(EMODE(j)))
486 wcolor_set(rover.window, RVC_CHR, NULL);
487 else if (S_ISBLK(EMODE(j)))
488 wcolor_set(rover.window, RVC_BLK, NULL);
489 else if (S_ISFIFO(EMODE(j)))
490 wcolor_set(rover.window, RVC_FIFO, NULL);
491 else if (S_ISSOCK(EMODE(j)))
492 wcolor_set(rover.window, RVC_SOCK, NULL);
493 if (S_ISDIR(EMODE(j))) {
494 mbstowcs(WBUF, ENAME(j), PATH_MAX);
495 if (ISLINK(j))
496 wcscat(WBUF, L"/");
497 } else {
498 char *suffix, *suffixes = "BKMGTPEZY";
499 off_t human_size = ESIZE(j) * 10;
500 int length = mbstowcs(NULL, ENAME(j), 0);
501 for (suffix = suffixes; human_size >= 10240; suffix++)
502 human_size = (human_size + 512) / 1024;
503 if (*suffix == 'B')
504 swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
505 (int) (COLS - length - 6),
506 (int) human_size / 10, *suffix);
507 else
508 swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
509 (int) (COLS - length - 8),
510 (int) human_size / 10, (int) human_size % 10, *suffix);
512 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
513 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
514 if (marking && MARKED(j)) {
515 wcolor_set(rover.window, RVC_MARKS, NULL);
516 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
517 } else
518 mvwaddch(rover.window, i + 1, 1, ' ');
519 if (j == ESEL)
520 wattr_off(rover.window, A_REVERSE, NULL);
522 for (; i < HEIGHT; i++)
523 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
524 if (rover.nfiles > HEIGHT) {
525 int center, height;
526 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
527 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
528 if (!height) height = 1;
529 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
530 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
532 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
533 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
534 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
535 if (!rover.nfiles)
536 strcpy(BUF2, "0/0");
537 else
538 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
539 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
540 color_set(RVC_STATUS, NULL);
541 mvaddstr(LINES - 1, STATUSPOS, BUF1);
542 wrefresh(rover.window);
545 /* Show a message on the status bar. */
546 static void
547 message(Color color, char *fmt, ...)
549 int len, pos;
550 va_list args;
552 va_start(args, fmt);
553 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
554 va_end(args);
555 len = strlen(BUF1);
556 pos = (STATUSPOS - len) / 2;
557 attr_on(A_BOLD, NULL);
558 color_set(color, NULL);
559 mvaddstr(LINES - 1, pos, BUF1);
560 color_set(DEFAULT, NULL);
561 attr_off(A_BOLD, NULL);
564 /* Clear message area, leaving only status info. */
565 static void
566 clear_message()
568 mvhline(LINES - 1, 0, ' ', STATUSPOS);
571 /* Comparison used to sort listing entries. */
572 static int
573 rowcmp(const void *a, const void *b)
575 int isdir1, isdir2, cmpdir;
576 const Row *r1 = a;
577 const Row *r2 = b;
578 isdir1 = S_ISDIR(r1->mode);
579 isdir2 = S_ISDIR(r2->mode);
580 cmpdir = isdir2 - isdir1;
581 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
584 /* Get all entries in current working directory. */
585 static int
586 ls(Row **rowsp, uint8_t flags)
588 DIR *dp;
589 struct dirent *ep;
590 struct stat statbuf;
591 Row *rows;
592 int i, n;
594 if(!(dp = opendir("."))) return -1;
595 n = -2; /* We don't want the entries "." and "..". */
596 while (readdir(dp)) n++;
597 rewinddir(dp);
598 rows = malloc(n * sizeof *rows);
599 i = 0;
600 while ((ep = readdir(dp))) {
601 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
602 continue;
603 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
604 continue;
605 lstat(ep->d_name, &statbuf);
606 rows[i].islink = S_ISLNK(statbuf.st_mode);
607 stat(ep->d_name, &statbuf);
608 if (S_ISDIR(statbuf.st_mode)) {
609 if (flags & SHOW_DIRS) {
610 rows[i].name = malloc(strlen(ep->d_name) + 2);
611 strcpy(rows[i].name, ep->d_name);
612 if (!rows[i].islink)
613 strcat(rows[i].name, "/");
614 rows[i].mode = statbuf.st_mode;
615 i++;
617 } else if (flags & SHOW_FILES) {
618 rows[i].name = malloc(strlen(ep->d_name) + 1);
619 strcpy(rows[i].name, ep->d_name);
620 rows[i].size = statbuf.st_size;
621 rows[i].mode = statbuf.st_mode;
622 i++;
625 n = i; /* Ignore unused space in array caused by filters. */
626 qsort(rows, n, sizeof (*rows), rowcmp);
627 closedir(dp);
628 *rowsp = rows;
629 return n;
632 static void
633 free_rows(Row **rowsp, int nfiles)
635 int i;
637 for (i = 0; i < nfiles; i++)
638 free((*rowsp)[i].name);
639 free(*rowsp);
640 *rowsp = NULL;
643 /* Change working directory to the path in CWD. */
644 static void
645 cd(int reset)
647 int i, j;
649 message(CYAN, "Loading \"%s\"...", CWD);
650 refresh();
651 if (chdir(CWD) == -1) {
652 getcwd(CWD, PATH_MAX-1);
653 if (CWD[strlen(CWD)-1] != '/')
654 strcat(CWD, "/");
655 goto done;
657 if (reset) ESEL = SCROLL = 0;
658 if (rover.nfiles)
659 free_rows(&rover.rows, rover.nfiles);
660 rover.nfiles = ls(&rover.rows, FLAGS);
661 if (!strcmp(CWD, rover.marks.dirpath)) {
662 for (i = 0; i < rover.nfiles; i++) {
663 for (j = 0; j < rover.marks.bulk; j++)
664 if (
665 rover.marks.entries[j] &&
666 !strcmp(rover.marks.entries[j], ENAME(i))
668 break;
669 MARKED(i) = j < rover.marks.bulk;
671 } else
672 for (i = 0; i < rover.nfiles; i++)
673 MARKED(i) = 0;
674 done:
675 clear_message();
676 update_view();
679 /* Select a target entry, if it is present. */
680 static void
681 try_to_sel(const char *target)
683 ESEL = 0;
684 if (!ISDIR(target))
685 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
686 ESEL++;
687 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
688 ESEL++;
691 /* Reload CWD, but try to keep selection. */
692 static void
693 reload()
695 if (rover.nfiles) {
696 strcpy(INPUT, ENAME(ESEL));
697 cd(0);
698 try_to_sel(INPUT);
699 update_view();
700 } else
701 cd(1);
704 static off_t
705 count_dir(const char *path)
707 DIR *dp;
708 struct dirent *ep;
709 struct stat statbuf;
710 char subpath[PATH_MAX];
711 off_t total;
713 if(!(dp = opendir(path))) return 0;
714 total = 0;
715 while ((ep = readdir(dp))) {
716 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
717 continue;
718 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
719 lstat(subpath, &statbuf);
720 if (S_ISDIR(statbuf.st_mode)) {
721 strcat(subpath, "/");
722 total += count_dir(subpath);
723 } else
724 total += statbuf.st_size;
726 closedir(dp);
727 return total;
730 static off_t
731 count_marked()
733 int i;
734 char *entry;
735 off_t total;
736 struct stat statbuf;
738 total = 0;
739 chdir(rover.marks.dirpath);
740 for (i = 0; i < rover.marks.bulk; i++) {
741 entry = rover.marks.entries[i];
742 if (entry) {
743 if (ISDIR(entry)) {
744 total += count_dir(entry);
745 } else {
746 lstat(entry, &statbuf);
747 total += statbuf.st_size;
751 chdir(CWD);
752 return total;
755 /* Recursively process a source directory using CWD as destination root.
756 For each node (i.e. directory), do the following:
757 1. call pre(destination);
758 2. call proc() on every child leaf (i.e. files);
759 3. recurse into every child node;
760 4. call pos(source).
761 E.g. to move directory /src/ (and all its contents) inside /dst/:
762 strcpy(CWD, "/dst/");
763 process_dir(adddir, movfile, deldir, "/src/"); */
764 static int
765 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
767 int ret;
768 DIR *dp;
769 struct dirent *ep;
770 struct stat statbuf;
771 char subpath[PATH_MAX];
773 ret = 0;
774 if (pre) {
775 char dstpath[PATH_MAX];
776 strcpy(dstpath, CWD);
777 strcat(dstpath, path + strlen(rover.marks.dirpath));
778 ret |= pre(dstpath);
780 if(!(dp = opendir(path))) return -1;
781 while ((ep = readdir(dp))) {
782 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
783 continue;
784 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
785 lstat(subpath, &statbuf);
786 if (S_ISDIR(statbuf.st_mode)) {
787 strcat(subpath, "/");
788 ret |= process_dir(pre, proc, pos, subpath);
789 } else
790 ret |= proc(subpath);
792 closedir(dp);
793 if (pos) ret |= pos(path);
794 return ret;
797 /* Process all marked entries using CWD as destination root.
798 All marked entries that are directories will be recursively processed.
799 See process_dir() for details on the parameters. */
800 static void
801 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
802 const char *msg_doing, const char *msg_done)
804 int i, ret;
805 char *entry;
806 char path[PATH_MAX];
808 clear_message();
809 message(CYAN, "%s...", msg_doing);
810 refresh();
811 rover.prog = (Prog) {0, count_marked(), msg_doing};
812 for (i = 0; i < rover.marks.bulk; i++) {
813 entry = rover.marks.entries[i];
814 if (entry) {
815 ret = 0;
816 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
817 if (ISDIR(entry)) {
818 if (!strncmp(path, CWD, strlen(path)))
819 ret = -1;
820 else
821 ret = process_dir(pre, proc, pos, path);
822 } else
823 ret = proc(path);
824 if (!ret) {
825 del_mark(&rover.marks, entry);
826 reload();
830 rover.prog.total = 0;
831 reload();
832 if (!rover.marks.nentries)
833 message(GREEN, "%s all marked entries.", msg_done);
834 else
835 message(RED, "Some errors occured while %s.", msg_doing);
836 RV_ALERT();
839 static void
840 update_progress(off_t delta)
842 int percent;
844 if (!rover.prog.total) return;
845 rover.prog.partial += delta;
846 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
847 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
848 refresh();
851 /* Wrappers for file operations. */
852 static int delfile(const char *path) {
853 int ret;
854 struct stat st;
856 ret = lstat(path, &st);
857 if (ret < 0) return ret;
858 update_progress(st.st_size);
859 return unlink(path);
861 static PROCESS deldir = rmdir;
862 static int addfile(const char *path) {
863 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
864 int ret;
866 ret = creat(path, 0644);
867 if (ret < 0) return ret;
868 return close(ret);
870 static int cpyfile(const char *srcpath) {
871 int src, dst, ret;
872 size_t size;
873 struct stat st;
874 char buf[BUFSIZ];
875 char dstpath[PATH_MAX];
877 strcpy(dstpath, CWD);
878 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
879 ret = lstat(srcpath, &st);
880 if (ret < 0) return ret;
881 if (S_ISLNK(st.st_mode)) {
882 ret = readlink(srcpath, BUF1, BUFLEN-1);
883 if (ret < 0) return ret;
884 BUF1[ret] = '\0';
885 ret = symlink(BUF1, dstpath);
886 } else {
887 ret = src = open(srcpath, O_RDONLY);
888 if (ret < 0) return ret;
889 ret = dst = creat(dstpath, st.st_mode);
890 if (ret < 0) return ret;
891 while ((size = read(src, buf, BUFSIZ)) > 0) {
892 write(dst, buf, size);
893 update_progress(size);
894 sync_signals();
896 close(src);
897 close(dst);
898 ret = 0;
900 return ret;
902 static int adddir(const char *path) {
903 int ret;
904 struct stat st;
906 ret = stat(CWD, &st);
907 if (ret < 0) return ret;
908 return mkdir(path, st.st_mode);
910 static int movfile(const char *srcpath) {
911 int ret;
912 struct stat st;
913 char dstpath[PATH_MAX];
915 strcpy(dstpath, CWD);
916 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
917 ret = rename(srcpath, dstpath);
918 if (ret == 0) {
919 ret = lstat(dstpath, &st);
920 if (ret < 0) return ret;
921 update_progress(st.st_size);
922 } else if (errno == EXDEV) {
923 ret = cpyfile(srcpath);
924 if (ret < 0) return ret;
925 ret = unlink(srcpath);
927 return ret;
930 static void
931 start_line_edit(const char *init_input)
933 curs_set(TRUE);
934 strncpy(INPUT, init_input, BUFLEN);
935 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
936 rover.edit.right = BUFLEN - 1;
937 rover.edit.buffer[BUFLEN] = L'\0';
938 rover.edit_scroll = 0;
941 /* Read input and change editing state accordingly. */
942 static EditStat
943 get_line_edit()
945 wchar_t eraser, killer, wch;
946 int ret, length;
948 ret = rover_get_wch((wint_t *) &wch);
949 erasewchar(&eraser);
950 killwchar(&killer);
951 if (ret == KEY_CODE_YES) {
952 if (wch == KEY_ENTER) {
953 curs_set(FALSE);
954 return CONFIRM;
955 } else if (wch == KEY_LEFT) {
956 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
957 } else if (wch == KEY_RIGHT) {
958 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
959 } else if (wch == KEY_UP) {
960 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
961 } else if (wch == KEY_DOWN) {
962 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
963 } else if (wch == KEY_BACKSPACE) {
964 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
965 } else if (wch == KEY_DC) {
966 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
968 } else {
969 if (wch == L'\r' || wch == L'\n') {
970 curs_set(FALSE);
971 return CONFIRM;
972 } else if (wch == L'\t') {
973 curs_set(FALSE);
974 return CANCEL;
975 } else if (wch == eraser) {
976 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
977 } else if (wch == killer) {
978 EDIT_CLEAR(rover.edit);
979 clear_message();
980 } else if (iswprint(wch)) {
981 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
984 /* Encode edit contents in INPUT. */
985 rover.edit.buffer[rover.edit.left] = L'\0';
986 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
987 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
988 BUFLEN-length);
989 return CONTINUE;
992 /* Update line input on the screen. */
993 static void
994 update_input(const char *prompt, Color color)
996 int plen, ilen, maxlen;
998 plen = strlen(prompt);
999 ilen = mbstowcs(NULL, INPUT, 0);
1000 maxlen = STATUSPOS - plen - 2;
1001 if (ilen - rover.edit_scroll < maxlen)
1002 rover.edit_scroll = MAX(ilen - maxlen, 0);
1003 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
1004 rover.edit_scroll = rover.edit.left - maxlen;
1005 else if (rover.edit.left < rover.edit_scroll)
1006 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
1007 color_set(RVC_PROMPT, NULL);
1008 mvaddstr(LINES - 1, 0, prompt);
1009 color_set(color, NULL);
1010 mbstowcs(WBUF, INPUT, COLS);
1011 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
1012 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
1013 color_set(DEFAULT, NULL);
1014 if (rover.edit_scroll)
1015 mvaddch(LINES - 1, plen - 1, '<');
1016 if (ilen > rover.edit_scroll + maxlen)
1017 mvaddch(LINES - 1, plen + maxlen, '>');
1018 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
1021 int
1022 main(int argc, char *argv[])
1024 int i, ch;
1025 char *program;
1026 char *entry;
1027 const char *key;
1028 const char *clip_path;
1029 DIR *d;
1030 EditStat edit_stat;
1031 FILE *save_cwd_file = NULL;
1032 FILE *save_marks_file = NULL;
1033 FILE *clip_file;
1035 if (argc >= 2) {
1036 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
1037 printf("rover %s\n", RV_VERSION);
1038 return 0;
1039 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1040 printf(
1041 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
1042 " Browse current directory or the ones specified.\n\n"
1043 " or: rover -h|--help\n"
1044 " Print this help message and exit.\n\n"
1045 " or: rover -v|--version\n"
1046 " Print program version and exit.\n\n"
1047 "See rover(1) for more information.\n"
1048 "Rover homepage: <https://github.com/lecram/rover>.\n"
1050 return 0;
1051 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
1052 if (argc > 2) {
1053 save_cwd_file = fopen(argv[2], "w");
1054 argc -= 2; argv += 2;
1055 } else {
1056 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1057 return 1;
1059 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
1060 if (argc > 2) {
1061 save_marks_file = fopen(argv[2], "a");
1062 argc -= 2; argv += 2;
1063 } else {
1064 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1065 return 1;
1069 get_user_programs();
1070 init_term();
1071 rover.nfiles = 0;
1072 for (i = 0; i < 10; i++) {
1073 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1074 rover.tabs[i].flags = RV_FLAGS;
1076 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1077 for (i = 1; i < argc && i < 10; i++) {
1078 if ((d = opendir(argv[i]))) {
1079 realpath(argv[i], rover.tabs[i].cwd);
1080 closedir(d);
1081 } else
1082 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1084 getcwd(rover.tabs[i].cwd, PATH_MAX);
1085 for (i++; i < 10; i++)
1086 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1087 for (i = 0; i < 10; i++)
1088 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1089 strcat(rover.tabs[i].cwd, "/");
1090 rover.tab = 1;
1091 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1092 init_marks(&rover.marks);
1093 cd(1);
1094 strcpy(CLIPBOARD, CWD);
1095 strcat(CLIPBOARD, ENAME(ESEL));
1096 while (1) {
1097 ch = rover_getch();
1098 key = keyname(ch);
1099 clear_message();
1100 if (!strcmp(key, RVK_QUIT)) break;
1101 else if (ch >= '0' && ch <= '9') {
1102 rover.tab = ch - '0';
1103 cd(0);
1104 } else if (!strcmp(key, RVK_HELP)) {
1105 spawn((char *[]) {"man", "rover", NULL});
1106 } else if (!strcmp(key, RVK_DOWN)) {
1107 if (!rover.nfiles) continue;
1108 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1109 update_view();
1110 } else if (!strcmp(key, RVK_UP)) {
1111 if (!rover.nfiles) continue;
1112 ESEL = MAX(ESEL - 1, 0);
1113 update_view();
1114 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1115 if (!rover.nfiles) continue;
1116 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1117 if (rover.nfiles > HEIGHT)
1118 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1119 update_view();
1120 } else if (!strcmp(key, RVK_JUMP_UP)) {
1121 if (!rover.nfiles) continue;
1122 ESEL = MAX(ESEL - RV_JUMP, 0);
1123 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1124 update_view();
1125 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1126 if (!rover.nfiles) continue;
1127 ESEL = 0;
1128 update_view();
1129 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1130 if (!rover.nfiles) continue;
1131 ESEL = rover.nfiles - 1;
1132 update_view();
1133 } else if (!strcmp(key, RVK_CD_DOWN)) {
1134 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1135 if (chdir(ENAME(ESEL)) == -1) {
1136 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1137 continue;
1139 strcat(CWD, ENAME(ESEL));
1140 cd(1);
1141 } else if (!strcmp(key, RVK_CD_UP)) {
1142 char *dirname, first;
1143 if (!strcmp(CWD, "/")) continue;
1144 CWD[strlen(CWD) - 1] = '\0';
1145 dirname = strrchr(CWD, '/') + 1;
1146 first = dirname[0];
1147 dirname[0] = '\0';
1148 cd(1);
1149 dirname[0] = first;
1150 dirname[strlen(dirname)] = '/';
1151 try_to_sel(dirname);
1152 dirname[0] = '\0';
1153 if (rover.nfiles > HEIGHT)
1154 SCROLL = ESEL - HEIGHT / 2;
1155 update_view();
1156 } else if (!strcmp(key, RVK_HOME)) {
1157 strcpy(CWD, getenv("HOME"));
1158 if (CWD[strlen(CWD) - 1] != '/')
1159 strcat(CWD, "/");
1160 cd(1);
1161 } else if (!strcmp(key, RVK_TARGET)) {
1162 char *bname, first;
1163 int is_dir = S_ISDIR(EMODE(ESEL));
1164 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1165 if (len == -1) continue;
1166 BUF1[len] = '\0';
1167 if (access(BUF1, F_OK) == -1) {
1168 char *msg;
1169 switch (errno) {
1170 case EACCES:
1171 msg = "Cannot access \"%s\".";
1172 break;
1173 case ENOENT:
1174 msg = "\"%s\" does not exist.";
1175 break;
1176 default:
1177 msg = "Cannot navigate to \"%s\".";
1179 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1180 message(RED, msg, BUF2);
1181 continue;
1183 realpath(BUF1, CWD);
1184 len = strlen(CWD);
1185 if (CWD[len - 1] == '/')
1186 CWD[len - 1] = '\0';
1187 bname = strrchr(CWD, '/') + 1;
1188 first = *bname;
1189 *bname = '\0';
1190 cd(1);
1191 *bname = first;
1192 if (is_dir)
1193 strcat(CWD, "/");
1194 try_to_sel(bname);
1195 *bname = '\0';
1196 update_view();
1197 } else if (!strcmp(key, RVK_COPY_PATH)) {
1198 clip_path = getenv("CLIP");
1199 if (!clip_path) goto copy_path_fail;
1200 clip_file = fopen(clip_path, "w");
1201 if (!clip_file) goto copy_path_fail;
1202 fprintf(clip_file, "%s%s\n", CWD, ENAME(ESEL));
1203 fclose(clip_file);
1204 goto copy_path_done;
1205 copy_path_fail:
1206 strcpy(CLIPBOARD, CWD);
1207 strcat(CLIPBOARD, ENAME(ESEL));
1208 copy_path_done:
1210 } else if (!strcmp(key, RVK_PASTE_PATH)) {
1211 clip_path = getenv("CLIP");
1212 if (!clip_path) goto paste_path_fail;
1213 clip_file = fopen(clip_path, "r");
1214 if (!clip_file) goto paste_path_fail;
1215 fscanf(clip_file, "%s\n", CLIPBOARD);
1216 fclose(clip_file);
1217 paste_path_fail:
1218 strcpy(BUF1, CLIPBOARD);
1219 strcpy(CWD, dirname(BUF1));
1220 if (strcmp(CWD, "/"))
1221 strcat(CWD, "/");
1222 cd(1);
1223 strcpy(BUF1, CLIPBOARD);
1224 try_to_sel(strstr(CLIPBOARD, basename(BUF1)));
1225 update_view();
1226 } else if (!strcmp(key, RVK_REFRESH)) {
1227 reload();
1228 } else if (!strcmp(key, RVK_SHELL)) {
1229 program = user_shell;
1230 if (program) {
1231 #ifdef RV_SHELL
1232 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1233 #else
1234 spawn((char *[]) {program, NULL});
1235 #endif
1236 reload();
1238 } else if (!strcmp(key, RVK_VIEW)) {
1239 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1240 if (open_with_env(user_pager, ENAME(ESEL)))
1241 cd(0);
1242 } else if (!strcmp(key, RVK_EDIT)) {
1243 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1244 if (open_with_env(user_editor, ENAME(ESEL)))
1245 cd(0);
1246 } else if (!strcmp(key, RVK_OPEN)) {
1247 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1248 if (open_with_env(user_open, ENAME(ESEL)))
1249 cd(0);
1250 } else if (!strcmp(key, RVK_SEARCH)) {
1251 int oldsel, oldscroll, length;
1252 if (!rover.nfiles) continue;
1253 oldsel = ESEL;
1254 oldscroll = SCROLL;
1255 start_line_edit("");
1256 update_input(RVP_SEARCH, RED);
1257 while ((edit_stat = get_line_edit()) == CONTINUE) {
1258 int sel;
1259 Color color = RED;
1260 length = strlen(INPUT);
1261 if (length) {
1262 for (sel = 0; sel < rover.nfiles; sel++)
1263 if (!strncmp(ENAME(sel), INPUT, length))
1264 break;
1265 if (sel < rover.nfiles) {
1266 color = GREEN;
1267 ESEL = sel;
1268 if (rover.nfiles > HEIGHT) {
1269 if (sel < 3)
1270 SCROLL = 0;
1271 else if (sel - 3 > rover.nfiles - HEIGHT)
1272 SCROLL = rover.nfiles - HEIGHT;
1273 else
1274 SCROLL = sel - 3;
1277 } else {
1278 ESEL = oldsel;
1279 SCROLL = oldscroll;
1281 update_view();
1282 update_input(RVP_SEARCH, color);
1284 if (edit_stat == CANCEL) {
1285 ESEL = oldsel;
1286 SCROLL = oldscroll;
1288 clear_message();
1289 update_view();
1290 } else if (!strcmp(key, RVK_TG_FILES)) {
1291 FLAGS ^= SHOW_FILES;
1292 reload();
1293 } else if (!strcmp(key, RVK_TG_DIRS)) {
1294 FLAGS ^= SHOW_DIRS;
1295 reload();
1296 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1297 FLAGS ^= SHOW_HIDDEN;
1298 reload();
1299 } else if (!strcmp(key, RVK_NEW_FILE)) {
1300 int ok = 0;
1301 start_line_edit("");
1302 update_input(RVP_NEW_FILE, RED);
1303 while ((edit_stat = get_line_edit()) == CONTINUE) {
1304 int length = strlen(INPUT);
1305 ok = length;
1306 for (i = 0; i < rover.nfiles; i++) {
1307 if (
1308 !strncmp(ENAME(i), INPUT, length) &&
1309 (!strcmp(ENAME(i) + length, "") ||
1310 !strcmp(ENAME(i) + length, "/"))
1311 ) {
1312 ok = 0;
1313 break;
1316 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1318 clear_message();
1319 if (edit_stat == CONFIRM) {
1320 if (ok) {
1321 if (addfile(INPUT) == 0) {
1322 cd(1);
1323 try_to_sel(INPUT);
1324 update_view();
1325 } else
1326 message(RED, "Could not create \"%s\".", INPUT);
1327 } else
1328 message(RED, "\"%s\" already exists.", INPUT);
1330 } else if (!strcmp(key, RVK_NEW_DIR)) {
1331 int ok = 0;
1332 start_line_edit("");
1333 update_input(RVP_NEW_DIR, RED);
1334 while ((edit_stat = get_line_edit()) == CONTINUE) {
1335 int length = strlen(INPUT);
1336 ok = length;
1337 for (i = 0; i < rover.nfiles; i++) {
1338 if (
1339 !strncmp(ENAME(i), INPUT, length) &&
1340 (!strcmp(ENAME(i) + length, "") ||
1341 !strcmp(ENAME(i) + length, "/"))
1342 ) {
1343 ok = 0;
1344 break;
1347 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1349 clear_message();
1350 if (edit_stat == CONFIRM) {
1351 if (ok) {
1352 if (adddir(INPUT) == 0) {
1353 cd(1);
1354 strcat(INPUT, "/");
1355 try_to_sel(INPUT);
1356 update_view();
1357 } else
1358 message(RED, "Could not create \"%s/\".", INPUT);
1359 } else
1360 message(RED, "\"%s\" already exists.", INPUT);
1362 } else if (!strcmp(key, RVK_RENAME)) {
1363 int ok = 0;
1364 char *last;
1365 int isdir;
1366 strcpy(INPUT, ENAME(ESEL));
1367 last = INPUT + strlen(INPUT) - 1;
1368 if ((isdir = *last == '/'))
1369 *last = '\0';
1370 start_line_edit(INPUT);
1371 update_input(RVP_RENAME, RED);
1372 while ((edit_stat = get_line_edit()) == CONTINUE) {
1373 int length = strlen(INPUT);
1374 ok = length;
1375 for (i = 0; i < rover.nfiles; i++)
1376 if (
1377 !strncmp(ENAME(i), INPUT, length) &&
1378 (!strcmp(ENAME(i) + length, "") ||
1379 !strcmp(ENAME(i) + length, "/"))
1380 ) {
1381 ok = 0;
1382 break;
1384 update_input(RVP_RENAME, ok ? GREEN : RED);
1386 clear_message();
1387 if (edit_stat == CONFIRM) {
1388 if (isdir)
1389 strcat(INPUT, "/");
1390 if (ok) {
1391 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1392 del_mark(&rover.marks, ENAME(ESEL));
1393 add_mark(&rover.marks, CWD, INPUT);
1395 cd(1);
1396 try_to_sel(INPUT);
1397 update_view();
1398 } else
1399 message(RED, "\"%s\" already exists.", INPUT);
1401 } else if (!strcmp(key, RVK_TG_EXEC)) {
1402 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1403 if (S_IXUSR & EMODE(ESEL))
1404 EMODE(ESEL) &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
1405 else
1406 EMODE(ESEL) |= S_IXUSR | S_IXGRP | S_IXOTH ;
1407 if (chmod(ENAME(ESEL), EMODE(ESEL))) {
1408 message(RED, "Failed to change mode of \"%s\".", ENAME(ESEL));
1409 } else {
1410 message(GREEN, "Changed mode of \"%s\".", ENAME(ESEL));
1411 update_view();
1413 } else if (!strcmp(key, RVK_DELETE)) {
1414 if (rover.nfiles) {
1415 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1416 if (rover_getch() == 'Y') {
1417 const char *name = ENAME(ESEL);
1418 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1419 reload();
1420 if (ret)
1421 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1422 } else
1423 clear_message();
1424 } else
1425 message(RED, "No entry selected for deletion.");
1426 } else if (!strcmp(key, RVK_TG_MARK)) {
1427 if (MARKED(ESEL))
1428 del_mark(&rover.marks, ENAME(ESEL));
1429 else
1430 add_mark(&rover.marks, CWD, ENAME(ESEL));
1431 MARKED(ESEL) = !MARKED(ESEL);
1432 ESEL = (ESEL + 1) % rover.nfiles;
1433 update_view();
1434 } else if (!strcmp(key, RVK_INVMARK)) {
1435 for (i = 0; i < rover.nfiles; i++) {
1436 if (MARKED(i))
1437 del_mark(&rover.marks, ENAME(i));
1438 else
1439 add_mark(&rover.marks, CWD, ENAME(i));
1440 MARKED(i) = !MARKED(i);
1442 update_view();
1443 } else if (!strcmp(key, RVK_MARKALL)) {
1444 for (i = 0; i < rover.nfiles; i++)
1445 if (!MARKED(i)) {
1446 add_mark(&rover.marks, CWD, ENAME(i));
1447 MARKED(i) = 1;
1449 update_view();
1450 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1451 if (rover.marks.nentries) {
1452 message(YELLOW, "Delete all marked entries? (Y/n)");
1453 if (rover_getch() == 'Y')
1454 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1455 else
1456 clear_message();
1457 } else
1458 message(RED, "No entries marked for deletion.");
1459 } else if (!strcmp(key, RVK_MARK_COPY)) {
1460 if (rover.marks.nentries) {
1461 if (strcmp(CWD, rover.marks.dirpath))
1462 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1463 else
1464 message(RED, "Cannot copy to the same path.");
1465 } else
1466 message(RED, "No entries marked for copying.");
1467 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1468 if (rover.marks.nentries) {
1469 if (strcmp(CWD, rover.marks.dirpath))
1470 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1471 else
1472 message(RED, "Cannot move to the same path.");
1473 } else
1474 message(RED, "No entries marked for moving.");
1477 if (rover.nfiles)
1478 free_rows(&rover.rows, rover.nfiles);
1479 delwin(rover.window);
1480 if (save_cwd_file != NULL) {
1481 fputs(CWD, save_cwd_file);
1482 fclose(save_cwd_file);
1484 if (save_marks_file != NULL) {
1485 for (i = 0; i < rover.marks.bulk; i++) {
1486 entry = rover.marks.entries[i];
1487 if (entry)
1488 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1490 fclose(save_marks_file);
1492 free_marks(&rover.marks);
1493 return 0;