5 #include <sys/types.h> /* pid_t, ... */
6 #include <stdio.h> /* FILENAME_MAX */
7 #include <locale.h> /* setlocale(), LC_ALL */
8 #include <unistd.h> /* chdir(), getcwd() */
9 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
11 #include <sys/wait.h> /* waitpid() */
19 char STATUS[STATUSSZ];
21 char SEARCH[SEARCHSZ];
25 typedef enum {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} color_t;
27 #define HEIGHT (LINES-4)
29 #define SHOW_FILES 0x01u
30 #define SHOW_DIRS 0x02u
31 #define SHOW_HIDDEN 0x04u
46 char cwd[10][FILENAME_MAX];
49 #define FNAME(I) rover.rows[I].name
50 #define FSIZE(I) rover.rows[I].size
51 #define SCROLL rover.scroll[rover.tab]
52 #define FSEL rover.fsel[rover.tab]
53 #define FLAGS rover.flags[rover.tab]
54 #define CWD rover.cwd[rover.tab]
57 rowcmp(const void *a, const void *b)
59 int isdir1, isdir2, cmpdir;
62 isdir1 = strchr(r1->name, '/') != NULL;
63 isdir2 = strchr(r2->name, '/') != NULL;
64 cmpdir = isdir2 - isdir1;
65 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
69 free_rows(row_t **rowsp, int nfiles)
73 for (i = 0; i < nfiles; i++)
74 free((*rowsp)[i].name);
80 ls(char *path, row_t **rowsp, uint8_t flags)
88 if((dp = opendir(path)) == NULL)
90 n = -2; /* We don't want the entries "." and "..". */
91 while (readdir(dp)) n++;
93 rows = (row_t *) malloc(n * sizeof(row_t));
95 while ((ep = readdir(dp))) {
96 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
98 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
100 /* FIXME: ANSI C doesn't have lstat(). How do we handle symlinks? */
101 stat(ep->d_name, &statbuf);
102 if (S_ISDIR(statbuf.st_mode)) {
103 if (flags & SHOW_DIRS) {
104 rows[i].name = (char *) malloc(strlen(ep->d_name) + 2);
105 strcpy(rows[i].name, ep->d_name);
106 strcat(rows[i].name, "/");
110 else if (flags & SHOW_FILES) {
111 rows[i].name = (char *) malloc(strlen(ep->d_name) + 1);
112 strcpy(rows[i].name, ep->d_name);
113 rows[i].size = statbuf.st_size;
117 n = i; /* Ignore unused space in array caused by filters. */
118 qsort(rows, n, sizeof(row_t), rowcmp);
130 void handle_winch(int sig);
137 setlocale(LC_ALL, "");
139 cbreak(); /* Get one character at a time. */
141 nonl(); /* No NL->CR/NL on output. */
142 intrflush(stdscr, FALSE);
143 keypad(stdscr, TRUE);
144 curs_set(FALSE); /* Hide blinking cursor. */
145 memset(&sa, 0, sizeof(struct sigaction));
146 sa.sa_handler = handle_winch;
147 sigaction(SIGWINCH, &sa, NULL);
150 init_pair(RED, COLOR_RED, COLOR_BLACK);
151 init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);
152 init_pair(YELLOW, COLOR_YELLOW,COLOR_BLACK);
153 init_pair(BLUE, COLOR_BLUE, COLOR_BLACK);
154 init_pair(CYAN, COLOR_CYAN, COLOR_BLACK);
155 init_pair(MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
156 init_pair(WHITE, COLOR_WHITE, COLOR_BLACK);
167 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
168 ishidden = FNAME(j)[0] == '.';
169 isdir = strchr(FNAME(j), '/') != NULL;
171 wattr_on(rover.window, A_REVERSE, NULL);
173 wcolor_set(rover.window, RVC_HIDDEN, NULL);
175 wcolor_set(rover.window, RVC_DIR, NULL);
177 wcolor_set(rover.window, RVC_FILE, NULL);
179 sprintf(ROW, "%s%*d", FNAME(j),
180 COLS - strlen(FNAME(j)) - 2, (int) FSIZE(j));
182 strcpy(ROW, FNAME(j));
183 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
184 mvwaddnstr(rover.window, i + 1, 1, ROW, COLS - 2);
185 wcolor_set(rover.window, DEFAULT, NULL);
187 wattr_off(rover.window, A_REVERSE, NULL);
189 if (rover.nfiles > HEIGHT) {
191 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
192 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
193 if (!height) height = 1;
194 wcolor_set(rover.window, RVC_BORDER, NULL);
195 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
196 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
197 mvwvline(rover.window, center-(height>>1)+1, COLS-1, ACS_CKBOARD, height);
198 wcolor_set(rover.window, DEFAULT, NULL);
200 wrefresh(rover.window);
201 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
202 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
203 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
207 sprintf(ROW, "%d/%d", FSEL + 1, rover.nfiles);
208 sprintf(STATUS+3, "%*s", 12, ROW);
209 color_set(RVC_STATUS, NULL);
210 mvaddstr(LINES - 1, COLS - 15, STATUS);
211 color_set(DEFAULT, NULL);
215 /* NOTE: The caller needs to write the new path to CWD
216 * *before* calling this function. */
223 mvhline(0, 0, ' ', COLS);
224 color_set(RVC_CWD, NULL);
225 mvaddnstr(0, 0, CWD, COLS);
226 color_set(DEFAULT, NULL);
228 attr_on(A_BOLD, NULL);
229 color_set(RVC_TABNUM, NULL);
230 echochar(rover.tab + '0');
231 color_set(DEFAULT, NULL);
232 attr_off(A_BOLD, NULL);
234 free_rows(&rover.rows, rover.nfiles);
235 rover.nfiles = ls(CWD, &rover.rows, FLAGS);
236 wclear(rover.window);
237 wcolor_set(rover.window, RVC_BORDER, NULL);
238 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
239 wcolor_set(rover.window, DEFAULT, NULL);
251 /* fork() succeeded. */
253 waitpid(pid, &status, 0);
259 execvp(args[0], args);
263 /* Interactive getstr(). */
265 igetstr(char *buffer, int maxlen)
269 length = strlen(buffer);
271 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER)
273 else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
275 buffer[--length] = '\0';
277 else if (ch == killchar()) {
281 else if (length < maxlen - 1 && isprint(ch)) {
282 buffer[length++] = ch;
283 buffer[length] = '\0';
289 handle_winch(int sig)
292 delwin(rover.window);
296 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
301 main(int argc, char *argv[])
308 /* Avoid invalid free() calls in cd() by zeroing the tally. */
310 for (i = 0; i < 10; i++) {
311 rover.fsel[i] = rover.scroll[i] = 0;
312 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
314 strcpy(rover.cwd[0], getenv("HOME"));
315 for (i = 1; i < argc && i < 10; i++) {
316 d = opendir(argv[i]);
318 strcpy(rover.cwd[i], argv[i]);
321 else strcpy(rover.cwd[i], rover.cwd[0]);
323 getcwd(rover.cwd[i], FILENAME_MAX);
324 for (i++; i < 10; i++)
325 strcpy(rover.cwd[i], rover.cwd[i-1]);
326 for (i = 0; i < 10; i++)
327 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
328 strcat(rover.cwd[i], "/");
330 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
335 if (!strcmp(key, RVK_QUIT))
337 else if (ch >= '0' && ch <= '9') {
338 rover.tab = ch - '0';
341 else if (!strcmp(key, RVK_DOWN)) {
342 if (!rover.nfiles) continue;
343 if (FSEL == rover.nfiles - 1)
347 if ((FSEL - SCROLL) == HEIGHT)
352 else if (!strcmp(key, RVK_UP)) {
353 if (!rover.nfiles) continue;
355 FSEL = rover.nfiles - 1;
356 SCROLL = rover.nfiles - HEIGHT;
367 else if (!strcmp(key, RVK_JUMP_DOWN)) {
368 if (!rover.nfiles) continue;
370 if (FSEL >= rover.nfiles)
371 FSEL = rover.nfiles - 1;
372 if (rover.nfiles > HEIGHT) {
374 if (SCROLL > rover.nfiles - HEIGHT)
375 SCROLL = rover.nfiles - HEIGHT;
379 else if (!strcmp(key, RVK_JUMP_UP)) {
380 if (!rover.nfiles) continue;
389 else if (!strcmp(key, RVK_CD_DOWN)) {
390 if (!rover.nfiles) continue;
391 if (strchr(FNAME(FSEL), '/') == NULL)
393 strcat(CWD, FNAME(FSEL));
396 else if (!strcmp(key, RVK_CD_UP)) {
397 char *dirname, first;
398 if (strlen(CWD) == 1)
400 CWD[strlen(CWD) - 1] = '\0';
401 dirname = strrchr(CWD, '/') + 1;
405 if ((FLAGS & SHOW_DIRS) &&
406 ((FLAGS & SHOW_HIDDEN) || (first != '.'))
409 dirname[strlen(dirname)] = '/';
410 while (strcmp(FNAME(FSEL), dirname))
412 if (rover.nfiles > HEIGHT) {
413 SCROLL = FSEL - (HEIGHT >> 1);
416 if (SCROLL > rover.nfiles - HEIGHT)
417 SCROLL = rover.nfiles - HEIGHT;
423 else if (!strcmp(key, RVK_HOME)) {
424 strcpy(CWD, getenv("HOME"));
425 if (CWD[strlen(CWD) - 1] != '/')
429 else if (!strcmp(key, RVK_SHELL)) {
430 program = getenv("SHELL");
437 else if (!strcmp(key, RVK_VIEW)) {
438 if (!rover.nfiles) continue;
439 if (strchr(FNAME(FSEL), '/') != NULL)
441 program = getenv("PAGER");
444 args[1] = FNAME(FSEL);
449 else if (!strcmp(key, RVK_EDIT)) {
450 if (!rover.nfiles) continue;
451 if (strchr(FNAME(FSEL), '/') != NULL)
453 program = getenv("EDITOR");
456 args[1] = FNAME(FSEL);
461 else if (!strcmp(key, RVK_SEARCH)) {
462 int oldsel, oldscroll;
463 if (!rover.nfiles) continue;
467 color_set(RVC_PROMPT, NULL);
468 mvaddstr(LINES - 1, 0, "search: ");
469 color_set(DEFAULT, NULL);
470 while (igetstr(SEARCH, SEARCHSZ)) {
473 length = strlen(SEARCH);
475 for (sel = 0; sel < rover.nfiles; sel++)
476 if (!strncmp(FNAME(sel), SEARCH, length))
478 if (sel < rover.nfiles) {
481 if (rover.nfiles > HEIGHT) {
484 else if (sel - 3 > rover.nfiles - HEIGHT)
485 SCROLL = rover.nfiles - HEIGHT;
499 color_set(color, NULL);
500 mvaddstr(LINES - 1, 8, SEARCH);
501 color_set(DEFAULT, NULL);
502 SEARCH[length] = '\0';
508 else if (!strcmp(key, RVK_TG_FILES)) {
512 else if (!strcmp(key, RVK_TG_DIRS)) {
516 else if (!strcmp(key, RVK_TG_HIDDEN)) {
517 FLAGS ^= SHOW_HIDDEN;
522 free_rows(&rover.rows, rover.nfiles);
524 delwin(rover.window);