Fix off-by-one error in readlink() call.
[rover.git] / rover.c
blobafe7d34a8c8920aaa634ac932660a8ad523042d8
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) - strlen(path) - 2);
329 spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
330 #else
331 spawn((char *[]) {program, path, NULL});
332 #endif
333 return 1;
335 return 0;
338 /* Curses setup. */
339 static void
340 init_term()
342 setlocale(LC_ALL, "");
343 initscr();
344 cbreak(); /* Get one character at a time. */
345 timeout(100); /* For getch(). */
346 noecho();
347 nonl(); /* No NL->CR/NL on output. */
348 intrflush(stdscr, FALSE);
349 keypad(stdscr, TRUE);
350 curs_set(FALSE); /* Hide blinking cursor. */
351 if (has_colors()) {
352 short bg;
353 start_color();
354 #ifdef NCURSES_EXT_FUNCS
355 use_default_colors();
356 bg = -1;
357 #else
358 bg = COLOR_BLACK;
359 #endif
360 init_pair(RED, COLOR_RED, bg);
361 init_pair(GREEN, COLOR_GREEN, bg);
362 init_pair(YELLOW, COLOR_YELLOW, bg);
363 init_pair(BLUE, COLOR_BLUE, bg);
364 init_pair(CYAN, COLOR_CYAN, bg);
365 init_pair(MAGENTA, COLOR_MAGENTA, bg);
366 init_pair(WHITE, COLOR_WHITE, bg);
367 init_pair(BLACK, COLOR_BLACK, bg);
369 atexit((void (*)(void)) endwin);
370 enable_handlers();
373 /* Update the listing view. */
374 static void
375 update_view()
377 int i, j;
378 int numsize;
379 int ishidden;
380 int marking;
382 mvhline(0, 0, ' ', COLS);
383 attr_on(A_BOLD, NULL);
384 color_set(RVC_TABNUM, NULL);
385 mvaddch(0, COLS - 2, rover.tab + '0');
386 attr_off(A_BOLD, NULL);
387 if (rover.marks.nentries) {
388 numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
389 color_set(RVC_MARKS, NULL);
390 mvaddstr(0, COLS - 3 - numsize, BUF1);
391 } else
392 numsize = -1;
393 color_set(RVC_CWD, NULL);
394 mbstowcs(WBUF, CWD, PATH_MAX);
395 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
396 wcolor_set(rover.window, RVC_BORDER, NULL);
397 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
398 ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
399 /* Selection might not be visible, due to cursor wrapping or window
400 shrinking. In that case, the scroll must be moved to make it visible. */
401 if (rover.nfiles > HEIGHT) {
402 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
403 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
404 } else
405 SCROLL = 0;
406 marking = !strcmp(CWD, rover.marks.dirpath);
407 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
408 ishidden = ENAME(j)[0] == '.';
409 if (j == ESEL)
410 wattr_on(rover.window, A_REVERSE, NULL);
411 if (ISLINK(j))
412 wcolor_set(rover.window, RVC_LINK, NULL);
413 else if (ishidden)
414 wcolor_set(rover.window, RVC_HIDDEN, NULL);
415 else if (S_ISREG(EMODE(j))) {
416 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
417 wcolor_set(rover.window, RVC_EXEC, NULL);
418 else
419 wcolor_set(rover.window, RVC_REG, NULL);
420 } else if (S_ISDIR(EMODE(j)))
421 wcolor_set(rover.window, RVC_DIR, NULL);
422 else if (S_ISCHR(EMODE(j)))
423 wcolor_set(rover.window, RVC_CHR, NULL);
424 else if (S_ISBLK(EMODE(j)))
425 wcolor_set(rover.window, RVC_BLK, NULL);
426 else if (S_ISFIFO(EMODE(j)))
427 wcolor_set(rover.window, RVC_FIFO, NULL);
428 else if (S_ISSOCK(EMODE(j)))
429 wcolor_set(rover.window, RVC_SOCK, NULL);
430 if (S_ISDIR(EMODE(j))) {
431 mbstowcs(WBUF, ENAME(j), PATH_MAX);
432 if (ISLINK(j))
433 wcscat(WBUF, L"/");
434 } else {
435 char *suffix, *suffixes = "BKMGTPEZY";
436 off_t human_size = ESIZE(j) * 10;
437 int length = mbstowcs(NULL, ENAME(j), 0);
438 for (suffix = suffixes; human_size >= 10240; suffix++)
439 human_size = (human_size + 512) / 1024;
440 if (*suffix == 'B')
441 swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
442 (int) (COLS - length - 6),
443 (int) human_size / 10, *suffix);
444 else
445 swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
446 (int) (COLS - length - 8),
447 (int) human_size / 10, (int) human_size % 10, *suffix);
449 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
450 mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
451 if (marking && MARKED(j)) {
452 wcolor_set(rover.window, RVC_MARKS, NULL);
453 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
454 } else
455 mvwaddch(rover.window, i + 1, 1, ' ');
456 if (j == ESEL)
457 wattr_off(rover.window, A_REVERSE, NULL);
459 for (; i < HEIGHT; i++)
460 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
461 if (rover.nfiles > HEIGHT) {
462 int center, height;
463 center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
464 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
465 if (!height) height = 1;
466 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
467 mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
469 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
470 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
471 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
472 if (!rover.nfiles)
473 strcpy(BUF2, "0/0");
474 else
475 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
476 snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
477 color_set(RVC_STATUS, NULL);
478 mvaddstr(LINES - 1, STATUSPOS, BUF1);
479 wrefresh(rover.window);
482 /* Show a message on the status bar. */
483 static void
484 message(Color color, char *fmt, ...)
486 int len, pos;
487 va_list args;
489 va_start(args, fmt);
490 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
491 va_end(args);
492 len = strlen(BUF1);
493 pos = (STATUSPOS - len) / 2;
494 attr_on(A_BOLD, NULL);
495 color_set(color, NULL);
496 mvaddstr(LINES - 1, pos, BUF1);
497 color_set(DEFAULT, NULL);
498 attr_off(A_BOLD, NULL);
501 /* Clear message area, leaving only status info. */
502 static void
503 clear_message()
505 mvhline(LINES - 1, 0, ' ', STATUSPOS);
508 /* Comparison used to sort listing entries. */
509 static int
510 rowcmp(const void *a, const void *b)
512 int isdir1, isdir2, cmpdir;
513 const Row *r1 = a;
514 const Row *r2 = b;
515 isdir1 = S_ISDIR(r1->mode);
516 isdir2 = S_ISDIR(r2->mode);
517 cmpdir = isdir2 - isdir1;
518 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
521 /* Get all entries in current working directory. */
522 static int
523 ls(Row **rowsp, uint8_t flags)
525 DIR *dp;
526 struct dirent *ep;
527 struct stat statbuf;
528 Row *rows;
529 int i, n;
531 if(!(dp = opendir("."))) return -1;
532 n = -2; /* We don't want the entries "." and "..". */
533 while (readdir(dp)) n++;
534 rewinddir(dp);
535 rows = malloc(n * sizeof *rows);
536 i = 0;
537 while ((ep = readdir(dp))) {
538 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
539 continue;
540 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
541 continue;
542 lstat(ep->d_name, &statbuf);
543 rows[i].islink = S_ISLNK(statbuf.st_mode);
544 stat(ep->d_name, &statbuf);
545 if (S_ISDIR(statbuf.st_mode)) {
546 if (flags & SHOW_DIRS) {
547 rows[i].name = malloc(strlen(ep->d_name) + 2);
548 strcpy(rows[i].name, ep->d_name);
549 if (!rows[i].islink)
550 strcat(rows[i].name, "/");
551 rows[i].mode = statbuf.st_mode;
552 i++;
554 } else if (flags & SHOW_FILES) {
555 rows[i].name = malloc(strlen(ep->d_name) + 1);
556 strcpy(rows[i].name, ep->d_name);
557 rows[i].size = statbuf.st_size;
558 rows[i].mode = statbuf.st_mode;
559 i++;
562 n = i; /* Ignore unused space in array caused by filters. */
563 qsort(rows, n, sizeof (*rows), rowcmp);
564 closedir(dp);
565 *rowsp = rows;
566 return n;
569 static void
570 free_rows(Row **rowsp, int nfiles)
572 int i;
574 for (i = 0; i < nfiles; i++)
575 free((*rowsp)[i].name);
576 free(*rowsp);
577 *rowsp = NULL;
580 /* Change working directory to the path in CWD. */
581 static void
582 cd(int reset)
584 int i, j;
586 message(CYAN, "Loading...");
587 refresh();
588 if (reset) ESEL = SCROLL = 0;
589 chdir(CWD);
590 if (rover.nfiles)
591 free_rows(&rover.rows, rover.nfiles);
592 rover.nfiles = ls(&rover.rows, FLAGS);
593 if (!strcmp(CWD, rover.marks.dirpath)) {
594 for (i = 0; i < rover.nfiles; i++) {
595 for (j = 0; j < rover.marks.bulk; j++)
596 if (
597 rover.marks.entries[j] &&
598 !strcmp(rover.marks.entries[j], ENAME(i))
600 break;
601 MARKED(i) = j < rover.marks.bulk;
603 } else
604 for (i = 0; i < rover.nfiles; i++)
605 MARKED(i) = 0;
606 clear_message();
607 update_view();
610 /* Select a target entry, if it is present. */
611 static void
612 try_to_sel(const char *target)
614 ESEL = 0;
615 if (!ISDIR(target))
616 while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
617 ESEL++;
618 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
619 ESEL++;
622 /* Reload CWD, but try to keep selection. */
623 static void
624 reload()
626 if (rover.nfiles) {
627 strcpy(INPUT, ENAME(ESEL));
628 cd(0);
629 try_to_sel(INPUT);
630 update_view();
631 } else
632 cd(1);
635 static off_t
636 count_dir(const char *path)
638 DIR *dp;
639 struct dirent *ep;
640 struct stat statbuf;
641 char subpath[PATH_MAX];
642 off_t total;
644 if(!(dp = opendir(path))) return 0;
645 total = 0;
646 while ((ep = readdir(dp))) {
647 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
648 continue;
649 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
650 lstat(subpath, &statbuf);
651 if (S_ISDIR(statbuf.st_mode)) {
652 strcat(subpath, "/");
653 total += count_dir(subpath);
654 } else
655 total += statbuf.st_size;
657 closedir(dp);
658 return total;
661 static off_t
662 count_marked()
664 int i;
665 char *entry;
666 off_t total;
667 struct stat statbuf;
669 total = 0;
670 chdir(rover.marks.dirpath);
671 for (i = 0; i < rover.marks.bulk; i++) {
672 entry = rover.marks.entries[i];
673 if (entry) {
674 if (ISDIR(entry)) {
675 total += count_dir(entry);
676 } else {
677 lstat(entry, &statbuf);
678 total += statbuf.st_size;
682 chdir(CWD);
683 return total;
686 /* Recursively process a source directory using CWD as destination root.
687 For each node (i.e. directory), do the following:
688 1. call pre(destination);
689 2. call proc() on every child leaf (i.e. files);
690 3. recurse into every child node;
691 4. call pos(source).
692 E.g. to move directory /src/ (and all its contents) inside /dst/:
693 strcpy(CWD, "/dst/");
694 process_dir(adddir, movfile, deldir, "/src/"); */
695 static int
696 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
698 int ret;
699 DIR *dp;
700 struct dirent *ep;
701 struct stat statbuf;
702 char subpath[PATH_MAX];
704 ret = 0;
705 if (pre) {
706 char dstpath[PATH_MAX];
707 strcpy(dstpath, CWD);
708 strcat(dstpath, path + strlen(rover.marks.dirpath));
709 ret |= pre(dstpath);
711 if(!(dp = opendir(path))) return -1;
712 while ((ep = readdir(dp))) {
713 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
714 continue;
715 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
716 lstat(subpath, &statbuf);
717 if (S_ISDIR(statbuf.st_mode)) {
718 strcat(subpath, "/");
719 ret |= process_dir(pre, proc, pos, subpath);
720 } else
721 ret |= proc(subpath);
723 closedir(dp);
724 if (pos) ret |= pos(path);
725 return ret;
728 /* Process all marked entries using CWD as destination root.
729 All marked entries that are directories will be recursively processed.
730 See process_dir() for details on the parameters. */
731 static void
732 process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
733 const char *msg_doing, const char *msg_done)
735 int i, ret;
736 char *entry;
737 char path[PATH_MAX];
739 clear_message();
740 message(CYAN, "%s...", msg_doing);
741 refresh();
742 rover.prog = (Prog) {0, count_marked(), msg_doing};
743 for (i = 0; i < rover.marks.bulk; i++) {
744 entry = rover.marks.entries[i];
745 if (entry) {
746 ret = 0;
747 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
748 if (ISDIR(entry)) {
749 if (!strncmp(path, CWD, strlen(path)))
750 ret = -1;
751 else
752 ret = process_dir(pre, proc, pos, path);
753 } else
754 ret = proc(path);
755 if (!ret) {
756 del_mark(&rover.marks, entry);
757 reload();
761 rover.prog.total = 0;
762 reload();
763 if (!rover.marks.nentries)
764 message(GREEN, "%s all marked entries.", msg_done);
765 else
766 message(RED, "Some errors occured while %s.", msg_doing);
767 RV_ALERT();
770 static void
771 update_progress(off_t delta)
773 int percent;
775 if (!rover.prog.total) return;
776 rover.prog.partial += delta;
777 percent = (int) (rover.prog.partial * 100 / rover.prog.total);
778 message(CYAN, "%s...%d%%", rover.prog.msg, percent);
779 refresh();
782 /* Wrappers for file operations. */
783 static int delfile(const char *path) {
784 int ret;
785 struct stat st;
787 ret = lstat(path, &st);
788 if (ret < 0) return ret;
789 update_progress(st.st_size);
790 return unlink(path);
792 static PROCESS deldir = rmdir;
793 static int addfile(const char *path) {
794 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
795 int ret;
797 ret = creat(path, 0644);
798 if (ret < 0) return ret;
799 return close(ret);
801 static int cpyfile(const char *srcpath) {
802 int src, dst, ret;
803 size_t size;
804 struct stat st;
805 char buf[BUFSIZ];
806 char dstpath[PATH_MAX];
808 strcpy(dstpath, CWD);
809 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
810 ret = lstat(srcpath, &st);
811 if (ret < 0) return ret;
812 if (S_ISLNK(st.st_mode)) {
813 ret = readlink(srcpath, BUF1, BUFLEN-1);
814 if (ret < 0) return ret;
815 BUF1[ret] = '\0';
816 ret = symlink(BUF1, dstpath);
817 } else {
818 ret = src = open(srcpath, O_RDONLY);
819 if (ret < 0) return ret;
820 ret = dst = creat(dstpath, st.st_mode);
821 if (ret < 0) return ret;
822 while ((size = read(src, buf, BUFSIZ)) > 0) {
823 write(dst, buf, size);
824 update_progress(size);
825 sync_signals();
827 close(src);
828 close(dst);
829 ret = 0;
831 return ret;
833 static int adddir(const char *path) {
834 int ret;
835 struct stat st;
837 ret = stat(CWD, &st);
838 if (ret < 0) return ret;
839 return mkdir(path, st.st_mode);
841 static int movfile(const char *srcpath) {
842 int ret;
843 struct stat st;
844 char dstpath[PATH_MAX];
846 strcpy(dstpath, CWD);
847 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
848 ret = rename(srcpath, dstpath);
849 if (ret == 0) {
850 ret = lstat(dstpath, &st);
851 if (ret < 0) return ret;
852 update_progress(st.st_size);
853 } else if (errno == EXDEV) {
854 ret = cpyfile(srcpath);
855 if (ret < 0) return ret;
856 ret = unlink(srcpath);
858 return ret;
861 static void
862 start_line_edit(const char *init_input)
864 curs_set(TRUE);
865 strncpy(INPUT, init_input, BUFLEN);
866 rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
867 rover.edit.right = BUFLEN - 1;
868 rover.edit.buffer[BUFLEN] = L'\0';
869 rover.edit_scroll = 0;
872 /* Read input and change editing state accordingly. */
873 static EditStat
874 get_line_edit()
876 wchar_t eraser, killer, wch;
877 int ret, length;
879 ret = rover_get_wch((wint_t *) &wch);
880 erasewchar(&eraser);
881 killwchar(&killer);
882 if (ret == KEY_CODE_YES) {
883 if (wch == KEY_ENTER) {
884 curs_set(FALSE);
885 return CONFIRM;
886 } else if (wch == KEY_LEFT) {
887 if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
888 } else if (wch == KEY_RIGHT) {
889 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
890 } else if (wch == KEY_UP) {
891 while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
892 } else if (wch == KEY_DOWN) {
893 while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
894 } else if (wch == KEY_BACKSPACE) {
895 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
896 } else if (wch == KEY_DC) {
897 if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
899 } else {
900 if (wch == L'\r' || wch == L'\n') {
901 curs_set(FALSE);
902 return CONFIRM;
903 } else if (wch == L'\t') {
904 curs_set(FALSE);
905 return CANCEL;
906 } else if (wch == eraser) {
907 if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
908 } else if (wch == killer) {
909 EDIT_CLEAR(rover.edit);
910 clear_message();
911 } else if (iswprint(wch)) {
912 if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
915 /* Encode edit contents in INPUT. */
916 rover.edit.buffer[rover.edit.left] = L'\0';
917 length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
918 wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
919 BUFLEN-length);
920 return CONTINUE;
923 /* Update line input on the screen. */
924 static void
925 update_input(const char *prompt, Color color)
927 int plen, ilen, maxlen;
929 plen = strlen(prompt);
930 ilen = mbstowcs(NULL, INPUT, 0);
931 maxlen = STATUSPOS - plen - 2;
932 if (ilen - rover.edit_scroll < maxlen)
933 rover.edit_scroll = MAX(ilen - maxlen, 0);
934 else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
935 rover.edit_scroll = rover.edit.left - maxlen;
936 else if (rover.edit.left < rover.edit_scroll)
937 rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
938 color_set(RVC_PROMPT, NULL);
939 mvaddstr(LINES - 1, 0, prompt);
940 color_set(color, NULL);
941 mbstowcs(WBUF, INPUT, COLS);
942 mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
943 mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
944 color_set(DEFAULT, NULL);
945 if (rover.edit_scroll)
946 mvaddch(LINES - 1, plen - 1, '<');
947 if (ilen > rover.edit_scroll + maxlen)
948 mvaddch(LINES - 1, plen + maxlen, '>');
949 move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
953 main(int argc, char *argv[])
955 int i, ch;
956 char *program;
957 char *entry;
958 const char *key;
959 DIR *d;
960 EditStat edit_stat;
961 FILE *save_cwd_file = NULL;
962 FILE *save_marks_file = NULL;
964 if (argc >= 2) {
965 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
966 printf("rover %s\n", RV_VERSION);
967 return 0;
968 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
969 printf(
970 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
971 " Browse current directory or the ones specified.\n\n"
972 " or: rover -h|--help\n"
973 " Print this help message and exit.\n\n"
974 " or: rover -v|--version\n"
975 " Print program version and exit.\n\n"
976 "See rover(1) for more information.\n"
977 "Rover homepage: <https://github.com/lecram/rover>.\n"
979 return 0;
980 } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
981 if (argc > 2) {
982 save_cwd_file = fopen(argv[2], "w");
983 argc -= 2; argv += 2;
984 } else {
985 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
986 return 1;
988 } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
989 if (argc > 2) {
990 save_marks_file = fopen(argv[2], "a");
991 argc -= 2; argv += 2;
992 } else {
993 fprintf(stderr, "error: missing argument to %s\n", argv[1]);
994 return 1;
998 init_term();
999 rover.nfiles = 0;
1000 for (i = 0; i < 10; i++) {
1001 rover.tabs[i].esel = rover.tabs[i].scroll = 0;
1002 rover.tabs[i].flags = SHOW_FILES | SHOW_DIRS;
1004 strcpy(rover.tabs[0].cwd, getenv("HOME"));
1005 for (i = 1; i < argc && i < 10; i++) {
1006 if ((d = opendir(argv[i]))) {
1007 realpath(argv[i], rover.tabs[i].cwd);
1008 closedir(d);
1009 } else
1010 strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
1012 getcwd(rover.tabs[i].cwd, PATH_MAX);
1013 for (i++; i < 10; i++)
1014 strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
1015 for (i = 0; i < 10; i++)
1016 if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
1017 strcat(rover.tabs[i].cwd, "/");
1018 rover.tab = 1;
1019 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1020 init_marks(&rover.marks);
1021 cd(1);
1022 while (1) {
1023 ch = rover_getch();
1024 key = keyname(ch);
1025 clear_message();
1026 if (!strcmp(key, RVK_QUIT)) break;
1027 else if (ch >= '0' && ch <= '9') {
1028 rover.tab = ch - '0';
1029 cd(0);
1030 } else if (!strcmp(key, RVK_HELP)) {
1031 spawn((char *[]) {"man", "rover", NULL});
1032 } else if (!strcmp(key, RVK_DOWN)) {
1033 if (!rover.nfiles) continue;
1034 ESEL = MIN(ESEL + 1, rover.nfiles - 1);
1035 update_view();
1036 } else if (!strcmp(key, RVK_UP)) {
1037 if (!rover.nfiles) continue;
1038 ESEL = MAX(ESEL - 1, 0);
1039 update_view();
1040 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
1041 if (!rover.nfiles) continue;
1042 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
1043 if (rover.nfiles > HEIGHT)
1044 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
1045 update_view();
1046 } else if (!strcmp(key, RVK_JUMP_UP)) {
1047 if (!rover.nfiles) continue;
1048 ESEL = MAX(ESEL - RV_JUMP, 0);
1049 SCROLL = MAX(SCROLL - RV_JUMP, 0);
1050 update_view();
1051 } else if (!strcmp(key, RVK_JUMP_TOP)) {
1052 if (!rover.nfiles) continue;
1053 ESEL = 0;
1054 update_view();
1055 } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
1056 if (!rover.nfiles) continue;
1057 ESEL = rover.nfiles - 1;
1058 update_view();
1059 } else if (!strcmp(key, RVK_CD_DOWN)) {
1060 if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
1061 if (chdir(ENAME(ESEL)) == -1) {
1062 message(RED, "Cannot access \"%s\".", ENAME(ESEL));
1063 continue;
1065 strcat(CWD, ENAME(ESEL));
1066 cd(1);
1067 } else if (!strcmp(key, RVK_CD_UP)) {
1068 char *dirname, first;
1069 if (!strcmp(CWD, "/")) continue;
1070 CWD[strlen(CWD) - 1] = '\0';
1071 dirname = strrchr(CWD, '/') + 1;
1072 first = dirname[0];
1073 dirname[0] = '\0';
1074 cd(1);
1075 dirname[0] = first;
1076 dirname[strlen(dirname)] = '/';
1077 try_to_sel(dirname);
1078 dirname[0] = '\0';
1079 if (rover.nfiles > HEIGHT)
1080 SCROLL = ESEL - HEIGHT / 2;
1081 update_view();
1082 } else if (!strcmp(key, RVK_HOME)) {
1083 strcpy(CWD, getenv("HOME"));
1084 if (CWD[strlen(CWD) - 1] != '/')
1085 strcat(CWD, "/");
1086 cd(1);
1087 } else if (!strcmp(key, RVK_TARGET)) {
1088 char *bname, first;
1089 int is_dir = S_ISDIR(EMODE(ESEL));
1090 ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
1091 if (len == -1) continue;
1092 BUF1[len] = '\0';
1093 if (access(BUF1, F_OK) == -1) {
1094 char *msg;
1095 switch (errno) {
1096 case EACCES:
1097 msg = "Cannot access \"%s\".";
1098 break;
1099 case ENOENT:
1100 msg = "\"%s\" does not exist.";
1101 break;
1102 default:
1103 msg = "Cannot navigate to \"%s\".";
1105 strcpy(BUF2, BUF1); /* message() uses BUF1. */
1106 message(RED, msg, BUF2);
1107 continue;
1109 realpath(BUF1, CWD);
1110 len = strlen(CWD);
1111 if (CWD[len - 1] == '/')
1112 CWD[len - 1] = '\0';
1113 bname = strrchr(CWD, '/') + 1;
1114 first = *bname;
1115 *bname = '\0';
1116 cd(1);
1117 *bname = first;
1118 if (is_dir)
1119 strcat(CWD, "/");
1120 try_to_sel(bname);
1121 *bname = '\0';
1122 update_view();
1123 } else if (!strcmp(key, RVK_REFRESH)) {
1124 reload();
1125 } else if (!strcmp(key, RVK_SHELL)) {
1126 program = getenv("SHELL");
1127 if (program) {
1128 #ifdef RV_SHELL
1129 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
1130 #else
1131 spawn((char *[]) {program, NULL});
1132 #endif
1133 reload();
1135 } else if (!strcmp(key, RVK_VIEW)) {
1136 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1137 if (open_with_env("PAGER", ENAME(ESEL)))
1138 cd(0);
1139 } else if (!strcmp(key, RVK_EDIT)) {
1140 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1141 if (open_with_env("EDITOR", ENAME(ESEL)))
1142 cd(0);
1143 } else if (!strcmp(key, RVK_OPEN)) {
1144 if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
1145 if (open_with_env("ROVER_OPEN", ENAME(ESEL)))
1146 cd(0);
1147 } else if (!strcmp(key, RVK_SEARCH)) {
1148 int oldsel, oldscroll, length;
1149 if (!rover.nfiles) continue;
1150 oldsel = ESEL;
1151 oldscroll = SCROLL;
1152 start_line_edit("");
1153 update_input(RVP_SEARCH, RED);
1154 while ((edit_stat = get_line_edit()) == CONTINUE) {
1155 int sel;
1156 Color color = RED;
1157 length = strlen(INPUT);
1158 if (length) {
1159 for (sel = 0; sel < rover.nfiles; sel++)
1160 if (!strncmp(ENAME(sel), INPUT, length))
1161 break;
1162 if (sel < rover.nfiles) {
1163 color = GREEN;
1164 ESEL = sel;
1165 if (rover.nfiles > HEIGHT) {
1166 if (sel < 3)
1167 SCROLL = 0;
1168 else if (sel - 3 > rover.nfiles - HEIGHT)
1169 SCROLL = rover.nfiles - HEIGHT;
1170 else
1171 SCROLL = sel - 3;
1174 } else {
1175 ESEL = oldsel;
1176 SCROLL = oldscroll;
1178 update_view();
1179 update_input(RVP_SEARCH, color);
1181 if (edit_stat == CANCEL) {
1182 ESEL = oldsel;
1183 SCROLL = oldscroll;
1185 clear_message();
1186 update_view();
1187 } else if (!strcmp(key, RVK_TG_FILES)) {
1188 FLAGS ^= SHOW_FILES;
1189 reload();
1190 } else if (!strcmp(key, RVK_TG_DIRS)) {
1191 FLAGS ^= SHOW_DIRS;
1192 reload();
1193 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
1194 FLAGS ^= SHOW_HIDDEN;
1195 reload();
1196 } else if (!strcmp(key, RVK_NEW_FILE)) {
1197 int ok = 0;
1198 start_line_edit("");
1199 update_input(RVP_NEW_FILE, RED);
1200 while ((edit_stat = get_line_edit()) == CONTINUE) {
1201 int length = strlen(INPUT);
1202 ok = length;
1203 for (i = 0; i < rover.nfiles; i++) {
1204 if (
1205 !strncmp(ENAME(i), INPUT, length) &&
1206 (!strcmp(ENAME(i) + length, "") ||
1207 !strcmp(ENAME(i) + length, "/"))
1209 ok = 0;
1210 break;
1213 update_input(RVP_NEW_FILE, ok ? GREEN : RED);
1215 clear_message();
1216 if (edit_stat == CONFIRM) {
1217 if (ok) {
1218 if (addfile(INPUT) == 0) {
1219 cd(1);
1220 try_to_sel(INPUT);
1221 update_view();
1222 } else
1223 message(RED, "Could not create \"%s\".", INPUT);
1224 } else
1225 message(RED, "\"%s\" already exists.", INPUT);
1227 } else if (!strcmp(key, RVK_NEW_DIR)) {
1228 int ok = 0;
1229 start_line_edit("");
1230 update_input(RVP_NEW_DIR, RED);
1231 while ((edit_stat = get_line_edit()) == CONTINUE) {
1232 int length = strlen(INPUT);
1233 ok = length;
1234 for (i = 0; i < rover.nfiles; i++) {
1235 if (
1236 !strncmp(ENAME(i), INPUT, length) &&
1237 (!strcmp(ENAME(i) + length, "") ||
1238 !strcmp(ENAME(i) + length, "/"))
1240 ok = 0;
1241 break;
1244 update_input(RVP_NEW_DIR, ok ? GREEN : RED);
1246 clear_message();
1247 if (edit_stat == CONFIRM) {
1248 if (ok) {
1249 if (adddir(INPUT) == 0) {
1250 cd(1);
1251 strcat(INPUT, "/");
1252 try_to_sel(INPUT);
1253 update_view();
1254 } else
1255 message(RED, "Could not create \"%s/\".", INPUT);
1256 } else
1257 message(RED, "\"%s\" already exists.", INPUT);
1259 } else if (!strcmp(key, RVK_RENAME)) {
1260 int ok = 0;
1261 char *last;
1262 int isdir;
1263 strcpy(INPUT, ENAME(ESEL));
1264 last = INPUT + strlen(INPUT) - 1;
1265 if ((isdir = *last == '/'))
1266 *last = '\0';
1267 start_line_edit(INPUT);
1268 update_input(RVP_RENAME, RED);
1269 while ((edit_stat = get_line_edit()) == CONTINUE) {
1270 int length = strlen(INPUT);
1271 ok = length;
1272 for (i = 0; i < rover.nfiles; i++)
1273 if (
1274 !strncmp(ENAME(i), INPUT, length) &&
1275 (!strcmp(ENAME(i) + length, "") ||
1276 !strcmp(ENAME(i) + length, "/"))
1278 ok = 0;
1279 break;
1281 update_input(RVP_RENAME, ok ? GREEN : RED);
1283 clear_message();
1284 if (edit_stat == CONFIRM) {
1285 if (isdir)
1286 strcat(INPUT, "/");
1287 if (ok) {
1288 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
1289 del_mark(&rover.marks, ENAME(ESEL));
1290 add_mark(&rover.marks, CWD, INPUT);
1292 cd(1);
1293 try_to_sel(INPUT);
1294 update_view();
1295 } else
1296 message(RED, "\"%s\" already exists.", INPUT);
1298 } else if (!strcmp(key, RVK_DELETE)) {
1299 if (rover.nfiles) {
1300 message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
1301 if (rover_getch() == 'Y') {
1302 const char *name = ENAME(ESEL);
1303 int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
1304 reload();
1305 if (ret)
1306 message(RED, "Could not delete \"%s\".", ENAME(ESEL));
1307 } else
1308 clear_message();
1309 } else
1310 message(RED, "No entry selected for deletion.");
1311 } else if (!strcmp(key, RVK_TG_MARK)) {
1312 if (MARKED(ESEL))
1313 del_mark(&rover.marks, ENAME(ESEL));
1314 else
1315 add_mark(&rover.marks, CWD, ENAME(ESEL));
1316 MARKED(ESEL) = !MARKED(ESEL);
1317 ESEL = (ESEL + 1) % rover.nfiles;
1318 update_view();
1319 } else if (!strcmp(key, RVK_INVMARK)) {
1320 for (i = 0; i < rover.nfiles; i++) {
1321 if (MARKED(i))
1322 del_mark(&rover.marks, ENAME(i));
1323 else
1324 add_mark(&rover.marks, CWD, ENAME(i));
1325 MARKED(i) = !MARKED(i);
1327 update_view();
1328 } else if (!strcmp(key, RVK_MARKALL)) {
1329 for (i = 0; i < rover.nfiles; i++)
1330 if (!MARKED(i)) {
1331 add_mark(&rover.marks, CWD, ENAME(i));
1332 MARKED(i) = 1;
1334 update_view();
1335 } else if (!strcmp(key, RVK_MARK_DELETE)) {
1336 if (rover.marks.nentries) {
1337 message(YELLOW, "Delete all marked entries? (Y/n)");
1338 if (rover_getch() == 'Y')
1339 process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
1340 else
1341 clear_message();
1342 } else
1343 message(RED, "No entries marked for deletion.");
1344 } else if (!strcmp(key, RVK_MARK_COPY)) {
1345 if (rover.marks.nentries)
1346 process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
1347 else
1348 message(RED, "No entries marked for copying.");
1349 } else if (!strcmp(key, RVK_MARK_MOVE)) {
1350 if (rover.marks.nentries)
1351 process_marked(adddir, movfile, deldir, "Moving", "Moved");
1352 else
1353 message(RED, "No entries marked for moving.");
1356 if (rover.nfiles)
1357 free_rows(&rover.rows, rover.nfiles);
1358 delwin(rover.window);
1359 if (save_cwd_file != NULL) {
1360 fputs(CWD, save_cwd_file);
1361 fclose(save_cwd_file);
1363 if (save_marks_file != NULL) {
1364 for (i = 0; i < rover.marks.bulk; i++) {
1365 entry = rover.marks.entries[i];
1366 if (entry)
1367 fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
1369 fclose(save_marks_file);
1371 free_marks(&rover.marks);
1372 return 0;