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.

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