My build of nnn with minor changes
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

942 Zeilen
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. if (showdetail) {
  541. sprintf(cwd, "%d items", ndents);
  542. printmsg(cwd);
  543. }
  544. }
  545. void
  546. browse(char *ipath, char *ifilter)
  547. {
  548. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  549. char fltr[LINE_MAX];
  550. char *bin, *dir, *tmp, *run, *env;
  551. struct stat sb;
  552. regex_t re;
  553. int r, fd;
  554. strlcpy(path, ipath, sizeof(path));
  555. strlcpy(fltr, ifilter, sizeof(fltr));
  556. oldpath[0] = '\0';
  557. begin:
  558. r = populate(path, oldpath, fltr);
  559. if (r == -1) {
  560. printwarn();
  561. goto nochange;
  562. }
  563. for (;;) {
  564. redraw(path);
  565. nochange:
  566. switch (nextsel(&run, &env)) {
  567. case SEL_QUIT:
  568. dentfree(dents);
  569. return;
  570. case SEL_BACK:
  571. /* There is no going back */
  572. if (strcmp(path, "/") == 0 ||
  573. strcmp(path, ".") == 0 ||
  574. strchr(path, '/') == NULL)
  575. goto nochange;
  576. dir = xdirname(path);
  577. if (canopendir(dir) == 0) {
  578. printwarn();
  579. goto nochange;
  580. }
  581. /* Save history */
  582. strlcpy(oldpath, path, sizeof(oldpath));
  583. strlcpy(path, dir, sizeof(path));
  584. /* Reset filter */
  585. strlcpy(fltr, ifilter, sizeof(fltr));
  586. goto begin;
  587. case SEL_GOIN:
  588. /* Cannot descend in empty directories */
  589. if (ndents == 0)
  590. goto nochange;
  591. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  592. DPRINTF_S(newpath);
  593. /* Get path info */
  594. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  595. if (fd == -1) {
  596. printwarn();
  597. goto nochange;
  598. }
  599. r = fstat(fd, &sb);
  600. if (r == -1) {
  601. printwarn();
  602. close(fd);
  603. goto nochange;
  604. }
  605. close(fd);
  606. DPRINTF_U(sb.st_mode);
  607. switch (sb.st_mode & S_IFMT) {
  608. case S_IFDIR:
  609. if (canopendir(newpath) == 0) {
  610. printwarn();
  611. goto nochange;
  612. }
  613. strlcpy(path, newpath, sizeof(path));
  614. /* Reset filter */
  615. strlcpy(fltr, ifilter, sizeof(fltr));
  616. goto begin;
  617. case S_IFREG:
  618. /* If default mime opener is set, use it */
  619. if (opener) {
  620. char cmd[MAX_LEN];
  621. int status;
  622. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  623. opener, newpath);
  624. status = system(cmd);
  625. continue;
  626. }
  627. /* Try custom applications */
  628. bin = openwith(newpath);
  629. char *execvim = "vim";
  630. if (bin == NULL) {
  631. /* If a custom handler application is not set, open
  632. plain text files with vim, then try fallback_opener */
  633. FILE *fp;
  634. char cmd[MAX_LEN];
  635. int status;
  636. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  637. fp = popen(cmd, "r");
  638. if (fp == NULL)
  639. goto nochange;
  640. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  641. pclose(fp);
  642. goto nochange;
  643. }
  644. pclose(fp);
  645. if (strstr(cmd, "ASCII text") != NULL)
  646. bin = execvim;
  647. else if (fallback_opener) {
  648. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  649. fallback_opener, newpath);
  650. status = system(cmd);
  651. continue;
  652. } else {
  653. printmsg("No association");
  654. goto nochange;
  655. }
  656. }
  657. exitcurses();
  658. spawn(bin, newpath, NULL);
  659. initcurses();
  660. continue;
  661. default:
  662. printmsg("Unsupported file");
  663. goto nochange;
  664. }
  665. case SEL_FLTR:
  666. /* Read filter */
  667. printprompt("filter: ");
  668. tmp = readln();
  669. if (tmp == NULL)
  670. tmp = ifilter;
  671. /* Check and report regex errors */
  672. r = setfilter(&re, tmp);
  673. if (r != 0)
  674. goto nochange;
  675. strlcpy(fltr, tmp, sizeof(fltr));
  676. DPRINTF_S(fltr);
  677. /* Save current */
  678. if (ndents > 0)
  679. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  680. goto begin;
  681. case SEL_NEXT:
  682. if (cur < ndents - 1)
  683. cur++;
  684. else if (ndents)
  685. /* Roll over, set cursor to first entry */
  686. cur = 0;
  687. break;
  688. case SEL_PREV:
  689. if (cur > 0)
  690. cur--;
  691. else if (ndents)
  692. /* Roll over, set cursor to last entry */
  693. cur = ndents - 1;
  694. break;
  695. case SEL_PGDN:
  696. if (cur < ndents - 1)
  697. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  698. break;
  699. case SEL_PGUP:
  700. if (cur > 0)
  701. cur -= MIN((LINES - 4) / 2, cur);
  702. break;
  703. case SEL_HOME:
  704. cur = 0;
  705. break;
  706. case SEL_END:
  707. cur = ndents - 1;
  708. break;
  709. case SEL_CD:
  710. /* Read target dir */
  711. printprompt("chdir: ");
  712. tmp = readln();
  713. if (tmp == NULL) {
  714. clearprompt();
  715. goto nochange;
  716. }
  717. mkpath(path, tmp, newpath, sizeof(newpath));
  718. if (canopendir(newpath) == 0) {
  719. printwarn();
  720. goto nochange;
  721. }
  722. strlcpy(path, newpath, sizeof(path));
  723. /* Reset filter */
  724. strlcpy(fltr, ifilter, sizeof(fltr))
  725. DPRINTF_S(path);
  726. goto begin;
  727. case SEL_CDHOME:
  728. tmp = getenv("HOME");
  729. if (tmp == NULL) {
  730. clearprompt();
  731. goto nochange;
  732. }
  733. if (canopendir(tmp) == 0) {
  734. printwarn();
  735. goto nochange;
  736. }
  737. strlcpy(path, tmp, sizeof(path));
  738. /* Reset filter */
  739. strlcpy(fltr, ifilter, sizeof(fltr));
  740. DPRINTF_S(path);
  741. goto begin;
  742. case SEL_TOGGLEDOT:
  743. showhidden ^= 1;
  744. initfilter(showhidden, &ifilter);
  745. strlcpy(fltr, ifilter, sizeof(fltr));
  746. goto begin;
  747. case SEL_DETAIL:
  748. showdetail = !showdetail;
  749. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  750. goto begin;
  751. case SEL_FSIZE:
  752. sizeorder = !sizeorder;
  753. mtimeorder = 0;
  754. /* Save current */
  755. if (ndents > 0)
  756. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  757. goto begin;
  758. case SEL_MTIME:
  759. mtimeorder = !mtimeorder;
  760. sizeorder = 0;
  761. /* Save current */
  762. if (ndents > 0)
  763. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  764. goto begin;
  765. case SEL_REDRAW:
  766. /* Save current */
  767. if (ndents > 0)
  768. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  769. goto begin;
  770. case SEL_RUN:
  771. run = xgetenv(env, run);
  772. exitcurses();
  773. spawn(run, NULL, path);
  774. initcurses();
  775. /* Re-populate as directory content may have changed */
  776. goto begin;
  777. case SEL_RUNARG:
  778. run = xgetenv(env, run);
  779. exitcurses();
  780. spawn(run, dents[cur].name, path);
  781. initcurses();
  782. break;
  783. }
  784. /* Screensaver */
  785. if (idletimeout != 0 && idle == idletimeout) {
  786. idle = 0;
  787. exitcurses();
  788. spawn(idlecmd, NULL, NULL);
  789. initcurses();
  790. }
  791. }
  792. }
  793. void
  794. usage(char *argv0)
  795. {
  796. fprintf(stderr, "usage: %s [dir]\n", argv0);
  797. exit(1);
  798. }
  799. int
  800. main(int argc, char *argv[])
  801. {
  802. char cwd[PATH_MAX], *ipath;
  803. char *ifilter;
  804. if (argc > 2)
  805. usage(argv[0]);
  806. /* Confirm we are in a terminal */
  807. if (!isatty(0) || !isatty(1)) {
  808. fprintf(stderr, "stdin or stdout is not a tty\n");
  809. exit(1);
  810. }
  811. if (getuid() == 0)
  812. showhidden = 1;
  813. initfilter(showhidden, &ifilter);
  814. printptr = &printent;
  815. if (argv[1] != NULL) {
  816. ipath = argv[1];
  817. } else {
  818. ipath = getcwd(cwd, sizeof(cwd));
  819. if (ipath == NULL)
  820. ipath = "/";
  821. }
  822. /* Get the default desktop mime opener, if set */
  823. opener = getenv("NOICE_OPENER");
  824. /* Get the fallback desktop mime opener, if set */
  825. fallback_opener = getenv("NOICE_FALLBACK_OPENER");
  826. signal(SIGINT, SIG_IGN);
  827. /* Test initial path */
  828. if (canopendir(ipath) == 0) {
  829. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  830. exit(1);
  831. }
  832. /* Set locale before curses setup */
  833. setlocale(LC_ALL, "");
  834. initcurses();
  835. browse(ipath, ifilter);
  836. exitcurses();
  837. exit(0);
  838. }