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(WBUF, ENAME(j), PATH_MAX);
501 int namecols = wcswidth(WBUF, length);
502 for (suffix = suffixes; human_size >= 10240; suffix++)
503 human_size = (human_size + 512) / 1024;
504 if (*suffix == 'B')
505 swprintf(WBUF + length, PATH_MAX - length, L"%*d %c",
506 (int) (COLS - namecols - 6),
507 (int) human_size / 10, *suffix);
508 else
509 swprintf(WBUF + length, PATH_MAX - length, L"%*d.%d %c",
510 (int) (COLS - namecols - 8),
511 (int) human_size / 10, (int) human_size % 10, *suffix);
513 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
514 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
515 if (marking && MARKED(j)) {
516 wcolor_set(rover.window, RVC_MARKS, NULL);
517 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
518 } else
519 mvwaddch(rover.window, i + 1, 1, ' ');
520 if (j == ESEL)
521 wattr_off(rover.window, A_REVERSE, NULL);
523 for (; i < HEIGHT; i++)
524 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
525 if (rover.nfiles > HEIGHT) {
526 int center, height;
527 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
528 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
529 if (!height) height = 1;
530 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
531 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
533 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
534 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
535 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
536 if (!rover.nfiles)
537 strcpy(BUF2, "0/0");
538 else
539 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
540 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
541 color_set(RVC_STATUS, NULL);
542 mvaddstr(LINES - 1, STATUSPOS, BUF1);
543 wrefresh(rover.window);
546 /* Show a message on the status bar. */
547 static void
548 message(Color color, char *fmt, ...)
550 int len, pos;
551 va_list args;
553 va_start(args, fmt);
554 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
555 va_end(args);
556 len = strlen(BUF1);
557 pos = (STATUSPOS - len) / 2;
558 attr_on(A_BOLD, NULL);
559 color_set(color, NULL);
560 mvaddstr(LINES - 1, pos, BUF1);
561 color_set(DEFAULT, NULL);
562 attr_off(A_BOLD, NULL);
565 /* Clear message area, leaving only status info. */
566 static void
567 clear_message()
569 mvhline(LINES - 1, 0, ' ', STATUSPOS);
572 /* Comparison used to sort listing entries. */
573 static int
574 rowcmp(const void *a, const void *b)
576 int isdir1, isdir2, cmpdir;
577 const Row *r1 = a;
578 const Row *r2 = b;
579 isdir1 = S_ISDIR(r1->mode);
580 isdir2 = S_ISDIR(r2->mode);
581 cmpdir = isdir2 - isdir1;
582 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
585 /* Get all entries in current working directory. */
586 static int
587 ls(Row **rowsp, uint8_t flags)
589 DIR *dp;
590 struct dirent *ep;
591 struct stat statbuf;
592 Row *rows;
593 int i, n;
595 if(!(dp = opendir("."))) return -1;
596 n = -2; /* We don't want the entries "." and "..". */
597 while (readdir(dp)) n++;
598 if (n == 0) {
599 closedir(dp);
600 return 0;
602 rewinddir(dp);
603 rows = malloc(n * sizeof *rows);
604 i = 0;
605 while ((ep = readdir(dp))) {
606 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
607 continue;
608 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
609 continue;
610 lstat(ep->d_name, &statbuf);
611 rows[i].islink = S_ISLNK(statbuf.st_mode);
612 stat(ep->d_name, &statbuf);
613 if (S_ISDIR(statbuf.st_mode)) {
614 if (flags & SHOW_DIRS) {
615 rows[i].name = malloc(strlen(ep->d_name) + 2);
616 strcpy(rows[i].name, ep->d_name);
617 if (!rows[i].islink)
618 strcat(rows[i].name, "/");
619 rows[i].mode = statbuf.st_mode;
620 i++;
622 } else if (flags & SHOW_FILES) {
623 rows[i].name = malloc(strlen(ep->d_name) + 1);
624 strcpy(rows[i].name, ep->d_name);
625 rows[i].size = statbuf.st_size;
626 rows[i].mode = statbuf.st_mode;
627 i++;
630 n = i; /* Ignore unused space in array caused by filters. */
631 qsort(rows, n, sizeof (*rows), rowcmp);
632 closedir(dp);
633 *rowsp = rows;
634 return n;
637 static void
638 free_rows(Row **rowsp, int nfiles)
640 int i;
642 for (i = 0; i < nfiles; i++)
643 free((*rowsp)[i].name);
644 free(*rowsp);
645 *rowsp = NULL;
648 /* Change working directory to the path in CWD. */
649 static void
650 cd(int reset)
652 int i, j;
654 message(CYAN, "Loading \"%s\"...", CWD);
655 refresh();
656 if (chdir(CWD) == -1) {
657 getcwd(CWD, PATH_MAX-1);
658 if (CWD[strlen(CWD)-1] != '/')
659 strcat(CWD, "/");
660 goto done;
662 if (reset) ESEL = SCROLL = 0;
663 if (rover.nfiles)
664 free_rows(&rover.rows, rover.nfiles);
665 rover.nfiles = ls(&rover.rows, FLAGS);
666 if (!strcmp(CWD, rover.marks.dirpath)) {
667 for (i = 0; i < rover.nfiles; i++) {
668 for (j = 0; j < rover.marks.bulk; j++)
669 if (
670 rover.marks.entries[j] &&
671 !strcmp(rover.marks.entries[j], ENAME(i))
673 break;
674 MARKED(i) = j < rover.marks.bulk;
676 } else
677 for (i = 0; i < rover.nfiles; i++)
678 MARKED(i) = 0;
679 done:
680 clear_message();
681 update_view();
684 /* Select a target entry, if it is present. */
685 static void
686 try_to_sel(const char *target)
688 ESEL = 0;
689 if (!ISDIR(target))
690 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
691 ESEL++;
692 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
693 ESEL++;
696 /* Reload CWD, but try to keep selection. */
697 static void
698 reload()
700 if (rover.nfiles) {
701 strcpy(INPUT, ENAME(ESEL));
702 cd(0);
703 try_to_sel(INPUT);
704 update_view();
705 } else
706 cd(1);
709 static off_t
710 count_dir(const char *path)
712 DIR *dp;
713 struct dirent *ep;
714 struct stat statbuf;
715 char subpath[PATH_MAX];
716 off_t total;
718 if(!(dp = opendir(path))) return 0;
719 total = 0;
720 while ((ep = readdir(dp))) {
721 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
722 continue;
723 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
724 lstat(subpath, &statbuf);
725 if (S_ISDIR(statbuf.st_mode)) {
726 strcat(subpath, "/");
727 total += count_dir(subpath);
728 } else
729 total += statbuf.st_size;
731 closedir(dp);
732 return total;
735 static off_t
736 count_marked()
738 int i;
739 char *entry;
740 off_t total;
741 struct stat statbuf;
743 total = 0;
744 chdir(rover.marks.dirpath);
745 for (i = 0; i < rover.marks.bulk; i++) {
746 entry = rover.marks.entries[i];
747 if (entry) {
748 if (ISDIR(entry)) {
749 total += count_dir(entry);
750 } else {
751 lstat(entry, &statbuf);
752 total += statbuf.st_size;
756 chdir(CWD);
757 return total;
760 /* Recursively process a source directory using CWD as destination root.
761 For each node (i.e. directory), do the following:
762 1. call pre(destination);
763 2. call proc() on every child leaf (i.e. files);
764 3. recurse into every child node;
765 4. call pos(source).
766 E.g. to move directory /src/ (and all its contents) inside /dst/:
767 strcpy(CWD, "/dst/");
768 process_dir(adddir, movfile, deldir, "/src/"); */
769 static int
770 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
772 int ret;
773 DIR *dp;
774 struct dirent *ep;
775 struct stat statbuf;
776 char subpath[PATH_MAX];
778 ret = 0;
779 if (pre) {
780 char dstpath[PATH_MAX];
781 strcpy(dstpath, CWD);
782 strcat(dstpath, path + strlen(rover.marks.dirpath));
783 ret |= pre(dstpath);
785 if(!(dp = opendir(path))) return -1;
786 while ((ep = readdir(dp))) {
787 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
788 continue;
789 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
790 lstat(subpath, &statbuf);
791 if (S_ISDIR(statbuf.st_mode)) {
792 strcat(subpath, "/");
793 ret |= process_dir(pre, proc, pos, subpath);
794 } else
795 ret |= proc(subpath);
797 closedir(dp);
798 if (pos) ret |= pos(path);
799 return ret;
802 /* Process all marked entries using CWD as destination root.
803 All marked entries that are directories will be recursively processed.
804 See process_dir() for details on the parameters. */
805 static void
806 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
807 const char *msg_doing, const char *msg_done)
809 int i, ret;
810 char *entry;
811 char path[PATH_MAX];
813 clear_message();
814 message(CYAN, "%s...", msg_doing);
815 refresh();
816 rover.prog = (Prog) {0, count_marked(), msg_doing};
817 for (i = 0; i < rover.marks.bulk; i++) {
818 entry = rover.marks.entries[i];
819 if (entry) {
820 ret = 0;
821 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
822 if (ISDIR(entry)) {
823 if (!strncmp(path, CWD, strlen(path)))
824 ret = -1;
825 else
826 ret = process_dir(pre, proc, pos, path);
827 } else
828 ret = proc(path);
829 if (!ret) {
830 del_mark(&rover.marks, entry);
831 reload();
835 rover.prog.total = 0;
836 reload();
837 if (!rover.marks.nentries)
838 message(GREEN, "%s all marked entries.", msg_done);
839 else
840 message(RED, "Some errors occured while %s.", msg_doing);
841 RV_ALERT();
844 static void
845 update_progress(off_t delta)
847 int percent;
849 if (!rover.prog.total) return;
850 rover.prog.partial += delta;
851 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
852 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
853 refresh();
856 /* Wrappers for file operations. */
857 static int delfile(const char *path) {
858 int ret;
859 struct stat st;
861 ret = lstat(path, &st);
862 if (ret < 0) return ret;
863 update_progress(st.st_size);
864 return unlink(path);
866 static PROCESS deldir = rmdir;
867 static int addfile(const char *path) {
868 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
869 int ret;
871 ret = creat(path, 0644);
872 if (ret < 0) return ret;
873 return close(ret);
875 static int cpyfile(const char *srcpath) {
876 int src, dst, ret;
877 size_t size;
878 struct stat st;
879 char buf[BUFSIZ];
880 char dstpath[PATH_MAX];
882 strcpy(dstpath, CWD);
883 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
884 ret = lstat(srcpath, &st);
885 if (ret < 0) return ret;
886 if (S_ISLNK(st.st_mode)) {
887 ret = readlink(srcpath, BUF1, BUFLEN-1);
888 if (ret < 0) return ret;
889 BUF1[ret] = '\0';
890 ret = symlink(BUF1, dstpath);
891 } else {
892 ret = src = open(srcpath, O_RDONLY);
893 if (ret < 0) return ret;
894 ret = dst = creat(dstpath, st.st_mode);
895 if (ret < 0) return ret;
896 while ((size = read(src, buf, BUFSIZ)) > 0) {
897 write(dst, buf, size);
898 update_progress(size);
899 sync_signals();
901 close(src);
902 close(dst);
903 ret = 0;
905 return ret;
907 static int adddir(const char *path) {
908 int ret;
909 struct stat st;
911 ret = stat(CWD, &st);
912 if (ret < 0) return ret;
913 return mkdir(path, st.st_mode);
915 static int movfile(const char *srcpath) {
916 int ret;
917 struct stat st;
918 char dstpath[PATH_MAX];
920 strcpy(dstpath, CWD);
921 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
922 ret = rename(srcpath, dstpath);
923 if (ret == 0) {
924 ret = lstat(dstpath, &st);
925 if (ret < 0) return ret;
926 update_progress(st.st_size);
927 } else if (errno == EXDEV) {
928 ret = cpyfile(srcpath);
929 if (ret < 0) return ret;
930 ret = unlink(srcpath);
932 return ret;
935 static void
936 start_line_edit(const char *init_input)
938 curs_set(TRUE);
939 strncpy(INPUT, init_input, BUFLEN);
940 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
941 rover.edit.right = BUFLEN - 1;
942 rover.edit.buffer[BUFLEN] = L'\0';
943 rover.edit_scroll = 0;
946 /* Read input and change editing state accordingly. */
947 static EditStat
948 get_line_edit()
950 wchar_t eraser, killer, wch;
951 int ret, length;
953 ret = rover_get_wch((wint_t *) &wch);
954 erasewchar(&eraser);
955 killwchar(&killer);
956 if (ret == KEY_CODE_YES) {
957 if (wch == KEY_ENTER) {
958 curs_set(FALSE);
959 return CONFIRM;
960 } else if (wch == KEY_LEFT) {
961 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
962 } else if (wch == KEY_RIGHT) {
963 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
964 } else if (wch == KEY_UP) {
965 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
966 } else if (wch == KEY_DOWN) {
967 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
968 } else if (wch == KEY_BACKSPACE) {
969 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
970 } else if (wch == KEY_DC) {
971 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
973 } else {
974 if (wch == L'\r' || wch == L'\n') {
975 curs_set(FALSE);
976 return CONFIRM;
977 } else if (wch == L'\t') {
978 curs_set(FALSE);
979 return CANCEL;
980 } else if (wch == eraser) {
981 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
982 } else if (wch == killer) {
983 EDIT_CLEAR(rover.edit);
984 clear_message();
985 } else if (iswprint(wch)) {
986 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
989 /* Encode edit contents in INPUT. */
990 rover.edit.buffer[rover.edit.left] = L'\0';
991 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
992 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
993 BUFLEN-length);
994 return CONTINUE;
997 /* Update line input on the screen. */
998 static void
999 update_input(const char *prompt, Color color)
1001 int plen, ilen, maxlen;
1003 plen = strlen(prompt);
1004 ilen = mbstowcs(NULL, INPUT, 0);
1005 maxlen = STATUSPOS - plen - 2;
1006 if (ilen - rover.edit_scroll < maxlen)
1007 rover.edit_scroll = MAX(ilen - maxlen, 0);
1008 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
1009 rover.edit_scroll = rover.edit.left - maxlen;
1010 else if (rover.edit.left < rover.edit_scroll)
1011 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
1012 color_set(RVC_PROMPT, NULL);
1013 mvaddstr(LINES - 1, 0, prompt);
1014 color_set(color, NULL);
1015 mbstowcs(WBUF, INPUT, COLS);
1016 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
1017 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
1018 color_set(DEFAULT, NULL);
1019 if (rover.edit_scroll)
1020 mvaddch(LINES - 1, plen - 1, '<');
1021 if (ilen > rover.edit_scroll + maxlen)
1022 mvaddch(LINES - 1, plen + maxlen, '>');
1023 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
1026 int
1027 main(int argc, char *argv[])
1029 int i, ch;
1030 char *program;
1031 char *entry;
1032 const char *key;
1033 const char *clip_path;
1034 DIR *d;
1035 EditStat edit_stat;
1036 FILE *save_cwd_file = NULL;
1037 FILE *save_marks_file = NULL;
1038 FILE *clip_file;
1040 if (argc >= 2) {
1041 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
1042 printf("rover %s\n", RV_VERSION);
1043 return 0;
1044 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1045 printf(
1046 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
1047 " Browse current directory or the ones specified.\n\n"
1048 " or: rover -h|--help\n"
1049 " Print this help message and exit.\n\n"
1050 " or: rover -v|--version\n"
1051 " Print program version and exit.\n\n"
1052 "See rover(1) for more information.\n"
1053 "Rover homepage: <https://github.com/lecram/rover>.\n"
1055 return 0;
1056 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
1057 if (argc > 2) {
1058 save_cwd_file = fopen(argv[2], "w");
1059 argc -= 2; argv += 2;
1060 } else {
1061 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1062 return 1;
1064 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
1065 if (argc > 2) {
1066 save_marks_file = fopen(argv[2], "a");
1067 argc -= 2; argv += 2;
1068 } else {
1069 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
1070 return 1;
1074 get_user_programs();
1075 init_term();
1076 rover.nfiles = 0;
1077 for (i = 0; i < 10; i++) {
1078 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1079 rover.tabs[i].flags = RV_FLAGS;
1081 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1082 for (i = 1; i < argc && i < 10; i++) {
1083 if ((d = opendir(argv[i]))) {
1084 realpath(argv[i], rover.tabs[i].cwd);
1085 closedir(d);
1086 } else
1087 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1089 getcwd(rover.tabs[i].cwd, PATH_MAX);
1090 for (i++; i < 10; i++)
1091 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1092 for (i = 0; i < 10; i++)
1093 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1094 strcat(rover.tabs[i].cwd, "/");
1095 rover.tab = 1;
1096 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1097 init_marks(&rover.marks);
1098 cd(1);
1099 strcpy(CLIPBOARD, CWD);
1100 if (rover.nfiles > 0)
1101 strcat(CLIPBOARD, ENAME(ESEL));
1102 while (1) {
1103 ch = rover_getch();
1104 key = keyname(ch);
1105 clear_message();
1106 if (!strcmp(key, RVK_QUIT)) break;
1107 else if (ch >= '0' && ch <= '9') {
1108 rover.tab = ch - '0';
1109 cd(0);
1110 } else if (!strcmp(key, RVK_HELP)) {
1111 spawn((char *[]) {"man", "rover", NULL});
1112 } else if (!strcmp(key, RVK_DOWN)) {
1113 if (!rover.nfiles) continue;
1114 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1115 update_view();
1116 } else if (!strcmp(key, RVK_UP)) {
1117 if (!rover.nfiles) continue;
1118 ESEL = MAX(ESEL - 1, 0);
1119 update_view();
1120 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1121 if (!rover.nfiles) continue;
1122 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1123 if (rover.nfiles > HEIGHT)
1124 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1125 update_view();
1126 } else if (!strcmp(key, RVK_JUMP_UP)) {
1127 if (!rover.nfiles) continue;
1128 ESEL = MAX(ESEL - RV_JUMP, 0);
1129 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1130 update_view();
1131 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1132 if (!rover.nfiles) continue;
1133 ESEL = 0;
1134 update_view();
1135 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1136 if (!rover.nfiles) continue;
1137 ESEL = rover.nfiles - 1;
1138 update_view();
1139 } else if (!strcmp(key, RVK_CD_DOWN)) {
1140 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1141 if (chdir(ENAME(ESEL)) == -1) {
1142 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1143 continue;
1145 strcat(CWD, ENAME(ESEL));
1146 cd(1);
1147 } else if (!strcmp(key, RVK_CD_UP)) {
1148 char *dirname, first;
1149 if (!strcmp(CWD, "/")) continue;
1150 CWD[strlen(CWD) - 1] = '\0';
1151 dirname = strrchr(CWD, '/') + 1;
1152 first = dirname[0];
1153 dirname[0] = '\0';
1154 cd(1);
1155 dirname[0] = first;
1156 dirname[strlen(dirname)] = '/';
1157 try_to_sel(dirname);
1158 dirname[0] = '\0';
1159 if (rover.nfiles > HEIGHT)
1160 SCROLL = ESEL - HEIGHT / 2;
1161 update_view();
1162 } else if (!strcmp(key, RVK_HOME)) {
1163 strcpy(CWD, getenv("HOME"));
1164 if (CWD[strlen(CWD) - 1] != '/')
1165 strcat(CWD, "/");
1166 cd(1);
1167 } else if (!strcmp(key, RVK_TARGET)) {
1168 char *bname, first;
1169 int is_dir = S_ISDIR(EMODE(ESEL));
1170 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1171 if (len == -1) continue;
1172 BUF1[len] = '\0';
1173 if (access(BUF1, F_OK) == -1) {
1174 char *msg;
1175 switch (errno) {
1176 case EACCES:
1177 msg = "Cannot access \"%s\".";
1178 break;
1179 case ENOENT:
1180 msg = "\"%s\" does not exist.";
1181 break;
1182 default:
1183 msg = "Cannot navigate to \"%s\".";
1185 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1186 message(RED, msg, BUF2);
1187 continue;
1189 realpath(BUF1, CWD);
1190 len = strlen(CWD);
1191 if (CWD[len - 1] == '/')
1192 CWD[len - 1] = '\0';
1193 bname = strrchr(CWD, '/') + 1;
1194 first = *bname;
1195 *bname = '\0';
1196 cd(1);
1197 *bname = first;
1198 if (is_dir)
1199 strcat(CWD, "/");
1200 try_to_sel(bname);
1201 *bname = '\0';
1202 update_view();
1203 } else if (!strcmp(key, RVK_COPY_PATH)) {
1204 clip_path = getenv("CLIP");
1205 if (!clip_path) goto copy_path_fail;
1206 clip_file = fopen(clip_path, "w");
1207 if (!clip_file) goto copy_path_fail;
1208 fprintf(clip_file, "%s%s\n", CWD, ENAME(ESEL));
1209 fclose(clip_file);
1210 goto copy_path_done;
1211 copy_path_fail:
1212 strcpy(CLIPBOARD, CWD);
1213 strcat(CLIPBOARD, ENAME(ESEL));
1214 copy_path_done:
1216 } else if (!strcmp(key, RVK_PASTE_PATH)) {
1217 clip_path = getenv("CLIP");
1218 if (!clip_path) goto paste_path_fail;
1219 clip_file = fopen(clip_path, "r");
1220 if (!clip_file) goto paste_path_fail;
1221 fscanf(clip_file, "%s\n", CLIPBOARD);
1222 fclose(clip_file);
1223 paste_path_fail:
1224 strcpy(BUF1, CLIPBOARD);
1225 strcpy(CWD, dirname(BUF1));
1226 if (strcmp(CWD, "/"))
1227 strcat(CWD, "/");
1228 cd(1);
1229 strcpy(BUF1, CLIPBOARD);
1230 try_to_sel(strstr(CLIPBOARD, basename(BUF1)));
1231 update_view();
1232 } else if (!strcmp(key, RVK_REFRESH)) {
1233 reload();
1234 } else if (!strcmp(key, RVK_SHELL)) {
1235 program = user_shell;
1236 if (program) {
1237 #ifdef RV_SHELL
1238 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1239 #else
1240 spawn((char *[]) {program, NULL});
1241 #endif
1242 reload();
1244 } else if (!strcmp(key, RVK_VIEW)) {
1245 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1246 if (open_with_env(user_pager, ENAME(ESEL)))
1247 cd(0);
1248 } else if (!strcmp(key, RVK_EDIT)) {
1249 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1250 if (open_with_env(user_editor, ENAME(ESEL)))
1251 cd(0);
1252 } else if (!strcmp(key, RVK_OPEN)) {
1253 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1254 if (open_with_env(user_open, ENAME(ESEL)))
1255 cd(0);
1256 } else if (!strcmp(key, RVK_SEARCH)) {
1257 int oldsel, oldscroll, length;
1258 if (!rover.nfiles) continue;
1259 oldsel = ESEL;
1260 oldscroll = SCROLL;
1261 start_line_edit("");
1262 update_input(RVP_SEARCH, RED);
1263 while ((edit_stat = get_line_edit()) == CONTINUE) {
1264 int sel;
1265 Color color = RED;
1266 length = strlen(INPUT);
1267 if (length) {
1268 for (sel = 0; sel < rover.nfiles; sel++)
1269 if (!strncmp(ENAME(sel), INPUT, length))
1270 break;
1271 if (sel < rover.nfiles) {
1272 color = GREEN;
1273 ESEL = sel;
1274 if (rover.nfiles > HEIGHT) {
1275 if (sel < 3)
1276 SCROLL = 0;
1277 else if (sel - 3 > rover.nfiles - HEIGHT)
1278 SCROLL = rover.nfiles - HEIGHT;
1279 else
1280 SCROLL = sel - 3;
1283 } else {
1284 ESEL = oldsel;
1285 SCROLL = oldscroll;
1287 update_view();
1288 update_input(RVP_SEARCH, color);
1290 if (edit_stat == CANCEL) {
1291 ESEL = oldsel;
1292 SCROLL = oldscroll;
1294 clear_message();
1295 update_view();
1296 } else if (!strcmp(key, RVK_TG_FILES)) {
1297 FLAGS ^= SHOW_FILES;
1298 reload();
1299 } else if (!strcmp(key, RVK_TG_DIRS)) {
1300 FLAGS ^= SHOW_DIRS;
1301 reload();
1302 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1303 FLAGS ^= SHOW_HIDDEN;
1304 reload();
1305 } else if (!strcmp(key, RVK_NEW_FILE)) {
1306 int ok = 0;
1307 start_line_edit("");
1308 update_input(RVP_NEW_FILE, RED);
1309 while ((edit_stat = get_line_edit()) == CONTINUE) {
1310 int length = strlen(INPUT);
1311 ok = length;
1312 for (i = 0; i < rover.nfiles; i++) {
1313 if (
1314 !strncmp(ENAME(i), INPUT, length) &&
1315 (!strcmp(ENAME(i) + length, "") ||
1316 !strcmp(ENAME(i) + length, "/"))
1317 ) {
1318 ok = 0;
1319 break;
1322 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1324 clear_message();
1325 if (edit_stat == CONFIRM) {
1326 if (ok) {
1327 if (addfile(INPUT) == 0) {
1328 cd(1);
1329 try_to_sel(INPUT);
1330 update_view();
1331 } else
1332 message(RED, "Could not create \"%s\".", INPUT);
1333 } else
1334 message(RED, "\"%s\" already exists.", INPUT);
1336 } else if (!strcmp(key, RVK_NEW_DIR)) {
1337 int ok = 0;
1338 start_line_edit("");
1339 update_input(RVP_NEW_DIR, RED);
1340 while ((edit_stat = get_line_edit()) == CONTINUE) {
1341 int length = strlen(INPUT);
1342 ok = length;
1343 for (i = 0; i < rover.nfiles; i++) {
1344 if (
1345 !strncmp(ENAME(i), INPUT, length) &&
1346 (!strcmp(ENAME(i) + length, "") ||
1347 !strcmp(ENAME(i) + length, "/"))
1348 ) {
1349 ok = 0;
1350 break;
1353 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1355 clear_message();
1356 if (edit_stat == CONFIRM) {
1357 if (ok) {
1358 if (adddir(INPUT) == 0) {
1359 cd(1);
1360 strcat(INPUT, "/");
1361 try_to_sel(INPUT);
1362 update_view();
1363 } else
1364 message(RED, "Could not create \"%s/\".", INPUT);
1365 } else
1366 message(RED, "\"%s\" already exists.", INPUT);
1368 } else if (!strcmp(key, RVK_RENAME)) {
1369 int ok = 0;
1370 char *last;
1371 int isdir;
1372 strcpy(INPUT, ENAME(ESEL));
1373 last = INPUT + strlen(INPUT) - 1;
1374 if ((isdir = *last == '/'))
1375 *last = '\0';
1376 start_line_edit(INPUT);
1377 update_input(RVP_RENAME, RED);
1378 while ((edit_stat = get_line_edit()) == CONTINUE) {
1379 int length = strlen(INPUT);
1380 ok = length;
1381 for (i = 0; i < rover.nfiles; i++)
1382 if (
1383 !strncmp(ENAME(i), INPUT, length) &&
1384 (!strcmp(ENAME(i) + length, "") ||
1385 !strcmp(ENAME(i) + length, "/"))
1386 ) {
1387 ok = 0;
1388 break;
1390 update_input(RVP_RENAME, ok ? GREEN : RED);
1392 clear_message();
1393 if (edit_stat == CONFIRM) {
1394 if (isdir)
1395 strcat(INPUT, "/");
1396 if (ok) {
1397 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1398 del_mark(&rover.marks, ENAME(ESEL));
1399 add_mark(&rover.marks, CWD, INPUT);
1401 cd(1);
1402 try_to_sel(INPUT);
1403 update_view();
1404 } else
1405 message(RED, "\"%s\" already exists.", INPUT);
1407 } else if (!strcmp(key, RVK_TG_EXEC)) {
1408 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1409 if (S_IXUSR & EMODE(ESEL))
1410 EMODE(ESEL) &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
1411 else
1412 EMODE(ESEL) |= S_IXUSR | S_IXGRP | S_IXOTH ;
1413 if (chmod(ENAME(ESEL), EMODE(ESEL))) {
1414 message(RED, "Failed to change mode of \"%s\".", ENAME(ESEL));
1415 } else {
1416 message(GREEN, "Changed mode of \"%s\".", ENAME(ESEL));
1417 update_view();
1419 } else if (!strcmp(key, RVK_DELETE)) {
1420 if (rover.nfiles) {
1421 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1422 if (rover_getch() == 'Y') {
1423 const char *name = ENAME(ESEL);
1424 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1425 reload();
1426 if (ret)
1427 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1428 } else
1429 clear_message();
1430 } else
1431 message(RED, "No entry selected for deletion.");
1432 } else if (!strcmp(key, RVK_TG_MARK)) {
1433 if (MARKED(ESEL))
1434 del_mark(&rover.marks, ENAME(ESEL));
1435 else
1436 add_mark(&rover.marks, CWD, ENAME(ESEL));
1437 MARKED(ESEL) = !MARKED(ESEL);
1438 ESEL = (ESEL + 1) % rover.nfiles;
1439 update_view();
1440 } else if (!strcmp(key, RVK_INVMARK)) {
1441 for (i = 0; i < rover.nfiles; i++) {
1442 if (MARKED(i))
1443 del_mark(&rover.marks, ENAME(i));
1444 else
1445 add_mark(&rover.marks, CWD, ENAME(i));
1446 MARKED(i) = !MARKED(i);
1448 update_view();
1449 } else if (!strcmp(key, RVK_MARKALL)) {
1450 for (i = 0; i < rover.nfiles; i++)
1451 if (!MARKED(i)) {
1452 add_mark(&rover.marks, CWD, ENAME(i));
1453 MARKED(i) = 1;
1455 update_view();
1456 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1457 if (rover.marks.nentries) {
1458 message(YELLOW, "Delete all marked entries? (Y/n)");
1459 if (rover_getch() == 'Y')
1460 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1461 else
1462 clear_message();
1463 } else
1464 message(RED, "No entries marked for deletion.");
1465 } else if (!strcmp(key, RVK_MARK_COPY)) {
1466 if (rover.marks.nentries) {
1467 if (strcmp(CWD, rover.marks.dirpath))
1468 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1469 else
1470 message(RED, "Cannot copy to the same path.");
1471 } else
1472 message(RED, "No entries marked for copying.");
1473 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1474 if (rover.marks.nentries) {
1475 if (strcmp(CWD, rover.marks.dirpath))
1476 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1477 else
1478 message(RED, "Cannot move to the same path.");
1479 } else
1480 message(RED, "No entries marked for moving.");
1483 if (rover.nfiles)
1484 free_rows(&rover.rows, rover.nfiles);
1485 delwin(rover.window);
1486 if (save_cwd_file != NULL) {
1487 fputs(CWD, save_cwd_file);
1488 fclose(save_cwd_file);
1490 if (save_marks_file != NULL) {
1491 for (i = 0; i < rover.marks.bulk; i++) {
1492 entry = rover.marks.entries[i];
1493 if (entry)
1494 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1496 fclose(save_marks_file);
1498 free_marks(&rover.marks);
1499 return 0;