My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

937 line
18 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <curses.h>
  6. #include <dirent.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <libgen.h>
  10. #include <limits.h>
  11. #include <locale.h>
  12. #include <regex.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include "util.h"
  20. #ifdef DEBUG
  21. #define DEBUG_FD 8
  22. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  23. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  24. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  25. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  26. #else
  27. #define DPRINTF_D(x)
  28. #define DPRINTF_U(x)
  29. #define DPRINTF_S(x)
  30. #define DPRINTF_P(x)
  31. #endif /* DEBUG */
  32. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  33. #undef MIN
  34. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  35. #define ISODD(x) ((x) & 1)
  36. #define CONTROL(c) ((c) ^ 0x40)
  37. #define TOUPPER(ch) \
  38. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  39. #define MAX_LEN 1024
  40. struct assoc {
  41. char *regex; /* Regex to match on filename */
  42. char *bin; /* Program */
  43. };
  44. /* Supported actions */
  45. enum action {
  46. SEL_QUIT = 1,
  47. SEL_BACK,
  48. SEL_GOIN,
  49. SEL_FLTR,
  50. SEL_NEXT,
  51. SEL_PREV,
  52. SEL_PGDN,
  53. SEL_PGUP,
  54. SEL_HOME,
  55. SEL_END,
  56. SEL_CD,
  57. SEL_CDHOME,
  58. SEL_TOGGLEDOT,
  59. SEL_DETAIL,
  60. SEL_FSIZE,
  61. SEL_MTIME,
  62. SEL_REDRAW,
  63. SEL_RUN,
  64. SEL_RUNARG,
  65. };
  66. struct key {
  67. int sym; /* Key pressed */
  68. enum action act; /* Action */
  69. char *run; /* Program to run */
  70. char *env; /* Environment variable to run */
  71. };
  72. #include "config.h"
  73. struct entry {
  74. char name[PATH_MAX];
  75. mode_t mode;
  76. time_t t;
  77. off_t size;
  78. };
  79. /* Global context */
  80. struct entry *dents;
  81. int ndents, cur;
  82. int idle;
  83. char *opener = NULL;
  84. char *fallback_opener = NULL;
  85. char size_buf[12]; /* Buffer to hold human readable size */
  86. const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  87. /*
  88. * Layout:
  89. * .---------
  90. * | cwd: /mnt/path
  91. * |
  92. * | file0
  93. * | file1
  94. * | > file2
  95. * | file3
  96. * | file4
  97. * ...
  98. * | filen
  99. * |
  100. * | Permission denied
  101. * '------
  102. */
  103. void (*printptr)(struct entry *ent, int active);
  104. void printmsg(char *);
  105. void printwarn(void);
  106. void printerr(int, char *);
  107. #undef dprintf
  108. int
  109. dprintf(int fd, const char *fmt, ...)
  110. {
  111. char buf[BUFSIZ];
  112. int r;
  113. va_list ap;
  114. va_start(ap, fmt);
  115. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  116. if (r > 0)
  117. r = write(fd, buf, r);
  118. va_end(ap);
  119. return r;
  120. }
  121. void *
  122. xmalloc(size_t size)
  123. {
  124. void *p;
  125. p = malloc(size);
  126. if (p == NULL)
  127. printerr(1, "malloc");
  128. return p;
  129. }
  130. void *
  131. xrealloc(void *p, size_t size)
  132. {
  133. p = realloc(p, size);
  134. if (p == NULL)
  135. printerr(1, "realloc");
  136. return p;
  137. }
  138. char *
  139. xstrdup(const char *s)
  140. {
  141. char *p;
  142. p = strdup(s);
  143. if (p == NULL)
  144. printerr(1, "strdup");
  145. return p;
  146. }
  147. /* Some implementations of dirname(3) may modify `path' and some
  148. * return a pointer inside `path'. */
  149. char *
  150. xdirname(const char *path)
  151. {
  152. static char out[PATH_MAX];
  153. char tmp[PATH_MAX], *p;
  154. strlcpy(tmp, path, sizeof(tmp));
  155. p = dirname(tmp);
  156. if (p == NULL)
  157. printerr(1, "dirname");
  158. strlcpy(out, p, sizeof(out));
  159. return out;
  160. }
  161. void
  162. spawn(char *file, char *arg, char *dir)
  163. {
  164. pid_t pid;
  165. int status;
  166. pid = fork();
  167. if (pid == 0) {
  168. if (dir != NULL)
  169. status = chdir(dir);
  170. execlp(file, file, arg, NULL);
  171. _exit(1);
  172. } else {
  173. /* Ignore interruptions */
  174. while (waitpid(pid, &status, 0) == -1)
  175. DPRINTF_D(status);
  176. DPRINTF_D(pid);
  177. }
  178. }
  179. char *
  180. xgetenv(char *name, char *fallback)
  181. {
  182. char *value;
  183. if (name == NULL)
  184. return fallback;
  185. value = getenv(name);
  186. return value && value[0] ? value : fallback;
  187. }
  188. int
  189. xstricmp(const char *s1, const char *s2)
  190. {
  191. while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2))
  192. s1++, s2++;
  193. /* In case of alphabetically same names, make sure
  194. lower case one comes before upper case one */
  195. if (!*s1 && !*s2)
  196. return 1;
  197. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  198. }
  199. char *
  200. openwith(char *file)
  201. {
  202. regex_t regex;
  203. char *bin = NULL;
  204. int i;
  205. for (i = 0; i < LEN(assocs); i++) {
  206. if (regcomp(&regex, assocs[i].regex,
  207. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  208. continue;
  209. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  210. bin = assocs[i].bin;
  211. break;
  212. }
  213. }
  214. DPRINTF_S(bin);
  215. return bin;
  216. }
  217. int
  218. setfilter(regex_t *regex, char *filter)
  219. {
  220. char errbuf[LINE_MAX];
  221. size_t len;
  222. int r;
  223. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  224. if (r != 0) {
  225. len = COLS;
  226. if (len > sizeof(errbuf))
  227. len = sizeof(errbuf);
  228. regerror(r, regex, errbuf, len);
  229. printmsg(errbuf);
  230. }
  231. return r;
  232. }
  233. void
  234. initfilter(int dot, char **ifilter)
  235. {
  236. *ifilter = dot ? "." : "^[^.]";
  237. }
  238. int
  239. visible(regex_t *regex, char *file)
  240. {
  241. return regexec(regex, file, 0, NULL, 0) == 0;
  242. }
  243. int
  244. entrycmp(const void *va, const void *vb)
  245. {
  246. if (mtimeorder)
  247. return ((struct entry *)vb)->t - ((struct entry *)va)->t;
  248. if (sizeorder)
  249. return ((struct entry *)vb)->size - ((struct entry *)va)->size;
  250. return xstricmp(((struct entry *)va)->name, ((struct entry *)vb)->name);
  251. }
  252. void
  253. initcurses(void)
  254. {
  255. if (initscr() == NULL) {
  256. char *term = getenv("TERM");
  257. if (term != NULL)
  258. fprintf(stderr, "error opening terminal: %s\n", term);
  259. else
  260. fprintf(stderr, "failed to initialize curses\n");
  261. exit(1);
  262. }
  263. cbreak();
  264. noecho();
  265. nonl();
  266. intrflush(stdscr, FALSE);
  267. keypad(stdscr, TRUE);
  268. curs_set(FALSE); /* Hide cursor */
  269. timeout(1000); /* One second */
  270. }
  271. void
  272. exitcurses(void)
  273. {
  274. endwin(); /* Restore terminal */
  275. }
  276. /* Messages show up at the bottom */
  277. void
  278. printmsg(char *msg)
  279. {
  280. move(LINES - 1, 0);
  281. printw("%s\n", msg);
  282. }
  283. /* Display warning as a message */
  284. void
  285. printwarn(void)
  286. {
  287. printmsg(strerror(errno));
  288. }
  289. /* Kill curses and display error before exiting */
  290. void
  291. printerr(int ret, char *prefix)
  292. {
  293. exitcurses();
  294. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  295. exit(ret);
  296. }
  297. /* Clear the last line */
  298. void
  299. clearprompt(void)
  300. {
  301. printmsg("");
  302. }
  303. /* Print prompt on the last line */
  304. void
  305. printprompt(char *str)
  306. {
  307. clearprompt();
  308. printw(str);
  309. }
  310. /* Returns SEL_* if key is bound and 0 otherwise.
  311. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  312. int
  313. nextsel(char **run, char **env)
  314. {
  315. int c, i;
  316. c = getch();
  317. if (c == -1)
  318. idle++;
  319. else
  320. idle = 0;
  321. for (i = 0; i < LEN(bindings); i++)
  322. if (c == bindings[i].sym) {
  323. *run = bindings[i].run;
  324. *env = bindings[i].env;
  325. return bindings[i].act;
  326. }
  327. return 0;
  328. }
  329. char *
  330. readln(void)
  331. {
  332. static char ln[LINE_MAX];
  333. timeout(-1);
  334. echo();
  335. curs_set(TRUE);
  336. memset(ln, 0, sizeof(ln));
  337. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  338. noecho();
  339. curs_set(FALSE);
  340. timeout(1000);
  341. return ln[0] ? ln : NULL;
  342. }
  343. int
  344. canopendir(char *path)
  345. {
  346. DIR *dirp;
  347. dirp = opendir(path);
  348. if (dirp == NULL)
  349. return 0;
  350. closedir(dirp);
  351. return 1;
  352. }
  353. char *
  354. mkpath(char *dir, char *name, char *out, size_t n)
  355. {
  356. /* Handle absolute path */
  357. if (name[0] == '/')
  358. strlcpy(out, name, n);
  359. else {
  360. /* Handle root case */
  361. if (strcmp(dir, "/") == 0)
  362. snprintf(out, n, "/%s", name);
  363. else
  364. snprintf(out, n, "%s/%s", dir, name);
  365. }
  366. return out;
  367. }
  368. void
  369. printent(struct entry *ent, int active)
  370. {
  371. if (S_ISDIR(ent->mode))
  372. printw("%s%s/\n", active ? CURSR : EMPTY, ent->name);
  373. else if (S_ISLNK(ent->mode))
  374. printw("%s%s@\n", active ? CURSR : EMPTY, ent->name);
  375. else if (S_ISSOCK(ent->mode))
  376. printw("%s%s=\n", active ? CURSR : EMPTY, ent->name);
  377. else if (S_ISFIFO(ent->mode))
  378. printw("%s%s|\n", active ? CURSR : EMPTY, ent->name);
  379. else if (ent->mode & S_IXUSR)
  380. printw("%s%s*\n", active ? CURSR : EMPTY, ent->name);
  381. else
  382. printw("%s%s\n", active ? CURSR : EMPTY, ent->name);
  383. }
  384. char*
  385. coolsize(off_t size)
  386. {
  387. int i = 0;
  388. long double fsize = (double)size;
  389. while (fsize > 1024) {
  390. fsize /= 1024;
  391. i++;
  392. }
  393. snprintf(size_buf, 12, "%.*Lf%s", i, fsize, size_units[i]);
  394. return size_buf;
  395. }
  396. void
  397. printent_long(struct entry *ent, int active)
  398. {
  399. if (S_ISDIR(ent->mode))
  400. printw("%s%-32.32s D\n", active ? CURSR : EMPTY, ent->name);
  401. else if (S_ISLNK(ent->mode))
  402. printw("%s%-32.32s L\n", active ? CURSR : EMPTY, ent->name);
  403. else if (S_ISSOCK(ent->mode))
  404. printw("%s%-32.32s S\n", active ? CURSR : EMPTY, ent->name);
  405. else if (S_ISFIFO(ent->mode))
  406. printw("%s%-32.32s F\n", active ? CURSR : EMPTY, ent->name);
  407. else if (S_ISBLK(ent->mode))
  408. printw("%s%-32.32s B\n", active ? CURSR : EMPTY, ent->name);
  409. else if (S_ISCHR(ent->mode))
  410. printw("%s%-32.32s C\n", active ? CURSR : EMPTY, ent->name);
  411. else if (ent->mode & S_IXUSR)
  412. printw("%s%-32.32s E %s\n", active ? CURSR : EMPTY, ent->name,
  413. coolsize(ent->size));
  414. else
  415. printw("%s%-32.32s R %s\n", active ? CURSR : EMPTY, ent->name,
  416. coolsize(ent->size));
  417. }
  418. int
  419. dentfill(char *path, struct entry **dents,
  420. int (*filter)(regex_t *, char *), regex_t *re)
  421. {
  422. char newpath[PATH_MAX];
  423. DIR *dirp;
  424. struct dirent *dp;
  425. struct stat sb;
  426. int r, n = 0;
  427. dirp = opendir(path);
  428. if (dirp == NULL)
  429. return 0;
  430. while ((dp = readdir(dirp)) != NULL) {
  431. /* Skip self and parent */
  432. if (strcmp(dp->d_name, ".") == 0 ||
  433. strcmp(dp->d_name, "..") == 0)
  434. continue;
  435. if (filter(re, dp->d_name) == 0)
  436. continue;
  437. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  438. strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  439. /* Get mode flags */
  440. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  441. r = lstat(newpath, &sb);
  442. if (r == -1)
  443. printerr(1, "lstat");
  444. (*dents)[n].mode = sb.st_mode;
  445. (*dents)[n].t = sb.st_mtime;
  446. (*dents)[n].size = sb.st_size;
  447. n++;
  448. }
  449. /* Should never be null */
  450. r = closedir(dirp);
  451. if (r == -1)
  452. printerr(1, "closedir");
  453. return n;
  454. }
  455. void
  456. dentfree(struct entry *dents)
  457. {
  458. free(dents);
  459. }
  460. /* Return the position of the matching entry or 0 otherwise */
  461. int
  462. dentfind(struct entry *dents, int n, char *cwd, char *path)
  463. {
  464. char tmp[PATH_MAX];
  465. int i;
  466. if (path == NULL)
  467. return 0;
  468. for (i = 0; i < n; i++) {
  469. mkpath(cwd, dents[i].name, tmp, sizeof(tmp));
  470. DPRINTF_S(path);
  471. DPRINTF_S(tmp);
  472. if (strcmp(tmp, path) == 0)
  473. return i;
  474. }
  475. return 0;
  476. }
  477. int
  478. populate(char *path, char *oldpath, char *fltr)
  479. {
  480. regex_t re;
  481. int r;
  482. /* Can fail when permissions change while browsing */
  483. if (canopendir(path) == 0)
  484. return -1;
  485. /* Search filter */
  486. r = setfilter(&re, fltr);
  487. if (r != 0)
  488. return -1;
  489. dentfree(dents);
  490. ndents = 0;
  491. dents = NULL;
  492. ndents = dentfill(path, &dents, visible, &re);
  493. qsort(dents, ndents, sizeof(*dents), entrycmp);
  494. /* Find cur from history */
  495. cur = dentfind(dents, ndents, path, oldpath);
  496. return 0;
  497. }
  498. void
  499. redraw(char *path)
  500. {
  501. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  502. size_t ncols;
  503. int nlines, odd;
  504. int i;
  505. nlines = MIN(LINES - 4, ndents);
  506. /* Clean screen */
  507. erase();
  508. /* Strip trailing slashes */
  509. for (i = strlen(path) - 1; i > 0; i--)
  510. if (path[i] == '/')
  511. path[i] = '\0';
  512. else
  513. break;
  514. DPRINTF_D(cur);
  515. DPRINTF_S(path);
  516. /* No text wrapping in cwd line */
  517. ncols = COLS;
  518. if (ncols > PATH_MAX)
  519. ncols = PATH_MAX;
  520. strlcpy(cwd, path, ncols);
  521. cwd[ncols - strlen(CWD) - 1] = '\0';
  522. if (!realpath(path, cwdresolved)) {
  523. printmsg("Cannot resolve path");
  524. return;
  525. }
  526. printw(CWD "%s\n\n", cwdresolved);
  527. /* Print listing */
  528. odd = ISODD(nlines);
  529. if (cur < (nlines >> 1)) {
  530. for (i = 0; i < nlines; i++)
  531. printptr(&dents[i], i == cur);
  532. } else if (cur >= ndents - (nlines >> 1)) {
  533. for (i = ndents - nlines; i < ndents; i++)
  534. printptr(&dents[i], i == cur);
  535. } else {
  536. nlines >>= 1;
  537. for (i = cur - nlines; i < cur + nlines + odd; i++)
  538. printptr(&dents[i], i == cur);
  539. }
  540. }
  541. void
  542. browse(char *ipath, char *ifilter)
  543. {
  544. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  545. char fltr[LINE_MAX];
  546. char *bin, *dir, *tmp, *run, *env;
  547. struct stat sb;
  548. regex_t re;
  549. int r, fd;
  550. strlcpy(path, ipath, sizeof(path));
  551. strlcpy(fltr, ifilter, sizeof(fltr));
  552. oldpath[0] = '\0';
  553. begin:
  554. r = populate(path, oldpath, fltr);
  555. if (r == -1) {
  556. printwarn();
  557. goto nochange;
  558. }
  559. for (;;) {
  560. redraw(path);
  561. nochange:
  562. switch (nextsel(&run, &env)) {
  563. case SEL_QUIT:
  564. dentfree(dents);
  565. return;
  566. case SEL_BACK:
  567. /* There is no going back */
  568. if (strcmp(path, "/") == 0 ||
  569. strcmp(path, ".") == 0 ||
  570. strchr(path, '/') == NULL)
  571. goto nochange;
  572. dir = xdirname(path);
  573. if (canopendir(dir) == 0) {
  574. printwarn();
  575. goto nochange;
  576. }
  577. /* Save history */
  578. strlcpy(oldpath, path, sizeof(oldpath));
  579. strlcpy(path, dir, sizeof(path));
  580. /* Reset filter */
  581. strlcpy(fltr, ifilter, sizeof(fltr));
  582. goto begin;
  583. case SEL_GOIN:
  584. /* Cannot descend in empty directories */
  585. if (ndents == 0)
  586. goto nochange;
  587. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  588. DPRINTF_S(newpath);
  589. /* Get path info */
  590. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  591. if (fd == -1) {
  592. printwarn();
  593. goto nochange;
  594. }
  595. r = fstat(fd, &sb);
  596. if (r == -1) {
  597. printwarn();
  598. close(fd);
  599. goto nochange;
  600. }
  601. close(fd);
  602. DPRINTF_U(sb.st_mode);
  603. switch (sb.st_mode & S_IFMT) {
  604. case S_IFDIR:
  605. if (canopendir(newpath) == 0) {
  606. printwarn();
  607. goto nochange;
  608. }
  609. strlcpy(path, newpath, sizeof(path));
  610. /* Reset filter */
  611. strlcpy(fltr, ifilter, sizeof(fltr));
  612. goto begin;
  613. case S_IFREG:
  614. /* If default mime opener is set, use it */
  615. if (opener) {
  616. char cmd[MAX_LEN];
  617. int status;
  618. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  619. opener, newpath);
  620. status = system(cmd);
  621. continue;
  622. }
  623. /* Try custom applications */
  624. bin = openwith(newpath);
  625. char *execvim = "vim";
  626. if (bin == NULL) {
  627. /* If a custom handler application is not set, open
  628. plain text files with vim, then try fallback_opener */
  629. FILE *fp;
  630. char cmd[MAX_LEN];
  631. int status;
  632. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  633. fp = popen(cmd, "r");
  634. if (fp == NULL)
  635. goto nochange;
  636. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  637. pclose(fp);
  638. goto nochange;
  639. }
  640. pclose(fp);
  641. if (strstr(cmd, "ASCII text") != NULL)
  642. bin = execvim;
  643. else if (fallback_opener) {
  644. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  645. fallback_opener, newpath);
  646. status = system(cmd);
  647. continue;
  648. } else {
  649. printmsg("No association");
  650. goto nochange;
  651. }
  652. }
  653. exitcurses();
  654. spawn(bin, newpath, NULL);
  655. initcurses();
  656. continue;
  657. default:
  658. printmsg("Unsupported file");
  659. goto nochange;
  660. }
  661. case SEL_FLTR:
  662. /* Read filter */
  663. printprompt("filter: ");
  664. tmp = readln();
  665. if (tmp == NULL)
  666. tmp = ifilter;
  667. /* Check and report regex errors */
  668. r = setfilter(&re, tmp);
  669. if (r != 0)
  670. goto nochange;
  671. strlcpy(fltr, tmp, sizeof(fltr));
  672. DPRINTF_S(fltr);
  673. /* Save current */
  674. if (ndents > 0)
  675. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  676. goto begin;
  677. case SEL_NEXT:
  678. if (cur < ndents - 1)
  679. cur++;
  680. else if (ndents)
  681. /* Roll over, set cursor to first entry */
  682. cur = 0;
  683. break;
  684. case SEL_PREV:
  685. if (cur > 0)
  686. cur--;
  687. else if (ndents)
  688. /* Roll over, set cursor to last entry */
  689. cur = ndents - 1;
  690. break;
  691. case SEL_PGDN:
  692. if (cur < ndents - 1)
  693. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  694. break;
  695. case SEL_PGUP:
  696. if (cur > 0)
  697. cur -= MIN((LINES - 4) / 2, cur);
  698. break;
  699. case SEL_HOME:
  700. cur = 0;
  701. break;
  702. case SEL_END:
  703. cur = ndents - 1;
  704. break;
  705. case SEL_CD:
  706. /* Read target dir */
  707. printprompt("chdir: ");
  708. tmp = readln();
  709. if (tmp == NULL) {
  710. clearprompt();
  711. goto nochange;
  712. }
  713. mkpath(path, tmp, newpath, sizeof(newpath));
  714. if (canopendir(newpath) == 0) {
  715. printwarn();
  716. goto nochange;
  717. }
  718. strlcpy(path, newpath, sizeof(path));
  719. /* Reset filter */
  720. strlcpy(fltr, ifilter, sizeof(fltr))
  721. DPRINTF_S(path);
  722. goto begin;
  723. case SEL_CDHOME:
  724. tmp = getenv("HOME");
  725. if (tmp == NULL) {
  726. clearprompt();
  727. goto nochange;
  728. }
  729. if (canopendir(tmp) == 0) {
  730. printwarn();
  731. goto nochange;
  732. }
  733. strlcpy(path, tmp, sizeof(path));
  734. /* Reset filter */
  735. strlcpy(fltr, ifilter, sizeof(fltr));
  736. DPRINTF_S(path);
  737. goto begin;
  738. case SEL_TOGGLEDOT:
  739. showhidden ^= 1;
  740. initfilter(showhidden, &ifilter);
  741. strlcpy(fltr, ifilter, sizeof(fltr));
  742. goto begin;
  743. case SEL_DETAIL:
  744. showdetail = !showdetail;
  745. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  746. goto begin;
  747. case SEL_FSIZE:
  748. sizeorder = !sizeorder;
  749. mtimeorder = 0;
  750. /* Save current */
  751. if (ndents > 0)
  752. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  753. goto begin;
  754. case SEL_MTIME:
  755. mtimeorder = !mtimeorder;
  756. sizeorder = 0;
  757. /* Save current */
  758. if (ndents > 0)
  759. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  760. goto begin;
  761. case SEL_REDRAW:
  762. /* Save current */
  763. if (ndents > 0)
  764. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  765. goto begin;
  766. case SEL_RUN:
  767. run = xgetenv(env, run);
  768. exitcurses();
  769. spawn(run, NULL, path);
  770. initcurses();
  771. /* Re-populate as directory content may have changed */
  772. goto begin;
  773. case SEL_RUNARG:
  774. run = xgetenv(env, run);
  775. exitcurses();
  776. spawn(run, dents[cur].name, path);
  777. initcurses();
  778. break;
  779. }
  780. /* Screensaver */
  781. if (idletimeout != 0 && idle == idletimeout) {
  782. idle = 0;
  783. exitcurses();
  784. spawn(idlecmd, NULL, NULL);
  785. initcurses();
  786. }
  787. }
  788. }
  789. void
  790. usage(char *argv0)
  791. {
  792. fprintf(stderr, "usage: %s [dir]\n", argv0);
  793. exit(1);
  794. }
  795. int
  796. main(int argc, char *argv[])
  797. {
  798. char cwd[PATH_MAX], *ipath;
  799. char *ifilter;
  800. if (argc > 2)
  801. usage(argv[0]);
  802. /* Confirm we are in a terminal */
  803. if (!isatty(0) || !isatty(1)) {
  804. fprintf(stderr, "stdin or stdout is not a tty\n");
  805. exit(1);
  806. }
  807. if (getuid() == 0)
  808. showhidden = 1;
  809. initfilter(showhidden, &ifilter);
  810. printptr = &printent;
  811. if (argv[1] != NULL) {
  812. ipath = argv[1];
  813. } else {
  814. ipath = getcwd(cwd, sizeof(cwd));
  815. if (ipath == NULL)
  816. ipath = "/";
  817. }
  818. /* Get the default desktop mime opener, if set */
  819. opener = getenv("NOICE_OPENER");
  820. /* Get the fallback desktop mime opener, if set */
  821. fallback_opener = getenv("NOICE_FALLBACK_OPENER");
  822. signal(SIGINT, SIG_IGN);
  823. /* Test initial path */
  824. if (canopendir(ipath) == 0) {
  825. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  826. exit(1);
  827. }
  828. /* Set locale before curses setup */
  829. setlocale(LC_ALL, "");
  830. initcurses();
  831. browse(ipath, ifilter);
  832. exitcurses();
  833. exit(0);
  834. }