My build of nnn with minor changes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

935 linhas
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 tmp[PATH_MAX], *p;
  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. strlcpy(tmp, path, sizeof(tmp));
  146. p = dirname(tmp);
  147. if (p == NULL)
  148. printerr(1, "dirname");
  149. /* Make sure this is a malloc(3)-ed string */
  150. return xstrdup(p);
  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;
  201. int r;
  202. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  203. if (r != 0) {
  204. errbuf = xmalloc(COLS);
  205. regerror(r, regex, errbuf, COLS);
  206. printmsg(errbuf);
  207. free(errbuf);
  208. }
  209. return r;
  210. }
  211. int
  212. visible(regex_t *regex, char *file)
  213. {
  214. return regexec(regex, file, 0, NULL, 0) == 0;
  215. }
  216. int
  217. entrycmp(const void *va, const void *vb)
  218. {
  219. const struct entry *a, *b;
  220. a = (struct entry *)va;
  221. b = (struct entry *)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;
  369. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  370. char cm = 0;
  371. /* Copy name locally */
  372. name = xstrdup(ent->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. free(name);
  397. }
  398. int
  399. dentfill(char *path, struct entry **dents,
  400. int (*filter)(regex_t *, char *), regex_t *re)
  401. {
  402. DIR *dirp;
  403. struct dirent *dp;
  404. struct stat sb;
  405. char *newpath;
  406. int r, n = 0;
  407. dirp = opendir(path);
  408. if (dirp == NULL)
  409. return 0;
  410. while ((dp = readdir(dirp)) != NULL) {
  411. /* Skip self and parent */
  412. if (strcmp(dp->d_name, ".") == 0
  413. || strcmp(dp->d_name, "..") == 0)
  414. continue;
  415. if (filter(re, dp->d_name) == 0)
  416. continue;
  417. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  418. (*dents)[n].name = xstrdup(dp->d_name);
  419. /* Get mode flags */
  420. newpath = mkpath(path, dp->d_name);
  421. r = lstat(newpath, &sb);
  422. if (r == -1)
  423. printerr(1, "lstat");
  424. (*dents)[n].mode = sb.st_mode;
  425. (*dents)[n].t = sb.st_mtime;
  426. n++;
  427. }
  428. /* Should never be null */
  429. r = closedir(dirp);
  430. if (r == -1)
  431. printerr(1, "closedir");
  432. return n;
  433. }
  434. void
  435. dentfree(struct entry *dents, int n)
  436. {
  437. int i;
  438. for (i = 0; i < n; i++)
  439. free(dents[i].name);
  440. free(dents);
  441. }
  442. char *
  443. mkpath(char *dir, char *name)
  444. {
  445. char path[PATH_MAX];
  446. /* Handle absolute path */
  447. if (name[0] == '/') {
  448. strlcpy(path, name, sizeof(path));
  449. } else {
  450. /* Handle root case */
  451. if (strcmp(dir, "/") == 0) {
  452. strlcpy(path, "/", sizeof(path));
  453. strlcat(path, name, sizeof(path));
  454. } else {
  455. strlcpy(path, dir, sizeof(path));
  456. strlcat(path, "/", sizeof(path));
  457. strlcat(path, name, sizeof(path));
  458. }
  459. }
  460. return xstrdup(path);
  461. }
  462. /* Return the position of the matching entry or 0 otherwise */
  463. int
  464. dentfind(struct entry *dents, int n, char *cwd, char *path)
  465. {
  466. int i;
  467. char *tmp;
  468. if (path == NULL)
  469. return 0;
  470. for (i = 0; i < n; i++) {
  471. tmp = mkpath(cwd, dents[i].name);
  472. DPRINTF_S(path);
  473. DPRINTF_S(tmp);
  474. if (strcmp(tmp, path) == 0) {
  475. free(tmp);
  476. return i;
  477. }
  478. free(tmp);
  479. }
  480. return 0;
  481. }
  482. int
  483. populate(void)
  484. {
  485. regex_t re;
  486. int r;
  487. /* Can fail when permissions change while browsing */
  488. if (canopendir(path) == 0)
  489. return -1;
  490. /* Search filter */
  491. r = setfilter(&re, fltr);
  492. if (r != 0)
  493. return -1;
  494. dentfree(dents, n);
  495. n = 0;
  496. dents = NULL;
  497. n = dentfill(path, &dents, visible, &re);
  498. qsort(dents, n, sizeof(*dents), entrycmp);
  499. /* Find cur from history */
  500. cur = dentfind(dents, n, path, oldpath);
  501. free(oldpath);
  502. oldpath = NULL;
  503. return 0;
  504. }
  505. void
  506. redraw(void)
  507. {
  508. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  509. size_t ncols;
  510. int nlines, odd;
  511. int i;
  512. nlines = MIN(LINES - 4, n);
  513. /* Clean screen */
  514. erase();
  515. /* Strip trailing slashes */
  516. for (i = strlen(path) - 1; i > 0; i--)
  517. if (path[i] == '/')
  518. path[i] = '\0';
  519. else
  520. break;
  521. DPRINTF_D(cur);
  522. DPRINTF_S(path);
  523. /* No text wrapping in cwd line */
  524. ncols = COLS;
  525. if (ncols > PATH_MAX)
  526. ncols = PATH_MAX;
  527. strlcpy(cwd, path, ncols);
  528. cwd[ncols - strlen(CWD) - 1] = '\0';
  529. realpath(cwd, cwdresolved);
  530. printw(CWD "%s\n\n", cwdresolved);
  531. /* Print listing */
  532. odd = ISODD(nlines);
  533. if (cur < nlines / 2) {
  534. for (i = 0; i < nlines; i++)
  535. printent(&dents[i], i == cur);
  536. } else if (cur >= n - nlines / 2) {
  537. for (i = n - nlines; i < n; i++)
  538. printent(&dents[i], i == cur);
  539. } else {
  540. for (i = cur - nlines / 2;
  541. i < cur + nlines / 2 + odd; i++)
  542. printent(&dents[i], i == cur);
  543. }
  544. }
  545. void
  546. browse(const char *ipath, const char *ifilter)
  547. {
  548. int r, fd;
  549. regex_t re;
  550. char *newpath;
  551. struct stat sb;
  552. char *name, *bin, *dir, *tmp, *run, *env;
  553. int nowtyping = 0;
  554. oldpath = NULL;
  555. path = xstrdup(ipath);
  556. fltr = xstrdup(ifilter);
  557. begin:
  558. /* Path and filter should be malloc(3)-ed strings at all times */
  559. r = populate();
  560. if (r == -1) {
  561. if (!nowtyping) {
  562. printwarn();
  563. goto nochange;
  564. }
  565. }
  566. for (;;) {
  567. redraw();
  568. /* Handle filter-as-you-type mode */
  569. if (nowtyping)
  570. goto moretyping;
  571. nochange:
  572. switch (nextsel(&run, &env)) {
  573. case SEL_QUIT:
  574. free(path);
  575. free(fltr);
  576. dentfree(dents, n);
  577. return;
  578. case SEL_BACK:
  579. /* There is no going back */
  580. if (strcmp(path, "/") == 0 ||
  581. strcmp(path, ".") == 0 ||
  582. strchr(path, '/') == NULL)
  583. goto nochange;
  584. dir = xdirname(path);
  585. if (canopendir(dir) == 0) {
  586. free(dir);
  587. printwarn();
  588. goto nochange;
  589. }
  590. /* Save history */
  591. oldpath = path;
  592. path = dir;
  593. /* Reset filter */
  594. free(fltr);
  595. fltr = xstrdup(ifilter);
  596. goto begin;
  597. case SEL_GOIN:
  598. /* Cannot descend in empty directories */
  599. if (n == 0)
  600. goto nochange;
  601. name = dents[cur].name;
  602. newpath = mkpath(path, name);
  603. DPRINTF_S(newpath);
  604. /* Get path info */
  605. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  606. if (fd == -1) {
  607. printwarn();
  608. free(newpath);
  609. goto nochange;
  610. }
  611. r = fstat(fd, &sb);
  612. if (r == -1) {
  613. printwarn();
  614. close(fd);
  615. free(newpath);
  616. goto nochange;
  617. }
  618. close(fd);
  619. DPRINTF_U(sb.st_mode);
  620. switch (sb.st_mode & S_IFMT) {
  621. case S_IFDIR:
  622. if (canopendir(newpath) == 0) {
  623. printwarn();
  624. free(newpath);
  625. goto nochange;
  626. }
  627. free(path);
  628. path = newpath;
  629. /* Reset filter */
  630. free(fltr);
  631. fltr = xstrdup(ifilter);
  632. goto begin;
  633. case S_IFREG:
  634. bin = openwith(newpath);
  635. if (bin == NULL) {
  636. printmsg("No association");
  637. free(newpath);
  638. goto nochange;
  639. }
  640. exitcurses();
  641. spawn(bin, newpath, NULL);
  642. initcurses();
  643. free(newpath);
  644. continue;
  645. default:
  646. printmsg("Unsupported file");
  647. goto nochange;
  648. }
  649. case SEL_FLTR:
  650. /* Read filter */
  651. printprompt("filter: ");
  652. tmp = readln();
  653. if (tmp == NULL)
  654. tmp = xstrdup(ifilter);
  655. /* Check and report regex errors */
  656. r = setfilter(&re, tmp);
  657. if (r != 0) {
  658. free(tmp);
  659. goto nochange;
  660. }
  661. free(fltr);
  662. fltr = tmp;
  663. DPRINTF_S(fltr);
  664. /* Save current */
  665. if (n > 0)
  666. oldpath = mkpath(path, dents[cur].name);
  667. goto begin;
  668. case SEL_TYPE:
  669. nowtyping = 1;
  670. tmp = NULL;
  671. moretyping:
  672. printprompt("type: ");
  673. if (tmp != NULL)
  674. printw("%s", tmp);
  675. r = readmore(&tmp);
  676. DPRINTF_D(r);
  677. DPRINTF_S(tmp);
  678. if (r == 1)
  679. nowtyping = 0;
  680. /* Check regex errors */
  681. if (tmp != NULL) {
  682. r = setfilter(&re, tmp);
  683. if (r != 0)
  684. if (nowtyping) {
  685. goto moretyping;
  686. } else {
  687. free(tmp);
  688. goto nochange;
  689. }
  690. }
  691. /* Copy or reset filter */
  692. free(fltr);
  693. if (tmp != NULL)
  694. fltr = xstrdup(tmp);
  695. else
  696. fltr = xstrdup(ifilter);
  697. /* Save current */
  698. if (n > 0)
  699. oldpath = mkpath(path, dents[cur].name);
  700. if (!nowtyping)
  701. free(tmp);
  702. goto begin;
  703. case SEL_NEXT:
  704. if (cur < n - 1)
  705. cur++;
  706. break;
  707. case SEL_PREV:
  708. if (cur > 0)
  709. cur--;
  710. break;
  711. case SEL_PGDN:
  712. if (cur < n - 1)
  713. cur += MIN((LINES - 4) / 2, n - 1 - cur);
  714. break;
  715. case SEL_PGUP:
  716. if (cur > 0)
  717. cur -= MIN((LINES - 4) / 2, cur);
  718. break;
  719. case SEL_HOME:
  720. cur = 0;
  721. break;
  722. case SEL_END:
  723. cur = n - 1;
  724. break;
  725. case SEL_CD:
  726. /* Read target dir */
  727. printprompt("chdir: ");
  728. tmp = readln();
  729. if (tmp == NULL) {
  730. clearprompt();
  731. goto nochange;
  732. }
  733. newpath = mkpath(path, tmp);
  734. free(tmp);
  735. if (canopendir(newpath) == 0) {
  736. free(newpath);
  737. printwarn();
  738. goto nochange;
  739. }
  740. free(path);
  741. path = newpath;
  742. free(fltr);
  743. fltr = xstrdup(ifilter); /* Reset filter */
  744. DPRINTF_S(path);
  745. goto begin;
  746. case SEL_MTIME:
  747. mtimeorder = !mtimeorder;
  748. /* Save current */
  749. if (n > 0)
  750. oldpath = mkpath(path, dents[cur].name);
  751. goto begin;
  752. case SEL_REDRAW:
  753. /* Save current */
  754. if (n > 0)
  755. oldpath = mkpath(path, dents[cur].name);
  756. goto begin;
  757. case SEL_RUN:
  758. run = xgetenv(env, run);
  759. exitcurses();
  760. spawn(run, NULL, path);
  761. initcurses();
  762. break;
  763. case SEL_RUNARG:
  764. name = dents[cur].name;
  765. run = xgetenv(env, run);
  766. exitcurses();
  767. spawn(run, name, path);
  768. initcurses();
  769. break;
  770. }
  771. /* Screensaver */
  772. if (idletimeout != 0 && idle == idletimeout) {
  773. idle = 0;
  774. exitcurses();
  775. spawn(idlecmd, NULL, NULL);
  776. initcurses();
  777. }
  778. }
  779. }
  780. void
  781. usage(char *argv0)
  782. {
  783. fprintf(stderr, "usage: %s [dir]\n", argv0);
  784. exit(1);
  785. }
  786. int
  787. main(int argc, char *argv[])
  788. {
  789. char cwd[PATH_MAX], *ipath;
  790. char *ifilter;
  791. if (argc > 2)
  792. usage(argv[0]);
  793. /* Confirm we are in a terminal */
  794. if (!isatty(0) || !isatty(1)) {
  795. fprintf(stderr, "stdin or stdout is not a tty\n");
  796. exit(1);
  797. }
  798. if (getuid() == 0)
  799. ifilter = ".";
  800. else
  801. ifilter = "^[^.]"; /* Hide dotfiles */
  802. if (argv[1] != NULL) {
  803. ipath = argv[1];
  804. } else {
  805. ipath = getcwd(cwd, sizeof(cwd));
  806. if (ipath == NULL)
  807. ipath = "/";
  808. }
  809. signal(SIGINT, SIG_IGN);
  810. /* Test initial path */
  811. if (canopendir(ipath) == 0) {
  812. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  813. exit(1);
  814. }
  815. /* Set locale before curses setup */
  816. setlocale(LC_ALL, "");
  817. initcurses();
  818. browse(ipath, ifilter);
  819. exitcurses();
  820. exit(0);
  821. }