Blame


1 71ee1627 2014-11-07 marcelgmr #include <stdlib.h>
2 bae1431c 2014-11-07 marcelgmr #include <stdint.h>
3 6beff8a2 2014-11-08 marcelgmr #include <ctype.h>
4 6beff8a2 2014-11-08 marcelgmr #include <string.h>
5 5f52c474 2014-11-08 marcelgmr #include <sys/types.h> /* pid_t, ... */
6 c04b0d89 2015-03-30 marcelgmr #include <stdio.h>
7 c04b0d89 2015-03-30 marcelgmr #include <limits.h> /* PATH_MAX */
8 71ee1627 2014-11-07 marcelgmr #include <locale.h> /* setlocale(), LC_ALL */
9 b7ee4ee2 2014-11-14 marcelgmr #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
10 71ee1627 2014-11-07 marcelgmr #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
11 71ee1627 2014-11-07 marcelgmr #include <sys/stat.h>
12 b7ee4ee2 2014-11-14 marcelgmr #include <fcntl.h> /* open() */
13 71ee1627 2014-11-07 marcelgmr #include <sys/wait.h> /* waitpid() */
14 b529416b 2014-11-11 marcelgmr #include <signal.h> /* struct sigaction, sigaction() */
15 8a757b0f 2015-03-07 marcelgmr #include <errno.h>
16 71ee1627 2014-11-07 marcelgmr #include <curses.h>
17 71ee1627 2014-11-07 marcelgmr
18 71ee1627 2014-11-07 marcelgmr #include "config.h"
19 71ee1627 2014-11-07 marcelgmr
20 3cd6bb98 2014-11-11 marcelgmr /* String buffers. */
21 576e768d 2014-11-08 marcelgmr #define ROWSZ 256
22 3cd6bb98 2014-11-11 marcelgmr static char ROW[ROWSZ];
23 a17e36e3 2014-11-07 marcelgmr #define STATUSSZ 256
24 3cd6bb98 2014-11-11 marcelgmr static char STATUS[STATUSSZ];
25 1447784d 2014-11-14 marcelgmr #define INPUTSZ 256
26 1447784d 2014-11-14 marcelgmr static char INPUT[INPUTSZ];
27 3cd6bb98 2014-11-11 marcelgmr
28 3cd6bb98 2014-11-11 marcelgmr /* Argument buffers for execvp(). */
29 71ee1627 2014-11-07 marcelgmr #define MAXARGS 256
30 3cd6bb98 2014-11-11 marcelgmr static char *ARGS[MAXARGS];
31 71ee1627 2014-11-07 marcelgmr
32 60128fcd 2014-11-15 marcelgmr /* Listing view parameters. */
33 60128fcd 2014-11-15 marcelgmr #define HEIGHT (LINES-4)
34 60128fcd 2014-11-15 marcelgmr #define STATUSPOS (COLS-16)
35 f05efd9e 2014-11-07 marcelgmr
36 3cd6bb98 2014-11-11 marcelgmr /* Listing view flags. */
37 bae1431c 2014-11-07 marcelgmr #define SHOW_FILES 0x01u
38 bae1431c 2014-11-07 marcelgmr #define SHOW_DIRS 0x02u
39 bae1431c 2014-11-07 marcelgmr #define SHOW_HIDDEN 0x04u
40 bae1431c 2014-11-07 marcelgmr
41 50c63039 2014-11-14 marcelgmr /* Marks parameters. */
42 18016333 2014-11-14 marcelgmr #define BULK_INIT 5
43 18016333 2014-11-14 marcelgmr #define BULK_THRESH 256
44 18016333 2014-11-14 marcelgmr
45 3cd6bb98 2014-11-11 marcelgmr /* Information associated to each entry in listing. */
46 8f853fb5 2015-03-25 marcelgmr typedef struct Row {
47 576e768d 2014-11-08 marcelgmr char *name;
48 576e768d 2014-11-08 marcelgmr off_t size;
49 18016333 2014-11-14 marcelgmr int marked;
50 eef51ac5 2015-03-24 marcelgmr } Row;
51 576e768d 2014-11-08 marcelgmr
52 5c4a6b62 2014-11-14 marcelgmr /* Dynamic array of marked entries. */
53 8f853fb5 2015-03-25 marcelgmr typedef struct Marks {
54 c04b0d89 2015-03-30 marcelgmr char dirpath[PATH_MAX];
55 18016333 2014-11-14 marcelgmr int bulk;
56 18016333 2014-11-14 marcelgmr int nentries;
57 18016333 2014-11-14 marcelgmr char **entries;
58 eef51ac5 2015-03-24 marcelgmr } Marks;
59 18016333 2014-11-14 marcelgmr
60 3cd6bb98 2014-11-11 marcelgmr /* Global state. Some basic info is allocated for ten tabs. */
61 eef51ac5 2015-03-24 marcelgmr static struct Rover {
62 ada8fba9 2014-11-09 marcelgmr int tab;
63 71ee1627 2014-11-07 marcelgmr int nfiles;
64 ada8fba9 2014-11-09 marcelgmr int scroll[10];
65 7c74df90 2014-11-15 marcelgmr int esel[10];
66 47e9f26d 2014-11-09 marcelgmr uint8_t flags[10];
67 eef51ac5 2015-03-24 marcelgmr Row *rows;
68 71ee1627 2014-11-07 marcelgmr WINDOW *window;
69 c04b0d89 2015-03-30 marcelgmr char cwd[10][PATH_MAX];
70 eef51ac5 2015-03-24 marcelgmr Marks marks;
71 f05efd9e 2014-11-07 marcelgmr } rover;
72 71ee1627 2014-11-07 marcelgmr
73 3cd6bb98 2014-11-11 marcelgmr /* Macros for accessing global state. */
74 7c74df90 2014-11-15 marcelgmr #define ENAME(I) rover.rows[I].name
75 7c74df90 2014-11-15 marcelgmr #define ESIZE(I) rover.rows[I].size
76 18016333 2014-11-14 marcelgmr #define MARKED(I) rover.rows[I].marked
77 3cd6bb98 2014-11-11 marcelgmr #define SCROLL rover.scroll[rover.tab]
78 7c74df90 2014-11-15 marcelgmr #define ESEL rover.esel[rover.tab]
79 3cd6bb98 2014-11-11 marcelgmr #define FLAGS rover.flags[rover.tab]
80 3cd6bb98 2014-11-11 marcelgmr #define CWD rover.cwd[rover.tab]
81 845f2b87 2014-11-14 marcelgmr
82 60128fcd 2014-11-15 marcelgmr /* Helpers. */
83 60128fcd 2014-11-15 marcelgmr #define MIN(A, B) ((A) < (B) ? (A) : (B))
84 60128fcd 2014-11-15 marcelgmr #define MAX(A, B) ((A) > (B) ? (A) : (B))
85 7c74df90 2014-11-15 marcelgmr #define ISDIR(E) (strchr((E), '/') != NULL)
86 7c74df90 2014-11-15 marcelgmr
87 8f853fb5 2015-03-25 marcelgmr typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} Color;
88 845f2b87 2014-11-14 marcelgmr typedef int (*PROCESS)(const char *path);
89 576e768d 2014-11-08 marcelgmr
90 d06e977d 2014-11-14 marcelgmr static void
91 eef51ac5 2015-03-24 marcelgmr init_marks(Marks *marks)
92 18016333 2014-11-14 marcelgmr {
93 18016333 2014-11-14 marcelgmr strcpy(marks->dirpath, "");
94 18016333 2014-11-14 marcelgmr marks->bulk = BULK_INIT;
95 18016333 2014-11-14 marcelgmr marks->nentries = 0;
96 f41c2ea1 2014-12-23 marcelgmr marks->entries = calloc(marks->bulk, sizeof *marks->entries);
97 18016333 2014-11-14 marcelgmr }
98 18016333 2014-11-14 marcelgmr
99 5c4a6b62 2014-11-14 marcelgmr /* Unmark all entries. */
100 d06e977d 2014-11-14 marcelgmr static void
101 eef51ac5 2015-03-24 marcelgmr mark_none(Marks *marks)
102 18016333 2014-11-14 marcelgmr {
103 18016333 2014-11-14 marcelgmr int i;
104 18016333 2014-11-14 marcelgmr
105 8ba07de5 2014-11-14 marcelgmr strcpy(marks->dirpath, "");
106 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk && marks->nentries; i++)
107 18016333 2014-11-14 marcelgmr if (marks->entries[i]) {
108 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
109 9a852838 2014-11-14 marcelgmr marks->entries[i] = NULL;
110 18016333 2014-11-14 marcelgmr marks->nentries--;
111 18016333 2014-11-14 marcelgmr }
112 18016333 2014-11-14 marcelgmr if (marks->bulk > BULK_THRESH) {
113 18016333 2014-11-14 marcelgmr /* Reset bulk to free some memory. */
114 18016333 2014-11-14 marcelgmr free(marks->entries);
115 18016333 2014-11-14 marcelgmr marks->bulk = BULK_INIT;
116 f41c2ea1 2014-12-23 marcelgmr marks->entries = calloc(marks->bulk, sizeof *marks->entries);
117 18016333 2014-11-14 marcelgmr }
118 18016333 2014-11-14 marcelgmr }
119 18016333 2014-11-14 marcelgmr
120 d06e977d 2014-11-14 marcelgmr static void
121 eef51ac5 2015-03-24 marcelgmr add_mark(Marks *marks, char *dirpath, char *entry)
122 18016333 2014-11-14 marcelgmr {
123 18016333 2014-11-14 marcelgmr int i;
124 18016333 2014-11-14 marcelgmr
125 18016333 2014-11-14 marcelgmr if (!strcmp(marks->dirpath, dirpath)) {
126 18016333 2014-11-14 marcelgmr /* Append mark to directory. */
127 18016333 2014-11-14 marcelgmr if (marks->nentries == marks->bulk) {
128 18016333 2014-11-14 marcelgmr /* Expand bulk to accomodate new entry. */
129 18016333 2014-11-14 marcelgmr int extra = marks->bulk >> 1;
130 18016333 2014-11-14 marcelgmr marks->bulk += extra; /* bulk *= 1.5; */
131 f41c2ea1 2014-12-23 marcelgmr marks->entries = realloc(marks->entries,
132 f41c2ea1 2014-12-23 marcelgmr marks->bulk * sizeof *marks->entries);
133 f41c2ea1 2014-12-23 marcelgmr memset(&marks->entries[marks->nentries], 0,
134 f41c2ea1 2014-12-23 marcelgmr extra * sizeof *marks->entries);
135 18016333 2014-11-14 marcelgmr i = marks->nentries;
136 590d39af 2014-12-02 marcelgmr } else {
137 18016333 2014-11-14 marcelgmr /* Search for empty slot (there must be one). */
138 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk; i++)
139 18016333 2014-11-14 marcelgmr if (!marks->entries[i])
140 18016333 2014-11-14 marcelgmr break;
141 18016333 2014-11-14 marcelgmr }
142 590d39af 2014-12-02 marcelgmr } else {
143 18016333 2014-11-14 marcelgmr /* Directory changed. Discard old marks. */
144 18016333 2014-11-14 marcelgmr mark_none(marks);
145 18016333 2014-11-14 marcelgmr strcpy(marks->dirpath, dirpath);
146 18016333 2014-11-14 marcelgmr i = 0;
147 18016333 2014-11-14 marcelgmr }
148 f41c2ea1 2014-12-23 marcelgmr marks->entries[i] = malloc(strlen(entry) + 1);
149 18016333 2014-11-14 marcelgmr strcpy(marks->entries[i], entry);
150 18016333 2014-11-14 marcelgmr marks->nentries++;
151 18016333 2014-11-14 marcelgmr }
152 18016333 2014-11-14 marcelgmr
153 d06e977d 2014-11-14 marcelgmr static void
154 eef51ac5 2015-03-24 marcelgmr del_mark(Marks *marks, char *entry)
155 18016333 2014-11-14 marcelgmr {
156 18016333 2014-11-14 marcelgmr int i;
157 18016333 2014-11-14 marcelgmr
158 18016333 2014-11-14 marcelgmr if (marks->nentries > 1) {
159 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk; i++)
160 18016333 2014-11-14 marcelgmr if (marks->entries[i] && !strcmp(marks->entries[i], entry))
161 18016333 2014-11-14 marcelgmr break;
162 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
163 18016333 2014-11-14 marcelgmr marks->entries[i] = NULL;
164 18016333 2014-11-14 marcelgmr marks->nentries--;
165 590d39af 2014-12-02 marcelgmr } else
166 590d39af 2014-12-02 marcelgmr mark_none(marks);
167 18016333 2014-11-14 marcelgmr }
168 18016333 2014-11-14 marcelgmr
169 d06e977d 2014-11-14 marcelgmr static void
170 eef51ac5 2015-03-24 marcelgmr free_marks(Marks *marks)
171 18016333 2014-11-14 marcelgmr {
172 18016333 2014-11-14 marcelgmr int i;
173 18016333 2014-11-14 marcelgmr
174 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk && marks->nentries; i++)
175 18016333 2014-11-14 marcelgmr if (marks->entries[i]) {
176 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
177 18016333 2014-11-14 marcelgmr marks->nentries--;
178 18016333 2014-11-14 marcelgmr }
179 18016333 2014-11-14 marcelgmr free(marks->entries);
180 18016333 2014-11-14 marcelgmr }
181 b25c9834 2014-11-14 marcelgmr
182 7896cfae 2015-04-17 marcelgmr static void update_view();
183 18016333 2014-11-14 marcelgmr
184 7896cfae 2015-04-17 marcelgmr /* SIGSEGV handler: clean up curses before exiting. */
185 7896cfae 2015-04-17 marcelgmr static void
186 7896cfae 2015-04-17 marcelgmr handle_segv(int sig)
187 7896cfae 2015-04-17 marcelgmr {
188 7896cfae 2015-04-17 marcelgmr (void) sig;
189 7896cfae 2015-04-17 marcelgmr endwin();
190 7896cfae 2015-04-17 marcelgmr fprintf(stderr, "Received SIGSEGV (segmentation fault).\n");
191 7896cfae 2015-04-17 marcelgmr exit(1);
192 7896cfae 2015-04-17 marcelgmr }
193 e1c42cfe 2014-11-11 marcelgmr
194 7896cfae 2015-04-17 marcelgmr /* SIGWINCH handler: resize application according to new terminal settings. */
195 71ee1627 2014-11-07 marcelgmr static void
196 7896cfae 2015-04-17 marcelgmr handle_winch(int sig)
197 71ee1627 2014-11-07 marcelgmr {
198 7896cfae 2015-04-17 marcelgmr (void) sig;
199 7896cfae 2015-04-17 marcelgmr delwin(rover.window);
200 7896cfae 2015-04-17 marcelgmr endwin();
201 7896cfae 2015-04-17 marcelgmr refresh();
202 7896cfae 2015-04-17 marcelgmr clear();
203 7896cfae 2015-04-17 marcelgmr rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
204 7896cfae 2015-04-17 marcelgmr update_view();
205 7896cfae 2015-04-17 marcelgmr }
206 7896cfae 2015-04-17 marcelgmr
207 7896cfae 2015-04-17 marcelgmr static void
208 7896cfae 2015-04-17 marcelgmr enable_handlers()
209 7896cfae 2015-04-17 marcelgmr {
210 e1c42cfe 2014-11-11 marcelgmr struct sigaction sa;
211 e1c42cfe 2014-11-11 marcelgmr
212 7896cfae 2015-04-17 marcelgmr memset(&sa, 0, sizeof (struct sigaction));
213 7896cfae 2015-04-17 marcelgmr sa.sa_handler = handle_segv;
214 7896cfae 2015-04-17 marcelgmr sigaction(SIGSEGV, &sa, NULL);
215 7896cfae 2015-04-17 marcelgmr sa.sa_handler = handle_winch;
216 7896cfae 2015-04-17 marcelgmr sigaction(SIGWINCH, &sa, NULL);
217 7896cfae 2015-04-17 marcelgmr }
218 7896cfae 2015-04-17 marcelgmr
219 7896cfae 2015-04-17 marcelgmr static void
220 7896cfae 2015-04-17 marcelgmr disable_handlers()
221 7896cfae 2015-04-17 marcelgmr {
222 7896cfae 2015-04-17 marcelgmr struct sigaction sa;
223 7896cfae 2015-04-17 marcelgmr
224 7896cfae 2015-04-17 marcelgmr memset(&sa, 0, sizeof (struct sigaction));
225 7896cfae 2015-04-17 marcelgmr sa.sa_handler = SIG_DFL;
226 7896cfae 2015-04-17 marcelgmr sigaction(SIGSEGV, &sa, NULL);
227 7896cfae 2015-04-17 marcelgmr sigaction(SIGWINCH, &sa, NULL);
228 7896cfae 2015-04-17 marcelgmr }
229 7896cfae 2015-04-17 marcelgmr
230 7896cfae 2015-04-17 marcelgmr /* Do a fork-exec to external program (e.g. $EDITOR). */
231 7896cfae 2015-04-17 marcelgmr static void
232 7896cfae 2015-04-17 marcelgmr spawn()
233 7896cfae 2015-04-17 marcelgmr {
234 7896cfae 2015-04-17 marcelgmr pid_t pid;
235 7896cfae 2015-04-17 marcelgmr int status;
236 7896cfae 2015-04-17 marcelgmr
237 7896cfae 2015-04-17 marcelgmr pid = fork();
238 7896cfae 2015-04-17 marcelgmr if (pid > 0) {
239 7896cfae 2015-04-17 marcelgmr /* fork() succeeded. */
240 7896cfae 2015-04-17 marcelgmr disable_handlers();
241 7896cfae 2015-04-17 marcelgmr endwin();
242 7896cfae 2015-04-17 marcelgmr waitpid(pid, &status, 0);
243 7896cfae 2015-04-17 marcelgmr enable_handlers();
244 7896cfae 2015-04-17 marcelgmr kill(getpid(), SIGWINCH);
245 7896cfae 2015-04-17 marcelgmr } else if (pid == 0) {
246 7896cfae 2015-04-17 marcelgmr /* Child process. */
247 7896cfae 2015-04-17 marcelgmr execvp(ARGS[0], ARGS);
248 7896cfae 2015-04-17 marcelgmr }
249 7896cfae 2015-04-17 marcelgmr }
250 7896cfae 2015-04-17 marcelgmr
251 7896cfae 2015-04-17 marcelgmr /* Curses setup. */
252 7896cfae 2015-04-17 marcelgmr static void
253 7896cfae 2015-04-17 marcelgmr init_term()
254 7896cfae 2015-04-17 marcelgmr {
255 71ee1627 2014-11-07 marcelgmr setlocale(LC_ALL, "");
256 71ee1627 2014-11-07 marcelgmr initscr();
257 71ee1627 2014-11-07 marcelgmr cbreak(); /* Get one character at a time. */
258 71ee1627 2014-11-07 marcelgmr noecho();
259 71ee1627 2014-11-07 marcelgmr nonl(); /* No NL->CR/NL on output. */
260 71ee1627 2014-11-07 marcelgmr intrflush(stdscr, FALSE);
261 71ee1627 2014-11-07 marcelgmr keypad(stdscr, TRUE);
262 71ee1627 2014-11-07 marcelgmr curs_set(FALSE); /* Hide blinking cursor. */
263 71ee1627 2014-11-07 marcelgmr if (has_colors()) {
264 eb265708 2015-03-24 marcelgmr short bg;
265 71ee1627 2014-11-07 marcelgmr start_color();
266 eb265708 2015-03-24 marcelgmr #ifdef NCURSES_EXT_FUNCS
267 eb265708 2015-03-24 marcelgmr use_default_colors();
268 eb265708 2015-03-24 marcelgmr bg = -1;
269 eb265708 2015-03-24 marcelgmr #else
270 eb265708 2015-03-24 marcelgmr bg = COLOR_BLACK;
271 eb265708 2015-03-24 marcelgmr #endif
272 eb265708 2015-03-24 marcelgmr init_pair(RED, COLOR_RED, bg);
273 eb265708 2015-03-24 marcelgmr init_pair(GREEN, COLOR_GREEN, bg);
274 eb265708 2015-03-24 marcelgmr init_pair(YELLOW, COLOR_YELLOW, bg);
275 eb265708 2015-03-24 marcelgmr init_pair(BLUE, COLOR_BLUE, bg);
276 eb265708 2015-03-24 marcelgmr init_pair(CYAN, COLOR_CYAN, bg);
277 eb265708 2015-03-24 marcelgmr init_pair(MAGENTA, COLOR_MAGENTA, bg);
278 eb265708 2015-03-24 marcelgmr init_pair(WHITE, COLOR_WHITE, bg);
279 71ee1627 2014-11-07 marcelgmr }
280 7c74df90 2014-11-15 marcelgmr atexit((void (*)(void)) endwin);
281 7896cfae 2015-04-17 marcelgmr enable_handlers();
282 71ee1627 2014-11-07 marcelgmr }
283 71ee1627 2014-11-07 marcelgmr
284 3cd6bb98 2014-11-11 marcelgmr /* Update the listing view. */
285 71ee1627 2014-11-07 marcelgmr static void
286 743e70b8 2014-11-14 marcelgmr update_view()
287 71ee1627 2014-11-07 marcelgmr {
288 222db952 2014-11-08 marcelgmr int i, j;
289 38347b2a 2015-03-30 marcelgmr int numsize;
290 576e768d 2014-11-08 marcelgmr int ishidden, isdir;
291 18016333 2014-11-14 marcelgmr int marking;
292 71ee1627 2014-11-07 marcelgmr
293 41aae0b0 2014-11-11 marcelgmr mvhline(0, 0, ' ', COLS);
294 41aae0b0 2014-11-11 marcelgmr attr_on(A_BOLD, NULL);
295 41aae0b0 2014-11-11 marcelgmr color_set(RVC_TABNUM, NULL);
296 38347b2a 2015-03-30 marcelgmr mvaddch(0, COLS - 2, rover.tab + '0');
297 41aae0b0 2014-11-11 marcelgmr color_set(DEFAULT, NULL);
298 41aae0b0 2014-11-11 marcelgmr attr_off(A_BOLD, NULL);
299 38347b2a 2015-03-30 marcelgmr if (rover.marks.nentries) {
300 e5fe9820 2015-04-17 marcelgmr numsize = snprintf(STATUS, STATUSSZ, "%d", rover.marks.nentries);
301 38347b2a 2015-03-30 marcelgmr color_set(RVC_NMARKS, NULL);
302 38347b2a 2015-03-30 marcelgmr mvaddstr(0, COLS - 3 - numsize, STATUS);
303 38347b2a 2015-03-30 marcelgmr color_set(DEFAULT, NULL);
304 38347b2a 2015-03-30 marcelgmr } else
305 38347b2a 2015-03-30 marcelgmr numsize = -1;
306 38347b2a 2015-03-30 marcelgmr color_set(RVC_CWD, NULL);
307 38347b2a 2015-03-30 marcelgmr mvaddnstr(0, 0, CWD, COLS - 4 - numsize);
308 38347b2a 2015-03-30 marcelgmr color_set(DEFAULT, NULL);
309 41aae0b0 2014-11-11 marcelgmr wcolor_set(rover.window, RVC_BORDER, NULL);
310 41aae0b0 2014-11-11 marcelgmr wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
311 41aae0b0 2014-11-11 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
312 667d4907 2014-11-14 marcelgmr /* Selection might not be visible, due to cursor wrapping or window
313 667d4907 2014-11-14 marcelgmr shrinking. In that case, the scroll must be moved to make it visible. */
314 60128fcd 2014-11-15 marcelgmr SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
315 18016333 2014-11-14 marcelgmr marking = !strcmp(CWD, rover.marks.dirpath);
316 ada8fba9 2014-11-09 marcelgmr for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
317 7c74df90 2014-11-15 marcelgmr ishidden = ENAME(j)[0] == '.';
318 7c74df90 2014-11-15 marcelgmr isdir = ISDIR(ENAME(j));
319 7c74df90 2014-11-15 marcelgmr if (j == ESEL)
320 f05efd9e 2014-11-07 marcelgmr wattr_on(rover.window, A_REVERSE, NULL);
321 576e768d 2014-11-08 marcelgmr if (ishidden)
322 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_HIDDEN, NULL);
323 576e768d 2014-11-08 marcelgmr else if (isdir)
324 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_DIR, NULL);
325 37233869 2014-11-07 marcelgmr else
326 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_FILE, NULL);
327 576e768d 2014-11-08 marcelgmr if (!isdir)
328 e5fe9820 2015-04-17 marcelgmr snprintf(ROW, ROWSZ, "%s%*d", ENAME(j),
329 14b5cd28 2015-01-06 marcelgmr (int) (COLS - strlen(ENAME(j)) - 4), (int) ESIZE(j));
330 576e768d 2014-11-08 marcelgmr else
331 7c74df90 2014-11-15 marcelgmr strcpy(ROW, ENAME(j));
332 a81852bf 2014-11-08 marcelgmr mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
333 a651a76d 2014-11-14 marcelgmr if (marking && MARKED(j))
334 e6a7541a 2014-11-14 marcelgmr mvwaddch(rover.window, i + 1, 1, RVS_MARK);
335 18016333 2014-11-14 marcelgmr else
336 18016333 2014-11-14 marcelgmr mvwaddch(rover.window, i + 1, 1, ' ');
337 b9837001 2014-11-24 marcelgmr mvwaddnstr(rover.window, i + 1, 2, ROW, COLS - 4);
338 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
339 7c74df90 2014-11-15 marcelgmr if (j == ESEL)
340 f05efd9e 2014-11-07 marcelgmr wattr_off(rover.window, A_REVERSE, NULL);
341 a81852bf 2014-11-08 marcelgmr }
342 37087140 2015-03-30 marcelgmr for (;i < HEIGHT; i++)
343 37087140 2015-03-30 marcelgmr mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
344 a81852bf 2014-11-08 marcelgmr if (rover.nfiles > HEIGHT) {
345 a81852bf 2014-11-08 marcelgmr int center, height;
346 ada8fba9 2014-11-09 marcelgmr center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
347 a81852bf 2014-11-08 marcelgmr height = (HEIGHT-1) * HEIGHT / rover.nfiles;
348 a81852bf 2014-11-08 marcelgmr if (!height) height = 1;
349 a81852bf 2014-11-08 marcelgmr wcolor_set(rover.window, RVC_BORDER, NULL);
350 a81852bf 2014-11-08 marcelgmr wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
351 2a2b72b6 2014-11-08 marcelgmr wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
352 4c8a3f34 2014-11-14 marcelgmr mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
353 a81852bf 2014-11-08 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
354 71ee1627 2014-11-07 marcelgmr }
355 47e9f26d 2014-11-09 marcelgmr STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
356 47e9f26d 2014-11-09 marcelgmr STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
357 47e9f26d 2014-11-09 marcelgmr STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
358 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles)
359 222db952 2014-11-08 marcelgmr strcpy(ROW, "0/0");
360 bb90b2d2 2014-11-08 marcelgmr else
361 e5fe9820 2015-04-17 marcelgmr snprintf(ROW, ROWSZ, "%d/%d", ESEL + 1, rover.nfiles);
362 e5fe9820 2015-04-17 marcelgmr snprintf(STATUS+3, STATUSSZ-3, "%12s", ROW);
363 e1f31679 2014-11-07 marcelgmr color_set(RVC_STATUS, NULL);
364 50c63039 2014-11-14 marcelgmr mvaddstr(LINES - 1, STATUSPOS, STATUS);
365 e1f31679 2014-11-07 marcelgmr color_set(DEFAULT, NULL);
366 37087140 2015-03-30 marcelgmr wrefresh(rover.window);
367 71ee1627 2014-11-07 marcelgmr }
368 71ee1627 2014-11-07 marcelgmr
369 7896cfae 2015-04-17 marcelgmr /* Show a message on the status bar. */
370 7896cfae 2015-04-17 marcelgmr static void
371 7896cfae 2015-04-17 marcelgmr message(const char *msg, Color color)
372 41aae0b0 2014-11-11 marcelgmr {
373 7896cfae 2015-04-17 marcelgmr int len, pos;
374 7896cfae 2015-04-17 marcelgmr
375 7896cfae 2015-04-17 marcelgmr len = strlen(msg);
376 7896cfae 2015-04-17 marcelgmr pos = (STATUSPOS - len) >> 1;
377 7896cfae 2015-04-17 marcelgmr attr_on(A_BOLD, NULL);
378 7896cfae 2015-04-17 marcelgmr color_set(color, NULL);
379 7896cfae 2015-04-17 marcelgmr mvaddstr(LINES - 1, pos, msg);
380 7896cfae 2015-04-17 marcelgmr color_set(DEFAULT, NULL);
381 7896cfae 2015-04-17 marcelgmr attr_off(A_BOLD, NULL);
382 41aae0b0 2014-11-11 marcelgmr }
383 41aae0b0 2014-11-11 marcelgmr
384 7896cfae 2015-04-17 marcelgmr /* Clear message area, leaving only status info. */
385 41aae0b0 2014-11-11 marcelgmr static void
386 7896cfae 2015-04-17 marcelgmr clear_message()
387 41aae0b0 2014-11-11 marcelgmr {
388 7896cfae 2015-04-17 marcelgmr mvhline(LINES - 1, 0, ' ', STATUSPOS);
389 41aae0b0 2014-11-11 marcelgmr }
390 41aae0b0 2014-11-11 marcelgmr
391 3cd6bb98 2014-11-11 marcelgmr /* Comparison used to sort listing entries. */
392 3cd6bb98 2014-11-11 marcelgmr static int
393 3cd6bb98 2014-11-11 marcelgmr rowcmp(const void *a, const void *b)
394 3cd6bb98 2014-11-11 marcelgmr {
395 3cd6bb98 2014-11-11 marcelgmr int isdir1, isdir2, cmpdir;
396 eef51ac5 2015-03-24 marcelgmr const Row *r1 = a;
397 eef51ac5 2015-03-24 marcelgmr const Row *r2 = b;
398 7c74df90 2014-11-15 marcelgmr isdir1 = ISDIR(r1->name);
399 7c74df90 2014-11-15 marcelgmr isdir2 = ISDIR(r2->name);
400 3cd6bb98 2014-11-11 marcelgmr cmpdir = isdir2 - isdir1;
401 3cd6bb98 2014-11-11 marcelgmr return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
402 3cd6bb98 2014-11-11 marcelgmr }
403 3cd6bb98 2014-11-11 marcelgmr
404 496dd2b0 2015-03-24 marcelgmr /* Get all entries in current working directory. */
405 3cd6bb98 2014-11-11 marcelgmr static int
406 496dd2b0 2015-03-24 marcelgmr ls(Row **rowsp, uint8_t flags)
407 3cd6bb98 2014-11-11 marcelgmr {
408 3cd6bb98 2014-11-11 marcelgmr DIR *dp;
409 3cd6bb98 2014-11-11 marcelgmr struct dirent *ep;
410 3cd6bb98 2014-11-11 marcelgmr struct stat statbuf;
411 eef51ac5 2015-03-24 marcelgmr Row *rows;
412 3cd6bb98 2014-11-11 marcelgmr int i, n;
413 3cd6bb98 2014-11-11 marcelgmr
414 496dd2b0 2015-03-24 marcelgmr if(!(dp = opendir("."))) return -1;
415 3cd6bb98 2014-11-11 marcelgmr n = -2; /* We don't want the entries "." and "..". */
416 3cd6bb98 2014-11-11 marcelgmr while (readdir(dp)) n++;
417 3cd6bb98 2014-11-11 marcelgmr rewinddir(dp);
418 f41c2ea1 2014-12-23 marcelgmr rows = malloc(n * sizeof *rows);
419 3cd6bb98 2014-11-11 marcelgmr i = 0;
420 3cd6bb98 2014-11-11 marcelgmr while ((ep = readdir(dp))) {
421 3cd6bb98 2014-11-11 marcelgmr if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
422 3cd6bb98 2014-11-11 marcelgmr continue;
423 3cd6bb98 2014-11-11 marcelgmr if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
424 3cd6bb98 2014-11-11 marcelgmr continue;
425 f1e9b90e 2015-03-30 marcelgmr lstat(ep->d_name, &statbuf);
426 3cd6bb98 2014-11-11 marcelgmr if (S_ISDIR(statbuf.st_mode)) {
427 3cd6bb98 2014-11-11 marcelgmr if (flags & SHOW_DIRS) {
428 f41c2ea1 2014-12-23 marcelgmr rows[i].name = malloc(strlen(ep->d_name) + 2);
429 3cd6bb98 2014-11-11 marcelgmr strcpy(rows[i].name, ep->d_name);
430 3cd6bb98 2014-11-11 marcelgmr strcat(rows[i].name, "/");
431 3cd6bb98 2014-11-11 marcelgmr i++;
432 3cd6bb98 2014-11-11 marcelgmr }
433 590d39af 2014-12-02 marcelgmr } else if (flags & SHOW_FILES) {
434 f41c2ea1 2014-12-23 marcelgmr rows[i].name = malloc(strlen(ep->d_name) + 1);
435 3cd6bb98 2014-11-11 marcelgmr strcpy(rows[i].name, ep->d_name);
436 3cd6bb98 2014-11-11 marcelgmr rows[i].size = statbuf.st_size;
437 3cd6bb98 2014-11-11 marcelgmr i++;
438 3cd6bb98 2014-11-11 marcelgmr }
439 3cd6bb98 2014-11-11 marcelgmr }
440 3cd6bb98 2014-11-11 marcelgmr n = i; /* Ignore unused space in array caused by filters. */
441 f41c2ea1 2014-12-23 marcelgmr qsort(rows, n, sizeof (*rows), rowcmp);
442 3cd6bb98 2014-11-11 marcelgmr closedir(dp);
443 3cd6bb98 2014-11-11 marcelgmr *rowsp = rows;
444 3cd6bb98 2014-11-11 marcelgmr return n;
445 3cd6bb98 2014-11-11 marcelgmr }
446 3cd6bb98 2014-11-11 marcelgmr
447 3cd6bb98 2014-11-11 marcelgmr static void
448 eef51ac5 2015-03-24 marcelgmr free_rows(Row **rowsp, int nfiles)
449 3cd6bb98 2014-11-11 marcelgmr {
450 3cd6bb98 2014-11-11 marcelgmr int i;
451 3cd6bb98 2014-11-11 marcelgmr
452 3cd6bb98 2014-11-11 marcelgmr for (i = 0; i < nfiles; i++)
453 3cd6bb98 2014-11-11 marcelgmr free((*rowsp)[i].name);
454 3cd6bb98 2014-11-11 marcelgmr free(*rowsp);
455 3cd6bb98 2014-11-11 marcelgmr *rowsp = NULL;
456 3cd6bb98 2014-11-11 marcelgmr }
457 3cd6bb98 2014-11-11 marcelgmr
458 8f853fb5 2015-03-25 marcelgmr /* Change working directory to the path in CWD. */
459 71ee1627 2014-11-07 marcelgmr static void
460 ada8fba9 2014-11-09 marcelgmr cd(int reset)
461 71ee1627 2014-11-07 marcelgmr {
462 18016333 2014-11-14 marcelgmr int i, j;
463 18016333 2014-11-14 marcelgmr
464 d0cdbd3c 2014-11-15 marcelgmr message("Loading...", CYAN);
465 d0cdbd3c 2014-11-15 marcelgmr refresh();
466 60128fcd 2014-11-15 marcelgmr if (reset) ESEL = SCROLL = 0;
467 ea0214c3 2014-11-09 marcelgmr chdir(CWD);
468 f05efd9e 2014-11-07 marcelgmr if (rover.nfiles)
469 576e768d 2014-11-08 marcelgmr free_rows(&rover.rows, rover.nfiles);
470 496dd2b0 2015-03-24 marcelgmr rover.nfiles = ls(&rover.rows, FLAGS);
471 18016333 2014-11-14 marcelgmr if (!strcmp(CWD, rover.marks.dirpath)) {
472 18016333 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
473 18016333 2014-11-14 marcelgmr for (j = 0; j < rover.marks.bulk; j++)
474 18016333 2014-11-14 marcelgmr if (
475 18016333 2014-11-14 marcelgmr rover.marks.entries[j] &&
476 7c74df90 2014-11-15 marcelgmr !strcmp(rover.marks.entries[j], ENAME(i))
477 18016333 2014-11-14 marcelgmr )
478 18016333 2014-11-14 marcelgmr break;
479 18016333 2014-11-14 marcelgmr MARKED(i) = j < rover.marks.bulk;
480 18016333 2014-11-14 marcelgmr }
481 590d39af 2014-12-02 marcelgmr } else
482 590d39af 2014-12-02 marcelgmr for (i = 0; i < rover.nfiles; i++)
483 590d39af 2014-12-02 marcelgmr MARKED(i) = 0;
484 d0cdbd3c 2014-11-15 marcelgmr clear_message();
485 743e70b8 2014-11-14 marcelgmr update_view();
486 548a3163 2014-11-14 marcelgmr }
487 548a3163 2014-11-14 marcelgmr
488 430cd7d8 2014-11-19 marcelgmr /* Select a target entry, if it is present. */
489 430cd7d8 2014-11-19 marcelgmr static void
490 430cd7d8 2014-11-19 marcelgmr try_to_sel(const char *target)
491 430cd7d8 2014-11-19 marcelgmr {
492 430cd7d8 2014-11-19 marcelgmr ESEL = 0;
493 f499f7c7 2014-12-03 marcelgmr if (!ISDIR(target))
494 f499f7c7 2014-12-03 marcelgmr while ((ESEL+1) < rover.nfiles && ISDIR(ENAME(ESEL)))
495 f499f7c7 2014-12-03 marcelgmr ESEL++;
496 00ecdc1e 2014-11-19 marcelgmr while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
497 430cd7d8 2014-11-19 marcelgmr ESEL++;
498 430cd7d8 2014-11-19 marcelgmr if (rover.nfiles > HEIGHT) {
499 430cd7d8 2014-11-19 marcelgmr SCROLL = ESEL - (HEIGHT >> 1);
500 430cd7d8 2014-11-19 marcelgmr SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
501 430cd7d8 2014-11-19 marcelgmr }
502 430cd7d8 2014-11-19 marcelgmr }
503 430cd7d8 2014-11-19 marcelgmr
504 00ecdc1e 2014-11-19 marcelgmr /* Reload CWD, but try to keep selection. */
505 00ecdc1e 2014-11-19 marcelgmr static void
506 00ecdc1e 2014-11-19 marcelgmr reload()
507 00ecdc1e 2014-11-19 marcelgmr {
508 00ecdc1e 2014-11-19 marcelgmr if (rover.nfiles) {
509 00ecdc1e 2014-11-19 marcelgmr strcpy(INPUT, ENAME(ESEL));
510 00ecdc1e 2014-11-19 marcelgmr cd(1);
511 00ecdc1e 2014-11-19 marcelgmr try_to_sel(INPUT);
512 00ecdc1e 2014-11-19 marcelgmr update_view();
513 590d39af 2014-12-02 marcelgmr } else
514 590d39af 2014-12-02 marcelgmr cd(1);
515 00ecdc1e 2014-11-19 marcelgmr }
516 00ecdc1e 2014-11-19 marcelgmr
517 5c4a6b62 2014-11-14 marcelgmr /* Recursively process a source directory using CWD as destination root.
518 c8891c90 2014-12-23 marcelgmr For each node (i.e. directory), do the following:
519 c8891c90 2014-12-23 marcelgmr 1. call pre(destination);
520 c8891c90 2014-12-23 marcelgmr 2. call proc() on every child leaf (i.e. files);
521 c8891c90 2014-12-23 marcelgmr 3. recurse into every child node;
522 c8891c90 2014-12-23 marcelgmr 4. call pos(source).
523 c8891c90 2014-12-23 marcelgmr E.g. to move directory /src/ (and all its contents) inside /dst/:
524 c8891c90 2014-12-23 marcelgmr strcpy(CWD, "/dst/");
525 c8891c90 2014-12-23 marcelgmr process_dir(adddir, movfile, deldir, "/src/"); */
526 2a117cd2 2014-11-15 marcelgmr static int
527 845f2b87 2014-11-14 marcelgmr process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
528 548a3163 2014-11-14 marcelgmr {
529 2a117cd2 2014-11-15 marcelgmr int ret;
530 548a3163 2014-11-14 marcelgmr DIR *dp;
531 548a3163 2014-11-14 marcelgmr struct dirent *ep;
532 548a3163 2014-11-14 marcelgmr struct stat statbuf;
533 c04b0d89 2015-03-30 marcelgmr char subpath[PATH_MAX];
534 548a3163 2014-11-14 marcelgmr
535 2a117cd2 2014-11-15 marcelgmr ret = 0;
536 68ff0591 2014-11-14 marcelgmr if (pre) {
537 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
538 68ff0591 2014-11-14 marcelgmr strcpy(dstpath, CWD);
539 68ff0591 2014-11-14 marcelgmr strcat(dstpath, path + strlen(rover.marks.dirpath));
540 2a117cd2 2014-11-15 marcelgmr ret |= pre(dstpath);
541 68ff0591 2014-11-14 marcelgmr }
542 2a117cd2 2014-11-15 marcelgmr if(!(dp = opendir(path))) return -1;
543 548a3163 2014-11-14 marcelgmr while ((ep = readdir(dp))) {
544 548a3163 2014-11-14 marcelgmr if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
545 548a3163 2014-11-14 marcelgmr continue;
546 e5fe9820 2015-04-17 marcelgmr snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
547 548a3163 2014-11-14 marcelgmr stat(subpath, &statbuf);
548 548a3163 2014-11-14 marcelgmr if (S_ISDIR(statbuf.st_mode)) {
549 548a3163 2014-11-14 marcelgmr strcat(subpath, "/");
550 2a117cd2 2014-11-15 marcelgmr ret |= process_dir(pre, proc, pos, subpath);
551 590d39af 2014-12-02 marcelgmr } else
552 590d39af 2014-12-02 marcelgmr ret |= proc(subpath);
553 548a3163 2014-11-14 marcelgmr }
554 548a3163 2014-11-14 marcelgmr closedir(dp);
555 2a117cd2 2014-11-15 marcelgmr if (pos) ret |= pos(path);
556 2a117cd2 2014-11-15 marcelgmr return ret;
557 548a3163 2014-11-14 marcelgmr }
558 548a3163 2014-11-14 marcelgmr
559 5c4a6b62 2014-11-14 marcelgmr /* Process all marked entries using CWD as destination root.
560 c8891c90 2014-12-23 marcelgmr All marked entries that are directories will be recursively processed.
561 c8891c90 2014-12-23 marcelgmr See process_dir() for details on the parameters. */
562 548a3163 2014-11-14 marcelgmr static void
563 845f2b87 2014-11-14 marcelgmr process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
564 548a3163 2014-11-14 marcelgmr {
565 2a117cd2 2014-11-15 marcelgmr int i, ret;
566 c04b0d89 2015-03-30 marcelgmr char path[PATH_MAX];
567 548a3163 2014-11-14 marcelgmr
568 d0cdbd3c 2014-11-15 marcelgmr clear_message();
569 d0cdbd3c 2014-11-15 marcelgmr message("Processing...", CYAN);
570 d0cdbd3c 2014-11-15 marcelgmr refresh();
571 548a3163 2014-11-14 marcelgmr for (i = 0; i < rover.marks.bulk; i++)
572 548a3163 2014-11-14 marcelgmr if (rover.marks.entries[i]) {
573 2a117cd2 2014-11-15 marcelgmr ret = 0;
574 e5fe9820 2015-04-17 marcelgmr snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
575 7c74df90 2014-11-15 marcelgmr if (ISDIR(rover.marks.entries[i])) {
576 b25c9834 2014-11-14 marcelgmr if (!strncmp(path, CWD, strlen(path)))
577 2a117cd2 2014-11-15 marcelgmr ret = -1;
578 b25c9834 2014-11-14 marcelgmr else
579 2a117cd2 2014-11-15 marcelgmr ret = process_dir(pre, proc, pos, path);
580 590d39af 2014-12-02 marcelgmr } else
581 590d39af 2014-12-02 marcelgmr ret = proc(path);
582 2a117cd2 2014-11-15 marcelgmr if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
583 2a117cd2 2014-11-15 marcelgmr }
584 00ecdc1e 2014-11-19 marcelgmr reload();
585 2a117cd2 2014-11-15 marcelgmr if (!rover.marks.nentries)
586 bad36707 2014-11-19 marcelgmr message("Done.", GREEN);
587 2a117cd2 2014-11-15 marcelgmr else
588 bad36707 2014-11-19 marcelgmr message("Some errors occured.", RED);
589 3cd6bb98 2014-11-11 marcelgmr }
590 3cd6bb98 2014-11-11 marcelgmr
591 b7ee4ee2 2014-11-14 marcelgmr /* Wrappers for file operations. */
592 ca8ebdce 2014-11-14 marcelgmr static PROCESS delfile = unlink;
593 b7ee4ee2 2014-11-14 marcelgmr static PROCESS deldir = rmdir;
594 1447784d 2014-11-14 marcelgmr static int addfile(const char *path) {
595 1447784d 2014-11-14 marcelgmr /* Using creat(2) because mknod(2) doesn't seem to be portable. */
596 1447784d 2014-11-14 marcelgmr int ret;
597 1447784d 2014-11-14 marcelgmr
598 1447784d 2014-11-14 marcelgmr ret = creat(path, 0644);
599 1447784d 2014-11-14 marcelgmr if (ret < 0) return ret;
600 1447784d 2014-11-14 marcelgmr return close(ret);
601 1447784d 2014-11-14 marcelgmr }
602 ca8ebdce 2014-11-14 marcelgmr static int cpyfile(const char *srcpath) {
603 b7ee4ee2 2014-11-14 marcelgmr int src, dst, ret;
604 b7ee4ee2 2014-11-14 marcelgmr size_t size;
605 b7ee4ee2 2014-11-14 marcelgmr struct stat st;
606 b7ee4ee2 2014-11-14 marcelgmr char buf[BUFSIZ];
607 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
608 b7ee4ee2 2014-11-14 marcelgmr
609 b7ee4ee2 2014-11-14 marcelgmr ret = src = open(srcpath, O_RDONLY);
610 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
611 b7ee4ee2 2014-11-14 marcelgmr ret = fstat(src, &st);
612 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
613 b7ee4ee2 2014-11-14 marcelgmr strcpy(dstpath, CWD);
614 b7ee4ee2 2014-11-14 marcelgmr strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
615 b7ee4ee2 2014-11-14 marcelgmr ret = dst = creat(dstpath, st.st_mode);
616 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
617 b7ee4ee2 2014-11-14 marcelgmr while ((size = read(src, buf, BUFSIZ)) > 0)
618 b7ee4ee2 2014-11-14 marcelgmr write(dst, buf, size);
619 b7ee4ee2 2014-11-14 marcelgmr close(src);
620 b7ee4ee2 2014-11-14 marcelgmr close(dst);
621 b7ee4ee2 2014-11-14 marcelgmr return 0;
622 b7ee4ee2 2014-11-14 marcelgmr }
623 b7ee4ee2 2014-11-14 marcelgmr static int adddir(const char *path) {
624 b7ee4ee2 2014-11-14 marcelgmr int ret;
625 b7ee4ee2 2014-11-14 marcelgmr struct stat st;
626 b7ee4ee2 2014-11-14 marcelgmr
627 b7ee4ee2 2014-11-14 marcelgmr ret = stat(CWD, &st);
628 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
629 b7ee4ee2 2014-11-14 marcelgmr return mkdir(path, st.st_mode);
630 ca8ebdce 2014-11-14 marcelgmr }
631 0ea64589 2014-11-14 marcelgmr static int movfile(const char *srcpath) {
632 8a757b0f 2015-03-07 marcelgmr int ret;
633 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
634 ca8ebdce 2014-11-14 marcelgmr
635 0ea64589 2014-11-14 marcelgmr strcpy(dstpath, CWD);
636 0ea64589 2014-11-14 marcelgmr strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
637 8a757b0f 2015-03-07 marcelgmr ret = rename(srcpath, dstpath);
638 8a757b0f 2015-03-07 marcelgmr if (ret < 0 && errno == EXDEV) {
639 8a757b0f 2015-03-07 marcelgmr ret = cpyfile(srcpath);
640 8a757b0f 2015-03-07 marcelgmr if (ret < 0) return ret;
641 8a757b0f 2015-03-07 marcelgmr ret = delfile(srcpath);
642 8a757b0f 2015-03-07 marcelgmr }
643 8a757b0f 2015-03-07 marcelgmr return ret;
644 b7ee4ee2 2014-11-14 marcelgmr }
645 b7ee4ee2 2014-11-14 marcelgmr
646 411ec846 2014-11-08 marcelgmr /* Interactive getstr(). */
647 3cd6bb98 2014-11-11 marcelgmr static int
648 411ec846 2014-11-08 marcelgmr igetstr(char *buffer, int maxlen)
649 411ec846 2014-11-08 marcelgmr {
650 411ec846 2014-11-08 marcelgmr int ch, length;
651 411ec846 2014-11-08 marcelgmr
652 411ec846 2014-11-08 marcelgmr length = strlen(buffer);
653 d5fe31b0 2014-11-14 marcelgmr curs_set(TRUE);
654 411ec846 2014-11-08 marcelgmr ch = getch();
655 d5fe31b0 2014-11-14 marcelgmr if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
656 d5fe31b0 2014-11-14 marcelgmr curs_set(FALSE);
657 411ec846 2014-11-08 marcelgmr return 0;
658 590d39af 2014-12-02 marcelgmr } else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
659 411ec846 2014-11-08 marcelgmr if (length)
660 411ec846 2014-11-08 marcelgmr buffer[--length] = '\0';
661 590d39af 2014-12-02 marcelgmr } else if (ch == killchar()) {
662 411ec846 2014-11-08 marcelgmr length = 0;
663 411ec846 2014-11-08 marcelgmr buffer[0] = '\0';
664 590d39af 2014-12-02 marcelgmr } else if (length < maxlen - 1 && isprint(ch)) {
665 411ec846 2014-11-08 marcelgmr buffer[length++] = ch;
666 411ec846 2014-11-08 marcelgmr buffer[length] = '\0';
667 411ec846 2014-11-08 marcelgmr }
668 411ec846 2014-11-08 marcelgmr return 1;
669 743e70b8 2014-11-14 marcelgmr }
670 743e70b8 2014-11-14 marcelgmr
671 5c4a6b62 2014-11-14 marcelgmr /* Update line input on the screen. */
672 743e70b8 2014-11-14 marcelgmr static void
673 eef51ac5 2015-03-24 marcelgmr update_input(char *prompt, Color color)
674 743e70b8 2014-11-14 marcelgmr {
675 743e70b8 2014-11-14 marcelgmr int plen, ilen;
676 743e70b8 2014-11-14 marcelgmr
677 743e70b8 2014-11-14 marcelgmr plen = strlen(prompt);
678 743e70b8 2014-11-14 marcelgmr ilen = strlen(INPUT);
679 743e70b8 2014-11-14 marcelgmr color_set(RVC_PROMPT, NULL);
680 743e70b8 2014-11-14 marcelgmr mvaddstr(LINES - 1, 0, prompt);
681 743e70b8 2014-11-14 marcelgmr color_set(color, NULL);
682 743e70b8 2014-11-14 marcelgmr mvaddstr(LINES - 1, plen, INPUT);
683 743e70b8 2014-11-14 marcelgmr mvaddch(LINES - 1, ilen + plen, ' ');
684 743e70b8 2014-11-14 marcelgmr move(LINES - 1, ilen + plen);
685 743e70b8 2014-11-14 marcelgmr color_set(DEFAULT, NULL);
686 e1c42cfe 2014-11-11 marcelgmr }
687 e1c42cfe 2014-11-11 marcelgmr
688 71ee1627 2014-11-07 marcelgmr int
689 38a357b6 2014-11-09 marcelgmr main(int argc, char *argv[])
690 71ee1627 2014-11-07 marcelgmr {
691 ada8fba9 2014-11-09 marcelgmr int i, ch;
692 14b5cd28 2015-01-06 marcelgmr char *program;
693 14b5cd28 2015-01-06 marcelgmr const char *key;
694 38a357b6 2014-11-09 marcelgmr DIR *d;
695 71ee1627 2014-11-07 marcelgmr
696 ff684771 2014-12-02 marcelgmr if (argc == 2) {
697 ff684771 2014-12-02 marcelgmr if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
698 ff684771 2014-12-02 marcelgmr printf("rover %s\n", RV_VERSION);
699 ff684771 2014-12-02 marcelgmr return 0;
700 ff684771 2014-12-02 marcelgmr } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
701 ff684771 2014-12-02 marcelgmr printf(
702 ff684771 2014-12-02 marcelgmr "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
703 ff684771 2014-12-02 marcelgmr " or: rover [OPTION]\n"
704 ff684771 2014-12-02 marcelgmr "Browse current working directory or the ones specified.\n\n"
705 ff684771 2014-12-02 marcelgmr "Options:\n"
706 ff684771 2014-12-02 marcelgmr " -h, --help print this help message and exit\n"
707 ff684771 2014-12-02 marcelgmr " -v, --version print program version and exit\n\n"
708 ff684771 2014-12-02 marcelgmr "See rover(1) for more information.\n\n"
709 ff684771 2014-12-02 marcelgmr "Rover homepage: <https://github.com/lecram/rover>.\n"
710 ff684771 2014-12-02 marcelgmr );
711 ff684771 2014-12-02 marcelgmr return 0;
712 ff684771 2014-12-02 marcelgmr }
713 ff684771 2014-12-02 marcelgmr }
714 f05efd9e 2014-11-07 marcelgmr init_term();
715 f05efd9e 2014-11-07 marcelgmr rover.nfiles = 0;
716 47e9f26d 2014-11-09 marcelgmr for (i = 0; i < 10; i++) {
717 7c74df90 2014-11-15 marcelgmr rover.esel[i] = rover.scroll[i] = 0;
718 47e9f26d 2014-11-09 marcelgmr rover.flags[i] = SHOW_FILES | SHOW_DIRS;
719 47e9f26d 2014-11-09 marcelgmr }
720 ada8fba9 2014-11-09 marcelgmr strcpy(rover.cwd[0], getenv("HOME"));
721 38a357b6 2014-11-09 marcelgmr for (i = 1; i < argc && i < 10; i++) {
722 7c74df90 2014-11-15 marcelgmr if ((d = opendir(argv[i]))) {
723 c04b0d89 2015-03-30 marcelgmr realpath(argv[i], rover.cwd[i]);
724 38a357b6 2014-11-09 marcelgmr closedir(d);
725 590d39af 2014-12-02 marcelgmr } else
726 590d39af 2014-12-02 marcelgmr strcpy(rover.cwd[i], rover.cwd[0]);
727 38a357b6 2014-11-09 marcelgmr }
728 c04b0d89 2015-03-30 marcelgmr getcwd(rover.cwd[i], PATH_MAX);
729 38a357b6 2014-11-09 marcelgmr for (i++; i < 10; i++)
730 38a357b6 2014-11-09 marcelgmr strcpy(rover.cwd[i], rover.cwd[i-1]);
731 38a357b6 2014-11-09 marcelgmr for (i = 0; i < 10; i++)
732 38a357b6 2014-11-09 marcelgmr if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
733 38a357b6 2014-11-09 marcelgmr strcat(rover.cwd[i], "/");
734 ada8fba9 2014-11-09 marcelgmr rover.tab = 1;
735 ada8fba9 2014-11-09 marcelgmr rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
736 18016333 2014-11-14 marcelgmr init_marks(&rover.marks);
737 ada8fba9 2014-11-09 marcelgmr cd(1);
738 0b301167 2014-11-07 marcelgmr while (1) {
739 ada8fba9 2014-11-09 marcelgmr ch = getch();
740 ada8fba9 2014-11-09 marcelgmr key = keyname(ch);
741 865c9839 2014-11-15 marcelgmr clear_message();
742 a31a7b29 2014-11-15 marcelgmr if (!strcmp(key, RVK_QUIT)) break;
743 ada8fba9 2014-11-09 marcelgmr else if (ch >= '0' && ch <= '9') {
744 ada8fba9 2014-11-09 marcelgmr rover.tab = ch - '0';
745 ada8fba9 2014-11-09 marcelgmr cd(0);
746 ff684771 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_HELP)) {
747 ff684771 2014-12-02 marcelgmr ARGS[0] = "man";
748 ff684771 2014-12-02 marcelgmr ARGS[1] = "rover";
749 ff684771 2014-12-02 marcelgmr ARGS[2] = NULL;
750 ff684771 2014-12-02 marcelgmr spawn();
751 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_DOWN)) {
752 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
753 7c74df90 2014-11-15 marcelgmr ESEL = (ESEL + 1) % rover.nfiles;
754 743e70b8 2014-11-14 marcelgmr update_view();
755 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_UP)) {
756 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
757 7c74df90 2014-11-15 marcelgmr ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
758 743e70b8 2014-11-14 marcelgmr update_view();
759 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_JUMP_DOWN)) {
760 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
761 60128fcd 2014-11-15 marcelgmr ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
762 60128fcd 2014-11-15 marcelgmr if (rover.nfiles > HEIGHT)
763 60128fcd 2014-11-15 marcelgmr SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
764 743e70b8 2014-11-14 marcelgmr update_view();
765 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_JUMP_UP)) {
766 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
767 60128fcd 2014-11-15 marcelgmr ESEL = MAX(ESEL - RV_JUMP, 0);
768 60128fcd 2014-11-15 marcelgmr SCROLL = MAX(SCROLL - RV_JUMP, 0);
769 743e70b8 2014-11-14 marcelgmr update_view();
770 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_CD_DOWN)) {
771 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
772 7c74df90 2014-11-15 marcelgmr strcat(CWD, ENAME(ESEL));
773 ada8fba9 2014-11-09 marcelgmr cd(1);
774 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_CD_UP)) {
775 45020106 2014-11-08 marcelgmr char *dirname, first;
776 58fbefc5 2014-11-19 marcelgmr if (!strcmp(CWD, "/")) continue;
777 ada8fba9 2014-11-09 marcelgmr CWD[strlen(CWD) - 1] = '\0';
778 ada8fba9 2014-11-09 marcelgmr dirname = strrchr(CWD, '/') + 1;
779 45020106 2014-11-08 marcelgmr first = dirname[0];
780 45020106 2014-11-08 marcelgmr dirname[0] = '\0';
781 ada8fba9 2014-11-09 marcelgmr cd(1);
782 430cd7d8 2014-11-19 marcelgmr dirname[0] = first;
783 430cd7d8 2014-11-19 marcelgmr dirname[strlen(dirname)] = '/';
784 430cd7d8 2014-11-19 marcelgmr try_to_sel(dirname);
785 430cd7d8 2014-11-19 marcelgmr dirname[0] = '\0';
786 430cd7d8 2014-11-19 marcelgmr update_view();
787 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_HOME)) {
788 ada8fba9 2014-11-09 marcelgmr strcpy(CWD, getenv("HOME"));
789 ada8fba9 2014-11-09 marcelgmr if (CWD[strlen(CWD) - 1] != '/')
790 ada8fba9 2014-11-09 marcelgmr strcat(CWD, "/");
791 ada8fba9 2014-11-09 marcelgmr cd(1);
792 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_SHELL)) {
793 71ee1627 2014-11-07 marcelgmr program = getenv("SHELL");
794 71ee1627 2014-11-07 marcelgmr if (program) {
795 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
796 3cd6bb98 2014-11-11 marcelgmr ARGS[1] = NULL;
797 71ee1627 2014-11-07 marcelgmr spawn();
798 00ecdc1e 2014-11-19 marcelgmr reload();
799 71ee1627 2014-11-07 marcelgmr }
800 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_VIEW)) {
801 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
802 4b66a2c4 2014-11-07 marcelgmr program = getenv("PAGER");
803 4b66a2c4 2014-11-07 marcelgmr if (program) {
804 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
805 7c74df90 2014-11-15 marcelgmr ARGS[1] = ENAME(ESEL);
806 3cd6bb98 2014-11-11 marcelgmr ARGS[2] = NULL;
807 4b66a2c4 2014-11-07 marcelgmr spawn();
808 4b66a2c4 2014-11-07 marcelgmr }
809 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_EDIT)) {
810 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
811 71ee1627 2014-11-07 marcelgmr program = getenv("EDITOR");
812 71ee1627 2014-11-07 marcelgmr if (program) {
813 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
814 7c74df90 2014-11-15 marcelgmr ARGS[1] = ENAME(ESEL);
815 3cd6bb98 2014-11-11 marcelgmr ARGS[2] = NULL;
816 71ee1627 2014-11-07 marcelgmr spawn();
817 39ee8f33 2014-11-14 marcelgmr cd(0);
818 71ee1627 2014-11-07 marcelgmr }
819 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_SEARCH)) {
820 1447784d 2014-11-14 marcelgmr int oldsel, oldscroll, length;
821 743e70b8 2014-11-14 marcelgmr char *prompt = "search: ";
822 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
823 7c74df90 2014-11-15 marcelgmr oldsel = ESEL;
824 ada8fba9 2014-11-09 marcelgmr oldscroll = SCROLL;
825 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
826 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
827 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
828 1447784d 2014-11-14 marcelgmr int sel;
829 eef51ac5 2015-03-24 marcelgmr Color color = RED;
830 1447784d 2014-11-14 marcelgmr length = strlen(INPUT);
831 a17e36e3 2014-11-07 marcelgmr if (length) {
832 f05efd9e 2014-11-07 marcelgmr for (sel = 0; sel < rover.nfiles; sel++)
833 7c74df90 2014-11-15 marcelgmr if (!strncmp(ENAME(sel), INPUT, length))
834 a17e36e3 2014-11-07 marcelgmr break;
835 f05efd9e 2014-11-07 marcelgmr if (sel < rover.nfiles) {
836 a17e36e3 2014-11-07 marcelgmr color = GREEN;
837 7c74df90 2014-11-15 marcelgmr ESEL = sel;
838 f05efd9e 2014-11-07 marcelgmr if (rover.nfiles > HEIGHT) {
839 89cfd83b 2014-11-08 marcelgmr if (sel < 3)
840 ada8fba9 2014-11-09 marcelgmr SCROLL = 0;
841 89cfd83b 2014-11-08 marcelgmr else if (sel - 3 > rover.nfiles - HEIGHT)
842 ada8fba9 2014-11-09 marcelgmr SCROLL = rover.nfiles - HEIGHT;
843 a17e36e3 2014-11-07 marcelgmr else
844 ada8fba9 2014-11-09 marcelgmr SCROLL = sel - 3;
845 a17e36e3 2014-11-07 marcelgmr }
846 a17e36e3 2014-11-07 marcelgmr }
847 590d39af 2014-12-02 marcelgmr } else {
848 7c74df90 2014-11-15 marcelgmr ESEL = oldsel;
849 ada8fba9 2014-11-09 marcelgmr SCROLL = oldscroll;
850 411ec846 2014-11-08 marcelgmr }
851 743e70b8 2014-11-14 marcelgmr update_view();
852 743e70b8 2014-11-14 marcelgmr update_input(prompt, color);
853 a17e36e3 2014-11-07 marcelgmr }
854 865c9839 2014-11-15 marcelgmr clear_message();
855 743e70b8 2014-11-14 marcelgmr update_view();
856 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_FILES)) {
857 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_FILES;
858 00ecdc1e 2014-11-19 marcelgmr reload();
859 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_DIRS)) {
860 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_DIRS;
861 00ecdc1e 2014-11-19 marcelgmr reload();
862 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_HIDDEN)) {
863 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_HIDDEN;
864 00ecdc1e 2014-11-19 marcelgmr reload();
865 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_NEW_FILE)) {
866 743e70b8 2014-11-14 marcelgmr int ok = 0;
867 743e70b8 2014-11-14 marcelgmr char *prompt = "new file: ";
868 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
869 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
870 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
871 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
872 1447784d 2014-11-14 marcelgmr ok = 1;
873 e444d3b1 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
874 ddbe9d62 2014-11-14 marcelgmr if (
875 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
876 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
877 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
878 ddbe9d62 2014-11-14 marcelgmr ) {
879 1447784d 2014-11-14 marcelgmr ok = 0;
880 1447784d 2014-11-14 marcelgmr break;
881 1447784d 2014-11-14 marcelgmr }
882 e444d3b1 2014-11-14 marcelgmr }
883 743e70b8 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
884 1447784d 2014-11-14 marcelgmr }
885 865c9839 2014-11-15 marcelgmr clear_message();
886 743e70b8 2014-11-14 marcelgmr if (strlen(INPUT)) {
887 430cd7d8 2014-11-19 marcelgmr if (ok) {
888 430cd7d8 2014-11-19 marcelgmr addfile(INPUT);
889 430cd7d8 2014-11-19 marcelgmr cd(1);
890 430cd7d8 2014-11-19 marcelgmr try_to_sel(INPUT);
891 430cd7d8 2014-11-19 marcelgmr update_view();
892 590d39af 2014-12-02 marcelgmr } else
893 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
894 1447784d 2014-11-14 marcelgmr }
895 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_NEW_DIR)) {
896 743e70b8 2014-11-14 marcelgmr int ok = 0;
897 743e70b8 2014-11-14 marcelgmr char *prompt = "new directory: ";
898 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
899 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
900 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
901 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
902 1447784d 2014-11-14 marcelgmr ok = 1;
903 e444d3b1 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
904 ddbe9d62 2014-11-14 marcelgmr if (
905 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
906 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
907 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
908 ddbe9d62 2014-11-14 marcelgmr ) {
909 1447784d 2014-11-14 marcelgmr ok = 0;
910 1447784d 2014-11-14 marcelgmr break;
911 1447784d 2014-11-14 marcelgmr }
912 e444d3b1 2014-11-14 marcelgmr }
913 743e70b8 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
914 1447784d 2014-11-14 marcelgmr }
915 865c9839 2014-11-15 marcelgmr clear_message();
916 743e70b8 2014-11-14 marcelgmr if (strlen(INPUT)) {
917 430cd7d8 2014-11-19 marcelgmr if (ok) {
918 430cd7d8 2014-11-19 marcelgmr adddir(INPUT);
919 430cd7d8 2014-11-19 marcelgmr cd(1);
920 430cd7d8 2014-11-19 marcelgmr try_to_sel(INPUT);
921 430cd7d8 2014-11-19 marcelgmr update_view();
922 590d39af 2014-12-02 marcelgmr } else
923 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
924 1447784d 2014-11-14 marcelgmr }
925 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_RENAME)) {
926 bdb195d0 2014-11-14 marcelgmr int ok = 0;
927 bdb195d0 2014-11-14 marcelgmr char *prompt = "rename: ";
928 f499f7c7 2014-12-03 marcelgmr char *last;
929 f499f7c7 2014-12-03 marcelgmr int isdir;
930 7c74df90 2014-11-15 marcelgmr strcpy(INPUT, ENAME(ESEL));
931 f499f7c7 2014-12-03 marcelgmr last = INPUT + strlen(INPUT) - 1;
932 f499f7c7 2014-12-03 marcelgmr if ((isdir = *last == '/'))
933 f499f7c7 2014-12-03 marcelgmr *last = '\0';
934 ddbe9d62 2014-11-14 marcelgmr update_input(prompt, RED);
935 bdb195d0 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
936 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
937 bdb195d0 2014-11-14 marcelgmr ok = 1;
938 5f047452 2014-12-03 marcelgmr for (i = 0; i < rover.nfiles; i++)
939 ddbe9d62 2014-11-14 marcelgmr if (
940 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
941 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
942 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
943 ddbe9d62 2014-11-14 marcelgmr ) {
944 bdb195d0 2014-11-14 marcelgmr ok = 0;
945 bdb195d0 2014-11-14 marcelgmr break;
946 bdb195d0 2014-11-14 marcelgmr }
947 bdb195d0 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
948 bdb195d0 2014-11-14 marcelgmr }
949 865c9839 2014-11-15 marcelgmr clear_message();
950 bdb195d0 2014-11-14 marcelgmr if (strlen(INPUT)) {
951 f499f7c7 2014-12-03 marcelgmr if (isdir)
952 f499f7c7 2014-12-03 marcelgmr strcat(INPUT, "/");
953 00ecdc1e 2014-11-19 marcelgmr if (ok) {
954 dbb53e68 2014-12-03 marcelgmr if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
955 dbb53e68 2014-12-03 marcelgmr del_mark(&rover.marks, ENAME(ESEL));
956 dbb53e68 2014-12-03 marcelgmr add_mark(&rover.marks, CWD, INPUT);
957 dbb53e68 2014-12-03 marcelgmr }
958 00ecdc1e 2014-11-19 marcelgmr cd(1);
959 00ecdc1e 2014-11-19 marcelgmr try_to_sel(INPUT);
960 00ecdc1e 2014-11-19 marcelgmr update_view();
961 590d39af 2014-12-02 marcelgmr } else
962 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
963 bdb195d0 2014-11-14 marcelgmr }
964 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_MARK)) {
965 7c74df90 2014-11-15 marcelgmr if (MARKED(ESEL))
966 7c74df90 2014-11-15 marcelgmr del_mark(&rover.marks, ENAME(ESEL));
967 18016333 2014-11-14 marcelgmr else
968 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(ESEL));
969 7c74df90 2014-11-15 marcelgmr MARKED(ESEL) = !MARKED(ESEL);
970 7c74df90 2014-11-15 marcelgmr ESEL = (ESEL + 1) % rover.nfiles;
971 743e70b8 2014-11-14 marcelgmr update_view();
972 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_INVMARK)) {
973 5fc8b927 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
974 5fc8b927 2014-11-14 marcelgmr if (MARKED(i))
975 7c74df90 2014-11-15 marcelgmr del_mark(&rover.marks, ENAME(i));
976 5fc8b927 2014-11-14 marcelgmr else
977 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(i));
978 5fc8b927 2014-11-14 marcelgmr MARKED(i) = !MARKED(i);
979 5fc8b927 2014-11-14 marcelgmr }
980 743e70b8 2014-11-14 marcelgmr update_view();
981 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_MARKALL)) {
982 5fc8b927 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++)
983 5fc8b927 2014-11-14 marcelgmr if (!MARKED(i)) {
984 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(i));
985 5fc8b927 2014-11-14 marcelgmr MARKED(i) = 1;
986 5fc8b927 2014-11-14 marcelgmr }
987 743e70b8 2014-11-14 marcelgmr update_view();
988 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_DELETE)) {
989 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries) {
990 a31a7b29 2014-11-15 marcelgmr message("Delete marked entries? (Y to confirm)", YELLOW);
991 a31a7b29 2014-11-15 marcelgmr if (getch() == 'Y')
992 a31a7b29 2014-11-15 marcelgmr process_marked(NULL, delfile, deldir);
993 590d39af 2014-12-02 marcelgmr else
994 590d39af 2014-12-02 marcelgmr clear_message();
995 590d39af 2014-12-02 marcelgmr } else
996 590d39af 2014-12-02 marcelgmr message("No entries marked for deletion.", RED);
997 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_COPY)) {
998 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries)
999 a31a7b29 2014-11-15 marcelgmr process_marked(adddir, cpyfile, NULL);
1000 590d39af 2014-12-02 marcelgmr else
1001 590d39af 2014-12-02 marcelgmr message("No entries marked for copying.", RED);
1002 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_MOVE)) {
1003 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries)
1004 a31a7b29 2014-11-15 marcelgmr process_marked(adddir, movfile, deldir);
1005 590d39af 2014-12-02 marcelgmr else
1006 590d39af 2014-12-02 marcelgmr message("No entries marked for moving.", RED);
1007 a31a7b29 2014-11-15 marcelgmr }
1008 576e768d 2014-11-08 marcelgmr }
1009 18016333 2014-11-14 marcelgmr if (rover.nfiles)
1010 18016333 2014-11-14 marcelgmr free_rows(&rover.rows, rover.nfiles);
1011 c3758004 2014-11-15 marcelgmr free_marks(&rover.marks);
1012 f05efd9e 2014-11-07 marcelgmr delwin(rover.window);
1013 71ee1627 2014-11-07 marcelgmr return 0;
1014 71ee1627 2014-11-07 marcelgmr }