1 #define _XOPEN_SOURCE 700
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _FILE_OFFSET_BITS 64
11 #include <sys/types.h> /* pid_t, ... */
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(), ... */
19 #include <fcntl.h> /* open() */
20 #include <sys/wait.h> /* waitpid() */
21 #include <signal.h> /* struct sigaction, sigaction() */
28 /* This signal is not defined by POSIX, but should be
29 present on all systems that have resizable terminals. */
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. */
58 #define BULK_THRESH 256
60 /* Information associated to each entry in listing. */
69 /* Dynamic array of marked entries. */
70 typedef struct Marks
{
71 char dirpath
[PATH_MAX
];
77 /* Line editing state. */
79 wchar_t buffer
[BUFLEN
+1];
83 /* Each tab only stores the following information. */
106 volatile sig_atomic_t pending_usr1
;
107 volatile sig_atomic_t pending_winch
;
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
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
);
144 init_marks(Marks
*marks
)
146 strcpy(marks
->dirpath
, "");
147 marks
->bulk
= BULK_INIT
;
149 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
152 /* Unmark all entries. */
154 mark_none(Marks
*marks
)
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
;
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
);
174 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
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
);
190 /* Search for empty slot (there must be one). */
191 for (i
= 0; i
< marks
->bulk
; i
++)
192 if (!marks
->entries
[i
])
196 /* Directory changed. Discard old marks. */
198 strcpy(marks
->dirpath
, dirpath
);
201 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
202 strcpy(marks
->entries
[i
], entry
);
207 del_mark(Marks
*marks
, char *entry
)
211 if (marks
->nentries
> 1) {
212 for (i
= 0; i
< marks
->bulk
; i
++)
213 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
215 free(marks
->entries
[i
]);
216 marks
->entries
[i
] = NULL
;
223 free_marks(Marks
*marks
)
227 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
228 if (marks
->entries
[i
]) {
229 free(marks
->entries
[i
]);
232 free(marks
->entries
);
238 rover
.pending_usr1
= 1;
242 handle_winch(int sig
)
244 rover
.pending_winch
= 1;
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
);
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. */
277 if (rover
.pending_usr1
) {
278 /* SIGUSR1 received: refresh directory listing. */
280 rover
.pending_usr1
= 0;
282 if (rover
.pending_winch
) {
283 /* SIGWINCH received: resize application accordingly. */
284 delwin(rover
.window
);
288 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
289 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
290 SCROLL
= ESEL
- HEIGHT
;
292 rover
.pending_winch
= 0;
296 /* This function must be used in place of getch().
297 It handles signals while waiting for user input. */
303 while ((ch
= getch()) == ERR
)
308 /* This function must be used in place of get_wch().
309 It handles signals while waiting for user input. */
311 rover_get_wch(wint_t *wch
)
315 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
320 /* Get user programs from the environment. */
322 #define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
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). */
341 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
344 /* fork() succeeded. */
347 waitpid(pid
, &status
, 0);
349 kill(getpid(), SIGWINCH
);
350 } else if (pid
== 0) {
352 execvp(args
[0], args
);
357 shell_escaped_cat(char *buf
, char *str
, size_t n
)
359 char *p
= buf
+ strlen(buf
);
361 for (n
--; n
; n
--, str
++) {
382 open_with_env(char *program
, char *path
)
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
});
391 spawn((char *[]) {program
, path
, NULL
});
402 setlocale(LC_ALL
, "");
404 cbreak(); /* Get one character at a time. */
405 timeout(100); /* For getch(). */
407 nonl(); /* No NL->CR/NL on output. */
408 intrflush(stdscr
, FALSE
);
409 keypad(stdscr
, TRUE
);
410 curs_set(FALSE
); /* Hide blinking cursor. */
414 #ifdef NCURSES_EXT_FUNCS
415 use_default_colors();
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
);
433 /* Update the listing view. */
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
);
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
);
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] == '.';
470 wattr_on(rover
.window
, A_REVERSE
, NULL
);
472 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
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
);
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
);
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;
501 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
502 (int) (COLS
- length
- 6),
503 (int) human_size
/ 10, *suffix
);
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
);
515 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
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
) {
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' : ' ';
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. */
544 message(Color color
, char *fmt
, ...)
550 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
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. */
565 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
568 /* Comparison used to sort listing entries. */
570 rowcmp(const void *a
, const void *b
)
572 int isdir1
, isdir2
, cmpdir
;
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. */
583 ls(Row
**rowsp
, uint8_t flags
)
591 if(!(dp
= opendir("."))) return -1;
592 n
= -2; /* We don't want the entries "." and "..". */
593 while (readdir(dp
)) n
++;
595 rows
= malloc(n
* sizeof *rows
);
597 while ((ep
= readdir(dp
))) {
598 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
600 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
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
);
610 strcat(rows
[i
].name
, "/");
611 rows
[i
].mode
= statbuf
.st_mode
;
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
;
622 n
= i
; /* Ignore unused space in array caused by filters. */
623 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
630 free_rows(Row
**rowsp
, int nfiles
)
634 for (i
= 0; i
< nfiles
; i
++)
635 free((*rowsp
)[i
].name
);
640 /* Change working directory to the path in CWD. */
646 message(CYAN
, "Loading \"%s\"...", CWD
);
648 if (reset
) ESEL
= SCROLL
= 0;
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
++)
657 rover
.marks
.entries
[j
] &&
658 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
661 MARKED(i
) = j
< rover
.marks
.bulk
;
664 for (i
= 0; i
< rover
.nfiles
; i
++)
670 /* Select a target entry, if it is present. */
672 try_to_sel(const char *target
)
676 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
678 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
682 /* Reload CWD, but try to keep selection. */
687 strcpy(INPUT
, ENAME(ESEL
));
696 count_dir(const char *path
)
701 char subpath
[PATH_MAX
];
704 if(!(dp
= opendir(path
))) return 0;
706 while ((ep
= readdir(dp
))) {
707 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
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
);
715 total
+= statbuf
.st_size
;
730 chdir(rover
.marks
.dirpath
);
731 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
732 entry
= rover
.marks
.entries
[i
];
735 total
+= count_dir(entry
);
737 lstat(entry
, &statbuf
);
738 total
+= statbuf
.st_size
;
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;
752 E.g. to move directory /src/ (and all its contents) inside /dst/:
753 strcpy(CWD, "/dst/");
754 process_dir(adddir, movfile, deldir, "/src/"); */
756 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
762 char subpath
[PATH_MAX
];
766 char dstpath
[PATH_MAX
];
767 strcpy(dstpath
, CWD
);
768 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
771 if(!(dp
= opendir(path
))) return -1;
772 while ((ep
= readdir(dp
))) {
773 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
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
);
781 ret
|= proc(subpath
);
784 if (pos
) ret
|= pos(path
);
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. */
792 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
793 const char *msg_doing
, const char *msg_done
)
800 message(CYAN
, "%s...", msg_doing
);
802 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
803 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
804 entry
= rover
.marks
.entries
[i
];
807 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
809 if (!strncmp(path
, CWD
, strlen(path
)))
812 ret
= process_dir(pre
, proc
, pos
, path
);
816 del_mark(&rover
.marks
, entry
);
821 rover
.prog
.total
= 0;
823 if (!rover
.marks
.nentries
)
824 message(GREEN
, "%s all marked entries.", msg_done
);
826 message(RED
, "Some errors occured while %s.", msg_doing
);
831 update_progress(off_t delta
)
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
);
842 /* Wrappers for file operations. */
843 static int delfile(const char *path
) {
847 ret
= lstat(path
, &st
);
848 if (ret
< 0) return ret
;
849 update_progress(st
.st_size
);
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. */
857 ret
= creat(path
, 0644);
858 if (ret
< 0) return ret
;
861 static int cpyfile(const char *srcpath
) {
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
;
876 ret
= symlink(BUF1
, dstpath
);
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
);
893 static int adddir(const char *path
) {
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
) {
904 char dstpath
[PATH_MAX
];
906 strcpy(dstpath
, CWD
);
907 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
908 ret
= rename(srcpath
, dstpath
);
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
);
922 start_line_edit(const char *init_input
)
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. */
936 wchar_t eraser
, killer
, wch
;
939 ret
= rover_get_wch((wint_t *) &wch
);
942 if (ret
== KEY_CODE_YES
) {
943 if (wch
== KEY_ENTER
) {
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
);
960 if (wch
== L
'\r' || wch
== L
'\n') {
963 } else if (wch
== L
'\t') {
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
);
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],
983 /* Update line input on the screen. */
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
);
1013 main(int argc
, char *argv
[])
1021 FILE *save_cwd_file
= NULL
;
1022 FILE *save_marks_file
= NULL
;
1025 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
1026 printf("rover %s\n", RV_VERSION
);
1028 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
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"
1040 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
1042 save_cwd_file
= fopen(argv
[2], "w");
1043 argc
-= 2; argv
+= 2;
1045 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1048 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
1050 save_marks_file
= fopen(argv
[2], "a");
1051 argc
-= 2; argv
+= 2;
1053 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1058 get_user_programs();
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
);
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
, "/");
1080 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
1081 init_marks(&rover
.marks
);
1087 if (!strcmp(key
, RVK_QUIT
)) break;
1088 else if (ch
>= '0' && ch
<= '9') {
1089 rover
.tab
= ch
- '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);
1097 } else if (!strcmp(key
, RVK_UP
)) {
1098 if (!rover
.nfiles
) continue;
1099 ESEL
= MAX(ESEL
- 1, 0);
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
);
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);
1112 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1113 if (!rover
.nfiles
) continue;
1116 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1117 if (!rover
.nfiles
) continue;
1118 ESEL
= rover
.nfiles
- 1;
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
));
1126 strcat(CWD
, ENAME(ESEL
));
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;
1137 dirname
[strlen(dirname
)] = '/';
1138 try_to_sel(dirname
);
1140 if (rover
.nfiles
> HEIGHT
)
1141 SCROLL
= ESEL
- HEIGHT
/ 2;
1143 } else if (!strcmp(key
, RVK_HOME
)) {
1144 strcpy(CWD
, getenv("HOME"));
1145 if (CWD
[strlen(CWD
) - 1] != '/')
1148 } else if (!strcmp(key
, RVK_TARGET
)) {
1150 int is_dir
= S_ISDIR(EMODE(ESEL
));
1151 ssize_t len
= readlink(ENAME(ESEL
), BUF1
, BUFLEN
-1);
1152 if (len
== -1) continue;
1154 if (access(BUF1
, F_OK
) == -1) {
1158 msg
= "Cannot access \"%s\".";
1161 msg
= "\"%s\" does not exist.";
1164 msg
= "Cannot navigate to \"%s\".";
1166 strcpy(BUF2
, BUF1
); /* message() uses BUF1. */
1167 message(RED
, msg
, BUF2
);
1170 realpath(BUF1
, CWD
);
1172 if (CWD
[len
- 1] == '/')
1173 CWD
[len
- 1] = '\0';
1174 bname
= strrchr(CWD
, '/') + 1;
1184 } else if (!strcmp(key
, RVK_REFRESH
)) {
1186 } else if (!strcmp(key
, RVK_SHELL
)) {
1187 program
= user_shell
;
1190 spawn((char *[]) {RV_SHELL
, "-c", program
, NULL
});
1192 spawn((char *[]) {program
, NULL
});
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
)))
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
)))
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
)))
1208 } else if (!strcmp(key
, RVK_SEARCH
)) {
1209 int oldsel
, oldscroll
, length
;
1210 if (!rover
.nfiles
) continue;
1213 start_line_edit("");
1214 update_input(RVP_SEARCH
, RED
);
1215 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1218 length
= strlen(INPUT
);
1220 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1221 if (!strncmp(ENAME(sel
), INPUT
, length
))
1223 if (sel
< rover
.nfiles
) {
1226 if (rover
.nfiles
> HEIGHT
) {
1229 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1230 SCROLL
= rover
.nfiles
- HEIGHT
;
1240 update_input(RVP_SEARCH
, color
);
1242 if (edit_stat
== CANCEL
) {
1248 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1249 FLAGS
^= SHOW_FILES
;
1251 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1254 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1255 FLAGS
^= SHOW_HIDDEN
;
1257 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1259 start_line_edit("");
1260 update_input(RVP_NEW_FILE
, RED
);
1261 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1262 int length
= strlen(INPUT
);
1264 for (i
= 0; i
< rover
.nfiles
; i
++) {
1266 !strncmp(ENAME(i
), INPUT
, length
) &&
1267 (!strcmp(ENAME(i
) + length
, "") ||
1268 !strcmp(ENAME(i
) + length
, "/"))
1274 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1277 if (edit_stat
== CONFIRM
) {
1279 if (addfile(INPUT
) == 0) {
1284 message(RED
, "Could not create \"%s\".", INPUT
);
1286 message(RED
, "\"%s\" already exists.", INPUT
);
1288 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1290 start_line_edit("");
1291 update_input(RVP_NEW_DIR
, RED
);
1292 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1293 int length
= strlen(INPUT
);
1295 for (i
= 0; i
< rover
.nfiles
; i
++) {
1297 !strncmp(ENAME(i
), INPUT
, length
) &&
1298 (!strcmp(ENAME(i
) + length
, "") ||
1299 !strcmp(ENAME(i
) + length
, "/"))
1305 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1308 if (edit_stat
== CONFIRM
) {
1310 if (adddir(INPUT
) == 0) {
1316 message(RED
, "Could not create \"%s/\".", INPUT
);
1318 message(RED
, "\"%s\" already exists.", INPUT
);
1320 } else if (!strcmp(key
, RVK_RENAME
)) {
1324 strcpy(INPUT
, ENAME(ESEL
));
1325 last
= INPUT
+ strlen(INPUT
) - 1;
1326 if ((isdir
= *last
== '/'))
1328 start_line_edit(INPUT
);
1329 update_input(RVP_RENAME
, RED
);
1330 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1331 int length
= strlen(INPUT
);
1333 for (i
= 0; i
< rover
.nfiles
; i
++)
1335 !strncmp(ENAME(i
), INPUT
, length
) &&
1336 (!strcmp(ENAME(i
) + length
, "") ||
1337 !strcmp(ENAME(i
) + length
, "/"))
1342 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1345 if (edit_stat
== CONFIRM
) {
1349 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1350 del_mark(&rover
.marks
, ENAME(ESEL
));
1351 add_mark(&rover
.marks
, CWD
, INPUT
);
1357 message(RED
, "\"%s\" already exists.", INPUT
);
1359 } else if (!strcmp(key
, RVK_DELETE
)) {
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
);
1367 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1371 message(RED
, "No entry selected for deletion.");
1372 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1374 del_mark(&rover
.marks
, ENAME(ESEL
));
1376 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1377 MARKED(ESEL
) = !MARKED(ESEL
);
1378 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1380 } else if (!strcmp(key
, RVK_INVMARK
)) {
1381 for (i
= 0; i
< rover
.nfiles
; i
++) {
1383 del_mark(&rover
.marks
, ENAME(i
));
1385 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1386 MARKED(i
) = !MARKED(i
);
1389 } else if (!strcmp(key
, RVK_MARKALL
)) {
1390 for (i
= 0; i
< rover
.nfiles
; i
++)
1392 add_mark(&rover
.marks
, CWD
, ENAME(i
));
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");
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");
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");
1414 message(RED
, "No entries marked for moving.");
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
];
1428 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1430 fclose(save_marks_file
);
1432 free_marks(&rover
.marks
);