My build of nnn with minor changes
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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