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 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. */
59 #define BULK_THRESH 256
61 /* Information associated to each entry in listing. */
70 /* Dynamic array of marked entries. */
71 typedef struct Marks
{
72 char dirpath
[PATH_MAX
];
78 /* Line editing state. */
80 wchar_t buffer
[BUFLEN
+1];
84 /* Each tab only stores the following information. */
107 volatile sig_atomic_t pending_usr1
;
108 volatile sig_atomic_t pending_winch
;
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
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
);
145 init_marks(Marks
*marks
)
147 strcpy(marks
->dirpath
, "");
148 marks
->bulk
= BULK_INIT
;
150 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
153 /* Unmark all entries. */
155 mark_none(Marks
*marks
)
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
;
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
);
175 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
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
);
191 /* Search for empty slot (there must be one). */
192 for (i
= 0; i
< marks
->bulk
; i
++)
193 if (!marks
->entries
[i
])
197 /* Directory changed. Discard old marks. */
199 strcpy(marks
->dirpath
, dirpath
);
202 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
203 strcpy(marks
->entries
[i
], entry
);
208 del_mark(Marks
*marks
, char *entry
)
212 if (marks
->nentries
> 1) {
213 for (i
= 0; i
< marks
->bulk
; i
++)
214 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
216 free(marks
->entries
[i
]);
217 marks
->entries
[i
] = NULL
;
224 free_marks(Marks
*marks
)
228 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
229 if (marks
->entries
[i
]) {
230 free(marks
->entries
[i
]);
233 free(marks
->entries
);
239 rover
.pending_usr1
= 1;
243 handle_winch(int sig
)
245 rover
.pending_winch
= 1;
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
);
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. */
278 if (rover
.pending_usr1
) {
279 /* SIGUSR1 received: refresh directory listing. */
281 rover
.pending_usr1
= 0;
283 if (rover
.pending_winch
) {
284 /* SIGWINCH received: resize application accordingly. */
285 delwin(rover
.window
);
289 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
290 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
291 SCROLL
= ESEL
- HEIGHT
;
293 rover
.pending_winch
= 0;
297 /* This function must be used in place of getch().
298 It handles signals while waiting for user input. */
304 while ((ch
= getch()) == ERR
)
309 /* This function must be used in place of get_wch().
310 It handles signals while waiting for user input. */
312 rover_get_wch(wint_t *wch
)
316 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
321 /* Get user programs from the environment. */
323 #define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
329 ROVER_ENV(user_shell
, SHELL
)
330 ROVER_ENV(user_pager
, PAGER
)
331 ROVER_ENV(user_editor
, VISUAL
)
333 ROVER_ENV(user_editor
, EDITOR
)
334 ROVER_ENV(user_open
, OPEN
)
337 /* Do a fork-exec to external program (e.g. $EDITOR). */
344 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
347 /* fork() succeeded. */
350 waitpid(pid
, &status
, 0);
352 kill(getpid(), SIGWINCH
);
353 } else if (pid
== 0) {
355 execvp(args
[0], args
);
360 shell_escaped_cat(char *buf
, char *str
, size_t n
)
362 char *p
= buf
+ strlen(buf
);
364 for (n
--; n
; n
--, str
++) {
385 open_with_env(char *program
, char *path
)
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
});
394 spawn((char *[]) {program
, path
, NULL
});
405 setlocale(LC_ALL
, "");
407 cbreak(); /* Get one character at a time. */
408 timeout(100); /* For getch(). */
410 nonl(); /* No NL->CR/NL on output. */
411 intrflush(stdscr
, FALSE
);
412 keypad(stdscr
, TRUE
);
413 curs_set(FALSE
); /* Hide blinking cursor. */
417 #ifdef NCURSES_EXT_FUNCS
418 use_default_colors();
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
);
436 /* Update the listing view. */
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
);
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
);
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] == '.';
473 wattr_on(rover
.window
, A_REVERSE
, NULL
);
475 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
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
);
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
);
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;
505 swprintf(WBUF
+ length
, PATH_MAX
- length
, L
"%*d %c",
506 (int) (COLS
- namecols
- 6),
507 (int) human_size
/ 10, *suffix
);
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
);
519 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
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
) {
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' : ' ';
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. */
548 message(Color color
, char *fmt
, ...)
554 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
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. */
569 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
572 /* Comparison used to sort listing entries. */
574 rowcmp(const void *a
, const void *b
)
576 int isdir1
, isdir2
, cmpdir
;
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. */
587 ls(Row
**rowsp
, uint8_t flags
)
595 if(!(dp
= opendir("."))) return -1;
596 n
= -2; /* We don't want the entries "." and "..". */
597 while (readdir(dp
)) n
++;
599 rows
= malloc(n
* sizeof *rows
);
601 while ((ep
= readdir(dp
))) {
602 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
604 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
606 lstat(ep
->d_name
, &statbuf
);
607 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
608 stat(ep
->d_name
, &statbuf
);
609 if (S_ISDIR(statbuf
.st_mode
)) {
610 if (flags
& SHOW_DIRS
) {
611 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
612 strcpy(rows
[i
].name
, ep
->d_name
);
614 strcat(rows
[i
].name
, "/");
615 rows
[i
].mode
= statbuf
.st_mode
;
618 } else if (flags
& SHOW_FILES
) {
619 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
620 strcpy(rows
[i
].name
, ep
->d_name
);
621 rows
[i
].size
= statbuf
.st_size
;
622 rows
[i
].mode
= statbuf
.st_mode
;
626 n
= i
; /* Ignore unused space in array caused by filters. */
627 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
634 free_rows(Row
**rowsp
, int nfiles
)
638 for (i
= 0; i
< nfiles
; i
++)
639 free((*rowsp
)[i
].name
);
644 /* Change working directory to the path in CWD. */
650 message(CYAN
, "Loading \"%s\"...", CWD
);
652 if (chdir(CWD
) == -1) {
653 getcwd(CWD
, PATH_MAX
-1);
654 if (CWD
[strlen(CWD
)-1] != '/')
658 if (reset
) ESEL
= SCROLL
= 0;
660 free_rows(&rover
.rows
, rover
.nfiles
);
661 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
662 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
663 for (i
= 0; i
< rover
.nfiles
; i
++) {
664 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
666 rover
.marks
.entries
[j
] &&
667 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
670 MARKED(i
) = j
< rover
.marks
.bulk
;
673 for (i
= 0; i
< rover
.nfiles
; i
++)
680 /* Select a target entry, if it is present. */
682 try_to_sel(const char *target
)
686 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
688 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
692 /* Reload CWD, but try to keep selection. */
697 strcpy(INPUT
, ENAME(ESEL
));
706 count_dir(const char *path
)
711 char subpath
[PATH_MAX
];
714 if(!(dp
= opendir(path
))) return 0;
716 while ((ep
= readdir(dp
))) {
717 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
719 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
720 lstat(subpath
, &statbuf
);
721 if (S_ISDIR(statbuf
.st_mode
)) {
722 strcat(subpath
, "/");
723 total
+= count_dir(subpath
);
725 total
+= statbuf
.st_size
;
740 chdir(rover
.marks
.dirpath
);
741 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
742 entry
= rover
.marks
.entries
[i
];
745 total
+= count_dir(entry
);
747 lstat(entry
, &statbuf
);
748 total
+= statbuf
.st_size
;
756 /* Recursively process a source directory using CWD as destination root.
757 For each node (i.e. directory), do the following:
758 1. call pre(destination);
759 2. call proc() on every child leaf (i.e. files);
760 3. recurse into every child node;
762 E.g. to move directory /src/ (and all its contents) inside /dst/:
763 strcpy(CWD, "/dst/");
764 process_dir(adddir, movfile, deldir, "/src/"); */
766 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
772 char subpath
[PATH_MAX
];
776 char dstpath
[PATH_MAX
];
777 strcpy(dstpath
, CWD
);
778 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
781 if(!(dp
= opendir(path
))) return -1;
782 while ((ep
= readdir(dp
))) {
783 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
785 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
786 lstat(subpath
, &statbuf
);
787 if (S_ISDIR(statbuf
.st_mode
)) {
788 strcat(subpath
, "/");
789 ret
|= process_dir(pre
, proc
, pos
, subpath
);
791 ret
|= proc(subpath
);
794 if (pos
) ret
|= pos(path
);
798 /* Process all marked entries using CWD as destination root.
799 All marked entries that are directories will be recursively processed.
800 See process_dir() for details on the parameters. */
802 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
803 const char *msg_doing
, const char *msg_done
)
810 message(CYAN
, "%s...", msg_doing
);
812 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
813 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
814 entry
= rover
.marks
.entries
[i
];
817 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
819 if (!strncmp(path
, CWD
, strlen(path
)))
822 ret
= process_dir(pre
, proc
, pos
, path
);
826 del_mark(&rover
.marks
, entry
);
831 rover
.prog
.total
= 0;
833 if (!rover
.marks
.nentries
)
834 message(GREEN
, "%s all marked entries.", msg_done
);
836 message(RED
, "Some errors occured while %s.", msg_doing
);
841 update_progress(off_t delta
)
845 if (!rover
.prog
.total
) return;
846 rover
.prog
.partial
+= delta
;
847 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
848 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
852 /* Wrappers for file operations. */
853 static int delfile(const char *path
) {
857 ret
= lstat(path
, &st
);
858 if (ret
< 0) return ret
;
859 update_progress(st
.st_size
);
862 static PROCESS deldir
= rmdir
;
863 static int addfile(const char *path
) {
864 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
867 ret
= creat(path
, 0644);
868 if (ret
< 0) return ret
;
871 static int cpyfile(const char *srcpath
) {
876 char dstpath
[PATH_MAX
];
878 strcpy(dstpath
, CWD
);
879 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
880 ret
= lstat(srcpath
, &st
);
881 if (ret
< 0) return ret
;
882 if (S_ISLNK(st
.st_mode
)) {
883 ret
= readlink(srcpath
, BUF1
, BUFLEN
-1);
884 if (ret
< 0) return ret
;
886 ret
= symlink(BUF1
, dstpath
);
888 ret
= src
= open(srcpath
, O_RDONLY
);
889 if (ret
< 0) return ret
;
890 ret
= dst
= creat(dstpath
, st
.st_mode
);
891 if (ret
< 0) return ret
;
892 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
893 write(dst
, buf
, size
);
894 update_progress(size
);
903 static int adddir(const char *path
) {
907 ret
= stat(CWD
, &st
);
908 if (ret
< 0) return ret
;
909 return mkdir(path
, st
.st_mode
);
911 static int movfile(const char *srcpath
) {
914 char dstpath
[PATH_MAX
];
916 strcpy(dstpath
, CWD
);
917 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
918 ret
= rename(srcpath
, dstpath
);
920 ret
= lstat(dstpath
, &st
);
921 if (ret
< 0) return ret
;
922 update_progress(st
.st_size
);
923 } else if (errno
== EXDEV
) {
924 ret
= cpyfile(srcpath
);
925 if (ret
< 0) return ret
;
926 ret
= unlink(srcpath
);
932 start_line_edit(const char *init_input
)
935 strncpy(INPUT
, init_input
, BUFLEN
);
936 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
937 rover
.edit
.right
= BUFLEN
- 1;
938 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
939 rover
.edit_scroll
= 0;
942 /* Read input and change editing state accordingly. */
946 wchar_t eraser
, killer
, wch
;
949 ret
= rover_get_wch((wint_t *) &wch
);
952 if (ret
== KEY_CODE_YES
) {
953 if (wch
== KEY_ENTER
) {
956 } else if (wch
== KEY_LEFT
) {
957 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
958 } else if (wch
== KEY_RIGHT
) {
959 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
960 } else if (wch
== KEY_UP
) {
961 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
962 } else if (wch
== KEY_DOWN
) {
963 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
964 } else if (wch
== KEY_BACKSPACE
) {
965 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
966 } else if (wch
== KEY_DC
) {
967 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
970 if (wch
== L
'\r' || wch
== L
'\n') {
973 } else if (wch
== L
'\t') {
976 } else if (wch
== eraser
) {
977 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
978 } else if (wch
== killer
) {
979 EDIT_CLEAR(rover
.edit
);
981 } else if (iswprint(wch
)) {
982 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
985 /* Encode edit contents in INPUT. */
986 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
987 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
988 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
993 /* Update line input on the screen. */
995 update_input(const char *prompt
, Color color
)
997 int plen
, ilen
, maxlen
;
999 plen
= strlen(prompt
);
1000 ilen
= mbstowcs(NULL
, INPUT
, 0);
1001 maxlen
= STATUSPOS
- plen
- 2;
1002 if (ilen
- rover
.edit_scroll
< maxlen
)
1003 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
1004 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
1005 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
1006 else if (rover
.edit
.left
< rover
.edit_scroll
)
1007 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
1008 color_set(RVC_PROMPT
, NULL
);
1009 mvaddstr(LINES
- 1, 0, prompt
);
1010 color_set(color
, NULL
);
1011 mbstowcs(WBUF
, INPUT
, COLS
);
1012 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
1013 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
1014 color_set(DEFAULT
, NULL
);
1015 if (rover
.edit_scroll
)
1016 mvaddch(LINES
- 1, plen
- 1, '<');
1017 if (ilen
> rover
.edit_scroll
+ maxlen
)
1018 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
1019 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
1023 main(int argc
, char *argv
[])
1029 const char *clip_path
;
1032 FILE *save_cwd_file
= NULL
;
1033 FILE *save_marks_file
= NULL
;
1037 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
1038 printf("rover %s\n", RV_VERSION
);
1040 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
1042 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
1043 " Browse current directory or the ones specified.\n\n"
1044 " or: rover -h|--help\n"
1045 " Print this help message and exit.\n\n"
1046 " or: rover -v|--version\n"
1047 " Print program version and exit.\n\n"
1048 "See rover(1) for more information.\n"
1049 "Rover homepage: <https://github.com/lecram/rover>.\n"
1052 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
1054 save_cwd_file
= fopen(argv
[2], "w");
1055 argc
-= 2; argv
+= 2;
1057 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1060 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
1062 save_marks_file
= fopen(argv
[2], "a");
1063 argc
-= 2; argv
+= 2;
1065 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1070 get_user_programs();
1073 for (i
= 0; i
< 10; i
++) {
1074 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
1075 rover
.tabs
[i
].flags
= RV_FLAGS
;
1077 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
1078 for (i
= 1; i
< argc
&& i
< 10; i
++) {
1079 if ((d
= opendir(argv
[i
]))) {
1080 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
1083 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
1085 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
1086 for (i
++; i
< 10; i
++)
1087 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
1088 for (i
= 0; i
< 10; i
++)
1089 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
1090 strcat(rover
.tabs
[i
].cwd
, "/");
1092 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
1093 init_marks(&rover
.marks
);
1095 strcpy(CLIPBOARD
, CWD
);
1096 strcat(CLIPBOARD
, ENAME(ESEL
));
1101 if (!strcmp(key
, RVK_QUIT
)) break;
1102 else if (ch
>= '0' && ch
<= '9') {
1103 rover
.tab
= ch
- '0';
1105 } else if (!strcmp(key
, RVK_HELP
)) {
1106 spawn((char *[]) {"man", "rover", NULL
});
1107 } else if (!strcmp(key
, RVK_DOWN
)) {
1108 if (!rover
.nfiles
) continue;
1109 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
1111 } else if (!strcmp(key
, RVK_UP
)) {
1112 if (!rover
.nfiles
) continue;
1113 ESEL
= MAX(ESEL
- 1, 0);
1115 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
1116 if (!rover
.nfiles
) continue;
1117 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
1118 if (rover
.nfiles
> HEIGHT
)
1119 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
1121 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
1122 if (!rover
.nfiles
) continue;
1123 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
1124 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
1126 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1127 if (!rover
.nfiles
) continue;
1130 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1131 if (!rover
.nfiles
) continue;
1132 ESEL
= rover
.nfiles
- 1;
1134 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1135 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1136 if (chdir(ENAME(ESEL
)) == -1) {
1137 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1140 strcat(CWD
, ENAME(ESEL
));
1142 } else if (!strcmp(key
, RVK_CD_UP
)) {
1143 char *dirname
, first
;
1144 if (!strcmp(CWD
, "/")) continue;
1145 CWD
[strlen(CWD
) - 1] = '\0';
1146 dirname
= strrchr(CWD
, '/') + 1;
1151 dirname
[strlen(dirname
)] = '/';
1152 try_to_sel(dirname
);
1154 if (rover
.nfiles
> HEIGHT
)
1155 SCROLL
= ESEL
- HEIGHT
/ 2;
1157 } else if (!strcmp(key
, RVK_HOME
)) {
1158 strcpy(CWD
, getenv("HOME"));
1159 if (CWD
[strlen(CWD
) - 1] != '/')
1162 } else if (!strcmp(key
, RVK_TARGET
)) {
1164 int is_dir
= S_ISDIR(EMODE(ESEL
));
1165 ssize_t len
= readlink(ENAME(ESEL
), BUF1
, BUFLEN
-1);
1166 if (len
== -1) continue;
1168 if (access(BUF1
, F_OK
) == -1) {
1172 msg
= "Cannot access \"%s\".";
1175 msg
= "\"%s\" does not exist.";
1178 msg
= "Cannot navigate to \"%s\".";
1180 strcpy(BUF2
, BUF1
); /* message() uses BUF1. */
1181 message(RED
, msg
, BUF2
);
1184 realpath(BUF1
, CWD
);
1186 if (CWD
[len
- 1] == '/')
1187 CWD
[len
- 1] = '\0';
1188 bname
= strrchr(CWD
, '/') + 1;
1198 } else if (!strcmp(key
, RVK_COPY_PATH
)) {
1199 clip_path
= getenv("CLIP");
1200 if (!clip_path
) goto copy_path_fail
;
1201 clip_file
= fopen(clip_path
, "w");
1202 if (!clip_file
) goto copy_path_fail
;
1203 fprintf(clip_file
, "%s%s\n", CWD
, ENAME(ESEL
));
1205 goto copy_path_done
;
1207 strcpy(CLIPBOARD
, CWD
);
1208 strcat(CLIPBOARD
, ENAME(ESEL
));
1211 } else if (!strcmp(key
, RVK_PASTE_PATH
)) {
1212 clip_path
= getenv("CLIP");
1213 if (!clip_path
) goto paste_path_fail
;
1214 clip_file
= fopen(clip_path
, "r");
1215 if (!clip_file
) goto paste_path_fail
;
1216 fscanf(clip_file
, "%s\n", CLIPBOARD
);
1219 strcpy(BUF1
, CLIPBOARD
);
1220 strcpy(CWD
, dirname(BUF1
));
1221 if (strcmp(CWD
, "/"))
1224 strcpy(BUF1
, CLIPBOARD
);
1225 try_to_sel(strstr(CLIPBOARD
, basename(BUF1
)));
1227 } else if (!strcmp(key
, RVK_REFRESH
)) {
1229 } else if (!strcmp(key
, RVK_SHELL
)) {
1230 program
= user_shell
;
1233 spawn((char *[]) {RV_SHELL
, "-c", program
, NULL
});
1235 spawn((char *[]) {program
, NULL
});
1239 } else if (!strcmp(key
, RVK_VIEW
)) {
1240 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1241 if (open_with_env(user_pager
, ENAME(ESEL
)))
1243 } else if (!strcmp(key
, RVK_EDIT
)) {
1244 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1245 if (open_with_env(user_editor
, ENAME(ESEL
)))
1247 } else if (!strcmp(key
, RVK_OPEN
)) {
1248 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1249 if (open_with_env(user_open
, ENAME(ESEL
)))
1251 } else if (!strcmp(key
, RVK_SEARCH
)) {
1252 int oldsel
, oldscroll
, length
;
1253 if (!rover
.nfiles
) continue;
1256 start_line_edit("");
1257 update_input(RVP_SEARCH
, RED
);
1258 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1261 length
= strlen(INPUT
);
1263 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1264 if (!strncmp(ENAME(sel
), INPUT
, length
))
1266 if (sel
< rover
.nfiles
) {
1269 if (rover
.nfiles
> HEIGHT
) {
1272 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1273 SCROLL
= rover
.nfiles
- HEIGHT
;
1283 update_input(RVP_SEARCH
, color
);
1285 if (edit_stat
== CANCEL
) {
1291 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1292 FLAGS
^= SHOW_FILES
;
1294 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1297 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1298 FLAGS
^= SHOW_HIDDEN
;
1300 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1302 start_line_edit("");
1303 update_input(RVP_NEW_FILE
, RED
);
1304 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1305 int length
= strlen(INPUT
);
1307 for (i
= 0; i
< rover
.nfiles
; i
++) {
1309 !strncmp(ENAME(i
), INPUT
, length
) &&
1310 (!strcmp(ENAME(i
) + length
, "") ||
1311 !strcmp(ENAME(i
) + length
, "/"))
1317 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1320 if (edit_stat
== CONFIRM
) {
1322 if (addfile(INPUT
) == 0) {
1327 message(RED
, "Could not create \"%s\".", INPUT
);
1329 message(RED
, "\"%s\" already exists.", INPUT
);
1331 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1333 start_line_edit("");
1334 update_input(RVP_NEW_DIR
, RED
);
1335 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1336 int length
= strlen(INPUT
);
1338 for (i
= 0; i
< rover
.nfiles
; i
++) {
1340 !strncmp(ENAME(i
), INPUT
, length
) &&
1341 (!strcmp(ENAME(i
) + length
, "") ||
1342 !strcmp(ENAME(i
) + length
, "/"))
1348 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1351 if (edit_stat
== CONFIRM
) {
1353 if (adddir(INPUT
) == 0) {
1359 message(RED
, "Could not create \"%s/\".", INPUT
);
1361 message(RED
, "\"%s\" already exists.", INPUT
);
1363 } else if (!strcmp(key
, RVK_RENAME
)) {
1367 strcpy(INPUT
, ENAME(ESEL
));
1368 last
= INPUT
+ strlen(INPUT
) - 1;
1369 if ((isdir
= *last
== '/'))
1371 start_line_edit(INPUT
);
1372 update_input(RVP_RENAME
, RED
);
1373 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1374 int length
= strlen(INPUT
);
1376 for (i
= 0; i
< rover
.nfiles
; i
++)
1378 !strncmp(ENAME(i
), INPUT
, length
) &&
1379 (!strcmp(ENAME(i
) + length
, "") ||
1380 !strcmp(ENAME(i
) + length
, "/"))
1385 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1388 if (edit_stat
== CONFIRM
) {
1392 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1393 del_mark(&rover
.marks
, ENAME(ESEL
));
1394 add_mark(&rover
.marks
, CWD
, INPUT
);
1400 message(RED
, "\"%s\" already exists.", INPUT
);
1402 } else if (!strcmp(key
, RVK_TG_EXEC
)) {
1403 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1404 if (S_IXUSR
& EMODE(ESEL
))
1405 EMODE(ESEL
) &= ~(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1407 EMODE(ESEL
) |= S_IXUSR
| S_IXGRP
| S_IXOTH
;
1408 if (chmod(ENAME(ESEL
), EMODE(ESEL
))) {
1409 message(RED
, "Failed to change mode of \"%s\".", ENAME(ESEL
));
1411 message(GREEN
, "Changed mode of \"%s\".", ENAME(ESEL
));
1414 } else if (!strcmp(key
, RVK_DELETE
)) {
1416 message(YELLOW
, "Delete \"%s\"? (Y/n)", ENAME(ESEL
));
1417 if (rover_getch() == 'Y') {
1418 const char *name
= ENAME(ESEL
);
1419 int ret
= ISDIR(ENAME(ESEL
)) ? deldir(name
) : delfile(name
);
1422 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1426 message(RED
, "No entry selected for deletion.");
1427 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1429 del_mark(&rover
.marks
, ENAME(ESEL
));
1431 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1432 MARKED(ESEL
) = !MARKED(ESEL
);
1433 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1435 } else if (!strcmp(key
, RVK_INVMARK
)) {
1436 for (i
= 0; i
< rover
.nfiles
; i
++) {
1438 del_mark(&rover
.marks
, ENAME(i
));
1440 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1441 MARKED(i
) = !MARKED(i
);
1444 } else if (!strcmp(key
, RVK_MARKALL
)) {
1445 for (i
= 0; i
< rover
.nfiles
; i
++)
1447 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1451 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1452 if (rover
.marks
.nentries
) {
1453 message(YELLOW
, "Delete all marked entries? (Y/n)");
1454 if (rover_getch() == 'Y')
1455 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1459 message(RED
, "No entries marked for deletion.");
1460 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1461 if (rover
.marks
.nentries
) {
1462 if (strcmp(CWD
, rover
.marks
.dirpath
))
1463 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1465 message(RED
, "Cannot copy to the same path.");
1467 message(RED
, "No entries marked for copying.");
1468 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1469 if (rover
.marks
.nentries
) {
1470 if (strcmp(CWD
, rover
.marks
.dirpath
))
1471 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1473 message(RED
, "Cannot move to the same path.");
1475 message(RED
, "No entries marked for moving.");
1479 free_rows(&rover
.rows
, rover
.nfiles
);
1480 delwin(rover
.window
);
1481 if (save_cwd_file
!= NULL
) {
1482 fputs(CWD
, save_cwd_file
);
1483 fclose(save_cwd_file
);
1485 if (save_marks_file
!= NULL
) {
1486 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
1487 entry
= rover
.marks
.entries
[i
];
1489 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1491 fclose(save_marks_file
);
1493 free_marks(&rover
.marks
);