My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nnn.c 30 KiB

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