My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

955 lignes
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. char name[PATH_MAX];
  372. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  373. char cm = 0;
  374. /* Copy name locally */
  375. strlcpy(name, ent->name, sizeof(name));
  376. if (S_ISDIR(ent->mode)) {
  377. cm = '/';
  378. maxlen--;
  379. } else if (S_ISLNK(ent->mode)) {
  380. cm = '@';
  381. maxlen--;
  382. } else if (S_ISSOCK(ent->mode)) {
  383. cm = '=';
  384. maxlen--;
  385. } else if (S_ISFIFO(ent->mode)) {
  386. cm = '|';
  387. maxlen--;
  388. } else if (ent->mode & S_IXUSR) {
  389. cm = '*';
  390. maxlen--;
  391. }
  392. /* No text wrapping in entries */
  393. if (strlen(name) > maxlen)
  394. name[maxlen] = '\0';
  395. if (cm == 0)
  396. printw("%s%s\n", active ? CURSR : EMPTY, name);
  397. else
  398. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  399. }
  400. char*
  401. coolsize(off_t size)
  402. {
  403. int i = 0;
  404. long double fsize = (double)size;
  405. while (fsize > 1024) {
  406. fsize /= 1024;
  407. i++;
  408. }
  409. snprintf(size_buf, 12, "%.*Lf%s", i, fsize, size_units[i]);
  410. return size_buf;
  411. }
  412. void
  413. printent_long(struct entry *ent, int active)
  414. {
  415. if (S_ISDIR(ent->mode))
  416. printw("%s%-32.32s DIR\n", active ? CURSR : EMPTY, ent->name);
  417. else if (S_ISLNK(ent->mode))
  418. printw("%s%-32.32s SYM\n", active ? CURSR : EMPTY, ent->name);
  419. else if (S_ISSOCK(ent->mode))
  420. printw("%s%-32.32s SOCK\n", active ? CURSR : EMPTY, ent->name);
  421. else if (S_ISFIFO(ent->mode))
  422. printw("%s%-32.32s FIFO\n", active ? CURSR : EMPTY, ent->name);
  423. else if (S_ISBLK(ent->mode))
  424. printw("%s%-32.32s BLK\n", active ? CURSR : EMPTY, ent->name);
  425. else if (S_ISCHR(ent->mode))
  426. printw("%s%-32.32s CHR\n", active ? CURSR : EMPTY, ent->name);
  427. else if (ent->mode & S_IXUSR)
  428. printw("%s%-32.32s EXE %s\n", active ? CURSR : EMPTY, ent->name, coolsize(ent->size));
  429. else
  430. printw("%s%-32.32s REG %s\n", active ? CURSR : EMPTY, ent->name, coolsize(ent->size));
  431. }
  432. int
  433. dentfill(char *path, struct entry **dents,
  434. int (*filter)(regex_t *, char *), regex_t *re)
  435. {
  436. char newpath[PATH_MAX];
  437. DIR *dirp;
  438. struct dirent *dp;
  439. struct stat sb;
  440. int r, n = 0;
  441. dirp = opendir(path);
  442. if (dirp == NULL)
  443. return 0;
  444. while ((dp = readdir(dirp)) != NULL) {
  445. /* Skip self and parent */
  446. if (strcmp(dp->d_name, ".") == 0 ||
  447. strcmp(dp->d_name, "..") == 0)
  448. continue;
  449. if (filter(re, dp->d_name) == 0)
  450. continue;
  451. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  452. strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  453. /* Get mode flags */
  454. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  455. r = lstat(newpath, &sb);
  456. if (r == -1)
  457. printerr(1, "lstat");
  458. (*dents)[n].mode = sb.st_mode;
  459. (*dents)[n].t = sb.st_mtime;
  460. (*dents)[n].size = sb.st_size;
  461. n++;
  462. }
  463. /* Should never be null */
  464. r = closedir(dirp);
  465. if (r == -1)
  466. printerr(1, "closedir");
  467. return n;
  468. }
  469. void
  470. dentfree(struct entry *dents)
  471. {
  472. free(dents);
  473. }
  474. /* Return the position of the matching entry or 0 otherwise */
  475. int
  476. dentfind(struct entry *dents, int n, char *cwd, char *path)
  477. {
  478. char tmp[PATH_MAX];
  479. int i;
  480. if (path == NULL)
  481. return 0;
  482. for (i = 0; i < n; i++) {
  483. mkpath(cwd, dents[i].name, tmp, sizeof(tmp));
  484. DPRINTF_S(path);
  485. DPRINTF_S(tmp);
  486. if (strcmp(tmp, path) == 0)
  487. return i;
  488. }
  489. return 0;
  490. }
  491. int
  492. populate(char *path, char *oldpath, char *fltr)
  493. {
  494. regex_t re;
  495. int r;
  496. /* Can fail when permissions change while browsing */
  497. if (canopendir(path) == 0)
  498. return -1;
  499. /* Search filter */
  500. r = setfilter(&re, fltr);
  501. if (r != 0)
  502. return -1;
  503. dentfree(dents);
  504. ndents = 0;
  505. dents = NULL;
  506. ndents = dentfill(path, &dents, visible, &re);
  507. qsort(dents, ndents, sizeof(*dents), entrycmp);
  508. /* Find cur from history */
  509. cur = dentfind(dents, ndents, path, oldpath);
  510. return 0;
  511. }
  512. void
  513. redraw(char *path)
  514. {
  515. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  516. size_t ncols;
  517. int nlines, odd;
  518. int i;
  519. nlines = MIN(LINES - 4, ndents);
  520. /* Clean screen */
  521. erase();
  522. /* Strip trailing slashes */
  523. for (i = strlen(path) - 1; i > 0; i--)
  524. if (path[i] == '/')
  525. path[i] = '\0';
  526. else
  527. break;
  528. DPRINTF_D(cur);
  529. DPRINTF_S(path);
  530. /* No text wrapping in cwd line */
  531. ncols = COLS;
  532. if (ncols > PATH_MAX)
  533. ncols = PATH_MAX;
  534. strlcpy(cwd, path, ncols);
  535. cwd[ncols - strlen(CWD) - 1] = '\0';
  536. if (!realpath(cwd, cwdresolved)) {
  537. printmsg("Cannot resolve path");
  538. return;
  539. }
  540. printw(CWD "%s\n\n", cwdresolved);
  541. /* Print listing */
  542. odd = ISODD(nlines);
  543. if (cur < (nlines >> 1)) {
  544. for (i = 0; i < nlines; i++)
  545. printptr(&dents[i], i == cur);
  546. } else if (cur >= ndents - (nlines >> 1)) {
  547. for (i = ndents - nlines; i < ndents; i++)
  548. printptr(&dents[i], i == cur);
  549. } else {
  550. nlines >>= 1;
  551. for (i = cur - nlines; i < cur + nlines + odd; i++)
  552. printptr(&dents[i], i == cur);
  553. }
  554. }
  555. void
  556. browse(char *ipath, char *ifilter)
  557. {
  558. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  559. char fltr[LINE_MAX];
  560. char *bin, *dir, *tmp, *run, *env;
  561. struct stat sb;
  562. regex_t re;
  563. int r, fd;
  564. strlcpy(path, ipath, sizeof(path));
  565. strlcpy(fltr, ifilter, sizeof(fltr));
  566. oldpath[0] = '\0';
  567. begin:
  568. r = populate(path, oldpath, fltr);
  569. if (r == -1) {
  570. printwarn();
  571. goto nochange;
  572. }
  573. for (;;) {
  574. redraw(path);
  575. nochange:
  576. switch (nextsel(&run, &env)) {
  577. case SEL_QUIT:
  578. dentfree(dents);
  579. return;
  580. case SEL_BACK:
  581. /* There is no going back */
  582. if (strcmp(path, "/") == 0 ||
  583. strcmp(path, ".") == 0 ||
  584. strchr(path, '/') == NULL)
  585. goto nochange;
  586. dir = xdirname(path);
  587. if (canopendir(dir) == 0) {
  588. printwarn();
  589. goto nochange;
  590. }
  591. /* Save history */
  592. strlcpy(oldpath, path, sizeof(oldpath));
  593. strlcpy(path, dir, sizeof(path));
  594. /* Reset filter */
  595. strlcpy(fltr, ifilter, sizeof(fltr));
  596. goto begin;
  597. case SEL_GOIN:
  598. /* Cannot descend in empty directories */
  599. if (ndents == 0)
  600. goto nochange;
  601. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  602. DPRINTF_S(newpath);
  603. /* Get path info */
  604. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  605. if (fd == -1) {
  606. printwarn();
  607. goto nochange;
  608. }
  609. r = fstat(fd, &sb);
  610. if (r == -1) {
  611. printwarn();
  612. close(fd);
  613. goto nochange;
  614. }
  615. close(fd);
  616. DPRINTF_U(sb.st_mode);
  617. switch (sb.st_mode & S_IFMT) {
  618. case S_IFDIR:
  619. if (canopendir(newpath) == 0) {
  620. printwarn();
  621. goto nochange;
  622. }
  623. strlcpy(path, newpath, sizeof(path));
  624. /* Reset filter */
  625. strlcpy(fltr, ifilter, sizeof(fltr));
  626. goto begin;
  627. case S_IFREG:
  628. /* If default mime opener is set, use it */
  629. if (opener) {
  630. char cmd[MAX_LEN];
  631. int status;
  632. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  633. opener, newpath);
  634. status = system(cmd);
  635. continue;
  636. }
  637. /* Try custom applications */
  638. bin = openwith(newpath);
  639. char *execvim = "vim";
  640. if (bin == NULL) {
  641. /* If a custom handler application is not set, open
  642. plain text files with vim, then try fallback_opener */
  643. FILE *fp;
  644. char cmd[MAX_LEN];
  645. int status;
  646. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  647. fp = popen(cmd, "r");
  648. if (fp == NULL)
  649. goto nochange;
  650. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  651. pclose(fp);
  652. goto nochange;
  653. }
  654. pclose(fp);
  655. if (strstr(cmd, "ASCII text") != NULL)
  656. bin = execvim;
  657. else if (fallback_opener) {
  658. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  659. fallback_opener, newpath);
  660. status = system(cmd);
  661. continue;
  662. } else {
  663. printmsg("No association");
  664. goto nochange;
  665. }
  666. }
  667. exitcurses();
  668. spawn(bin, newpath, NULL);
  669. initcurses();
  670. continue;
  671. default:
  672. printmsg("Unsupported file");
  673. goto nochange;
  674. }
  675. case SEL_FLTR:
  676. /* Read filter */
  677. printprompt("filter: ");
  678. tmp = readln();
  679. if (tmp == NULL)
  680. tmp = ifilter;
  681. /* Check and report regex errors */
  682. r = setfilter(&re, tmp);
  683. if (r != 0)
  684. goto nochange;
  685. strlcpy(fltr, tmp, sizeof(fltr));
  686. DPRINTF_S(fltr);
  687. /* Save current */
  688. if (ndents > 0)
  689. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  690. goto begin;
  691. case SEL_NEXT:
  692. if (cur < ndents - 1)
  693. cur++;
  694. else if (ndents)
  695. /* Roll over, set cursor to first entry */
  696. cur = 0;
  697. break;
  698. case SEL_PREV:
  699. if (cur > 0)
  700. cur--;
  701. else if (ndents)
  702. /* Roll over, set cursor to last entry */
  703. cur = ndents - 1;
  704. break;
  705. case SEL_PGDN:
  706. if (cur < ndents - 1)
  707. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  708. break;
  709. case SEL_PGUP:
  710. if (cur > 0)
  711. cur -= MIN((LINES - 4) / 2, cur);
  712. break;
  713. case SEL_HOME:
  714. cur = 0;
  715. break;
  716. case SEL_END:
  717. cur = ndents - 1;
  718. break;
  719. case SEL_CD:
  720. /* Read target dir */
  721. printprompt("chdir: ");
  722. tmp = readln();
  723. if (tmp == NULL) {
  724. clearprompt();
  725. goto nochange;
  726. }
  727. mkpath(path, tmp, newpath, sizeof(newpath));
  728. if (canopendir(newpath) == 0) {
  729. printwarn();
  730. goto nochange;
  731. }
  732. strlcpy(path, newpath, sizeof(path));
  733. /* Reset filter */
  734. strlcpy(fltr, ifilter, sizeof(fltr))
  735. DPRINTF_S(path);
  736. goto begin;
  737. case SEL_CDHOME:
  738. tmp = getenv("HOME");
  739. if (tmp == NULL) {
  740. clearprompt();
  741. goto nochange;
  742. }
  743. if (canopendir(tmp) == 0) {
  744. printwarn();
  745. goto nochange;
  746. }
  747. strlcpy(path, tmp, sizeof(path));
  748. /* Reset filter */
  749. strlcpy(fltr, ifilter, sizeof(fltr));
  750. DPRINTF_S(path);
  751. goto begin;
  752. case SEL_TOGGLEDOT:
  753. showhidden ^= 1;
  754. initfilter(showhidden, &ifilter);
  755. strlcpy(fltr, ifilter, sizeof(fltr));
  756. goto begin;
  757. case SEL_DETAIL:
  758. showdetail = !showdetail;
  759. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  760. goto begin;
  761. case SEL_FSIZE:
  762. sizeorder = !sizeorder;
  763. mtimeorder = 0;
  764. /* Save current */
  765. if (ndents > 0)
  766. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  767. goto begin;
  768. case SEL_MTIME:
  769. mtimeorder = !mtimeorder;
  770. sizeorder = 0;
  771. /* Save current */
  772. if (ndents > 0)
  773. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  774. goto begin;
  775. case SEL_REDRAW:
  776. /* Save current */
  777. if (ndents > 0)
  778. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  779. goto begin;
  780. case SEL_RUN:
  781. run = xgetenv(env, run);
  782. exitcurses();
  783. spawn(run, NULL, path);
  784. initcurses();
  785. /* Re-populate as directory content may have changed */
  786. goto begin;
  787. case SEL_RUNARG:
  788. run = xgetenv(env, run);
  789. exitcurses();
  790. spawn(run, dents[cur].name, path);
  791. initcurses();
  792. break;
  793. }
  794. /* Screensaver */
  795. if (idletimeout != 0 && idle == idletimeout) {
  796. idle = 0;
  797. exitcurses();
  798. spawn(idlecmd, NULL, NULL);
  799. initcurses();
  800. }
  801. }
  802. }
  803. void
  804. usage(char *argv0)
  805. {
  806. fprintf(stderr, "usage: %s [dir]\n", argv0);
  807. exit(1);
  808. }
  809. int
  810. main(int argc, char *argv[])
  811. {
  812. char cwd[PATH_MAX], *ipath;
  813. char *ifilter;
  814. if (argc > 2)
  815. usage(argv[0]);
  816. /* Confirm we are in a terminal */
  817. if (!isatty(0) || !isatty(1)) {
  818. fprintf(stderr, "stdin or stdout is not a tty\n");
  819. exit(1);
  820. }
  821. if (getuid() == 0)
  822. showhidden = 1;
  823. initfilter(showhidden, &ifilter);
  824. printptr = &printent;
  825. if (argv[1] != NULL) {
  826. ipath = argv[1];
  827. } else {
  828. ipath = getcwd(cwd, sizeof(cwd));
  829. if (ipath == NULL)
  830. ipath = "/";
  831. }
  832. /* Get the default desktop mime opener, if set */
  833. opener = getenv("NOICE_OPENER");
  834. /* Get the fallback desktop mime opener, if set */
  835. fallback_opener = getenv("NOICE_FALLBACK_OPENER");
  836. signal(SIGINT, SIG_IGN);
  837. /* Test initial path */
  838. if (canopendir(ipath) == 0) {
  839. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  840. exit(1);
  841. }
  842. /* Set locale before curses setup */
  843. setlocale(LC_ALL, "");
  844. initcurses();
  845. browse(ipath, ifilter);
  846. exitcurses();
  847. exit(0);
  848. }