Fix pathname splitting when running external programs.
[rover.git] / rover.c
blob47699d3dfda45aac1703a3156787968b2d2017d5
1 #define _XOPEN_SOURCE 700
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _FILE_OFFSET_BITS 64
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <ctype.h>
8 #include <wchar.h>
9 #include <wctype.h>
10 #include <string.h>
11 #include <sys/types.h> /* pid_t, ... */
12 #include <stdio.h>
13 #include <limits.h> /* PATH_MAX */
14 #include <locale.h> /* setlocale(), LC_ALL */
15 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
16 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
17 #include <libgen.h>
18 #include <sys/stat.h>
19 #include <fcntl.h> /* open() */
20 #include <sys/wait.h> /* waitpid() */
21 #include <signal.h> /* struct sigaction, sigaction() */
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <curses.h>
26 #include "config.h"
28 /* This signal is not defined by POSIX, but should be
29 present on all systems that have resizable terminals. */
30 #ifndef SIGWINCH
31 #define SIGWINCH 28
32 #endif
34 /* String buffers. */
35 #define BUFLEN PATH_MAX
36 static char BUF1[BUFLEN];
37 static char BUF2[BUFLEN];
38 static char INPUT[BUFLEN];
39 static wchar_t WBUF[BUFLEN];
41 /* Listing view parameters. */
42 #define HEIGHT (LINES-4)
43 #define STATUSPOS (COLS-16)
45 /* Listing view flags. */
46 #define SHOW_FILES 0x01u
47 #define SHOW_DIRS 0x02u
48 #define SHOW_HIDDEN 0x04u
50 /* Marks parameters. */
51 #define BULK_INIT 5
52 #define BULK_THRESH 256
54 /* Information associated to each entry in listing. */
55 typedef struct Row {
56 char *name;
57 off_t size;
58 mode_t mode;
59 int islink;
60 int marked;
61 } Row;
63 /* Dynamic array of marked entries. */
64 typedef struct Marks {
65 char dirpath[PATH_MAX];
66 int bulk;
67 int nentries;
68 char **entries;
69 } Marks;
71 /* Line editing state. */
72 typedef struct Edit {
73 wchar_t buffer[BUFLEN+1];
74 int left, right;
75 } Edit;
77 /* Each tab only stores the following information. */
78 typedef struct Tab {
79 int scroll;
80 int esel;
81 uint8_t flags;
82 char cwd[PATH_MAX];
83 } Tab;
85 typedef struct Prog {
86 off_t partial;
87 off_t total;
88 const char *msg;
89 } Prog;
91 /* Global state. */
92 static struct Rover {
93 int tab;
94 int nfiles;
95 Row *rows;
96 WINDOW *window;
97 Marks marks;
98 Edit edit;
99 int edit_scroll;
100 volatile sig_atomic_t pending_winch;
101 Prog prog;
102 Tab tabs[10];
103 } rover;
105 /* Macros for accessing global state. */
106 #define ENAME(I) rover.rows[I].name
107 #define ESIZE(I) rover.rows[I].size
108 #define EMODE(I) rover.rows[I].mode
109 #define ISLINK(I) rover.rows[I].islink
110 #define MARKED(I) rover.rows[I].marked
111 #define SCROLL rover.tabs[rover.tab].scroll
112 #define ESEL rover.tabs[rover.tab].esel
113 #define FLAGS rover.tabs[rover.tab].flags
114 #define CWD rover.tabs[rover.tab].cwd
116 /* Helpers. */
117 #define MIN(A, B) ((A) < (B) ? (A) : (B))
118 #define MAX(A, B) ((A) > (B) ? (A) : (B))
119 #define ISDIR(E) (strchr((E), '/') != NULL)
121 /* Line Editing Macros. */
122 #define EDIT_FULL(E) ((E).left == (E).right)
123 #define EDIT_CAN_LEFT(E) ((E).left)
124 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
125 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
126 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
127 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
128 #define EDIT_BACKSPACE(E) (E).left--
129 #define EDIT_DELETE(E) (E).right++
130 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
132 typedef enum EditStat {CONTINUE, CONFIRM, CANCEL} EditStat;
133 typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK} Color;
134 typedef int (*PROCESS)(const char *path);
136 static void
137 init_marks(Marks *marks)
139 strcpy(marks->dirpath, "");
140 marks->bulk = BULK_INIT;
141 marks->nentries = 0;
142 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
145 /* Unmark all entries. */
146 static void
147 mark_none(Marks *marks)
149 int i;
151 strcpy(marks->dirpath, "");
152 for (i = 0; i < marks->bulk && marks->nentries; i++)
153 if (marks->entries[i]) {
154 free(marks->entries[i]);
155 marks->entries[i] = NULL;
156 marks->nentries--;
158 if (marks->bulk > BULK_THRESH) {
159 /* Reset bulk to free some memory. */
160 free(marks->entries);
161 marks->bulk = BULK_INIT;
162 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
166 static void
167 add_mark(Marks *marks, char *dirpath, char *entry)
169 int i;
171 if (!strcmp(marks->dirpath, dirpath)) {
172 /* Append mark to directory. */
173 if (marks->nentries == marks->bulk) {
174 /* Expand bulk to accomodate new entry. */
175 int extra = marks->bulk / 2;
176 marks->bulk += extra; /* bulk *= 1.5; */
177 marks->entries = realloc(marks->entries,
178 marks->bulk * sizeof *marks->entries);
179 memset(&marks->entries[marks->nentries], 0,
180 extra * sizeof *marks->entries);
181 i = marks->nentries;
182 } else {
183 /* Search for empty slot (there must be one). */
184 for (i = 0; i < marks->bulk; i++)
185 if (!marks->entries[i])
186 break;
188 } else {
189 /* Directory changed. Discard old marks. */
190 mark_none(marks);
191 strcpy(marks->dirpath, dirpath);
192 i = 0;
194 marks->entries[i] = malloc(strlen(entry) + 1);
195 strcpy(marks->entries[i], entry);
196 marks->nentries++;
199 static void
200 del_mark(Marks *marks, char *entry)
202 int i;
204 if (marks->nentries > 1) {
205 for (i = 0; i < marks->bulk; i++)
206 if (marks->entries[i] && !strcmp(marks->entries[i], entry))
207 break;
208 free(marks->entries[i]);
209 marks->entries[i] = NULL;
210 marks->nentries--;
211 } else
212 mark_none(marks);
215 static void
216 free_marks(Marks *marks)
218 int i;
220 for (i = 0; i < marks->bulk && marks->nentries; i++)
221 if (marks->entries[i]) {
222 free(marks->entries[i]);
223 marks->nentries--;
225 free(marks->entries);
228 static void
229 handle_winch(int sig)
231 rover.pending_winch = 1;
234 static void
235 enable_handlers()
237 struct sigaction sa;
239 memset(&sa, 0, sizeof (struct sigaction));
240 sa.sa_handler = handle_winch;
241 sigaction(SIGWINCH, &sa, NULL);
244 static void
245 disable_handlers()
247 struct sigaction sa;
249 memset(&sa, 0, sizeof (struct sigaction));
250 sa.sa_handler = SIG_DFL;
251 sigaction(SIGWINCH, &sa, NULL);
254 static void update_view();
256 /* Handle any signals received since last call. */
257 static void
258 sync_signals()
260 if (rover.pending_winch) {
261 /* SIGWINCH received: resize application accordingly. */
262 delwin(rover.window);
263 endwin();
264 refresh();
265 clear();
266 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
267 if (HEIGHT < rover.nfiles && SCROLL + HEIGHT > rover.nfiles)
268 SCROLL = ESEL - HEIGHT;
269 update_view();
270 rover.pending_winch = 0;
274 /* This function must be used in place of getch().
275 It handles signals while waiting for user input. */
276 static int
277 rover_getch()
279 int ch;
281 while ((ch = getch()) == ERR)
282 sync_signals();
283 return ch;
286 /* This function must be used in place of get_wch().
287 It handles signals while waiting for user input. */
288 static int
289 rover_get_wch(wint_t *wch)
291 wint_t ret;
293 while ((ret = get_wch(wch)) == (wint_t) ERR)
294 sync_signals();
295 return ret;
298 /* Do a fork-exec to external program (e.g. $EDITOR). */
299 static void
300 spawn(char **args)
302 pid_t pid;
303 int status;
305 setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
306 pid = fork();
307 if (pid > 0) {
308 /* fork() succeeded. */
309 disable_handlers();
310 endwin();
311 waitpid(pid, &status, 0);
312 enable_handlers();
313 kill(getpid(), SIGWINCH);
314 } else if (pid == 0) {
315 /* Child process. */
316 execvp(args[0], args);
320 static int
321 open_with_env(const char *env, char *path)
323 char *program = getenv(env);
324 if (program) {
325 #ifdef RV_SHELL
326 strncpy(BUF1, program, BUFLEN - 1);
327 strncat(BUF1, " '", BUFLEN - strlen(program) - 1);
328 strncat(BUF1, path, BUFLEN - strlen(program) - 3);
329 strncat(BUF1, "'", BUFLEN - strlen(program) - strlen(path) - 3);
330 spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
331 #else
332 spawn((char *[]) {program, path, NULL});
333 #endif
334 return 1;
336 return 0;
339 /* Curses setup. */
340 static void
341 init_term()
343 setlocale(LC_ALL, "");
344 initscr();
345 cbreak(); /* Get one character at a time. */
346 timeout(100); /* For getch(). */
347 noecho();
348 nonl(); /* No NL->CR/NL on output. */
349 intrflush(stdscr, FALSE);
350 keypad(stdscr, TRUE);
351 curs_set(FALSE); /* Hide blinking cursor. */
352 if (has_colors()) {
353 short bg;
354 start_color();
355 #ifdef NCURSES_EXT_FUNCS
356 use_default_colors();
357 bg = -1;
358 #else
359 bg = COLOR_BLACK;
360 #endif
361 init_pair(RED, COLOR_RED, bg);
362 init_pair(GREEN, COLOR_GREEN, bg);
363 init_pair(YELLOW, COLOR_YELLOW, bg);
364 init_pair(BLUE, COLOR_BLUE, bg);
365 init_pair(CYAN, COLOR_CYAN, bg);
366 init_pair(MAGENTA, COLOR_MAGENTA, bg);
367 init_pair(WHITE, COLOR_WHITE, bg);
368 init_pair(BLACK, COLOR_BLACK, bg);
370 atexit((void (*)(void)) endwin);
371 enable_handlers();
374 /* Update the listing view. */
375 static void
376 update_view()
378 int i, j;
379 int numsize;
380 int ishidden;
381 int marking;
383 mvhline(0, 0, ' ', COLS);
384 attr_on(A_BOLD, NULL);
385 color_set(RVC_TABNUM, NULL);
386 mvaddch(0, COLS - 2, rover.tab + '0');
387 attr_off(A_BOLD, NULL);
388 if (rover.marks.nentries) {
389 numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
390 color_set(RVC_MARKS, NULL);
391 mvaddstr(0, COLS - 3 - numsize, BUF1);
392 } else
393 numsize = -1;
394 color_set(RVC_CWD, NULL);
395 mbstowcs(WBUF, CWD, PATH_MAX);
396 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
397 wcolor_set(rover.window, RVC_BORDER, NULL);
398 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
399 ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
400 /* Selection might not be visible, due to cursor wrapping or window
401 shrinking. In that case, the scroll must be moved to make it visible. */
402 if (rover.nfiles > HEIGHT) {
403 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
404 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
405 } else
406 SCROLL = 0;
407 marking = !strcmp(CWD, rover.marks.dirpath);
408 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
409 ishidden = ENAME(j)[0] == '.';
410 if (j == ESEL)
411 wattr_on(rover.window, A_REVERSE, NULL);
412 if (ISLINK(j))
413 wcolor_set(rover.window, RVC_LINK, NULL);
414 else if (ishidden)
415 wcolor_set(rover.window, RVC_HIDDEN, NULL);
416 else if (S_ISREG(EMODE(j))) {
417 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
418 wcolor_set(rover.window, RVC_EXEC, NULL);
419 else
420 wcolor_set(rover.window, RVC_REG, NULL);
421 } else if (S_ISDIR(EMODE(j)))
422 wcolor_set(rover.window, RVC_DIR, NULL);
423 else if (S_ISCHR(EMODE(j)))
424 wcolor_set(rover.window, RVC_CHR, NULL);
425 else if (S_ISBLK(EMODE(j)))
426 wcolor_set(rover.window, RVC_BLK, NULL);
427 else if (S_ISFIFO(EMODE(j)))
428 wcolor_set(rover.window, RVC_FIFO, NULL);
429 else if (S_ISSOCK(EMODE(j)))
430 wcolor_set(rover.window, RVC_SOCK, NULL);
431 if (S_ISDIR(EMODE(j))) {
432 mbstowcs(WBUF, ENAME(j), PATH_MAX);
433 if (ISLINK(j))
434 wcscat(WBUF, L"/");
435 } else {
436 char *suffix, *suffixes = "BKMGTPEZY";
437 off_t human_size = ESIZE(j) * 10;
438 int length = mbstowcs(NULL, ENAME(j), 0);
439 for (suffix = suffixes; human_size >= 10240; suffix++)
440 human_size = (human_size + 512) / 1024;
441 if (*suffix == 'B')
442 swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
443 (int) (COLS - length - 6),
444 (int) human_size / 10, *suffix);
445 else
446 swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
447 (int) (COLS - length - 8),
448 (int) human_size / 10, (int) human_size % 10, *suffix);
450 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
451 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
452 if (marking && MARKED(j)) {
453 wcolor_set(rover.window, RVC_MARKS, NULL);
454 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
455 } else
456 mvwaddch(rover.window, i + 1, 1, ' ');
457 if (j == ESEL)
458 wattr_off(rover.window, A_REVERSE, NULL);
460 for (; i < HEIGHT; i++)
461 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
462 if (rover.nfiles > HEIGHT) {
463 int center, height;
464 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
465 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
466 if (!height) height = 1;
467 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
468 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
470 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
471 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
472 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
473 if (!rover.nfiles)
474 strcpy(BUF2, "0/0");
475 else
476 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
477 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
478 color_set(RVC_STATUS, NULL);
479 mvaddstr(LINES - 1, STATUSPOS, BUF1);
480 wrefresh(rover.window);
483 /* Show a message on the status bar. */
484 static void
485 message(Color color, char *fmt, ...)
487 int len, pos;
488 va_list args;
490 va_start(args, fmt);
491 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
492 va_end(args);
493 len = strlen(BUF1);
494 pos = (STATUSPOS - len) / 2;
495 attr_on(A_BOLD, NULL);
496 color_set(color, NULL);
497 mvaddstr(LINES - 1, pos, BUF1);
498 color_set(DEFAULT, NULL);
499 attr_off(A_BOLD, NULL);
502 /* Clear message area, leaving only status info. */
503 static void
504 clear_message()
506 mvhline(LINES - 1, 0, ' ', STATUSPOS);
509 /* Comparison used to sort listing entries. */
510 static int
511 rowcmp(const void *a, const void *b)
513 int isdir1, isdir2, cmpdir;
514 const Row *r1 = a;
515 const Row *r2 = b;
516 isdir1 = S_ISDIR(r1->mode);
517 isdir2 = S_ISDIR(r2->mode);
518 cmpdir = isdir2 - isdir1;
519 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
522 /* Get all entries in current working directory. */
523 static int
524 ls(Row **rowsp, uint8_t flags)
526 DIR *dp;
527 struct dirent *ep;
528 struct stat statbuf;
529 Row *rows;
530 int i, n;
532 if(!(dp = opendir("."))) return -1;
533 n = -2; /* We don't want the entries "." and "..". */
534 while (readdir(dp)) n++;
535 rewinddir(dp);
536 rows = malloc(n * sizeof *rows);
537 i = 0;
538 while ((ep = readdir(dp))) {
539 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
540 continue;
541 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
542 continue;
543 lstat(ep->d_name, &statbuf);
544 rows[i].islink = S_ISLNK(statbuf.st_mode);
545 stat(ep->d_name, &statbuf);
546 if (S_ISDIR(statbuf.st_mode)) {
547 if (flags & SHOW_DIRS) {
548 rows[i].name = malloc(strlen(ep->d_name) + 2);
549 strcpy(rows[i].name, ep->d_name);
550 if (!rows[i].islink)
551 strcat(rows[i].name, "/");
552 rows[i].mode = statbuf.st_mode;
553 i++;
555 } else if (flags & SHOW_FILES) {
556 rows[i].name = malloc(strlen(ep->d_name) + 1);
557 strcpy(rows[i].name, ep->d_name);
558 rows[i].size = statbuf.st_size;
559 rows[i].mode = statbuf.st_mode;
560 i++;
563 n = i; /* Ignore unused space in array caused by filters. */
564 qsort(rows, n, sizeof (*rows), rowcmp);
565 closedir(dp);
566 *rowsp = rows;
567 return n;
570 static void
571 free_rows(Row **rowsp, int nfiles)
573 int i;
575 for (i = 0; i < nfiles; i++)
576 free((*rowsp)[i].name);
577 free(*rowsp);
578 *rowsp = NULL;
581 /* Change working directory to the path in CWD. */
582 static void
583 cd(int reset)
585 int i, j;
587 message(CYAN, "Loading...");
588 refresh();
589 if (reset) ESEL = SCROLL = 0;
590 chdir(CWD);
591 if (rover.nfiles)
592 free_rows(&rover.rows, rover.nfiles);
593 rover.nfiles = ls(&rover.rows, FLAGS);
594 if (!strcmp(CWD, rover.marks.dirpath)) {
595 for (i = 0; i < rover.nfiles; i++) {
596 for (j = 0; j < rover.marks.bulk; j++)
597 if (
598 rover.marks.entries[j] &&
599 !strcmp(rover.marks.entries[j], ENAME(i))
601 break;
602 MARKED(i) = j < rover.marks.bulk;
604 } else
605 for (i = 0; i < rover.nfiles; i++)
606 MARKED(i) = 0;
607 clear_message();
608 update_view();
611 /* Select a target entry, if it is present. */
612 static void
613 try_to_sel(const char *target)
615 ESEL = 0;
616 if (!ISDIR(target))
617 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
618 ESEL++;
619 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
620 ESEL++;
623 /* Reload CWD, but try to keep selection. */
624 static void
625 reload()
627 if (rover.nfiles) {
628 strcpy(INPUT, ENAME(ESEL));
629 cd(0);
630 try_to_sel(INPUT);
631 update_view();
632 } else
633 cd(1);
636 static off_t
637 count_dir(const char *path)
639 DIR *dp;
640 struct dirent *ep;
641 struct stat statbuf;
642 char subpath[PATH_MAX];
643 off_t total;
645 if(!(dp = opendir(path))) return 0;
646 total = 0;
647 while ((ep = readdir(dp))) {
648 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
649 continue;
650 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
651 lstat(subpath, &statbuf);
652 if (S_ISDIR(statbuf.st_mode)) {
653 strcat(subpath, "/");
654 total += count_dir(subpath);
655 } else
656 total += statbuf.st_size;
658 closedir(dp);
659 return total;
662 static off_t
663 count_marked()
665 int i;
666 char *entry;
667 off_t total;
668 struct stat statbuf;
670 total = 0;
671 chdir(rover.marks.dirpath);
672 for (i = 0; i < rover.marks.bulk; i++) {
673 entry = rover.marks.entries[i];
674 if (entry) {
675 if (ISDIR(entry)) {
676 total += count_dir(entry);
677 } else {
678 lstat(entry, &statbuf);
679 total += statbuf.st_size;
683 chdir(CWD);
684 return total;
687 /* Recursively process a source directory using CWD as destination root.
688 For each node (i.e. directory), do the following:
689 1. call pre(destination);
690 2. call proc() on every child leaf (i.e. files);
691 3. recurse into every child node;
692 4. call pos(source).
693 E.g. to move directory /src/ (and all its contents) inside /dst/:
694 strcpy(CWD, "/dst/");
695 process_dir(adddir, movfile, deldir, "/src/"); */
696 static int
697 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
699 int ret;
700 DIR *dp;
701 struct dirent *ep;
702 struct stat statbuf;
703 char subpath[PATH_MAX];
705 ret = 0;
706 if (pre) {
707 char dstpath[PATH_MAX];
708 strcpy(dstpath, CWD);
709 strcat(dstpath, path + strlen(rover.marks.dirpath));
710 ret |= pre(dstpath);
712 if(!(dp = opendir(path))) return -1;
713 while ((ep = readdir(dp))) {
714 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
715 continue;
716 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
717 lstat(subpath, &statbuf);
718 if (S_ISDIR(statbuf.st_mode)) {
719 strcat(subpath, "/");
720 ret |= process_dir(pre, proc, pos, subpath);
721 } else
722 ret |= proc(subpath);
724 closedir(dp);
725 if (pos) ret |= pos(path);
726 return ret;
729 /* Process all marked entries using CWD as destination root.
730 All marked entries that are directories will be recursively processed.
731 See process_dir() for details on the parameters. */
732 static void
733 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
734 const char *msg_doing, const char *msg_done)
736 int i, ret;
737 char *entry;
738 char path[PATH_MAX];
740 clear_message();
741 message(CYAN, "%s...", msg_doing);
742 refresh();
743 rover.prog = (Prog) {0, count_marked(), msg_doing};
744 for (i = 0; i < rover.marks.bulk; i++) {
745 entry = rover.marks.entries[i];
746 if (entry) {
747 ret = 0;
748 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
749 if (ISDIR(entry)) {
750 if (!strncmp(path, CWD, strlen(path)))
751 ret = -1;
752 else
753 ret = process_dir(pre, proc, pos, path);
754 } else
755 ret = proc(path);
756 if (!ret) {
757 del_mark(&rover.marks, entry);
758 reload();
762 rover.prog.total = 0;
763 reload();
764 if (!rover.marks.nentries)
765 message(GREEN, "%s all marked entries.", msg_done);
766 else
767 message(RED, "Some errors occured while %s.", msg_doing);
768 RV_ALERT();
771 static void
772 update_progress(off_t delta)
774 int percent;
776 if (!rover.prog.total) return;
777 rover.prog.partial += delta;
778 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
779 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
780 refresh();
783 /* Wrappers for file operations. */
784 static int delfile(const char *path) {
785 int ret;
786 struct stat st;
788 ret = lstat(path, &st);
789 if (ret < 0) return ret;
790 update_progress(st.st_size);
791 return unlink(path);
793 static PROCESS deldir = rmdir;
794 static int addfile(const char *path) {
795 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
796 int ret;
798 ret = creat(path, 0644);
799 if (ret < 0) return ret;
800 return close(ret);
802 static int cpyfile(const char *srcpath) {
803 int src, dst, ret;
804 size_t size;
805 struct stat st;
806 char buf[BUFSIZ];
807 char dstpath[PATH_MAX];
809 strcpy(dstpath, CWD);
810 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
811 ret = lstat(srcpath, &st);
812 if (ret < 0) return ret;
813 if (S_ISLNK(st.st_mode)) {
814 ret = readlink(srcpath, BUF1, BUFLEN-1);
815 if (ret < 0) return ret;
816 BUF1[ret] = '\0';
817 ret = symlink(BUF1, dstpath);
818 } else {
819 ret = src = open(srcpath, O_RDONLY);
820 if (ret < 0) return ret;
821 ret = dst = creat(dstpath, st.st_mode);
822 if (ret < 0) return ret;
823 while ((size = read(src, buf, BUFSIZ)) > 0) {
824 write(dst, buf, size);
825 update_progress(size);
826 sync_signals();
828 close(src);
829 close(dst);
830 ret = 0;
832 return ret;
834 static int adddir(const char *path) {
835 int ret;
836 struct stat st;
838 ret = stat(CWD, &st);
839 if (ret < 0) return ret;
840 return mkdir(path, st.st_mode);
842 static int movfile(const char *srcpath) {
843 int ret;
844 struct stat st;
845 char dstpath[PATH_MAX];
847 strcpy(dstpath, CWD);
848 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
849 ret = rename(srcpath, dstpath);
850 if (ret == 0) {
851 ret = lstat(dstpath, &st);
852 if (ret < 0) return ret;
853 update_progress(st.st_size);
854 } else if (errno == EXDEV) {
855 ret = cpyfile(srcpath);
856 if (ret < 0) return ret;
857 ret = unlink(srcpath);
859 return ret;
862 static void
863 start_line_edit(const char *init_input)
865 curs_set(TRUE);
866 strncpy(INPUT, init_input, BUFLEN);
867 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
868 rover.edit.right = BUFLEN - 1;
869 rover.edit.buffer[BUFLEN] = L'\0';
870 rover.edit_scroll = 0;
873 /* Read input and change editing state accordingly. */
874 static EditStat
875 get_line_edit()
877 wchar_t eraser, killer, wch;
878 int ret, length;
880 ret = rover_get_wch((wint_t *) &wch);
881 erasewchar(&eraser);
882 killwchar(&killer);
883 if (ret == KEY_CODE_YES) {
884 if (wch == KEY_ENTER) {
885 curs_set(FALSE);
886 return CONFIRM;
887 } else if (wch == KEY_LEFT) {
888 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
889 } else if (wch == KEY_RIGHT) {
890 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
891 } else if (wch == KEY_UP) {
892 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
893 } else if (wch == KEY_DOWN) {
894 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
895 } else if (wch == KEY_BACKSPACE) {
896 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
897 } else if (wch == KEY_DC) {
898 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
900 } else {
901 if (wch == L'\r' || wch == L'\n') {
902 curs_set(FALSE);
903 return CONFIRM;
904 } else if (wch == L'\t') {
905 curs_set(FALSE);
906 return CANCEL;
907 } else if (wch == eraser) {
908 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
909 } else if (wch == killer) {
910 EDIT_CLEAR(rover.edit);
911 clear_message();
912 } else if (iswprint(wch)) {
913 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
916 /* Encode edit contents in INPUT. */
917 rover.edit.buffer[rover.edit.left] = L'\0';
918 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
919 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
920 BUFLEN-length);
921 return CONTINUE;
924 /* Update line input on the screen. */
925 static void
926 update_input(const char *prompt, Color color)
928 int plen, ilen, maxlen;
930 plen = strlen(prompt);
931 ilen = mbstowcs(NULL, INPUT, 0);
932 maxlen = STATUSPOS - plen - 2;
933 if (ilen - rover.edit_scroll < maxlen)
934 rover.edit_scroll = MAX(ilen - maxlen, 0);
935 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
936 rover.edit_scroll = rover.edit.left - maxlen;
937 else if (rover.edit.left < rover.edit_scroll)
938 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
939 color_set(RVC_PROMPT, NULL);
940 mvaddstr(LINES - 1, 0, prompt);
941 color_set(color, NULL);
942 mbstowcs(WBUF, INPUT, COLS);
943 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
944 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
945 color_set(DEFAULT, NULL);
946 if (rover.edit_scroll)
947 mvaddch(LINES - 1, plen - 1, '<');
948 if (ilen > rover.edit_scroll + maxlen)
949 mvaddch(LINES - 1, plen + maxlen, '>');
950 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
954 main(int argc, char *argv[])
956 int i, ch;
957 char *program;
958 char *entry;
959 const char *key;
960 DIR *d;
961 EditStat edit_stat;
962 FILE *save_cwd_file = NULL;
963 FILE *save_marks_file = NULL;
965 if (argc >= 2) {
966 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
967 printf("rover %s\n", RV_VERSION);
968 return 0;
969 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
970 printf(
971 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
972 " Browse current directory or the ones specified.\n\n"
973 " or: rover -h|--help\n"
974 " Print this help message and exit.\n\n"
975 " or: rover -v|--version\n"
976 " Print program version and exit.\n\n"
977 "See rover(1) for more information.\n"
978 "Rover homepage: <https://github.com/lecram/rover>.\n"
980 return 0;
981 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
982 if (argc > 2) {
983 save_cwd_file = fopen(argv[2], "w");
984 argc -= 2; argv += 2;
985 } else {
986 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
987 return 1;
989 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
990 if (argc > 2) {
991 save_marks_file = fopen(argv[2], "a");
992 argc -= 2; argv += 2;
993 } else {
994 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
995 return 1;
999 init_term();
1000 rover.nfiles = 0;
1001 for (i = 0; i < 10; i++) {
1002 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1003 rover.tabs[i].flags = SHOW_FILES | SHOW_DIRS;
1005 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1006 for (i = 1; i < argc && i < 10; i++) {
1007 if ((d = opendir(argv[i]))) {
1008 realpath(argv[i], rover.tabs[i].cwd);
1009 closedir(d);
1010 } else
1011 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1013 getcwd(rover.tabs[i].cwd, PATH_MAX);
1014 for (i++; i < 10; i++)
1015 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1016 for (i = 0; i < 10; i++)
1017 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1018 strcat(rover.tabs[i].cwd, "/");
1019 rover.tab = 1;
1020 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1021 init_marks(&rover.marks);
1022 cd(1);
1023 while (1) {
1024 ch = rover_getch();
1025 key = keyname(ch);
1026 clear_message();
1027 if (!strcmp(key, RVK_QUIT)) break;
1028 else if (ch >= '0' && ch <= '9') {
1029 rover.tab = ch - '0';
1030 cd(0);
1031 } else if (!strcmp(key, RVK_HELP)) {
1032 spawn((char *[]) {"man", "rover", NULL});
1033 } else if (!strcmp(key, RVK_DOWN)) {
1034 if (!rover.nfiles) continue;
1035 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1036 update_view();
1037 } else if (!strcmp(key, RVK_UP)) {
1038 if (!rover.nfiles) continue;
1039 ESEL = MAX(ESEL - 1, 0);
1040 update_view();
1041 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1042 if (!rover.nfiles) continue;
1043 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1044 if (rover.nfiles > HEIGHT)
1045 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1046 update_view();
1047 } else if (!strcmp(key, RVK_JUMP_UP)) {
1048 if (!rover.nfiles) continue;
1049 ESEL = MAX(ESEL - RV_JUMP, 0);
1050 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1051 update_view();
1052 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1053 if (!rover.nfiles) continue;
1054 ESEL = 0;
1055 update_view();
1056 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1057 if (!rover.nfiles) continue;
1058 ESEL = rover.nfiles - 1;
1059 update_view();
1060 } else if (!strcmp(key, RVK_CD_DOWN)) {
1061 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1062 if (chdir(ENAME(ESEL)) == -1) {
1063 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1064 continue;
1066 strcat(CWD, ENAME(ESEL));
1067 cd(1);
1068 } else if (!strcmp(key, RVK_CD_UP)) {
1069 char *dirname, first;
1070 if (!strcmp(CWD, "/")) continue;
1071 CWD[strlen(CWD) - 1] = '\0';
1072 dirname = strrchr(CWD, '/') + 1;
1073 first = dirname[0];
1074 dirname[0] = '\0';
1075 cd(1);
1076 dirname[0] = first;
1077 dirname[strlen(dirname)] = '/';
1078 try_to_sel(dirname);
1079 dirname[0] = '\0';
1080 if (rover.nfiles > HEIGHT)
1081 SCROLL = ESEL - HEIGHT / 2;
1082 update_view();
1083 } else if (!strcmp(key, RVK_HOME)) {
1084 strcpy(CWD, getenv("HOME"));
1085 if (CWD[strlen(CWD) - 1] != '/')
1086 strcat(CWD, "/");
1087 cd(1);
1088 } else if (!strcmp(key, RVK_TARGET)) {
1089 char *bname, first;
1090 int is_dir = S_ISDIR(EMODE(ESEL));
1091 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1092 if (len == -1) continue;
1093 BUF1[len] = '\0';
1094 if (access(BUF1, F_OK) == -1) {
1095 char *msg;
1096 switch (errno) {
1097 case EACCES:
1098 msg = "Cannot access \"%s\".";
1099 break;
1100 case ENOENT:
1101 msg = "\"%s\" does not exist.";
1102 break;
1103 default:
1104 msg = "Cannot navigate to \"%s\".";
1106 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1107 message(RED, msg, BUF2);
1108 continue;
1110 realpath(BUF1, CWD);
1111 len = strlen(CWD);
1112 if (CWD[len - 1] == '/')
1113 CWD[len - 1] = '\0';
1114 bname = strrchr(CWD, '/') + 1;
1115 first = *bname;
1116 *bname = '\0';
1117 cd(1);
1118 *bname = first;
1119 if (is_dir)
1120 strcat(CWD, "/");
1121 try_to_sel(bname);
1122 *bname = '\0';
1123 update_view();
1124 } else if (!strcmp(key, RVK_REFRESH)) {
1125 reload();
1126 } else if (!strcmp(key, RVK_SHELL)) {
1127 program = getenv("SHELL");
1128 if (program) {
1129 #ifdef RV_SHELL
1130 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1131 #else
1132 spawn((char *[]) {program, NULL});
1133 #endif
1134 reload();
1136 } else if (!strcmp(key, RVK_VIEW)) {
1137 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1138 if (open_with_env("PAGER", ENAME(ESEL)))
1139 cd(0);
1140 } else if (!strcmp(key, RVK_EDIT)) {
1141 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1142 if (open_with_env("EDITOR", ENAME(ESEL)))
1143 cd(0);
1144 } else if (!strcmp(key, RVK_OPEN)) {
1145 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1146 if (open_with_env("ROVER_OPEN", ENAME(ESEL)))
1147 cd(0);
1148 } else if (!strcmp(key, RVK_SEARCH)) {
1149 int oldsel, oldscroll, length;
1150 if (!rover.nfiles) continue;
1151 oldsel = ESEL;
1152 oldscroll = SCROLL;
1153 start_line_edit("");
1154 update_input(RVP_SEARCH, RED);
1155 while ((edit_stat = get_line_edit()) == CONTINUE) {
1156 int sel;
1157 Color color = RED;
1158 length = strlen(INPUT);
1159 if (length) {
1160 for (sel = 0; sel < rover.nfiles; sel++)
1161 if (!strncmp(ENAME(sel), INPUT, length))
1162 break;
1163 if (sel < rover.nfiles) {
1164 color = GREEN;
1165 ESEL = sel;
1166 if (rover.nfiles > HEIGHT) {
1167 if (sel < 3)
1168 SCROLL = 0;
1169 else if (sel - 3 > rover.nfiles - HEIGHT)
1170 SCROLL = rover.nfiles - HEIGHT;
1171 else
1172 SCROLL = sel - 3;
1175 } else {
1176 ESEL = oldsel;
1177 SCROLL = oldscroll;
1179 update_view();
1180 update_input(RVP_SEARCH, color);
1182 if (edit_stat == CANCEL) {
1183 ESEL = oldsel;
1184 SCROLL = oldscroll;
1186 clear_message();
1187 update_view();
1188 } else if (!strcmp(key, RVK_TG_FILES)) {
1189 FLAGS ^= SHOW_FILES;
1190 reload();
1191 } else if (!strcmp(key, RVK_TG_DIRS)) {
1192 FLAGS ^= SHOW_DIRS;
1193 reload();
1194 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1195 FLAGS ^= SHOW_HIDDEN;
1196 reload();
1197 } else if (!strcmp(key, RVK_NEW_FILE)) {
1198 int ok = 0;
1199 start_line_edit("");
1200 update_input(RVP_NEW_FILE, RED);
1201 while ((edit_stat = get_line_edit()) == CONTINUE) {
1202 int length = strlen(INPUT);
1203 ok = length;
1204 for (i = 0; i < rover.nfiles; i++) {
1205 if (
1206 !strncmp(ENAME(i), INPUT, length) &&
1207 (!strcmp(ENAME(i) + length, "") ||
1208 !strcmp(ENAME(i) + length, "/"))
1210 ok = 0;
1211 break;
1214 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1216 clear_message();
1217 if (edit_stat == CONFIRM) {
1218 if (ok) {
1219 if (addfile(INPUT) == 0) {
1220 cd(1);
1221 try_to_sel(INPUT);
1222 update_view();
1223 } else
1224 message(RED, "Could not create \"%s\".", INPUT);
1225 } else
1226 message(RED, "\"%s\" already exists.", INPUT);
1228 } else if (!strcmp(key, RVK_NEW_DIR)) {
1229 int ok = 0;
1230 start_line_edit("");
1231 update_input(RVP_NEW_DIR, RED);
1232 while ((edit_stat = get_line_edit()) == CONTINUE) {
1233 int length = strlen(INPUT);
1234 ok = length;
1235 for (i = 0; i < rover.nfiles; i++) {
1236 if (
1237 !strncmp(ENAME(i), INPUT, length) &&
1238 (!strcmp(ENAME(i) + length, "") ||
1239 !strcmp(ENAME(i) + length, "/"))
1241 ok = 0;
1242 break;
1245 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1247 clear_message();
1248 if (edit_stat == CONFIRM) {
1249 if (ok) {
1250 if (adddir(INPUT) == 0) {
1251 cd(1);
1252 strcat(INPUT, "/");
1253 try_to_sel(INPUT);
1254 update_view();
1255 } else
1256 message(RED, "Could not create \"%s/\".", INPUT);
1257 } else
1258 message(RED, "\"%s\" already exists.", INPUT);
1260 } else if (!strcmp(key, RVK_RENAME)) {
1261 int ok = 0;
1262 char *last;
1263 int isdir;
1264 strcpy(INPUT, ENAME(ESEL));
1265 last = INPUT + strlen(INPUT) - 1;
1266 if ((isdir = *last == '/'))
1267 *last = '\0';
1268 start_line_edit(INPUT);
1269 update_input(RVP_RENAME, RED);
1270 while ((edit_stat = get_line_edit()) == CONTINUE) {
1271 int length = strlen(INPUT);
1272 ok = length;
1273 for (i = 0; i < rover.nfiles; i++)
1274 if (
1275 !strncmp(ENAME(i), INPUT, length) &&
1276 (!strcmp(ENAME(i) + length, "") ||
1277 !strcmp(ENAME(i) + length, "/"))
1279 ok = 0;
1280 break;
1282 update_input(RVP_RENAME, ok ? GREEN : RED);
1284 clear_message();
1285 if (edit_stat == CONFIRM) {
1286 if (isdir)
1287 strcat(INPUT, "/");
1288 if (ok) {
1289 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1290 del_mark(&rover.marks, ENAME(ESEL));
1291 add_mark(&rover.marks, CWD, INPUT);
1293 cd(1);
1294 try_to_sel(INPUT);
1295 update_view();
1296 } else
1297 message(RED, "\"%s\" already exists.", INPUT);
1299 } else if (!strcmp(key, RVK_DELETE)) {
1300 if (rover.nfiles) {
1301 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1302 if (rover_getch() == 'Y') {
1303 const char *name = ENAME(ESEL);
1304 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1305 reload();
1306 if (ret)
1307 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1308 } else
1309 clear_message();
1310 } else
1311 message(RED, "No entry selected for deletion.");
1312 } else if (!strcmp(key, RVK_TG_MARK)) {
1313 if (MARKED(ESEL))
1314 del_mark(&rover.marks, ENAME(ESEL));
1315 else
1316 add_mark(&rover.marks, CWD, ENAME(ESEL));
1317 MARKED(ESEL) = !MARKED(ESEL);
1318 ESEL = (ESEL + 1) % rover.nfiles;
1319 update_view();
1320 } else if (!strcmp(key, RVK_INVMARK)) {
1321 for (i = 0; i < rover.nfiles; i++) {
1322 if (MARKED(i))
1323 del_mark(&rover.marks, ENAME(i));
1324 else
1325 add_mark(&rover.marks, CWD, ENAME(i));
1326 MARKED(i) = !MARKED(i);
1328 update_view();
1329 } else if (!strcmp(key, RVK_MARKALL)) {
1330 for (i = 0; i < rover.nfiles; i++)
1331 if (!MARKED(i)) {
1332 add_mark(&rover.marks, CWD, ENAME(i));
1333 MARKED(i) = 1;
1335 update_view();
1336 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1337 if (rover.marks.nentries) {
1338 message(YELLOW, "Delete all marked entries? (Y/n)");
1339 if (rover_getch() == 'Y')
1340 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1341 else
1342 clear_message();
1343 } else
1344 message(RED, "No entries marked for deletion.");
1345 } else if (!strcmp(key, RVK_MARK_COPY)) {
1346 if (rover.marks.nentries)
1347 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1348 else
1349 message(RED, "No entries marked for copying.");
1350 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1351 if (rover.marks.nentries)
1352 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1353 else
1354 message(RED, "No entries marked for moving.");
1357 if (rover.nfiles)
1358 free_rows(&rover.rows, rover.nfiles);
1359 delwin(rover.window);
1360 if (save_cwd_file != NULL) {
1361 fputs(CWD, save_cwd_file);
1362 fclose(save_cwd_file);
1364 if (save_marks_file != NULL) {
1365 for (i = 0; i < rover.marks.bulk; i++) {
1366 entry = rover.marks.entries[i];
1367 if (entry)
1368 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1370 fclose(save_marks_file);
1372 free_marks(&rover.marks);
1373 return 0;