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.
 
 
 
 
 
 

935 lignes
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 * sizeof(char));
  209. regerror(r, regex, errbuf, COLS * sizeof(char));
  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. int nlines, odd;
  513. char *cwd;
  514. int i;
  515. nlines = MIN(LINES - 4, n);
  516. /* Clean screen */
  517. erase();
  518. /* Strip trailing slashes */
  519. for (i = strlen(path) - 1; i > 0; i--)
  520. if (path[i] == '/')
  521. path[i] = '\0';
  522. else
  523. break;
  524. DPRINTF_D(cur);
  525. DPRINTF_S(path);
  526. /* No text wrapping in cwd line */
  527. cwd = xmalloc(COLS * sizeof(char));
  528. strlcpy(cwd, path, COLS * sizeof(char));
  529. cwd[COLS - strlen(CWD) - 1] = '\0';
  530. printw(CWD "%s\n\n", cwd);
  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. }