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.

nnn.c 43 KiB

il y a 9 ans
il y a 10 ans
il y a 9 ans
il y a 9 ans
il y a 10 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 7 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 8 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 8 ans
il y a 7 ans
il y a 7 ans
il y a 8 ans
il y a 8 ans
il y a 7 ans
il y a 7 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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 <ctype.h>
  8. #include <curses.h>
  9. #include <dirent.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <grp.h>
  13. #include <limits.h>
  14. #ifdef __gnu_hurd__
  15. #define PATH_MAX 4096
  16. #endif
  17. #include <locale.h>
  18. #include <pwd.h>
  19. #include <regex.h>
  20. #include <signal.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <wchar.h>
  28. #include <readline/readline.h>
  29. #ifndef __USE_XOPEN_EXTENDED
  30. #define __USE_XOPEN_EXTENDED 1
  31. #endif
  32. #include <ftw.h>
  33. #ifdef DEBUG
  34. static int
  35. xprintf(int fd, const char *fmt, ...)
  36. {
  37. char buf[BUFSIZ];
  38. int r;
  39. va_list ap;
  40. va_start(ap, fmt);
  41. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  42. if (r > 0)
  43. r = write(fd, buf, r);
  44. va_end(ap);
  45. return r;
  46. }
  47. #define DEBUG_FD 8
  48. #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
  49. #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
  50. #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
  51. #define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=0x%p\n", x)
  52. #else
  53. #define DPRINTF_D(x)
  54. #define DPRINTF_U(x)
  55. #define DPRINTF_S(x)
  56. #define DPRINTF_P(x)
  57. #endif /* DEBUG */
  58. #define VERSION "v1.1"
  59. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  60. #undef MIN
  61. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  62. #define ISODD(x) ((x) & 1)
  63. #define CONTROL(c) ((c) ^ 0x40)
  64. #define TOUPPER(ch) \
  65. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  66. #define MAX_CMD_LEN 5120
  67. #define CURSYM(flag) (flag ? CURSR : EMPTY)
  68. struct assoc {
  69. char *regex; /* Regex to match on filename */
  70. char *mime; /* File type */
  71. };
  72. /* Supported actions */
  73. enum action {
  74. SEL_QUIT = 1,
  75. SEL_CDQUIT,
  76. SEL_BACK,
  77. SEL_GOIN,
  78. SEL_FLTR,
  79. SEL_NEXT,
  80. SEL_PREV,
  81. SEL_PGDN,
  82. SEL_PGUP,
  83. SEL_HOME,
  84. SEL_END,
  85. SEL_CD,
  86. SEL_CDHOME,
  87. SEL_CDBEGIN,
  88. SEL_CDLAST,
  89. SEL_TOGGLEDOT,
  90. SEL_DETAIL,
  91. SEL_STATS,
  92. SEL_MEDIA,
  93. SEL_FMEDIA,
  94. SEL_DFB,
  95. SEL_FSIZE,
  96. SEL_BSIZE,
  97. SEL_MTIME,
  98. SEL_REDRAW,
  99. SEL_COPY,
  100. SEL_HELP,
  101. SEL_RUN,
  102. SEL_RUNARG,
  103. };
  104. struct key {
  105. int sym; /* Key pressed */
  106. enum action act; /* Action */
  107. char *run; /* Program to run */
  108. char *env; /* Environment variable to run */
  109. };
  110. #include "config.h"
  111. typedef struct entry {
  112. char name[NAME_MAX];
  113. mode_t mode;
  114. time_t t;
  115. off_t size;
  116. off_t bsize;
  117. } *pEntry;
  118. typedef unsigned long ulong;
  119. /* Externs */
  120. #ifdef __APPLE__
  121. extern int add_history(const char *);
  122. #else
  123. extern void add_history(const char *string);
  124. #endif
  125. extern int wget_wch(WINDOW *, wint_t *);
  126. /* Global context */
  127. static struct entry *dents;
  128. static int ndents, cur, total_dents;
  129. static int idle;
  130. static char *opener;
  131. static char *fb_opener;
  132. static char *player;
  133. static char *copier;
  134. static char *desktop_manager;
  135. static off_t blk_size;
  136. static size_t fs_free;
  137. static int open_max;
  138. static const double div_2_pow_10 = 1.0 / 1024.0;
  139. static const char *size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  140. /*
  141. * Layout:
  142. * .---------
  143. * | cwd: /mnt/path
  144. * |
  145. * | file0
  146. * | file1
  147. * | > file2
  148. * | file3
  149. * | file4
  150. * ...
  151. * | filen
  152. * |
  153. * | Permission denied
  154. * '------
  155. */
  156. static void printmsg(char *);
  157. static void printwarn(void);
  158. static void printerr(int, char *);
  159. static int dentfind(struct entry *dents, int n, char *path);
  160. static void redraw(char *path);
  161. static rlim_t
  162. max_openfds()
  163. {
  164. struct rlimit rl;
  165. rlim_t limit;
  166. limit = getrlimit(RLIMIT_NOFILE, &rl);
  167. if (limit != 0)
  168. return 32;
  169. limit = rl.rlim_cur;
  170. rl.rlim_cur = rl.rlim_max;
  171. if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
  172. return rl.rlim_max - 64;
  173. if (limit > 128)
  174. return limit - 64;
  175. return 32;
  176. }
  177. /* Just a safe strncpy(3) */
  178. static void
  179. xstrlcpy(char *dest, const char *src, size_t n)
  180. {
  181. strncpy(dest, src, n - 1);
  182. dest[n - 1] = '\0';
  183. }
  184. /*
  185. * The poor man's implementation of memrchr(3).
  186. * We are only looking for '/' in this program.
  187. */
  188. static void *
  189. xmemrchr(const void *s, int c, size_t n)
  190. {
  191. unsigned char *p;
  192. unsigned char ch = (unsigned char)c;
  193. if (!s || !n)
  194. return NULL;
  195. p = (unsigned char *)s + n - 1;
  196. while(n--)
  197. if ((*p--) == ch)
  198. return ++p;
  199. return NULL;
  200. }
  201. /*
  202. * The following dirname(3) implementation does not
  203. * modify the input. We use a copy of the original.
  204. *
  205. * Modified from the glibc (GNU LGPL) version.
  206. */
  207. static char *
  208. xdirname(const char *path)
  209. {
  210. static char buf[PATH_MAX];
  211. static char *last_slash;
  212. xstrlcpy(buf, path, PATH_MAX);
  213. /* Find last '/'. */
  214. last_slash = strrchr(buf, '/');
  215. if (last_slash != NULL && last_slash != buf && last_slash[1] == '\0') {
  216. /* Determine whether all remaining characters are slashes. */
  217. char *runp;
  218. for (runp = last_slash; runp != buf; --runp)
  219. if (runp[-1] != '/')
  220. break;
  221. /* The '/' is the last character, we have to look further. */
  222. if (runp != buf)
  223. last_slash = xmemrchr(buf, '/', runp - buf);
  224. }
  225. if (last_slash != NULL) {
  226. /* Determine whether all remaining characters are slashes. */
  227. char *runp;
  228. for (runp = last_slash; runp != buf; --runp)
  229. if (runp[-1] != '/')
  230. break;
  231. /* Terminate the buffer. */
  232. if (runp == buf) {
  233. /* The last slash is the first character in the string.
  234. We have to return "/". As a special case we have to
  235. return "//" if there are exactly two slashes at the
  236. beginning of the string. See XBD 4.10 Path Name
  237. Resolution for more information. */
  238. if (last_slash == buf + 1)
  239. ++last_slash;
  240. else
  241. last_slash = buf + 1;
  242. } else
  243. last_slash = runp;
  244. last_slash[0] = '\0';
  245. } else {
  246. /* This assignment is ill-designed but the XPG specs require to
  247. return a string containing "." in any case no directory part
  248. is found and so a static and constant string is required. */
  249. buf[0] = '.';
  250. buf[1] = '\0';
  251. }
  252. return buf;
  253. }
  254. /*
  255. * Return number of dots of all chars in a string are dots, else 0
  256. */
  257. static int
  258. all_dots(const char* ptr)
  259. {
  260. if (!ptr)
  261. return FALSE;
  262. int count = 0;
  263. while (*ptr == '.') {
  264. count++;
  265. ptr++;
  266. }
  267. if (*ptr)
  268. return 0;
  269. return count;
  270. }
  271. /*
  272. * Spawns a child process. Behaviour can be controlled using flag:
  273. * Limited to 2 arguments to a program
  274. * flag works on bit set:
  275. * - 0b1: draw a marker to indicate nnn spawned e.g., a shell
  276. * - 0b10: do not wait in parent for child process e.g. DE file manager
  277. * - 0b100: suppress stdout and stderr
  278. */
  279. static void
  280. spawn(char *file, char *arg1, char *arg2, char *dir, unsigned char flag)
  281. {
  282. pid_t pid;
  283. int status;
  284. pid = fork();
  285. if (pid == 0) {
  286. if (dir != NULL)
  287. status = chdir(dir);
  288. /* Show a marker (to indicate nnn spawned shell) */
  289. if (flag & 0b1)
  290. fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  291. /* Suppress stdout and stderr */
  292. if (flag & 0b100) {
  293. int fd = open("/dev/null", O_WRONLY, S_IWUSR);
  294. dup2(fd, 1);
  295. dup2(fd, 2);
  296. close(fd);
  297. }
  298. execlp(file, file, arg1, arg2, NULL);
  299. _exit(1);
  300. } else {
  301. if (!(flag & 0b10))
  302. /* Ignore interruptions */
  303. while (waitpid(pid, &status, 0) == -1)
  304. DPRINTF_D(status);
  305. DPRINTF_D(pid);
  306. }
  307. }
  308. static char *
  309. xgetenv(char *name, char *fallback)
  310. {
  311. char *value;
  312. if (name == NULL)
  313. return fallback;
  314. value = getenv(name);
  315. return value && value[0] ? value : fallback;
  316. }
  317. /*
  318. * We assume none of the strings are NULL.
  319. *
  320. * Let's have the logic to sort numeric names in numeric order.
  321. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  322. *
  323. * If the absolute numeric values are same, we fallback to alphasort.
  324. */
  325. static int
  326. xstricmp(const char *s1, const char *s2)
  327. {
  328. static char *c1, *c2;
  329. static long long num1, num2;
  330. num1 = strtoll(s1, &c1, 10);
  331. num2 = strtoll(s2, &c2, 10);
  332. if (*c1 == '\0' && *c2 == '\0') {
  333. if (num1 != num2) {
  334. if (num1 > num2)
  335. return 1;
  336. else
  337. return -1;
  338. }
  339. } else if (*c1 == '\0' && *c2 != '\0')
  340. return -1;
  341. else if (*c1 != '\0' && *c2 == '\0')
  342. return 1;
  343. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  344. s1++, s2++;
  345. /* In case of alphabetically same names, make sure
  346. lower case one comes before upper case one */
  347. if (!*s1 && !*s2)
  348. return 1;
  349. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  350. }
  351. /* Trim all whitespace from both ends, / from end */
  352. static char *
  353. strstrip(char *s)
  354. {
  355. if (!s || !*s)
  356. return s;
  357. size_t len = strlen(s) - 1;
  358. while (len != 0 && (isspace(s[len]) || s[len] == '/'))
  359. len--;
  360. s[len + 1] = '\0';
  361. while (*s && isspace(*s))
  362. s++;
  363. return s;
  364. }
  365. static char *
  366. getmime(char *file)
  367. {
  368. regex_t regex;
  369. unsigned int i;
  370. static unsigned int len = LEN(assocs);
  371. for (i = 0; i < len; i++) {
  372. if (regcomp(&regex, assocs[i].regex,
  373. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  374. continue;
  375. if (regexec(&regex, file, 0, NULL, 0) == 0)
  376. return assocs[i].mime;
  377. }
  378. return NULL;
  379. }
  380. static int
  381. setfilter(regex_t *regex, char *filter)
  382. {
  383. static char errbuf[LINE_MAX];
  384. static size_t len;
  385. static int r;
  386. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  387. if (r != 0) {
  388. len = COLS;
  389. if (len > LINE_MAX)
  390. len = LINE_MAX;
  391. regerror(r, regex, errbuf, len);
  392. printmsg(errbuf);
  393. }
  394. return r;
  395. }
  396. static void
  397. initfilter(int dot, char **ifilter)
  398. {
  399. *ifilter = dot ? "." : "^[^.]";
  400. }
  401. static int
  402. visible(regex_t *regex, char *file)
  403. {
  404. return regexec(regex, file, 0, NULL, 0) == 0;
  405. }
  406. static int
  407. entrycmp(const void *va, const void *vb)
  408. {
  409. static pEntry pa, pb;
  410. pa = (pEntry)va;
  411. pb = (pEntry)vb;
  412. /* Sort directories first */
  413. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  414. return 1;
  415. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  416. return -1;
  417. /* Do the actual sorting */
  418. if (mtimeorder)
  419. return pb->t - pa->t;
  420. if (sizeorder) {
  421. if (pb->size > pa->size)
  422. return 1;
  423. else if (pb->size < pa->size)
  424. return -1;
  425. }
  426. if (bsizeorder) {
  427. if (pb->bsize > pa->bsize)
  428. return 1;
  429. else if (pb->bsize < pa->bsize)
  430. return -1;
  431. }
  432. return xstricmp(pa->name, pb->name);
  433. }
  434. static void
  435. initcurses(void)
  436. {
  437. if (initscr() == NULL) {
  438. char *term = getenv("TERM");
  439. if (term != NULL)
  440. fprintf(stderr, "error opening terminal: %s\n", term);
  441. else
  442. fprintf(stderr, "failed to initialize curses\n");
  443. exit(1);
  444. }
  445. cbreak();
  446. noecho();
  447. nonl();
  448. intrflush(stdscr, FALSE);
  449. keypad(stdscr, TRUE);
  450. curs_set(FALSE); /* Hide cursor */
  451. start_color();
  452. use_default_colors();
  453. timeout(1000); /* One second */
  454. }
  455. static void
  456. exitcurses(void)
  457. {
  458. endwin(); /* Restore terminal */
  459. }
  460. /* Messages show up at the bottom */
  461. static void
  462. printmsg(char *msg)
  463. {
  464. move(LINES - 1, 0);
  465. printw("%s\n", msg);
  466. }
  467. /* Display warning as a message */
  468. static void
  469. printwarn(void)
  470. {
  471. printmsg(strerror(errno));
  472. }
  473. /* Kill curses and display error before exiting */
  474. static void
  475. printerr(int ret, char *prefix)
  476. {
  477. exitcurses();
  478. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  479. exit(ret);
  480. }
  481. /* Clear the last line */
  482. static void
  483. clearprompt(void)
  484. {
  485. printmsg("");
  486. }
  487. /* Print prompt on the last line */
  488. static void
  489. printprompt(char *str)
  490. {
  491. clearprompt();
  492. printw(str);
  493. }
  494. /* Returns SEL_* if key is bound and 0 otherwise.
  495. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  496. static int
  497. nextsel(char **run, char **env, int *ch)
  498. {
  499. int c = *ch;
  500. unsigned int i;
  501. static unsigned int len = LEN(bindings);
  502. if (c == 0)
  503. c = getch();
  504. else
  505. *ch = 0;
  506. if (c == -1)
  507. idle++;
  508. else
  509. idle = 0;
  510. for (i = 0; i < len; i++)
  511. if (c == bindings[i].sym) {
  512. *run = bindings[i].run;
  513. *env = bindings[i].env;
  514. return bindings[i].act;
  515. }
  516. return 0;
  517. }
  518. static int
  519. fill(struct entry **dents,
  520. int (*filter)(regex_t *, char *), regex_t *re)
  521. {
  522. static struct entry _dent;
  523. static int count, n;
  524. for (count = 0, n = 0; count < ndents; count++) {
  525. if (filter(re, (*dents)[count].name) == 0)
  526. continue;
  527. if (n != count) {
  528. /* Copy to tmp */
  529. xstrlcpy(_dent.name, (*dents)[n].name, NAME_MAX);
  530. _dent.mode = (*dents)[n].mode;
  531. _dent.t = (*dents)[n].t;
  532. _dent.size = (*dents)[n].size;
  533. _dent.bsize = (*dents)[n].bsize;
  534. /* Copy count to n */
  535. xstrlcpy((*dents)[n].name, (*dents)[count].name, NAME_MAX);
  536. (*dents)[n].mode = (*dents)[count].mode;
  537. (*dents)[n].t = (*dents)[count].t;
  538. (*dents)[n].size = (*dents)[count].size;
  539. (*dents)[n].bsize = (*dents)[count].bsize;
  540. /* Copy tmp to count */
  541. xstrlcpy((*dents)[count].name, _dent.name, NAME_MAX);
  542. (*dents)[count].mode = _dent.mode;
  543. (*dents)[count].t = _dent.t;
  544. (*dents)[count].size = _dent.size;
  545. (*dents)[count].bsize = _dent.bsize;
  546. }
  547. n++;
  548. }
  549. return n;
  550. }
  551. static int
  552. matches(char *fltr)
  553. {
  554. static regex_t re;
  555. /* Search filter */
  556. if (setfilter(&re, fltr) != 0)
  557. return -1;
  558. ndents = fill(&dents, visible, &re);
  559. qsort(dents, ndents, sizeof(*dents), entrycmp);
  560. return 0;
  561. }
  562. static int
  563. readln(char *path)
  564. {
  565. static char ln[LINE_MAX << 2];
  566. static wchar_t wln[LINE_MAX];
  567. static wint_t ch[2] = {0};
  568. int r, total = ndents;
  569. int oldcur = cur;
  570. int len = 1;
  571. char *pln = ln + 1;
  572. memset(wln, 0, LINE_MAX << 2);
  573. wln[0] = '/';
  574. ln[0] = '/';
  575. ln[1] = '\0';
  576. cur = 0;
  577. timeout(-1);
  578. echo();
  579. curs_set(TRUE);
  580. printprompt(ln);
  581. while ((r = wget_wch(stdscr, ch)) != ERR) {
  582. if (r == OK) {
  583. switch(*ch) {
  584. case '\r': // with nonl(), this is ENTER key value
  585. if (len == 1) {
  586. cur = oldcur;
  587. *ch = CONTROL('L');
  588. goto end;
  589. }
  590. if (matches(pln) == -1)
  591. goto end;
  592. redraw(path);
  593. goto end;
  594. case 127: // handle DEL
  595. if (len == 1) {
  596. cur = oldcur;
  597. *ch = CONTROL('L');
  598. goto end;
  599. }
  600. if (len == 2)
  601. cur = oldcur;
  602. wln[--len] = '\0';
  603. wcstombs(ln, wln, LINE_MAX << 2);
  604. ndents = total;
  605. if (matches(pln) == -1)
  606. continue;
  607. redraw(path);
  608. printprompt(ln);
  609. break;
  610. default:
  611. wln[len++] = (wchar_t)*ch;
  612. wln[len] = '\0';
  613. wcstombs(ln, wln, LINE_MAX << 2);
  614. ndents = total;
  615. if (matches(pln) == -1)
  616. continue;
  617. redraw(path);
  618. printprompt(ln);
  619. }
  620. } else {
  621. switch(*ch) {
  622. case KEY_DC:
  623. case KEY_BACKSPACE:
  624. if (len == 1) {
  625. cur = oldcur;
  626. *ch = CONTROL('L');
  627. goto end;
  628. }
  629. if (len == 2)
  630. cur = oldcur;
  631. wln[--len] = '\0';
  632. wcstombs(ln, wln, LINE_MAX << 2);
  633. ndents = total;
  634. if (matches(pln) == -1)
  635. continue;
  636. redraw(path);
  637. printprompt(ln);
  638. break;
  639. default:
  640. goto end;
  641. }
  642. }
  643. }
  644. end:
  645. noecho();
  646. curs_set(FALSE);
  647. timeout(1000);
  648. return *ch;
  649. }
  650. static int
  651. canopendir(char *path)
  652. {
  653. static DIR *dirp;
  654. dirp = opendir(path);
  655. if (dirp == NULL)
  656. return 0;
  657. closedir(dirp);
  658. return 1;
  659. }
  660. /*
  661. * Returns "dir/name or "/name"
  662. */
  663. static char *
  664. mkpath(char *dir, char *name, char *out, size_t n)
  665. {
  666. /* Handle absolute path */
  667. if (name[0] == '/')
  668. xstrlcpy(out, name, n);
  669. else {
  670. /* Handle root case */
  671. if (strcmp(dir, "/") == 0)
  672. snprintf(out, n, "/%s", name);
  673. else
  674. snprintf(out, n, "%s/%s", dir, name);
  675. }
  676. return out;
  677. }
  678. static char *
  679. replace_escape(const char *str)
  680. {
  681. static char buffer[PATH_MAX];
  682. static wchar_t wbuf[PATH_MAX];
  683. static wchar_t *buf;
  684. buffer[0] = '\0';
  685. buf = wbuf;
  686. /* Convert multi-byte to wide char */
  687. mbstowcs(wbuf, str, PATH_MAX);
  688. while (*buf) {
  689. if (*buf <= '\x1f' || *buf == '\x7f')
  690. *buf = '\?';
  691. buf++;
  692. }
  693. /* Convert wide char to multi-byte */
  694. wcstombs(buffer, wbuf, PATH_MAX);
  695. return buffer;
  696. }
  697. static void
  698. printent(struct entry *ent, int active)
  699. {
  700. static int ncols;
  701. static char str[PATH_MAX + 16];
  702. if (COLS > PATH_MAX + 16)
  703. ncols = PATH_MAX + 16;
  704. else
  705. ncols = COLS;
  706. if (S_ISDIR(ent->mode))
  707. snprintf(str, ncols, "%s%s/", CURSYM(active),
  708. replace_escape(ent->name));
  709. else if (S_ISLNK(ent->mode))
  710. snprintf(str, ncols, "%s%s@", CURSYM(active),
  711. replace_escape(ent->name));
  712. else if (S_ISSOCK(ent->mode))
  713. snprintf(str, ncols, "%s%s=", CURSYM(active),
  714. replace_escape(ent->name));
  715. else if (S_ISFIFO(ent->mode))
  716. snprintf(str, ncols, "%s%s|", CURSYM(active),
  717. replace_escape(ent->name));
  718. else if (ent->mode & S_IXUSR)
  719. snprintf(str, ncols, "%s%s*", CURSYM(active),
  720. replace_escape(ent->name));
  721. else
  722. snprintf(str, ncols, "%s%s", CURSYM(active),
  723. replace_escape(ent->name));
  724. printw("%s\n", str);
  725. }
  726. static void (*printptr)(struct entry *ent, int active) = &printent;
  727. static char*
  728. coolsize(off_t size)
  729. {
  730. static char size_buf[12]; /* Buffer to hold human readable size */
  731. static int i;
  732. static off_t fsize, tmp;
  733. static long double rem;
  734. i = 0;
  735. fsize = size;
  736. rem = 0;
  737. while (fsize > 1024) {
  738. tmp = fsize;
  739. //fsize *= div_2_pow_10;
  740. fsize >>= 10;
  741. rem = tmp - (fsize << 10);
  742. i++;
  743. }
  744. snprintf(size_buf, 12, "%.*Lf%s", i, fsize + rem * div_2_pow_10, size_units[i]);
  745. return size_buf;
  746. }
  747. static void
  748. printent_long(struct entry *ent, int active)
  749. {
  750. static int ncols;
  751. static char str[PATH_MAX + 32];
  752. static char buf[18];
  753. if (COLS > PATH_MAX + 32)
  754. ncols = PATH_MAX + 32;
  755. else
  756. ncols = COLS;
  757. strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t));
  758. if (active)
  759. attron(A_REVERSE);
  760. if (!bsizeorder) {
  761. if (S_ISDIR(ent->mode))
  762. snprintf(str, ncols, "%s%-16.16s / %s/",
  763. CURSYM(active), buf, replace_escape(ent->name));
  764. else if (S_ISLNK(ent->mode))
  765. snprintf(str, ncols, "%s%-16.16s @ %s@",
  766. CURSYM(active), buf, replace_escape(ent->name));
  767. else if (S_ISSOCK(ent->mode))
  768. snprintf(str, ncols, "%s%-16.16s = %s=",
  769. CURSYM(active), buf, replace_escape(ent->name));
  770. else if (S_ISFIFO(ent->mode))
  771. snprintf(str, ncols, "%s%-16.16s | %s|",
  772. CURSYM(active), buf, replace_escape(ent->name));
  773. else if (S_ISBLK(ent->mode))
  774. snprintf(str, ncols, "%s%-16.16s b %s",
  775. CURSYM(active), buf, replace_escape(ent->name));
  776. else if (S_ISCHR(ent->mode))
  777. snprintf(str, ncols, "%s%-16.16s c %s",
  778. CURSYM(active), buf, replace_escape(ent->name));
  779. else if (ent->mode & S_IXUSR)
  780. snprintf(str, ncols, "%s%-16.16s %8.8s* %s*",
  781. CURSYM(active), buf, coolsize(ent->size),
  782. replace_escape(ent->name));
  783. else
  784. snprintf(str, ncols, "%s%-16.16s %8.8s %s",
  785. CURSYM(active), buf, coolsize(ent->size),
  786. replace_escape(ent->name));
  787. } else {
  788. if (S_ISDIR(ent->mode))
  789. snprintf(str, ncols, "%s%-16.16s %8.8s/ %s/",
  790. CURSYM(active), buf, coolsize(ent->bsize << 9),
  791. replace_escape(ent->name));
  792. else if (S_ISLNK(ent->mode))
  793. snprintf(str, ncols, "%s%-16.16s @ %s@",
  794. CURSYM(active), buf,
  795. replace_escape(ent->name));
  796. else if (S_ISSOCK(ent->mode))
  797. snprintf(str, ncols, "%s%-16.16s = %s=",
  798. CURSYM(active), buf,
  799. replace_escape(ent->name));
  800. else if (S_ISFIFO(ent->mode))
  801. snprintf(str, ncols, "%s%-16.16s | %s|",
  802. CURSYM(active), buf,
  803. replace_escape(ent->name));
  804. else if (S_ISBLK(ent->mode))
  805. snprintf(str, ncols, "%s%-16.16s b %s",
  806. CURSYM(active), buf,
  807. replace_escape(ent->name));
  808. else if (S_ISCHR(ent->mode))
  809. snprintf(str, ncols, "%s%-16.16s c %s",
  810. CURSYM(active), buf,
  811. replace_escape(ent->name));
  812. else if (ent->mode & S_IXUSR)
  813. snprintf(str, ncols, "%s%-16.16s %8.8s* %s*",
  814. CURSYM(active), buf, coolsize(ent->bsize << 9),
  815. replace_escape(ent->name));
  816. else
  817. snprintf(str, ncols, "%s%-16.16s %8.8s %s",
  818. CURSYM(active), buf, coolsize(ent->bsize << 9),
  819. replace_escape(ent->name));
  820. }
  821. printw("%s\n", str);
  822. if (active)
  823. attroff(A_REVERSE);
  824. }
  825. static char
  826. get_fileind(mode_t mode, char *desc)
  827. {
  828. static char c;
  829. if (S_ISREG(mode)) {
  830. c = '-';
  831. sprintf(desc, "%s", "regular file");
  832. if (mode & S_IXUSR)
  833. strcat(desc, ", executable");
  834. } else if (S_ISDIR(mode)) {
  835. c = 'd';
  836. sprintf(desc, "%s", "directory");
  837. } else if (S_ISBLK(mode)) {
  838. c = 'b';
  839. sprintf(desc, "%s", "block special device");
  840. } else if (S_ISCHR(mode)) {
  841. c = 'c';
  842. sprintf(desc, "%s", "character special device");
  843. #ifdef S_ISFIFO
  844. } else if (S_ISFIFO(mode)) {
  845. c = 'p';
  846. sprintf(desc, "%s", "FIFO");
  847. #endif /* S_ISFIFO */
  848. #ifdef S_ISLNK
  849. } else if (S_ISLNK(mode)) {
  850. c = 'l';
  851. sprintf(desc, "%s", "symbolic link");
  852. #endif /* S_ISLNK */
  853. #ifdef S_ISSOCK
  854. } else if (S_ISSOCK(mode)) {
  855. c = 's';
  856. sprintf(desc, "%s", "socket");
  857. #endif /* S_ISSOCK */
  858. #ifdef S_ISDOOR
  859. /* Solaris 2.6, etc. */
  860. } else if (S_ISDOOR(mode)) {
  861. c = 'D';
  862. desc[0] = '\0';
  863. #endif /* S_ISDOOR */
  864. } else {
  865. /* Unknown type -- possibly a regular file? */
  866. c = '?';
  867. desc[0] = '\0';
  868. }
  869. return(c);
  870. }
  871. /* Convert a mode field into "ls -l" type perms field. */
  872. static char *
  873. get_lsperms(mode_t mode, char *desc)
  874. {
  875. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  876. "r--", "r-x", "rw-", "rwx"};
  877. static char bits[11];
  878. bits[0] = get_fileind(mode, desc);
  879. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  880. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  881. strcpy(&bits[7], rwx[(mode & 7)]);
  882. if (mode & S_ISUID)
  883. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  884. if (mode & S_ISGID)
  885. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  886. if (mode & S_ISVTX)
  887. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  888. bits[10] = '\0';
  889. return(bits);
  890. }
  891. /* Gets only a single line, that's what we need for now */
  892. static char *
  893. get_output(char *buf, size_t bytes)
  894. {
  895. char *ret;
  896. FILE *pf = popen(buf, "r");
  897. if (pf) {
  898. ret = fgets(buf, bytes, pf);
  899. pclose(pf);
  900. return ret;
  901. }
  902. return NULL;
  903. }
  904. /*
  905. * Follows the stat(1) output closely
  906. */
  907. static int
  908. show_stats(char* fpath, char* fname, struct stat *sb)
  909. {
  910. char buf[PATH_MAX + 16];
  911. char *perms = get_lsperms(sb->st_mode, buf);
  912. char *p, *begin = buf;
  913. char tmp[] = "/tmp/nnnXXXXXX";
  914. int fd = mkstemp(tmp);
  915. if (fd == -1)
  916. return -1;
  917. /* Show file name or 'symlink' -> 'target' */
  918. if (perms[0] == 'l') {
  919. char symtgt[PATH_MAX];
  920. ssize_t len = readlink(fpath, symtgt, PATH_MAX);
  921. if (len != -1) {
  922. symtgt[len] = '\0';
  923. dprintf(fd, " File: '%s' -> '%s'",
  924. replace_escape(fname),
  925. replace_escape(symtgt));
  926. }
  927. } else
  928. dprintf(fd, " File: '%s'", replace_escape(fname));
  929. /* Show size, blocks, file type */
  930. #ifdef __APPLE__
  931. dprintf(fd, "\n Size: %-15lld Blocks: %-10lld IO Block: %-6d %s",
  932. #else
  933. dprintf(fd, "\n Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s",
  934. #endif
  935. sb->st_size, sb->st_blocks, sb->st_blksize, buf);
  936. /* Show containing device, inode, hardlink count */
  937. #ifdef __APPLE__
  938. sprintf(buf, "%xh/%ud", sb->st_dev, sb->st_dev);
  939. dprintf(fd, "\n Device: %-15s Inode: %-11llu Links: %-9hu",
  940. #else
  941. sprintf(buf, "%lxh/%lud", sb->st_dev, sb->st_dev);
  942. dprintf(fd, "\n Device: %-15s Inode: %-11lu Links: %-9lu",
  943. #endif
  944. buf, sb->st_ino, sb->st_nlink);
  945. /* Show major, minor number for block or char device */
  946. if (perms[0] == 'b' || perms[0] == 'c')
  947. dprintf(fd, " Device type: %x,%x",
  948. major(sb->st_rdev), minor(sb->st_rdev));
  949. /* Show permissions, owner, group */
  950. dprintf(fd, "\n Access: 0%d%d%d/%s Uid: (%u/%s) Gid: (%u/%s)",
  951. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  952. perms,
  953. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  954. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  955. /* Show last access time */
  956. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  957. dprintf(fd, "\n\n Access: %s", buf);
  958. /* Show last modification time */
  959. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  960. dprintf(fd, "\n Modify: %s", buf);
  961. /* Show last status change time */
  962. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  963. dprintf(fd, "\n Change: %s", buf);
  964. if (S_ISREG(sb->st_mode)) {
  965. /* Show file(1) output */
  966. strcpy(buf, "file -b \"");
  967. xstrlcpy(buf + strlen(buf), fpath, sizeof(buf) - strlen(buf));
  968. strcat(buf, "\" 2>&1");
  969. p = get_output(buf, sizeof(buf));
  970. if (p) {
  971. dprintf(fd, "\n\n ");
  972. while (*p) {
  973. if (*p == ',') {
  974. *p = '\0';
  975. dprintf(fd, " %s\n", begin);
  976. begin = p + 1;
  977. }
  978. p++;
  979. }
  980. dprintf(fd, " %s", begin);
  981. }
  982. }
  983. dprintf(fd, "\n\n");
  984. close(fd);
  985. sprintf(buf, "cat %s | %s", tmp, xgetenv("PAGER", "less"));
  986. fd = system(buf);
  987. unlink(tmp);
  988. return fd;
  989. }
  990. static int
  991. show_mediainfo(const char* fpath, int full)
  992. {
  993. static char buf[MAX_CMD_LEN];
  994. strcpy(buf, "which mediainfo");
  995. if (get_output(buf, MAX_CMD_LEN) == NULL)
  996. return -1;
  997. strcpy(buf, "mediainfo \'");
  998. xstrlcpy(buf + strlen(buf), fpath, sizeof(buf) - strlen(buf));
  999. if (full)
  1000. strcat(buf, "\' -f ");
  1001. else
  1002. strcat(buf, "\' ");
  1003. sprintf(buf + strlen(buf), "2>&1 | %s", xgetenv("PAGER", "less"));
  1004. return system(buf);
  1005. }
  1006. static int
  1007. show_help(void)
  1008. {
  1009. char helpstr[2048] = ("echo \"\
  1010. Key | Function\n\
  1011. -+-\n\
  1012. Up, k, ^P | Previous entry\n\
  1013. Down, j, ^N | Next entry\n\
  1014. PgUp, ^U | Scroll half page up\n\
  1015. PgDn, ^D | Scroll half page down\n\
  1016. Home, g, ^, ^A | Jump to first entry\n\
  1017. End, G, $, ^E | Jump to last entry\n\
  1018. Right, Enter, l, ^M | Open file or enter dir\n\
  1019. Left, Bksp, h, ^H | Go to parent dir\n\
  1020. ~ | Jump to HOME dir\n\
  1021. & | Jump to initial dir\n\
  1022. - | Jump to last visited dir\n\
  1023. o | Open dir in NNN_DE_FILE_MANAGER\n\
  1024. / | Filter dir contents\n\
  1025. c | Show change dir prompt\n\
  1026. d | Toggle detail view\n\
  1027. D | Toggle current file details screen\n\
  1028. m | Show concise mediainfo\n\
  1029. M | Show full mediainfo\n\
  1030. . | Toggle hide .dot files\n\
  1031. s | Toggle sort by file size\n\
  1032. S | Toggle disk usage analyzer mode\n\
  1033. t | Toggle sort by modified time\n\
  1034. ! | Spawn SHELL in PWD (fallback sh)\n\
  1035. z | Run top\n\
  1036. e | Edit entry in EDITOR (fallback vi)\n\
  1037. p | Open entry in PAGER (fallback less)\n\
  1038. ^K | Invoke file name copier\n\
  1039. ^L | Force a redraw\n\
  1040. ? | Toggle help screen\n\
  1041. q | Quit\n\
  1042. Q | Quit and change directory\n\n\" | ");
  1043. return system(strcat(helpstr, xgetenv("PAGER", "less")));
  1044. }
  1045. static int
  1046. sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  1047. {
  1048. if(typeflag == FTW_F || typeflag == FTW_D)
  1049. blk_size += sb->st_blocks;
  1050. return 0;
  1051. }
  1052. static int
  1053. getorder(size_t size)
  1054. {
  1055. switch (size) {
  1056. case 4096:
  1057. return 12;
  1058. case 512:
  1059. return 9;
  1060. case 8192:
  1061. return 13;
  1062. case 16384:
  1063. return 14;
  1064. case 32768:
  1065. return 15;
  1066. case 65536:
  1067. return 16;
  1068. case 131072:
  1069. return 17;
  1070. case 262144:
  1071. return 18;
  1072. case 524288:
  1073. return 19;
  1074. case 1048576:
  1075. return 20;
  1076. case 2048:
  1077. return 11;
  1078. case 1024:
  1079. return 10;
  1080. default:
  1081. return 0;
  1082. }
  1083. }
  1084. static int
  1085. dentfill(char *path, struct entry **dents,
  1086. int (*filter)(regex_t *, char *), regex_t *re)
  1087. {
  1088. static char newpath[PATH_MAX];
  1089. static DIR *dirp;
  1090. static struct dirent *dp;
  1091. static struct stat sb;
  1092. static struct statvfs svb;
  1093. static int r, n;
  1094. r = n = 0;
  1095. dirp = opendir(path);
  1096. if (dirp == NULL)
  1097. return 0;
  1098. while ((dp = readdir(dirp)) != NULL) {
  1099. /* Skip self and parent */
  1100. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  1101. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  1102. continue;
  1103. if (filter(re, dp->d_name) == 0)
  1104. continue;
  1105. if (n == total_dents) {
  1106. total_dents += 64;
  1107. *dents = realloc(*dents, total_dents * sizeof(**dents));
  1108. if (*dents == NULL)
  1109. printerr(1, "realloc");
  1110. }
  1111. xstrlcpy((*dents)[n].name, dp->d_name, NAME_MAX);
  1112. /* Get mode flags */
  1113. mkpath(path, dp->d_name, newpath, PATH_MAX);
  1114. r = lstat(newpath, &sb);
  1115. if (r == -1) {
  1116. if (*dents)
  1117. free(*dents);
  1118. printerr(1, "lstat");
  1119. }
  1120. (*dents)[n].mode = sb.st_mode;
  1121. (*dents)[n].t = sb.st_mtime;
  1122. (*dents)[n].size = sb.st_size;
  1123. if (bsizeorder) {
  1124. if (S_ISDIR(sb.st_mode)) {
  1125. blk_size = 0;
  1126. if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
  1127. printmsg("nftw(3) failed");
  1128. (*dents)[n].bsize = sb.st_blocks;
  1129. } else
  1130. (*dents)[n].bsize = blk_size;
  1131. } else
  1132. (*dents)[n].bsize = sb.st_blocks;
  1133. }
  1134. n++;
  1135. }
  1136. if (bsizeorder) {
  1137. r = statvfs(path, &svb);
  1138. if (r == -1)
  1139. fs_free = 0;
  1140. else
  1141. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  1142. }
  1143. /* Should never be null */
  1144. r = closedir(dirp);
  1145. if (r == -1) {
  1146. if (*dents)
  1147. free(*dents);
  1148. printerr(1, "closedir");
  1149. }
  1150. return n;
  1151. }
  1152. static void
  1153. dentfree(struct entry *dents)
  1154. {
  1155. free(dents);
  1156. }
  1157. /* Return the position of the matching entry or 0 otherwise */
  1158. static int
  1159. dentfind(struct entry *dents, int n, char *path)
  1160. {
  1161. if (!path)
  1162. return 0;
  1163. static int i;
  1164. static char *p;
  1165. p = xmemrchr(path, '/', strlen(path));
  1166. if (!p)
  1167. p = path;
  1168. else
  1169. /* We are assuming an entry with actual
  1170. name ending in '/' will not appear */
  1171. p++;
  1172. DPRINTF_S(p);
  1173. for (i = 0; i < n; i++)
  1174. if (strcmp(p, dents[i].name) == 0)
  1175. return i;
  1176. return 0;
  1177. }
  1178. static int
  1179. populate(char *path, char *oldpath, char *fltr)
  1180. {
  1181. static regex_t re;
  1182. static int r;
  1183. /* Can fail when permissions change while browsing */
  1184. if (canopendir(path) == 0)
  1185. return -1;
  1186. /* Search filter */
  1187. r = setfilter(&re, fltr);
  1188. if (r != 0)
  1189. return -1;
  1190. ndents = dentfill(path, &dents, visible, &re);
  1191. qsort(dents, ndents, sizeof(*dents), entrycmp);
  1192. /* Find cur from history */
  1193. cur = dentfind(dents, ndents, oldpath);
  1194. return 0;
  1195. }
  1196. static void
  1197. redraw(char *path)
  1198. {
  1199. static char cwd[PATH_MAX];
  1200. static int nlines, odd;
  1201. static int i;
  1202. static size_t ncols;
  1203. nlines = MIN(LINES - 4, ndents);
  1204. /* Clean screen */
  1205. erase();
  1206. /* Strip trailing slashes */
  1207. for (i = strlen(path) - 1; i > 0; i--)
  1208. if (path[i] == '/')
  1209. path[i] = '\0';
  1210. else
  1211. break;
  1212. DPRINTF_D(cur);
  1213. DPRINTF_S(path);
  1214. /* No text wrapping in cwd line */
  1215. if (!realpath(path, cwd)) {
  1216. printmsg("Cannot resolve path");
  1217. return;
  1218. }
  1219. ncols = COLS;
  1220. if (ncols > PATH_MAX)
  1221. ncols = PATH_MAX;
  1222. cwd[ncols - strlen(CWD) - 1] = '\0';
  1223. printw(CWD "%s\n\n", cwd);
  1224. /* Print listing */
  1225. odd = ISODD(nlines);
  1226. if (cur < (nlines >> 1)) {
  1227. for (i = 0; i < nlines; i++)
  1228. printptr(&dents[i], i == cur);
  1229. } else if (cur >= ndents - (nlines >> 1)) {
  1230. for (i = ndents - nlines; i < ndents; i++)
  1231. printptr(&dents[i], i == cur);
  1232. } else {
  1233. nlines >>= 1;
  1234. for (i = cur - nlines; i < cur + nlines + odd; i++)
  1235. printptr(&dents[i], i == cur);
  1236. }
  1237. if (showdetail) {
  1238. if (ndents) {
  1239. static char ind[2] = "\0\0";
  1240. static char sort[17];
  1241. if (mtimeorder)
  1242. sprintf(sort, "by time ");
  1243. else if (sizeorder)
  1244. sprintf(sort, "by size ");
  1245. else
  1246. sort[0] = '\0';
  1247. if (S_ISDIR(dents[cur].mode))
  1248. ind[0] = '/';
  1249. else if (S_ISLNK(dents[cur].mode))
  1250. ind[0] = '@';
  1251. else if (S_ISSOCK(dents[cur].mode))
  1252. ind[0] = '=';
  1253. else if (S_ISFIFO(dents[cur].mode))
  1254. ind[0] = '|';
  1255. else if (dents[cur].mode & S_IXUSR)
  1256. ind[0] = '*';
  1257. else
  1258. ind[0] = '\0';
  1259. if (!bsizeorder)
  1260. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  1261. replace_escape(dents[cur].name), ind);
  1262. else
  1263. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  1264. ndents, coolsize(fs_free),
  1265. replace_escape(dents[cur].name), ind);
  1266. printmsg(cwd);
  1267. } else
  1268. printmsg("0 items");
  1269. }
  1270. }
  1271. static void
  1272. browse(char *ipath, char *ifilter)
  1273. {
  1274. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1275. static char lastdir[PATH_MAX];
  1276. static char fltr[LINE_MAX];
  1277. char *mime, *dir, *tmp, *run, *env;
  1278. struct stat sb;
  1279. int r, fd, filtered = FALSE;
  1280. enum action sel = SEL_RUNARG + 1;
  1281. xstrlcpy(path, ipath, PATH_MAX);
  1282. xstrlcpy(fltr, ifilter, LINE_MAX);
  1283. oldpath[0] = '\0';
  1284. newpath[0] = '\0';
  1285. lastdir[0] = '\0'; /* Can't move back from initial directory */
  1286. begin:
  1287. if (populate(path, oldpath, fltr) == -1) {
  1288. printwarn();
  1289. goto nochange;
  1290. }
  1291. for (;;) {
  1292. redraw(path);
  1293. nochange:
  1294. /* Exit if parent has exited */
  1295. if (getppid() == 1)
  1296. _exit(0);
  1297. sel = nextsel(&run, &env, &filtered);
  1298. switch (sel) {
  1299. case SEL_CDQUIT:
  1300. {
  1301. char *tmpfile = "/tmp/nnn";
  1302. if ((tmp = getenv("NNN_TMPFILE")) != NULL)
  1303. tmpfile = tmp;
  1304. FILE *fp = fopen(tmpfile, "w");
  1305. if (fp) {
  1306. fprintf(fp, "cd \"%s\"", path);
  1307. fclose(fp);
  1308. }
  1309. }
  1310. case SEL_QUIT:
  1311. dentfree(dents);
  1312. return;
  1313. case SEL_BACK:
  1314. /* There is no going back */
  1315. if (strcmp(path, "/") == 0 ||
  1316. strchr(path, '/') == NULL) {
  1317. printmsg("You are at /");
  1318. goto nochange;
  1319. }
  1320. dir = xdirname(path);
  1321. if (canopendir(dir) == 0) {
  1322. printwarn();
  1323. goto nochange;
  1324. }
  1325. /* Save history */
  1326. xstrlcpy(oldpath, path, PATH_MAX);
  1327. /* Save last working directory */
  1328. xstrlcpy(lastdir, path, PATH_MAX);
  1329. xstrlcpy(path, dir, PATH_MAX);
  1330. /* Reset filter */
  1331. xstrlcpy(fltr, ifilter, LINE_MAX);
  1332. goto begin;
  1333. case SEL_GOIN:
  1334. /* Cannot descend in empty directories */
  1335. if (ndents == 0)
  1336. goto begin;
  1337. mkpath(path, dents[cur].name, newpath, PATH_MAX);
  1338. DPRINTF_S(newpath);
  1339. /* Get path info */
  1340. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1341. if (fd == -1) {
  1342. printwarn();
  1343. goto nochange;
  1344. }
  1345. r = fstat(fd, &sb);
  1346. if (r == -1) {
  1347. printwarn();
  1348. close(fd);
  1349. goto nochange;
  1350. }
  1351. close(fd);
  1352. DPRINTF_U(sb.st_mode);
  1353. switch (sb.st_mode & S_IFMT) {
  1354. case S_IFDIR:
  1355. if (canopendir(newpath) == 0) {
  1356. printwarn();
  1357. goto nochange;
  1358. }
  1359. /* Save last working directory */
  1360. xstrlcpy(lastdir, path, PATH_MAX);
  1361. xstrlcpy(path, newpath, PATH_MAX);
  1362. oldpath[0] = '\0';
  1363. /* Reset filter */
  1364. xstrlcpy(fltr, ifilter, LINE_MAX);
  1365. goto begin;
  1366. case S_IFREG:
  1367. {
  1368. static char cmd[MAX_CMD_LEN];
  1369. /* If NNN_OPENER is set, use it */
  1370. if (opener) {
  1371. spawn(opener, newpath, NULL, NULL, 4);
  1372. continue;
  1373. }
  1374. /* Play with nlay if identified */
  1375. mime = getmime(dents[cur].name);
  1376. if (mime) {
  1377. exitcurses();
  1378. if (player)
  1379. spawn(player, newpath, mime, NULL, 0);
  1380. else
  1381. spawn("nlay", newpath, mime, NULL, 0);
  1382. initcurses();
  1383. continue;
  1384. }
  1385. /* If nlay doesn't handle it, open plain text
  1386. files with vi, then try NNN_FALLBACK_OPENER */
  1387. strcpy(cmd, "file -bi \'");
  1388. xstrlcpy(cmd + strlen(cmd), newpath, MAX_CMD_LEN - strlen(cmd));
  1389. strcat(cmd, "\'");
  1390. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1391. continue;
  1392. if (strstr(cmd, "text/") == cmd) {
  1393. exitcurses();
  1394. run = xgetenv("EDITOR", "vi");
  1395. spawn(run, newpath, NULL, NULL, 0);
  1396. initcurses();
  1397. continue;
  1398. } else if (fb_opener) {
  1399. spawn(fb_opener, newpath, NULL, NULL, 4);
  1400. continue;
  1401. }
  1402. printmsg("No association");
  1403. goto nochange;
  1404. }
  1405. default:
  1406. printmsg("Unsupported file");
  1407. goto nochange;
  1408. }
  1409. case SEL_FLTR:
  1410. filtered = readln(path);
  1411. xstrlcpy(fltr, ifilter, LINE_MAX);
  1412. DPRINTF_S(fltr);
  1413. /* Save current */
  1414. if (ndents > 0)
  1415. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1416. goto nochange;
  1417. case SEL_NEXT:
  1418. if (cur < ndents - 1)
  1419. cur++;
  1420. else if (ndents)
  1421. /* Roll over, set cursor to first entry */
  1422. cur = 0;
  1423. break;
  1424. case SEL_PREV:
  1425. if (cur > 0)
  1426. cur--;
  1427. else if (ndents)
  1428. /* Roll over, set cursor to last entry */
  1429. cur = ndents - 1;
  1430. break;
  1431. case SEL_PGDN:
  1432. if (cur < ndents - 1)
  1433. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1434. break;
  1435. case SEL_PGUP:
  1436. if (cur > 0)
  1437. cur -= MIN((LINES - 4) / 2, cur);
  1438. break;
  1439. case SEL_HOME:
  1440. cur = 0;
  1441. break;
  1442. case SEL_END:
  1443. cur = ndents - 1;
  1444. break;
  1445. case SEL_CD:
  1446. {
  1447. static char *tmp, *input;
  1448. static int truecd;
  1449. /* Save the program start dir */
  1450. tmp = getcwd(newpath, PATH_MAX);
  1451. if (tmp == NULL) {
  1452. printwarn();
  1453. goto nochange;
  1454. }
  1455. /* Switch to current path for readline(3) */
  1456. if (chdir(path) == -1) {
  1457. printwarn();
  1458. goto nochange;
  1459. }
  1460. exitcurses();
  1461. tmp = readline("chdir: ");
  1462. initcurses();
  1463. /* Change back to program start dir */
  1464. if (chdir(newpath) == -1)
  1465. printwarn();
  1466. if (tmp[0] == '\0')
  1467. break;
  1468. else
  1469. /* Add to readline(3) history */
  1470. add_history(tmp);
  1471. input = tmp;
  1472. tmp = strstrip(tmp);
  1473. if (tmp[0] == '\0') {
  1474. free(input);
  1475. break;
  1476. }
  1477. truecd = 0;
  1478. if (tmp[0] == '~') {
  1479. /* Expand ~ to HOME absolute path */
  1480. char *home = getenv("HOME");
  1481. if (home)
  1482. snprintf(newpath, PATH_MAX, "%s%s", home, tmp + 1);
  1483. else {
  1484. free(input);
  1485. break;
  1486. }
  1487. } else if (tmp[0] == '-' && tmp[1] == '\0') {
  1488. if (lastdir[0] == '\0') {
  1489. free(input);
  1490. break;
  1491. }
  1492. /* Switch to last visited dir */
  1493. xstrlcpy(newpath, lastdir, PATH_MAX);
  1494. truecd = 1;
  1495. } else if ((r = all_dots(tmp))) {
  1496. if (r == 1) {
  1497. /* Always in the current dir */
  1498. free(input);
  1499. break;
  1500. }
  1501. r--;
  1502. dir = path;
  1503. for (fd = 0; fd < r; fd++) {
  1504. /* Reached / ? */
  1505. if (strcmp(path, "/") == 0 ||
  1506. strchr(path, '/') == NULL) {
  1507. /* If it's a cd .. at / */
  1508. if (fd == 0) {
  1509. printmsg("You are at /");
  1510. free(input);
  1511. goto nochange;
  1512. }
  1513. /* Can't cd beyond / anyway */
  1514. break;
  1515. } else {
  1516. dir = xdirname(dir);
  1517. if (canopendir(dir) == 0) {
  1518. printwarn();
  1519. free(input);
  1520. goto nochange;
  1521. }
  1522. }
  1523. }
  1524. truecd = 1;
  1525. /* Save the path in case of cd ..
  1526. We mark the current dir in parent dir */
  1527. if (r == 1) {
  1528. xstrlcpy(oldpath, path, PATH_MAX);
  1529. truecd = 2;
  1530. }
  1531. xstrlcpy(newpath, dir, PATH_MAX);
  1532. } else
  1533. mkpath(path, tmp, newpath, PATH_MAX);
  1534. if (canopendir(newpath) == 0) {
  1535. printwarn();
  1536. free(input);
  1537. break;
  1538. }
  1539. if (truecd == 0) {
  1540. /* Probable change in dir */
  1541. /* No-op if it's the same directory */
  1542. if (strcmp(path, newpath) == 0) {
  1543. free(input);
  1544. break;
  1545. }
  1546. oldpath[0] = '\0';
  1547. } else if (truecd == 1)
  1548. /* Sure change in dir */
  1549. oldpath[0] = '\0';
  1550. /* Save last working directory */
  1551. xstrlcpy(lastdir, path, PATH_MAX);
  1552. /* Save the newly opted dir in path */
  1553. xstrlcpy(path, newpath, PATH_MAX);
  1554. /* Reset filter */
  1555. xstrlcpy(fltr, ifilter, LINE_MAX);
  1556. DPRINTF_S(path);
  1557. free(input);
  1558. goto begin;
  1559. }
  1560. case SEL_CDHOME:
  1561. tmp = getenv("HOME");
  1562. if (tmp == NULL) {
  1563. clearprompt();
  1564. goto nochange;
  1565. }
  1566. if (canopendir(tmp) == 0) {
  1567. printwarn();
  1568. goto nochange;
  1569. }
  1570. if (strcmp(path, tmp) == 0)
  1571. break;
  1572. /* Save last working directory */
  1573. xstrlcpy(lastdir, path, PATH_MAX);
  1574. xstrlcpy(path, tmp, PATH_MAX);
  1575. oldpath[0] = '\0';
  1576. /* Reset filter */
  1577. xstrlcpy(fltr, ifilter, LINE_MAX);
  1578. DPRINTF_S(path);
  1579. goto begin;
  1580. case SEL_CDBEGIN:
  1581. if (canopendir(ipath) == 0) {
  1582. printwarn();
  1583. goto nochange;
  1584. }
  1585. if (strcmp(path, ipath) == 0)
  1586. break;
  1587. /* Save last working directory */
  1588. xstrlcpy(lastdir, path, PATH_MAX);
  1589. xstrlcpy(path, ipath, PATH_MAX);
  1590. oldpath[0] = '\0';
  1591. /* Reset filter */
  1592. xstrlcpy(fltr, ifilter, LINE_MAX);
  1593. DPRINTF_S(path);
  1594. goto begin;
  1595. case SEL_CDLAST:
  1596. if (lastdir[0] == '\0')
  1597. break;
  1598. if (canopendir(lastdir) == 0) {
  1599. printwarn();
  1600. goto nochange;
  1601. }
  1602. xstrlcpy(newpath, lastdir, PATH_MAX);
  1603. xstrlcpy(lastdir, path, PATH_MAX);
  1604. xstrlcpy(path, newpath, PATH_MAX);
  1605. oldpath[0] = '\0';
  1606. /* Reset filter */
  1607. xstrlcpy(fltr, ifilter, LINE_MAX);
  1608. DPRINTF_S(path);
  1609. goto begin;
  1610. case SEL_TOGGLEDOT:
  1611. showhidden ^= 1;
  1612. initfilter(showhidden, &ifilter);
  1613. xstrlcpy(fltr, ifilter, LINE_MAX);
  1614. goto begin;
  1615. case SEL_DETAIL:
  1616. showdetail = !showdetail;
  1617. showdetail ? (printptr = &printent_long)
  1618. : (printptr = &printent);
  1619. /* Save current */
  1620. if (ndents > 0)
  1621. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1622. goto begin;
  1623. case SEL_STATS:
  1624. {
  1625. struct stat sb;
  1626. if (ndents > 0)
  1627. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1628. r = lstat(oldpath, &sb);
  1629. if (r == -1) {
  1630. if (dents)
  1631. dentfree(dents);
  1632. printerr(1, "lstat");
  1633. } else {
  1634. exitcurses();
  1635. r = show_stats(oldpath, dents[cur].name, &sb);
  1636. initcurses();
  1637. if (r < 0) {
  1638. printmsg(strerror(errno));
  1639. goto nochange;
  1640. }
  1641. }
  1642. break;
  1643. }
  1644. case SEL_MEDIA:
  1645. if (ndents > 0)
  1646. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1647. exitcurses();
  1648. r = show_mediainfo(oldpath, FALSE);
  1649. initcurses();
  1650. if (r < 0) {
  1651. printmsg("mediainfo missing");
  1652. goto nochange;
  1653. }
  1654. break;
  1655. case SEL_FMEDIA:
  1656. if (ndents > 0)
  1657. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1658. exitcurses();
  1659. r = show_mediainfo(oldpath, TRUE);
  1660. initcurses();
  1661. if (r < 0) {
  1662. printmsg("mediainfo missing");
  1663. goto nochange;
  1664. }
  1665. break;
  1666. case SEL_DFB:
  1667. if (!desktop_manager) {
  1668. printmsg("NNN_DE_FILE_MANAGER not set");
  1669. goto nochange;
  1670. }
  1671. spawn(desktop_manager, path, NULL, path, 2);
  1672. break;
  1673. case SEL_FSIZE:
  1674. sizeorder = !sizeorder;
  1675. mtimeorder = 0;
  1676. bsizeorder = 0;
  1677. /* Save current */
  1678. if (ndents > 0)
  1679. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1680. goto begin;
  1681. case SEL_BSIZE:
  1682. bsizeorder = !bsizeorder;
  1683. if (bsizeorder) {
  1684. showdetail = 1;
  1685. printptr = &printent_long;
  1686. }
  1687. mtimeorder = 0;
  1688. sizeorder = 0;
  1689. /* Save current */
  1690. if (ndents > 0)
  1691. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1692. goto begin;
  1693. case SEL_MTIME:
  1694. mtimeorder = !mtimeorder;
  1695. sizeorder = 0;
  1696. bsizeorder = 0;
  1697. /* Save current */
  1698. if (ndents > 0)
  1699. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1700. goto begin;
  1701. case SEL_REDRAW:
  1702. /* Save current */
  1703. if (ndents > 0)
  1704. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1705. goto begin;
  1706. case SEL_COPY:
  1707. if (copier && ndents) {
  1708. if (strcmp(path, "/") == 0)
  1709. snprintf(newpath, PATH_MAX, "/%s",
  1710. dents[cur].name);
  1711. else
  1712. snprintf(newpath, PATH_MAX, "%s/%s",
  1713. path, dents[cur].name);
  1714. spawn(copier, newpath, NULL, NULL, 0);
  1715. printmsg(newpath);
  1716. } else if (!copier)
  1717. printmsg("NNN_COPIER is not set");
  1718. goto nochange;
  1719. case SEL_HELP:
  1720. exitcurses();
  1721. show_help();
  1722. initcurses();
  1723. break;
  1724. case SEL_RUN:
  1725. run = xgetenv(env, run);
  1726. exitcurses();
  1727. spawn(run, NULL, NULL, path, 1);
  1728. initcurses();
  1729. /* Repopulate as directory content may have changed */
  1730. goto begin;
  1731. case SEL_RUNARG:
  1732. run = xgetenv(env, run);
  1733. exitcurses();
  1734. spawn(run, dents[cur].name, NULL, path, 0);
  1735. initcurses();
  1736. break;
  1737. }
  1738. /* Screensaver */
  1739. if (idletimeout != 0 && idle == idletimeout) {
  1740. idle = 0;
  1741. exitcurses();
  1742. spawn(idlecmd, NULL, NULL, NULL, 0);
  1743. initcurses();
  1744. }
  1745. }
  1746. }
  1747. static void
  1748. usage(void)
  1749. {
  1750. fprintf(stdout, "usage: nnn [-d] [-S] [-v] [h] [PATH]\n\n\
  1751. The missing terminal file browser for X.\n\n\
  1752. positional arguments:\n\
  1753. PATH directory to open [default: current dir]\n\n\
  1754. optional arguments:\n\
  1755. -d start in detail view mode\n\
  1756. -p path to custom nlay\n\
  1757. -S start in disk usage analyzer mode\n\
  1758. -v show program version and exit\n\
  1759. -h show this help and exit\n\n\
  1760. Version: %s\n\
  1761. License: BSD 2-Clause\n\
  1762. Webpage: https://github.com/jarun/nnn\n", VERSION);
  1763. exit(0);
  1764. }
  1765. int
  1766. main(int argc, char *argv[])
  1767. {
  1768. char cwd[PATH_MAX], *ipath;
  1769. char *ifilter;
  1770. int opt = 0;
  1771. /* Confirm we are in a terminal */
  1772. if (!isatty(0) || !isatty(1)) {
  1773. fprintf(stderr, "stdin or stdout is not a tty\n");
  1774. exit(1);
  1775. }
  1776. if (argc > 3)
  1777. usage();
  1778. while ((opt = getopt(argc, argv, "dSp:vh")) != -1) {
  1779. switch (opt) {
  1780. case 'S':
  1781. bsizeorder = 1;
  1782. case 'd':
  1783. /* Open in detail mode, if set */
  1784. showdetail = 1;
  1785. printptr = &printent_long;
  1786. break;
  1787. case 'p':
  1788. player = optarg;
  1789. break;
  1790. case 'v':
  1791. fprintf(stdout, "%s\n", VERSION);
  1792. return 0;
  1793. case 'h':
  1794. default:
  1795. usage();
  1796. }
  1797. }
  1798. if (argc == optind) {
  1799. /* Start in the current directory */
  1800. ipath = getcwd(cwd, PATH_MAX);
  1801. if (ipath == NULL)
  1802. ipath = "/";
  1803. } else {
  1804. ipath = realpath(argv[optind], cwd);
  1805. if (!ipath) {
  1806. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1807. exit(1);
  1808. }
  1809. }
  1810. open_max = max_openfds();
  1811. if (getuid() == 0)
  1812. showhidden = 1;
  1813. initfilter(showhidden, &ifilter);
  1814. /* Get the default desktop mime opener, if set */
  1815. opener = getenv("NNN_OPENER");
  1816. /* Get the fallback desktop mime opener, if set */
  1817. fb_opener = getenv("NNN_FALLBACK_OPENER");
  1818. /* Get the desktop file browser, if set */
  1819. desktop_manager = getenv("NNN_DE_FILE_MANAGER");
  1820. /* Get the default copier, if set */
  1821. copier = getenv("NNN_COPIER");
  1822. signal(SIGINT, SIG_IGN);
  1823. /* Test initial path */
  1824. if (canopendir(ipath) == 0) {
  1825. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1826. exit(1);
  1827. }
  1828. /* Set locale */
  1829. setlocale(LC_ALL, "");
  1830. initcurses();
  1831. browse(ipath, ifilter);
  1832. exitcurses();
  1833. exit(0);
  1834. }