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

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