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.
 
 
 
 
 
 

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