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.
 
 
 
 
 
 

1644 lignes
34 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 <sys/statvfs.h>
  6. #include <sys/resource.h>
  7. #include <curses.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <limits.h>
  12. #include <locale.h>
  13. #include <regex.h>
  14. #include <signal.h>
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include <pwd.h>
  22. #include <grp.h>
  23. #define __USE_XOPEN_EXTENDED
  24. #include <ftw.h>
  25. #ifdef DEBUG
  26. static int
  27. xprintf(int fd, const char *fmt, ...)
  28. {
  29. char buf[BUFSIZ];
  30. int r;
  31. va_list ap;
  32. va_start(ap, fmt);
  33. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  34. if (r > 0)
  35. r = write(fd, buf, r);
  36. va_end(ap);
  37. return r;
  38. }
  39. #define DEBUG_FD 8
  40. #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
  41. #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
  42. #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
  43. #define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=0x%p\n", x)
  44. #else
  45. #define DPRINTF_D(x)
  46. #define DPRINTF_U(x)
  47. #define DPRINTF_S(x)
  48. #define DPRINTF_P(x)
  49. #endif /* DEBUG */
  50. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  51. #undef MIN
  52. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  53. #define ISODD(x) ((x) & 1)
  54. #define CONTROL(c) ((c) ^ 0x40)
  55. #define TOUPPER(ch) \
  56. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  57. #define MAX_CMD_LEN (PATH_MAX << 1)
  58. #define CURSYM(flag) (flag ? CURSR : EMPTY)
  59. struct assoc {
  60. char *regex; /* Regex to match on filename */
  61. char *bin; /* Program */
  62. };
  63. /* Supported actions */
  64. enum action {
  65. SEL_QUIT = 1,
  66. SEL_BACK,
  67. SEL_GOIN,
  68. SEL_FLTR,
  69. SEL_NEXT,
  70. SEL_PREV,
  71. SEL_PGDN,
  72. SEL_PGUP,
  73. SEL_HOME,
  74. SEL_END,
  75. SEL_CD,
  76. SEL_CDHOME,
  77. SEL_LAST,
  78. SEL_TOGGLEDOT,
  79. SEL_DETAIL,
  80. SEL_STATS,
  81. SEL_FSIZE,
  82. SEL_BSIZE,
  83. SEL_MTIME,
  84. SEL_REDRAW,
  85. SEL_COPY,
  86. SEL_HELP,
  87. SEL_RUN,
  88. SEL_RUNARG,
  89. };
  90. struct key {
  91. int sym; /* Key pressed */
  92. enum action act; /* Action */
  93. char *run; /* Program to run */
  94. char *env; /* Environment variable to run */
  95. };
  96. #include "config.h"
  97. typedef struct entry {
  98. char name[PATH_MAX];
  99. mode_t mode;
  100. time_t t;
  101. off_t size;
  102. off_t bsize;
  103. } *pEntry;
  104. typedef unsigned long ulong;
  105. /* Global context */
  106. static struct entry *dents;
  107. static int ndents, cur;
  108. static int idle;
  109. static char *opener;
  110. static char *fallback_opener;
  111. static char *copier;
  112. static off_t blk_size;
  113. static size_t fs_free;
  114. static int open_max;
  115. static const double div_2_pow_10 = 1.0 / 1024.0;
  116. static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  117. /*
  118. * Layout:
  119. * .---------
  120. * | cwd: /mnt/path
  121. * |
  122. * | file0
  123. * | file1
  124. * | > file2
  125. * | file3
  126. * | file4
  127. * ...
  128. * | filen
  129. * |
  130. * | Permission denied
  131. * '------
  132. */
  133. static void printmsg(char *);
  134. static void printwarn(void);
  135. static void printerr(int, char *);
  136. static rlim_t
  137. max_openfds()
  138. {
  139. struct rlimit rl;
  140. rlim_t limit;
  141. limit = getrlimit(RLIMIT_NOFILE, &rl);
  142. if (limit != 0)
  143. return 32;
  144. limit = rl.rlim_cur;
  145. rl.rlim_cur = rl.rlim_max;
  146. if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
  147. return rl.rlim_max - 64;
  148. if (limit > 128)
  149. return limit - 64;
  150. return 32;
  151. }
  152. static size_t
  153. xstrlcpy(char *dest, const char *src, size_t n)
  154. {
  155. size_t i;
  156. for (i = 0; i < n && *src; i++)
  157. *dest++ = *src++;
  158. if (n) {
  159. *dest = '\0';
  160. #ifdef CHECK_XSTRLCPY_RET
  161. /* Compiling this out as we are not checking
  162. the return value anywhere (controlled case).
  163. Just returning the number of bytes copied. */
  164. while(*src++)
  165. i++;
  166. #endif
  167. }
  168. return i;
  169. }
  170. /*
  171. * The poor man's implementation of memrchr(3).
  172. * We are only looking for '/' in this program.
  173. */
  174. static void *
  175. xmemrchr(const void *s, int c, size_t n)
  176. {
  177. unsigned char *p;
  178. unsigned char ch = (unsigned char)c;
  179. if (!s || !n)
  180. return NULL;
  181. p = (unsigned char *)s + n - 1;
  182. while(n--)
  183. if ((*p--) == ch)
  184. return ++p;
  185. return NULL;
  186. }
  187. #if 0
  188. /* Some implementations of dirname(3) may modify `path' and some
  189. * return a pointer inside `path'. */
  190. static char *
  191. xdirname(const char *path)
  192. {
  193. static char out[PATH_MAX];
  194. char tmp[PATH_MAX], *p;
  195. xstrlcpy(tmp, path, sizeof(tmp));
  196. p = dirname(tmp);
  197. if (p == NULL)
  198. printerr(1, "dirname");
  199. xstrlcpy(out, p, sizeof(out));
  200. return out;
  201. }
  202. #endif
  203. /*
  204. * The following dirname(3) implementation does not
  205. * change the input. We use a copy of the original.
  206. *
  207. * Modified from the glibc (GNU LGPL) version.
  208. */
  209. static char *
  210. xdirname(const char *path)
  211. {
  212. static char name[PATH_MAX];
  213. char *last_slash;
  214. xstrlcpy(name, path, PATH_MAX);
  215. /* Find last '/'. */
  216. last_slash = strrchr(name, '/');
  217. if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
  218. /* Determine whether all remaining characters are slashes. */
  219. char *runp;
  220. for (runp = last_slash; runp != name; --runp)
  221. if (runp[-1] != '/')
  222. break;
  223. /* The '/' is the last character, we have to look further. */
  224. if (runp != name)
  225. last_slash = xmemrchr(name, '/', runp - name);
  226. }
  227. if (last_slash != NULL) {
  228. /* Determine whether all remaining characters are slashes. */
  229. char *runp;
  230. for (runp = last_slash; runp != name; --runp)
  231. if (runp[-1] != '/')
  232. break;
  233. /* Terminate the name. */
  234. if (runp == name) {
  235. /* The last slash is the first character in the string.
  236. We have to return "/". As a special case we have to
  237. return "//" if there are exactly two slashes at the
  238. beginning of the string. See XBD 4.10 Path Name
  239. Resolution for more information. */
  240. if (last_slash == name + 1)
  241. ++last_slash;
  242. else
  243. last_slash = name + 1;
  244. } else
  245. last_slash = runp;
  246. last_slash[0] = '\0';
  247. } else {
  248. /* This assignment is ill-designed but the XPG specs require to
  249. return a string containing "." in any case no directory part
  250. is found and so a static and constant string is required. */
  251. name[0] = '.';
  252. name[1] = '\0';
  253. }
  254. return name;
  255. }
  256. static void
  257. spawn(char *file, char *arg, char *dir, int notify)
  258. {
  259. pid_t pid;
  260. int status;
  261. pid = fork();
  262. if (pid == 0) {
  263. if (dir != NULL)
  264. status = chdir(dir);
  265. if (notify)
  266. fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  267. execlp(file, file, arg, NULL);
  268. _exit(1);
  269. } else {
  270. /* Ignore interruptions */
  271. while (waitpid(pid, &status, 0) == -1)
  272. DPRINTF_D(status);
  273. DPRINTF_D(pid);
  274. }
  275. }
  276. static char *
  277. xgetenv(char *name, char *fallback)
  278. {
  279. char *value;
  280. if (name == NULL)
  281. return fallback;
  282. value = getenv(name);
  283. return value && value[0] ? value : fallback;
  284. }
  285. /*
  286. * We assume none of the strings are NULL.
  287. *
  288. * Let's have the logic to sort numeric names in numeric order.
  289. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  290. *
  291. * If the absolute numeric values are same, we fallback to alphasort.
  292. */
  293. static int
  294. xstricmp(const char *s1, const char *s2)
  295. {
  296. static char *c1, *c2;
  297. static long long num1, num2;
  298. num1 = strtoll(s1, &c1, 10);
  299. num2 = strtoll(s2, &c2, 10);
  300. if (*c1 == '\0' && *c2 == '\0') {
  301. if (num1 != num2) {
  302. if (num1 > num2)
  303. return 1;
  304. else
  305. return -1;
  306. }
  307. } else if (*c1 == '\0' && *c2 != '\0')
  308. return -1;
  309. else if (*c1 != '\0' && *c2 == '\0')
  310. return 1;
  311. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  312. s1++, s2++;
  313. /* In case of alphabetically same names, make sure
  314. lower case one comes before upper case one */
  315. if (!*s1 && !*s2)
  316. return 1;
  317. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  318. }
  319. static char *
  320. openwith(char *file)
  321. {
  322. regex_t regex;
  323. char *bin = NULL;
  324. unsigned int i;
  325. for (i = 0; i < LEN(assocs); i++) {
  326. if (regcomp(&regex, assocs[i].regex,
  327. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  328. continue;
  329. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  330. bin = assocs[i].bin;
  331. break;
  332. }
  333. }
  334. DPRINTF_S(bin);
  335. return bin;
  336. }
  337. static int
  338. setfilter(regex_t *regex, char *filter)
  339. {
  340. char errbuf[LINE_MAX];
  341. size_t len;
  342. int r;
  343. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  344. if (r != 0) {
  345. len = COLS;
  346. if (len > sizeof(errbuf))
  347. len = sizeof(errbuf);
  348. regerror(r, regex, errbuf, len);
  349. printmsg(errbuf);
  350. }
  351. return r;
  352. }
  353. static void
  354. initfilter(int dot, char **ifilter)
  355. {
  356. *ifilter = dot ? "." : "^[^.]";
  357. }
  358. static int
  359. visible(regex_t *regex, char *file)
  360. {
  361. return regexec(regex, file, 0, NULL, 0) == 0;
  362. }
  363. static int
  364. entrycmp(const void *va, const void *vb)
  365. {
  366. static pEntry pa, pb;
  367. pa = (pEntry)va;
  368. pb = (pEntry)vb;
  369. /* Sort directories first */
  370. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  371. return 1;
  372. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  373. return -1;
  374. /* Do the actual sorting */
  375. if (mtimeorder)
  376. return pb->t - pa->t;
  377. if (sizeorder) {
  378. if (pb->size > pa->size)
  379. return 1;
  380. else if (pb->size < pa->size)
  381. return -1;
  382. }
  383. if (bsizeorder) {
  384. if (pb->bsize > pa->bsize)
  385. return 1;
  386. else if (pb->bsize < pa->bsize)
  387. return -1;
  388. }
  389. return xstricmp(pa->name, pb->name);
  390. }
  391. static void
  392. initcurses(void)
  393. {
  394. if (initscr() == NULL) {
  395. char *term = getenv("TERM");
  396. if (term != NULL)
  397. fprintf(stderr, "error opening terminal: %s\n", term);
  398. else
  399. fprintf(stderr, "failed to initialize curses\n");
  400. exit(1);
  401. }
  402. cbreak();
  403. noecho();
  404. nonl();
  405. intrflush(stdscr, FALSE);
  406. keypad(stdscr, TRUE);
  407. curs_set(FALSE); /* Hide cursor */
  408. timeout(1000); /* One second */
  409. }
  410. static void
  411. exitcurses(void)
  412. {
  413. endwin(); /* Restore terminal */
  414. }
  415. /* Messages show up at the bottom */
  416. static void
  417. printmsg(char *msg)
  418. {
  419. move(LINES - 1, 0);
  420. printw("%s\n", msg);
  421. }
  422. /* Display warning as a message */
  423. static void
  424. printwarn(void)
  425. {
  426. printmsg(strerror(errno));
  427. }
  428. /* Kill curses and display error before exiting */
  429. static void
  430. printerr(int ret, char *prefix)
  431. {
  432. exitcurses();
  433. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  434. exit(ret);
  435. }
  436. /* Clear the last line */
  437. static void
  438. clearprompt(void)
  439. {
  440. printmsg("");
  441. }
  442. /* Print prompt on the last line */
  443. static void
  444. printprompt(char *str)
  445. {
  446. clearprompt();
  447. printw(str);
  448. }
  449. /* Returns SEL_* if key is bound and 0 otherwise.
  450. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  451. static int
  452. nextsel(char **run, char **env)
  453. {
  454. int c;
  455. unsigned int i;
  456. c = getch();
  457. if (c == -1)
  458. idle++;
  459. else
  460. idle = 0;
  461. for (i = 0; i < LEN(bindings); i++)
  462. if (c == bindings[i].sym) {
  463. *run = bindings[i].run;
  464. *env = bindings[i].env;
  465. return bindings[i].act;
  466. }
  467. return 0;
  468. }
  469. static char *
  470. readln(void)
  471. {
  472. static char ln[LINE_MAX];
  473. timeout(-1);
  474. echo();
  475. curs_set(TRUE);
  476. memset(ln, 0, sizeof(ln));
  477. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  478. noecho();
  479. curs_set(FALSE);
  480. timeout(1000);
  481. return ln[0] ? ln : NULL;
  482. }
  483. static int
  484. canopendir(char *path)
  485. {
  486. static DIR *dirp;
  487. dirp = opendir(path);
  488. if (dirp == NULL)
  489. return 0;
  490. closedir(dirp);
  491. return 1;
  492. }
  493. /*
  494. * Returns "dir/name or "/name"
  495. */
  496. static char *
  497. mkpath(char *dir, char *name, char *out, size_t n)
  498. {
  499. /* Handle absolute path */
  500. if (name[0] == '/')
  501. xstrlcpy(out, name, n);
  502. else {
  503. /* Handle root case */
  504. if (strcmp(dir, "/") == 0)
  505. snprintf(out, n, "/%s", name);
  506. else
  507. snprintf(out, n, "%s/%s", dir, name);
  508. }
  509. return out;
  510. }
  511. static void
  512. printent(struct entry *ent, int active)
  513. {
  514. if (S_ISDIR(ent->mode))
  515. printw("%s%s/\n", CURSYM(active), ent->name);
  516. else if (S_ISLNK(ent->mode))
  517. printw("%s%s@\n", CURSYM(active), ent->name);
  518. else if (S_ISSOCK(ent->mode))
  519. printw("%s%s=\n", CURSYM(active), ent->name);
  520. else if (S_ISFIFO(ent->mode))
  521. printw("%s%s|\n", CURSYM(active), ent->name);
  522. else if (ent->mode & S_IXUSR)
  523. printw("%s%s*\n", CURSYM(active), ent->name);
  524. else
  525. printw("%s%s\n", CURSYM(active), ent->name);
  526. }
  527. static void (*printptr)(struct entry *ent, int active) = &printent;
  528. static char*
  529. coolsize(off_t size)
  530. {
  531. static char size_buf[12]; /* Buffer to hold human readable size */
  532. static int i;
  533. static off_t fsize, tmp;
  534. static long double rem;
  535. i = 0;
  536. fsize = size;
  537. rem = 0;
  538. while (fsize > 1024) {
  539. tmp = fsize;
  540. //fsize *= div_2_pow_10;
  541. fsize >>= 10;
  542. rem = tmp - (fsize << 10);
  543. i++;
  544. }
  545. snprintf(size_buf, 12, "%.*Lf%s", i, fsize + rem * div_2_pow_10, size_units[i]);
  546. return size_buf;
  547. }
  548. static void
  549. printent_long(struct entry *ent, int active)
  550. {
  551. static char buf[18];
  552. strftime(buf, 18, "%b %d %H:%M %Y", localtime(&ent->t));
  553. if (active)
  554. attron(A_REVERSE);
  555. if (!bsizeorder) {
  556. if (S_ISDIR(ent->mode))
  557. printw("%s%-17.17s / %s/\n",
  558. CURSYM(active), buf, ent->name);
  559. else if (S_ISLNK(ent->mode))
  560. printw("%s%-17.17s @ %s@\n",
  561. CURSYM(active), buf, ent->name);
  562. else if (S_ISSOCK(ent->mode))
  563. printw("%s%-17.17s = %s=\n",
  564. CURSYM(active), buf, ent->name);
  565. else if (S_ISFIFO(ent->mode))
  566. printw("%s%-17.17s | %s|\n",
  567. CURSYM(active), buf, ent->name);
  568. else if (S_ISBLK(ent->mode))
  569. printw("%s%-17.17s b %s\n",
  570. CURSYM(active), buf, ent->name);
  571. else if (S_ISCHR(ent->mode))
  572. printw("%s%-17.17s c %s\n",
  573. CURSYM(active), buf, ent->name);
  574. else if (ent->mode & S_IXUSR)
  575. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  576. buf, coolsize(ent->size), ent->name);
  577. else
  578. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  579. buf, coolsize(ent->size), ent->name);
  580. } else {
  581. if (S_ISDIR(ent->mode))
  582. printw("%s%-17.17s %8.8s/ %s/\n", CURSYM(active),
  583. buf, coolsize(ent->bsize << 9), ent->name);
  584. else if (S_ISLNK(ent->mode))
  585. printw("%s%-17.17s @ %s@\n",
  586. CURSYM(active), buf, ent->name);
  587. else if (S_ISSOCK(ent->mode))
  588. printw("%s%-17.17s = %s=\n",
  589. CURSYM(active), buf, ent->name);
  590. else if (S_ISFIFO(ent->mode))
  591. printw("%s%-17.17s | %s|\n",
  592. CURSYM(active), buf, ent->name);
  593. else if (S_ISBLK(ent->mode))
  594. printw("%s%-17.17s b %s\n",
  595. CURSYM(active), buf, ent->name);
  596. else if (S_ISCHR(ent->mode))
  597. printw("%s%-17.17s c %s\n",
  598. CURSYM(active), buf, ent->name);
  599. else if (ent->mode & S_IXUSR)
  600. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  601. buf, coolsize(ent->bsize << 9), ent->name);
  602. else
  603. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  604. buf, coolsize(ent->bsize << 9), ent->name);
  605. }
  606. if (active)
  607. attroff(A_REVERSE);
  608. }
  609. static char
  610. get_fileind(mode_t mode, char *desc)
  611. {
  612. static char c;
  613. if (S_ISREG(mode)) {
  614. c = '-';
  615. sprintf(desc, "%s", "regular file");
  616. if (mode & S_IXUSR)
  617. strcat(desc, ", executable");
  618. } else if (S_ISDIR(mode)) {
  619. c = 'd';
  620. sprintf(desc, "%s", "directory");
  621. } else if (S_ISBLK(mode)) {
  622. c = 'b';
  623. sprintf(desc, "%s", "block special device");
  624. } else if (S_ISCHR(mode)) {
  625. c = 'c';
  626. sprintf(desc, "%s", "character special device");
  627. #ifdef S_ISFIFO
  628. } else if (S_ISFIFO(mode)) {
  629. c = 'p';
  630. sprintf(desc, "%s", "FIFO");
  631. #endif /* S_ISFIFO */
  632. #ifdef S_ISLNK
  633. } else if (S_ISLNK(mode)) {
  634. c = 'l';
  635. sprintf(desc, "%s", "symbolic link");
  636. #endif /* S_ISLNK */
  637. #ifdef S_ISSOCK
  638. } else if (S_ISSOCK(mode)) {
  639. c = 's';
  640. sprintf(desc, "%s", "socket");
  641. #endif /* S_ISSOCK */
  642. #ifdef S_ISDOOR
  643. /* Solaris 2.6, etc. */
  644. } else if (S_ISDOOR(mode)) {
  645. c = 'D';
  646. desc[0] = '\0';
  647. #endif /* S_ISDOOR */
  648. } else {
  649. /* Unknown type -- possibly a regular file? */
  650. c = '?';
  651. desc[0] = '\0';
  652. }
  653. return(c);
  654. }
  655. /* Convert a mode field into "ls -l" type perms field. */
  656. static char *
  657. get_lsperms(mode_t mode, char *desc)
  658. {
  659. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  660. "r--", "r-x", "rw-", "rwx"};
  661. static char bits[11];
  662. bits[0] = get_fileind(mode, desc);
  663. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  664. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  665. strcpy(&bits[7], rwx[(mode & 7)]);
  666. if (mode & S_ISUID)
  667. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  668. if (mode & S_ISGID)
  669. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  670. if (mode & S_ISVTX)
  671. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  672. bits[10] = '\0';
  673. return(bits);
  674. }
  675. static char *
  676. get_output(char *buf, size_t bytes)
  677. {
  678. char *ret;
  679. FILE *pf = popen(buf, "r");
  680. if (pf) {
  681. ret = fgets(buf, bytes, pf);
  682. pclose(pf);
  683. return ret;
  684. }
  685. return NULL;
  686. }
  687. /*
  688. * Follows the stat(1) output closely
  689. */
  690. static void
  691. show_stats(char* fpath, char* fname, struct stat *sb)
  692. {
  693. char buf[PATH_MAX + 48];
  694. char *perms = get_lsperms(sb->st_mode, buf);
  695. char *p, *begin = buf;
  696. clear();
  697. /* Show file name or 'symlink' -> 'target' */
  698. if (perms[0] == 'l') {
  699. char symtgt[PATH_MAX];
  700. ssize_t len = readlink(fpath, symtgt, PATH_MAX);
  701. if (len != -1) {
  702. symtgt[len] = '\0';
  703. printw("\n\n File: '%s' -> '%s'", fname, symtgt);
  704. }
  705. } else
  706. printw("\n File: '%s'", fname);
  707. /* Show size, blocks, file type */
  708. printw("\n Size: %-15llu Blocks: %-10llu IO Block: %-6llu %s",
  709. sb->st_size, sb->st_blocks, sb->st_blksize, buf);
  710. /* Show containing device, inode, hardlink count */
  711. sprintf(buf, "%lxh/%lud", (ulong)sb->st_dev, (ulong)sb->st_dev);
  712. printw("\n Device: %-15s Inode: %-11lu Links: %-9lu",
  713. buf, sb->st_ino, sb->st_nlink);
  714. /* Show major, minor number for block or char device */
  715. if (perms[0] == 'b' || perms[0] == 'c')
  716. printw(" Device type: %lx,%lx",
  717. major(sb->st_rdev), minor(sb->st_rdev));
  718. /* Show permissions, owner, group */
  719. printw("\n Access: 0%d%d%d/%s Uid: (%lu/%s) Gid: (%lu/%s)",
  720. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  721. perms,
  722. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  723. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  724. /* Show last access time */
  725. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  726. printw("\n\n Access: %s", buf);
  727. /* Show last modification time */
  728. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  729. printw("\n Modify: %s", buf);
  730. /* Show last status change time */
  731. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  732. printw("\n Change: %s", buf);
  733. if (S_ISREG(sb->st_mode)) {
  734. /* Show file(1) output */
  735. sprintf(buf, "file -b \"%s\" 2>&1", fpath);
  736. p = get_output(buf, PATH_MAX + 48);
  737. if (p) {
  738. printw("\n\n ");
  739. while (*p) {
  740. if (*p == ',') {
  741. *p = '\0';
  742. printw(" %s\n", begin);
  743. begin = p + 1;
  744. }
  745. p++;
  746. }
  747. printw(" %s", begin);
  748. }
  749. #ifdef SUPPORT_CHKSUM
  750. /* Calculating checksums can take VERY long */
  751. /* Show md5 */
  752. sprintf(buf, "openssl md5 \"%s\" 2>&1", fpath);
  753. p = get_output(buf, PATH_MAX + 48);
  754. if (p) {
  755. p = xmemrchr(buf, ' ', strlen(buf));
  756. if (!p)
  757. p = buf;
  758. else
  759. p++;
  760. printw("\n md5: %s", p);
  761. }
  762. /* Show sha256 */
  763. sprintf(buf, "openssl sha256 \"%s\" 2>&1", fpath);
  764. p = get_output(buf, PATH_MAX + 48);
  765. if (p) {
  766. p = xmemrchr(buf, ' ', strlen(buf));
  767. if (!p)
  768. p = buf;
  769. else
  770. p++;
  771. printw(" sha256: %s", p);
  772. }
  773. #endif
  774. }
  775. /* Show exit keys */
  776. printw("\n\n << (D)");
  777. while ((*buf = getch()) != 'D');
  778. return;
  779. }
  780. static void
  781. show_help(void)
  782. {
  783. char c;
  784. clear();
  785. printw("\n\
  786. << Key >> << Function >>\n\n\
  787. [Up], k, ^P Previous entry\n\
  788. [Down], j, ^N Next entry\n\
  789. [PgUp], ^U Scroll half page up\n\
  790. [PgDn], ^D Scroll half page down\n\
  791. [Home], g, ^, ^A Jump to first entry\n\
  792. [End], G, $, ^E Jump to last entry\n\
  793. [Right], [Enter], l, ^M Open file or enter dir\n\
  794. [Left], [Backspace], h, ^H Go to parent dir\n\
  795. ~ Jump to HOME dir\n\
  796. - Jump to last visited dir\n\
  797. /, & Filter dir contents\n\
  798. c Show change dir prompt\n\
  799. d Toggle detail view\n\
  800. D Toggle current file details screen\n\
  801. . Toggle hide .dot files\n\
  802. s Toggle sort by file size\n\
  803. S Toggle disk usage analyzer mode\n\
  804. t Toggle sort by modified time\n\
  805. ! Spawn SHELL in PWD (fallback sh)\n\
  806. z Run top\n\
  807. e Edit entry in EDITOR (fallback vi)\n\
  808. p Open entry in PAGER (fallback less)\n\
  809. ^K Invoke file name copier\n\
  810. ^L Force a redraw\n\
  811. ? Toggle help screen\n\
  812. q Quit\n");
  813. /* Show exit keys */
  814. printw("\n\n << (?)");
  815. while ((c = getch()) != '?');
  816. return;
  817. }
  818. static int
  819. sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  820. {
  821. /* Handle permission problems */
  822. if(typeflag == FTW_NS) {
  823. printmsg("No stats (permissions ?)");
  824. return 0;
  825. }
  826. blk_size += sb->st_blocks;
  827. return 0;
  828. }
  829. static int
  830. getorder(size_t size)
  831. {
  832. switch (size) {
  833. case 4096:
  834. return 12;
  835. case 512:
  836. return 9;
  837. case 8192:
  838. return 13;
  839. case 16384:
  840. return 14;
  841. case 32768:
  842. return 15;
  843. case 65536:
  844. return 16;
  845. case 131072:
  846. return 17;
  847. case 262144:
  848. return 18;
  849. case 524288:
  850. return 19;
  851. case 1048576:
  852. return 20;
  853. case 2048:
  854. return 11;
  855. case 1024:
  856. return 10;
  857. default:
  858. return 0;
  859. }
  860. }
  861. static int
  862. dentfill(char *path, struct entry **dents,
  863. int (*filter)(regex_t *, char *), regex_t *re)
  864. {
  865. static char newpath[PATH_MAX];
  866. static DIR *dirp;
  867. static struct dirent *dp;
  868. static struct stat sb;
  869. static struct statvfs svb;
  870. static int r, n;
  871. r = n = 0;
  872. dirp = opendir(path);
  873. if (dirp == NULL)
  874. return 0;
  875. while ((dp = readdir(dirp)) != NULL) {
  876. /* Skip self and parent */
  877. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  878. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  879. continue;
  880. if (filter(re, dp->d_name) == 0)
  881. continue;
  882. if (((n >> 5) << 5) == n) {
  883. *dents = realloc(*dents, (n + 32) * sizeof(**dents));
  884. if (*dents == NULL)
  885. printerr(1, "realloc");
  886. }
  887. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  888. /* Get mode flags */
  889. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  890. r = lstat(newpath, &sb);
  891. if (r == -1)
  892. printerr(1, "lstat");
  893. (*dents)[n].mode = sb.st_mode;
  894. (*dents)[n].t = sb.st_mtime;
  895. (*dents)[n].size = sb.st_size;
  896. if (bsizeorder) {
  897. if (S_ISDIR(sb.st_mode)) {
  898. blk_size = 0;
  899. if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
  900. printmsg("nftw(3) failed");
  901. (*dents)[n].bsize = sb.st_blocks;
  902. } else
  903. (*dents)[n].bsize = blk_size;
  904. } else
  905. (*dents)[n].bsize = sb.st_blocks;
  906. }
  907. n++;
  908. }
  909. if (bsizeorder) {
  910. r = statvfs(path, &svb);
  911. if (r == -1)
  912. fs_free = 0;
  913. else
  914. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  915. }
  916. /* Should never be null */
  917. r = closedir(dirp);
  918. if (r == -1)
  919. printerr(1, "closedir");
  920. return n;
  921. }
  922. static void
  923. dentfree(struct entry *dents)
  924. {
  925. free(dents);
  926. }
  927. /* Return the position of the matching entry or 0 otherwise */
  928. static int
  929. dentfind(struct entry *dents, int n, char *path)
  930. {
  931. if (!path)
  932. return 0;
  933. static int i;
  934. static char *p;
  935. p = xmemrchr(path, '/', strlen(path));
  936. if (!p)
  937. p = path;
  938. else
  939. /* We are assuming an entry with actual
  940. name ending in '/' will not appear */
  941. p++;
  942. DPRINTF_S(p);
  943. for (i = 0; i < n; i++)
  944. if (strcmp(p, dents[i].name) == 0)
  945. return i;
  946. return 0;
  947. }
  948. static int
  949. populate(char *path, char *oldpath, char *fltr)
  950. {
  951. static regex_t re;
  952. static int r;
  953. /* Can fail when permissions change while browsing */
  954. if (canopendir(path) == 0)
  955. return -1;
  956. /* Search filter */
  957. r = setfilter(&re, fltr);
  958. if (r != 0)
  959. return -1;
  960. dentfree(dents);
  961. ndents = 0;
  962. dents = NULL;
  963. ndents = dentfill(path, &dents, visible, &re);
  964. qsort(dents, ndents, sizeof(*dents), entrycmp);
  965. /* Find cur from history */
  966. cur = dentfind(dents, ndents, oldpath);
  967. return 0;
  968. }
  969. static void
  970. redraw(char *path)
  971. {
  972. static char cwd[PATH_MAX];
  973. static int nlines, odd;
  974. static int i;
  975. nlines = MIN(LINES - 4, ndents);
  976. /* Clean screen */
  977. erase();
  978. /* Strip trailing slashes */
  979. for (i = strlen(path) - 1; i > 0; i--)
  980. if (path[i] == '/')
  981. path[i] = '\0';
  982. else
  983. break;
  984. DPRINTF_D(cur);
  985. DPRINTF_S(path);
  986. /* No text wrapping in cwd line */
  987. if (!realpath(path, cwd)) {
  988. printmsg("Cannot resolve path");
  989. return;
  990. }
  991. printw(CWD "%s\n\n", cwd);
  992. /* Print listing */
  993. odd = ISODD(nlines);
  994. if (cur < (nlines >> 1)) {
  995. for (i = 0; i < nlines; i++)
  996. printptr(&dents[i], i == cur);
  997. } else if (cur >= ndents - (nlines >> 1)) {
  998. for (i = ndents - nlines; i < ndents; i++)
  999. printptr(&dents[i], i == cur);
  1000. } else {
  1001. nlines >>= 1;
  1002. for (i = cur - nlines; i < cur + nlines + odd; i++)
  1003. printptr(&dents[i], i == cur);
  1004. }
  1005. if (showdetail) {
  1006. if (ndents) {
  1007. static char ind[2] = "\0\0";
  1008. static char sort[17];
  1009. if (mtimeorder)
  1010. sprintf(sort, "by time ");
  1011. else if (sizeorder)
  1012. sprintf(sort, "by size ");
  1013. else
  1014. sort[0] = '\0';
  1015. if (S_ISDIR(dents[cur].mode))
  1016. ind[0] = '/';
  1017. else if (S_ISLNK(dents[cur].mode))
  1018. ind[0] = '@';
  1019. else if (S_ISSOCK(dents[cur].mode))
  1020. ind[0] = '=';
  1021. else if (S_ISFIFO(dents[cur].mode))
  1022. ind[0] = '|';
  1023. else if (dents[cur].mode & S_IXUSR)
  1024. ind[0] = '*';
  1025. else
  1026. ind[0] = '\0';
  1027. if (!bsizeorder)
  1028. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  1029. dents[cur].name, ind);
  1030. else
  1031. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  1032. ndents, coolsize(fs_free), dents[cur].name, ind);
  1033. printmsg(cwd);
  1034. } else
  1035. printmsg("0 items");
  1036. }
  1037. }
  1038. static void
  1039. browse(char *ipath, char *ifilter)
  1040. {
  1041. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1042. static char lastdir[PATH_MAX];
  1043. static char fltr[LINE_MAX];
  1044. char *bin, *dir, *tmp, *run, *env;
  1045. struct stat sb;
  1046. regex_t re;
  1047. int r, fd;
  1048. enum action sel = SEL_RUNARG + 1;
  1049. xstrlcpy(path, ipath, sizeof(path));
  1050. xstrlcpy(lastdir, ipath, sizeof(lastdir));
  1051. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1052. oldpath[0] = '\0';
  1053. newpath[0] = '\0';
  1054. begin:
  1055. if (sel == SEL_GOIN && S_ISDIR(sb.st_mode))
  1056. r = populate(path, NULL, fltr);
  1057. else
  1058. r = populate(path, oldpath, fltr);
  1059. if (r == -1) {
  1060. printwarn();
  1061. goto nochange;
  1062. }
  1063. for (;;) {
  1064. redraw(path);
  1065. nochange:
  1066. sel = nextsel(&run, &env);
  1067. switch (sel) {
  1068. case SEL_QUIT:
  1069. dentfree(dents);
  1070. return;
  1071. case SEL_BACK:
  1072. /* There is no going back */
  1073. if (strcmp(path, "/") == 0 ||
  1074. strcmp(path, ".") == 0 ||
  1075. strchr(path, '/') == NULL) {
  1076. printmsg("You are at /");
  1077. goto nochange;
  1078. }
  1079. dir = xdirname(path);
  1080. if (canopendir(dir) == 0) {
  1081. printwarn();
  1082. goto nochange;
  1083. }
  1084. /* Save history */
  1085. xstrlcpy(oldpath, path, sizeof(oldpath));
  1086. /* Save last working directory */
  1087. xstrlcpy(lastdir, path, sizeof(lastdir));
  1088. xstrlcpy(path, dir, sizeof(path));
  1089. /* Reset filter */
  1090. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1091. goto begin;
  1092. case SEL_GOIN:
  1093. /* Cannot descend in empty directories */
  1094. if (ndents == 0)
  1095. goto nochange;
  1096. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  1097. DPRINTF_S(newpath);
  1098. /* Get path info */
  1099. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1100. if (fd == -1) {
  1101. printwarn();
  1102. goto nochange;
  1103. }
  1104. r = fstat(fd, &sb);
  1105. if (r == -1) {
  1106. printwarn();
  1107. close(fd);
  1108. goto nochange;
  1109. }
  1110. close(fd);
  1111. DPRINTF_U(sb.st_mode);
  1112. switch (sb.st_mode & S_IFMT) {
  1113. case S_IFDIR:
  1114. if (canopendir(newpath) == 0) {
  1115. printwarn();
  1116. goto nochange;
  1117. }
  1118. /* Save last working directory */
  1119. xstrlcpy(lastdir, path, sizeof(lastdir));
  1120. xstrlcpy(path, newpath, sizeof(path));
  1121. /* Reset filter */
  1122. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1123. goto begin;
  1124. case S_IFREG:
  1125. {
  1126. static char cmd[MAX_CMD_LEN];
  1127. static char *runvi = "vi";
  1128. static int status;
  1129. /* If default mime opener is set, use it */
  1130. if (opener) {
  1131. snprintf(cmd, MAX_CMD_LEN,
  1132. "%s \"%s\" > /dev/null 2>&1",
  1133. opener, newpath);
  1134. status = system(cmd);
  1135. continue;
  1136. }
  1137. /* Try custom applications */
  1138. bin = openwith(newpath);
  1139. /* If custom app doesn't exist try fallback */
  1140. snprintf(cmd, MAX_CMD_LEN, "which \"%s\"", bin);
  1141. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1142. bin = NULL;
  1143. if (bin == NULL) {
  1144. /* If a custom handler application is
  1145. not set, open plain text files with
  1146. vi, then try fallback_opener */
  1147. snprintf(cmd, MAX_CMD_LEN,
  1148. "file \"%s\"", newpath);
  1149. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1150. goto nochange;
  1151. if (strstr(cmd, "ASCII text") != NULL)
  1152. bin = runvi;
  1153. else if (fallback_opener) {
  1154. snprintf(cmd, MAX_CMD_LEN,
  1155. "%s \"%s\" > \
  1156. /dev/null 2>&1",
  1157. fallback_opener,
  1158. newpath);
  1159. status = system(cmd);
  1160. continue;
  1161. } else {
  1162. status++; /* Dummy operation */
  1163. printmsg("No association");
  1164. goto nochange;
  1165. }
  1166. }
  1167. exitcurses();
  1168. spawn(bin, newpath, NULL, 0);
  1169. initcurses();
  1170. continue;
  1171. }
  1172. default:
  1173. printmsg("Unsupported file");
  1174. goto nochange;
  1175. }
  1176. case SEL_FLTR:
  1177. /* Read filter */
  1178. printprompt("filter: ");
  1179. tmp = readln();
  1180. if (tmp == NULL)
  1181. tmp = ifilter;
  1182. /* Check and report regex errors */
  1183. r = setfilter(&re, tmp);
  1184. if (r != 0)
  1185. goto nochange;
  1186. xstrlcpy(fltr, tmp, sizeof(fltr));
  1187. DPRINTF_S(fltr);
  1188. /* Save current */
  1189. if (ndents > 0)
  1190. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1191. goto begin;
  1192. case SEL_NEXT:
  1193. if (cur < ndents - 1)
  1194. cur++;
  1195. else if (ndents)
  1196. /* Roll over, set cursor to first entry */
  1197. cur = 0;
  1198. break;
  1199. case SEL_PREV:
  1200. if (cur > 0)
  1201. cur--;
  1202. else if (ndents)
  1203. /* Roll over, set cursor to last entry */
  1204. cur = ndents - 1;
  1205. break;
  1206. case SEL_PGDN:
  1207. if (cur < ndents - 1)
  1208. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1209. break;
  1210. case SEL_PGUP:
  1211. if (cur > 0)
  1212. cur -= MIN((LINES - 4) / 2, cur);
  1213. break;
  1214. case SEL_HOME:
  1215. cur = 0;
  1216. break;
  1217. case SEL_END:
  1218. cur = ndents - 1;
  1219. break;
  1220. case SEL_CD:
  1221. /* Read target dir */
  1222. printprompt("chdir: ");
  1223. tmp = readln();
  1224. if (tmp == NULL) {
  1225. clearprompt();
  1226. goto nochange;
  1227. }
  1228. if (tmp[0] == '~') {
  1229. char *home = getenv("HOME");
  1230. if (home)
  1231. snprintf(newpath, PATH_MAX,
  1232. "%s%s", home, tmp + 1);
  1233. else
  1234. mkpath(path, tmp, newpath, sizeof(newpath));
  1235. } else if (tmp[0] == '-' && tmp[1] == '\0')
  1236. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1237. else
  1238. mkpath(path, tmp, newpath, sizeof(newpath));
  1239. if (canopendir(newpath) == 0) {
  1240. printwarn();
  1241. goto nochange;
  1242. }
  1243. /* Save last working directory */
  1244. xstrlcpy(lastdir, path, sizeof(lastdir));
  1245. xstrlcpy(path, newpath, sizeof(path));
  1246. /* Reset filter */
  1247. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1248. DPRINTF_S(path);
  1249. goto begin;
  1250. case SEL_CDHOME:
  1251. tmp = getenv("HOME");
  1252. if (tmp == NULL) {
  1253. clearprompt();
  1254. goto nochange;
  1255. }
  1256. if (canopendir(tmp) == 0) {
  1257. printwarn();
  1258. goto nochange;
  1259. }
  1260. /* Save last working directory */
  1261. xstrlcpy(lastdir, path, sizeof(lastdir));
  1262. xstrlcpy(path, tmp, sizeof(path));
  1263. /* Reset filter */
  1264. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1265. DPRINTF_S(path);
  1266. goto begin;
  1267. case SEL_LAST:
  1268. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1269. xstrlcpy(lastdir, path, sizeof(lastdir));
  1270. xstrlcpy(path, newpath, sizeof(path));
  1271. DPRINTF_S(path);
  1272. goto begin;
  1273. case SEL_TOGGLEDOT:
  1274. showhidden ^= 1;
  1275. initfilter(showhidden, &ifilter);
  1276. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1277. goto begin;
  1278. case SEL_DETAIL:
  1279. showdetail = !showdetail;
  1280. showdetail ? (printptr = &printent_long)
  1281. : (printptr = &printent);
  1282. /* Save current */
  1283. if (ndents > 0)
  1284. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1285. goto begin;
  1286. case SEL_STATS:
  1287. {
  1288. struct stat sb;
  1289. if (ndents > 0)
  1290. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1291. r = lstat(oldpath, &sb);
  1292. if (r == -1)
  1293. printerr(1, "lstat");
  1294. else
  1295. show_stats(oldpath, dents[cur].name, &sb);
  1296. goto begin;
  1297. }
  1298. case SEL_FSIZE:
  1299. sizeorder = !sizeorder;
  1300. mtimeorder = 0;
  1301. bsizeorder = 0;
  1302. /* Save current */
  1303. if (ndents > 0)
  1304. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1305. goto begin;
  1306. case SEL_BSIZE:
  1307. bsizeorder = !bsizeorder;
  1308. if (bsizeorder) {
  1309. showdetail = 1;
  1310. printptr = &printent_long;
  1311. }
  1312. mtimeorder = 0;
  1313. sizeorder = 0;
  1314. /* Save current */
  1315. if (ndents > 0)
  1316. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1317. goto begin;
  1318. case SEL_MTIME:
  1319. mtimeorder = !mtimeorder;
  1320. sizeorder = 0;
  1321. bsizeorder = 0;
  1322. /* Save current */
  1323. if (ndents > 0)
  1324. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1325. goto begin;
  1326. case SEL_REDRAW:
  1327. /* Save current */
  1328. if (ndents > 0)
  1329. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1330. goto begin;
  1331. case SEL_COPY:
  1332. if (copier && ndents) {
  1333. char abspath[PATH_MAX];
  1334. if (strcmp(path, "/") == 0)
  1335. snprintf(abspath, PATH_MAX, "/%s",
  1336. dents[cur].name);
  1337. else
  1338. snprintf(abspath, PATH_MAX, "%s/%s",
  1339. path, dents[cur].name);
  1340. spawn(copier, abspath, NULL, 0);
  1341. printmsg(abspath);
  1342. } else if (!copier)
  1343. printmsg("NNN_COPIER is not set");
  1344. goto nochange;
  1345. case SEL_HELP:
  1346. show_help();
  1347. /* Save current */
  1348. if (ndents > 0)
  1349. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1350. goto begin;
  1351. case SEL_RUN:
  1352. run = xgetenv(env, run);
  1353. exitcurses();
  1354. spawn(run, NULL, path, 1);
  1355. initcurses();
  1356. /* Repopulate as directory content may have changed */
  1357. goto begin;
  1358. case SEL_RUNARG:
  1359. run = xgetenv(env, run);
  1360. exitcurses();
  1361. spawn(run, dents[cur].name, path, 0);
  1362. initcurses();
  1363. break;
  1364. }
  1365. /* Screensaver */
  1366. if (idletimeout != 0 && idle == idletimeout) {
  1367. idle = 0;
  1368. exitcurses();
  1369. spawn(idlecmd, NULL, NULL, 0);
  1370. initcurses();
  1371. }
  1372. }
  1373. }
  1374. static void
  1375. usage(void)
  1376. {
  1377. fprintf(stderr, "usage: nnn [-d] [dir]\n");
  1378. exit(1);
  1379. }
  1380. int
  1381. main(int argc, char *argv[])
  1382. {
  1383. char cwd[PATH_MAX], *ipath;
  1384. char *ifilter;
  1385. int opt = 0;
  1386. /* Confirm we are in a terminal */
  1387. if (!isatty(0) || !isatty(1)) {
  1388. fprintf(stderr, "stdin or stdout is not a tty\n");
  1389. exit(1);
  1390. }
  1391. if (argc > 3)
  1392. usage();
  1393. while ((opt = getopt(argc, argv, "d")) != -1) {
  1394. switch (opt) {
  1395. case 'd':
  1396. /* Open in detail mode, if set */
  1397. showdetail = 1;
  1398. printptr = &printent_long;
  1399. break;
  1400. default:
  1401. usage();
  1402. }
  1403. }
  1404. if (argc == optind) {
  1405. /* Start in the current directory */
  1406. ipath = getcwd(cwd, sizeof(cwd));
  1407. if (ipath == NULL)
  1408. ipath = "/";
  1409. } else {
  1410. ipath = realpath(argv[optind], cwd);
  1411. if (!ipath) {
  1412. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1413. exit(1);
  1414. }
  1415. }
  1416. open_max = max_openfds();
  1417. if (getuid() == 0)
  1418. showhidden = 1;
  1419. initfilter(showhidden, &ifilter);
  1420. /* Get the default desktop mime opener, if set */
  1421. opener = getenv("NNN_OPENER");
  1422. /* Get the fallback desktop mime opener, if set */
  1423. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  1424. /* Get the default copier, if set */
  1425. copier = getenv("NNN_COPIER");
  1426. signal(SIGINT, SIG_IGN);
  1427. /* Test initial path */
  1428. if (canopendir(ipath) == 0) {
  1429. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1430. exit(1);
  1431. }
  1432. /* Set locale before curses setup */
  1433. setlocale(LC_ALL, "");
  1434. initcurses();
  1435. browse(ipath, ifilter);
  1436. exitcurses();
  1437. exit(0);
  1438. }