My build of nnn with minor changes
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

noice.c 15 KiB

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