My build of nnn with minor changes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

9 anos atrás
10 anos atrás
9 anos atrás
9 anos atrás
10 anos atrás
10 anos atrás
8 anos atrás
7 anos atrás
7 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
10 anos atrás
8 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
10 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
9 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
7 anos atrás
10 anos atrás
10 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
7 anos atrás
8 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
8 anos atrás
7 anos atrás
7 anos atrás
8 anos atrás
8 anos atrás
7 anos atrás
8 anos atrás
7 anos atrás
8 anos atrás
8 anos atrás
10 anos atrás
10 anos atrás
8 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. }
  413. static void
  414. exitcurses(void)
  415. {
  416. endwin(); /* Restore terminal */
  417. }
  418. /* Messages show up at the bottom */
  419. static void
  420. printmsg(char *msg)
  421. {
  422. move(LINES - 1, 0);
  423. printw("%s\n", msg);
  424. }
  425. /* Display warning as a message */
  426. static void
  427. printwarn(void)
  428. {
  429. printmsg(strerror(errno));
  430. }
  431. /* Kill curses and display error before exiting */
  432. static void
  433. printerr(int ret, char *prefix)
  434. {
  435. exitcurses();
  436. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  437. exit(ret);
  438. }
  439. /* Clear the last line */
  440. static void
  441. clearprompt(void)
  442. {
  443. printmsg("");
  444. }
  445. /* Print prompt on the last line */
  446. static void
  447. printprompt(char *str)
  448. {
  449. clearprompt();
  450. printw(str);
  451. }
  452. /* Returns SEL_* if key is bound and 0 otherwise.
  453. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  454. static int
  455. nextsel(char **run, char **env)
  456. {
  457. int c;
  458. unsigned int i;
  459. c = getch();
  460. if (c == -1)
  461. idle++;
  462. else
  463. idle = 0;
  464. for (i = 0; i < LEN(bindings); i++)
  465. if (c == bindings[i].sym) {
  466. *run = bindings[i].run;
  467. *env = bindings[i].env;
  468. return bindings[i].act;
  469. }
  470. return 0;
  471. }
  472. static char *
  473. readln(void)
  474. {
  475. static char ln[LINE_MAX];
  476. timeout(-1);
  477. echo();
  478. curs_set(TRUE);
  479. memset(ln, 0, sizeof(ln));
  480. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  481. noecho();
  482. curs_set(FALSE);
  483. timeout(1000);
  484. return ln[0] ? ln : NULL;
  485. }
  486. static int
  487. canopendir(char *path)
  488. {
  489. static DIR *dirp;
  490. dirp = opendir(path);
  491. if (dirp == NULL)
  492. return 0;
  493. closedir(dirp);
  494. return 1;
  495. }
  496. /*
  497. * Returns "dir/name or "/name"
  498. */
  499. static char *
  500. mkpath(char *dir, char *name, char *out, size_t n)
  501. {
  502. /* Handle absolute path */
  503. if (name[0] == '/')
  504. xstrlcpy(out, name, n);
  505. else {
  506. /* Handle root case */
  507. if (strcmp(dir, "/") == 0)
  508. snprintf(out, n, "/%s", name);
  509. else
  510. snprintf(out, n, "%s/%s", dir, name);
  511. }
  512. return out;
  513. }
  514. static void
  515. printent(struct entry *ent, int active)
  516. {
  517. if (S_ISDIR(ent->mode))
  518. printw("%s%s/\n", CURSYM(active), ent->name);
  519. else if (S_ISLNK(ent->mode))
  520. printw("%s%s@\n", CURSYM(active), ent->name);
  521. else if (S_ISSOCK(ent->mode))
  522. printw("%s%s=\n", CURSYM(active), ent->name);
  523. else if (S_ISFIFO(ent->mode))
  524. printw("%s%s|\n", CURSYM(active), ent->name);
  525. else if (ent->mode & S_IXUSR)
  526. printw("%s%s*\n", CURSYM(active), ent->name);
  527. else
  528. printw("%s%s\n", CURSYM(active), ent->name);
  529. }
  530. static void (*printptr)(struct entry *ent, int active) = &printent;
  531. static char*
  532. coolsize(off_t size)
  533. {
  534. static char size_buf[12]; /* Buffer to hold human readable size */
  535. static int i;
  536. static off_t fsize, tmp;
  537. static long double rem;
  538. i = 0;
  539. fsize = size;
  540. rem = 0;
  541. while (fsize > 1024) {
  542. tmp = fsize;
  543. //fsize *= div_2_pow_10;
  544. fsize >>= 10;
  545. rem = tmp - (fsize << 10);
  546. i++;
  547. }
  548. snprintf(size_buf, 12, "%.*Lf%s", i, fsize + rem * div_2_pow_10, size_units[i]);
  549. return size_buf;
  550. }
  551. static void
  552. printent_long(struct entry *ent, int active)
  553. {
  554. static char buf[18];
  555. strftime(buf, 18, "%b %d %H:%M %Y", localtime(&ent->t));
  556. if (active)
  557. attron(A_REVERSE);
  558. if (!bsizeorder) {
  559. if (S_ISDIR(ent->mode))
  560. printw("%s%-17.17s / %s/\n",
  561. CURSYM(active), buf, ent->name);
  562. else if (S_ISLNK(ent->mode))
  563. printw("%s%-17.17s @ %s@\n",
  564. CURSYM(active), buf, ent->name);
  565. else if (S_ISSOCK(ent->mode))
  566. printw("%s%-17.17s = %s=\n",
  567. CURSYM(active), buf, ent->name);
  568. else if (S_ISFIFO(ent->mode))
  569. printw("%s%-17.17s | %s|\n",
  570. CURSYM(active), buf, ent->name);
  571. else if (S_ISBLK(ent->mode))
  572. printw("%s%-17.17s b %s\n",
  573. CURSYM(active), buf, ent->name);
  574. else if (S_ISCHR(ent->mode))
  575. printw("%s%-17.17s c %s\n",
  576. CURSYM(active), buf, ent->name);
  577. else if (ent->mode & S_IXUSR)
  578. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  579. buf, coolsize(ent->size), ent->name);
  580. else
  581. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  582. buf, coolsize(ent->size), ent->name);
  583. } else {
  584. if (S_ISDIR(ent->mode))
  585. printw("%s%-17.17s %8.8s/ %s/\n", CURSYM(active),
  586. buf, coolsize(ent->bsize << 9), ent->name);
  587. else if (S_ISLNK(ent->mode))
  588. printw("%s%-17.17s @ %s@\n",
  589. CURSYM(active), buf, ent->name);
  590. else if (S_ISSOCK(ent->mode))
  591. printw("%s%-17.17s = %s=\n",
  592. CURSYM(active), buf, ent->name);
  593. else if (S_ISFIFO(ent->mode))
  594. printw("%s%-17.17s | %s|\n",
  595. CURSYM(active), buf, ent->name);
  596. else if (S_ISBLK(ent->mode))
  597. printw("%s%-17.17s b %s\n",
  598. CURSYM(active), buf, ent->name);
  599. else if (S_ISCHR(ent->mode))
  600. printw("%s%-17.17s c %s\n",
  601. CURSYM(active), buf, ent->name);
  602. else if (ent->mode & S_IXUSR)
  603. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  604. buf, coolsize(ent->bsize << 9), ent->name);
  605. else
  606. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  607. buf, coolsize(ent->bsize << 9), ent->name);
  608. }
  609. if (active)
  610. attroff(A_REVERSE);
  611. }
  612. static char
  613. get_fileind(mode_t mode, char *desc)
  614. {
  615. static char c;
  616. if (S_ISREG(mode)) {
  617. c = '-';
  618. sprintf(desc, "%s", "regular file");
  619. if (mode & S_IXUSR)
  620. strcat(desc, ", executable");
  621. } else if (S_ISDIR(mode)) {
  622. c = 'd';
  623. sprintf(desc, "%s", "directory");
  624. } else if (S_ISBLK(mode)) {
  625. c = 'b';
  626. sprintf(desc, "%s", "block special device");
  627. } else if (S_ISCHR(mode)) {
  628. c = 'c';
  629. sprintf(desc, "%s", "character special device");
  630. #ifdef S_ISFIFO
  631. } else if (S_ISFIFO(mode)) {
  632. c = 'p';
  633. sprintf(desc, "%s", "FIFO");
  634. #endif /* S_ISFIFO */
  635. #ifdef S_ISLNK
  636. } else if (S_ISLNK(mode)) {
  637. c = 'l';
  638. sprintf(desc, "%s", "symbolic link");
  639. #endif /* S_ISLNK */
  640. #ifdef S_ISSOCK
  641. } else if (S_ISSOCK(mode)) {
  642. c = 's';
  643. sprintf(desc, "%s", "socket");
  644. #endif /* S_ISSOCK */
  645. #ifdef S_ISDOOR
  646. /* Solaris 2.6, etc. */
  647. } else if (S_ISDOOR(mode)) {
  648. c = 'D';
  649. desc[0] = '\0';
  650. #endif /* S_ISDOOR */
  651. } else {
  652. /* Unknown type -- possibly a regular file? */
  653. c = '?';
  654. desc[0] = '\0';
  655. }
  656. return(c);
  657. }
  658. /* Convert a mode field into "ls -l" type perms field. */
  659. static char *
  660. get_lsperms(mode_t mode, char *desc)
  661. {
  662. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  663. "r--", "r-x", "rw-", "rwx"};
  664. static char bits[11];
  665. bits[0] = get_fileind(mode, desc);
  666. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  667. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  668. strcpy(&bits[7], rwx[(mode & 7)]);
  669. if (mode & S_ISUID)
  670. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  671. if (mode & S_ISGID)
  672. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  673. if (mode & S_ISVTX)
  674. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  675. bits[10] = '\0';
  676. return(bits);
  677. }
  678. static char *
  679. get_output(char *buf, size_t bytes)
  680. {
  681. char *ret;
  682. FILE *pf = popen(buf, "r");
  683. if (pf) {
  684. ret = fgets(buf, bytes, pf);
  685. pclose(pf);
  686. return ret;
  687. }
  688. return NULL;
  689. }
  690. /*
  691. * Follows the stat(1) output closely
  692. */
  693. static void
  694. show_stats(char* fpath, char* fname, struct stat *sb)
  695. {
  696. char buf[PATH_MAX + 48];
  697. char *perms = get_lsperms(sb->st_mode, buf);
  698. char *p, *begin = buf;
  699. clear();
  700. /* Show file name or 'symlink' -> 'target' */
  701. if (perms[0] == 'l') {
  702. char symtgt[PATH_MAX];
  703. ssize_t len = readlink(fpath, symtgt, PATH_MAX);
  704. if (len != -1) {
  705. symtgt[len] = '\0';
  706. printw("\n\n File: '%s' -> '%s'", fname, symtgt);
  707. }
  708. } else
  709. printw("\n File: '%s'", fname);
  710. /* Show size, blocks, file type */
  711. printw("\n Size: %-15llu Blocks: %-10llu IO Block: %-6llu %s",
  712. sb->st_size, sb->st_blocks, sb->st_blksize, buf);
  713. /* Show containing device, inode, hardlink count */
  714. sprintf(buf, "%lxh/%lud", (ulong)sb->st_dev, (ulong)sb->st_dev);
  715. printw("\n Device: %-15s Inode: %-11lu Links: %-9lu",
  716. buf, sb->st_ino, sb->st_nlink);
  717. /* Show major, minor number for block or char device */
  718. if (perms[0] == 'b' || perms[0] == 'c')
  719. printw(" Device type: %lx,%lx",
  720. major(sb->st_rdev), minor(sb->st_rdev));
  721. /* Show permissions, owner, group */
  722. printw("\n Access: 0%d%d%d/%s Uid: (%lu/%s) Gid: (%lu/%s)",
  723. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  724. perms,
  725. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  726. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  727. /* Show last access time */
  728. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  729. printw("\n\n Access: %s", buf);
  730. /* Show last modification time */
  731. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  732. printw("\n Modify: %s", buf);
  733. /* Show last status change time */
  734. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  735. printw("\n Change: %s", buf);
  736. if (S_ISREG(sb->st_mode)) {
  737. /* Show file(1) output */
  738. sprintf(buf, "file -b \"%s\" 2>&1", fpath);
  739. p = get_output(buf, PATH_MAX + 48);
  740. if (p) {
  741. printw("\n\n ");
  742. while (*p) {
  743. if (*p == ',') {
  744. *p = '\0';
  745. printw(" %s\n", begin);
  746. begin = p + 1;
  747. }
  748. p++;
  749. }
  750. printw(" %s", begin);
  751. }
  752. #ifdef SUPPORT_CHKSUM
  753. /* Calculating checksums can take VERY long */
  754. /* Show md5 */
  755. sprintf(buf, "openssl md5 \"%s\" 2>&1", fpath);
  756. p = get_output(buf, PATH_MAX + 48);
  757. if (p) {
  758. p = xmemrchr(buf, ' ', strlen(buf));
  759. if (!p)
  760. p = buf;
  761. else
  762. p++;
  763. printw("\n md5: %s", p);
  764. }
  765. /* Show sha256 */
  766. sprintf(buf, "openssl sha256 \"%s\" 2>&1", fpath);
  767. p = get_output(buf, PATH_MAX + 48);
  768. if (p) {
  769. p = xmemrchr(buf, ' ', strlen(buf));
  770. if (!p)
  771. p = buf;
  772. else
  773. p++;
  774. printw(" sha256: %s", p);
  775. }
  776. #endif
  777. }
  778. /* Show exit keys */
  779. printw("\n\n << (D)");
  780. while ((*buf = getch()) != 'D');
  781. return;
  782. }
  783. static void
  784. show_help(void)
  785. {
  786. char c;
  787. clear();
  788. printw("\n\
  789. << Key >> << Function >>\n\n\
  790. [Up], k, ^P Previous entry\n\
  791. [Down], j, ^N Next entry\n\
  792. [PgUp], ^U Scroll half page up\n\
  793. [PgDn], ^D Scroll half page down\n\
  794. [Home], g, ^, ^A Jump to first entry\n\
  795. [End], G, $, ^E Jump to last entry\n\
  796. [Right], [Enter], l, ^M Open file or enter dir\n\
  797. [Left], [Backspace], h, ^H Go to parent dir\n\
  798. ~ Jump to HOME dir\n\
  799. - Jump to last visited dir\n\
  800. o Open dir in desktop file manager\n\
  801. /, & Filter dir contents\n\
  802. c Show change dir prompt\n\
  803. d Toggle detail view\n\
  804. D Toggle current file details screen\n\
  805. . Toggle hide .dot files\n\
  806. s Toggle sort by file size\n\
  807. S Toggle disk usage analyzer mode\n\
  808. t Toggle sort by modified time\n\
  809. ! Spawn SHELL in PWD (fallback sh)\n\
  810. z Run top\n\
  811. e Edit entry in EDITOR (fallback vi)\n\
  812. p Open entry in PAGER (fallback less)\n\
  813. ^K Invoke file name copier\n\
  814. ^L Force a redraw\n\
  815. ? Toggle help screen\n\
  816. q Quit\n");
  817. /* Show exit keys */
  818. printw("\n\n << (?)");
  819. while ((c = getch()) != '?');
  820. return;
  821. }
  822. static int
  823. sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  824. {
  825. /* Handle permission problems */
  826. if(typeflag == FTW_NS) {
  827. printmsg("No stats (permissions ?)");
  828. return 0;
  829. }
  830. blk_size += sb->st_blocks;
  831. return 0;
  832. }
  833. static int
  834. getorder(size_t size)
  835. {
  836. switch (size) {
  837. case 4096:
  838. return 12;
  839. case 512:
  840. return 9;
  841. case 8192:
  842. return 13;
  843. case 16384:
  844. return 14;
  845. case 32768:
  846. return 15;
  847. case 65536:
  848. return 16;
  849. case 131072:
  850. return 17;
  851. case 262144:
  852. return 18;
  853. case 524288:
  854. return 19;
  855. case 1048576:
  856. return 20;
  857. case 2048:
  858. return 11;
  859. case 1024:
  860. return 10;
  861. default:
  862. return 0;
  863. }
  864. }
  865. static int
  866. dentfill(char *path, struct entry **dents,
  867. int (*filter)(regex_t *, char *), regex_t *re)
  868. {
  869. static char newpath[PATH_MAX];
  870. static DIR *dirp;
  871. static struct dirent *dp;
  872. static struct stat sb;
  873. static struct statvfs svb;
  874. static int r, n;
  875. r = n = 0;
  876. dirp = opendir(path);
  877. if (dirp == NULL)
  878. return 0;
  879. while ((dp = readdir(dirp)) != NULL) {
  880. /* Skip self and parent */
  881. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  882. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  883. continue;
  884. if (filter(re, dp->d_name) == 0)
  885. continue;
  886. if (((n >> 5) << 5) == n) {
  887. *dents = realloc(*dents, (n + 32) * sizeof(**dents));
  888. if (*dents == NULL)
  889. printerr(1, "realloc");
  890. }
  891. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  892. /* Get mode flags */
  893. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  894. r = lstat(newpath, &sb);
  895. if (r == -1)
  896. printerr(1, "lstat");
  897. (*dents)[n].mode = sb.st_mode;
  898. (*dents)[n].t = sb.st_mtime;
  899. (*dents)[n].size = sb.st_size;
  900. if (bsizeorder) {
  901. if (S_ISDIR(sb.st_mode)) {
  902. blk_size = 0;
  903. if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
  904. printmsg("nftw(3) failed");
  905. (*dents)[n].bsize = sb.st_blocks;
  906. } else
  907. (*dents)[n].bsize = blk_size;
  908. } else
  909. (*dents)[n].bsize = sb.st_blocks;
  910. }
  911. n++;
  912. }
  913. if (bsizeorder) {
  914. r = statvfs(path, &svb);
  915. if (r == -1)
  916. fs_free = 0;
  917. else
  918. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  919. }
  920. /* Should never be null */
  921. r = closedir(dirp);
  922. if (r == -1)
  923. printerr(1, "closedir");
  924. return n;
  925. }
  926. static void
  927. dentfree(struct entry *dents)
  928. {
  929. free(dents);
  930. }
  931. /* Return the position of the matching entry or 0 otherwise */
  932. static int
  933. dentfind(struct entry *dents, int n, char *path)
  934. {
  935. if (!path)
  936. return 0;
  937. static int i;
  938. static char *p;
  939. p = xmemrchr(path, '/', strlen(path));
  940. if (!p)
  941. p = path;
  942. else
  943. /* We are assuming an entry with actual
  944. name ending in '/' will not appear */
  945. p++;
  946. DPRINTF_S(p);
  947. for (i = 0; i < n; i++)
  948. if (strcmp(p, dents[i].name) == 0)
  949. return i;
  950. return 0;
  951. }
  952. static int
  953. populate(char *path, char *oldpath, char *fltr)
  954. {
  955. static regex_t re;
  956. static int r;
  957. /* Can fail when permissions change while browsing */
  958. if (canopendir(path) == 0)
  959. return -1;
  960. /* Search filter */
  961. r = setfilter(&re, fltr);
  962. if (r != 0)
  963. return -1;
  964. dentfree(dents);
  965. ndents = 0;
  966. dents = NULL;
  967. ndents = dentfill(path, &dents, visible, &re);
  968. qsort(dents, ndents, sizeof(*dents), entrycmp);
  969. /* Find cur from history */
  970. cur = dentfind(dents, ndents, oldpath);
  971. return 0;
  972. }
  973. static void
  974. redraw(char *path)
  975. {
  976. static char cwd[PATH_MAX];
  977. static int nlines, odd;
  978. static int i;
  979. nlines = MIN(LINES - 4, ndents);
  980. /* Clean screen */
  981. erase();
  982. /* Strip trailing slashes */
  983. for (i = strlen(path) - 1; i > 0; i--)
  984. if (path[i] == '/')
  985. path[i] = '\0';
  986. else
  987. break;
  988. DPRINTF_D(cur);
  989. DPRINTF_S(path);
  990. /* No text wrapping in cwd line */
  991. if (!realpath(path, cwd)) {
  992. printmsg("Cannot resolve path");
  993. return;
  994. }
  995. printw(CWD "%s\n\n", cwd);
  996. /* Print listing */
  997. odd = ISODD(nlines);
  998. if (cur < (nlines >> 1)) {
  999. for (i = 0; i < nlines; i++)
  1000. printptr(&dents[i], i == cur);
  1001. } else if (cur >= ndents - (nlines >> 1)) {
  1002. for (i = ndents - nlines; i < ndents; i++)
  1003. printptr(&dents[i], i == cur);
  1004. } else {
  1005. nlines >>= 1;
  1006. for (i = cur - nlines; i < cur + nlines + odd; i++)
  1007. printptr(&dents[i], i == cur);
  1008. }
  1009. if (showdetail) {
  1010. if (ndents) {
  1011. static char ind[2] = "\0\0";
  1012. static char sort[17];
  1013. if (mtimeorder)
  1014. sprintf(sort, "by time ");
  1015. else if (sizeorder)
  1016. sprintf(sort, "by size ");
  1017. else
  1018. sort[0] = '\0';
  1019. if (S_ISDIR(dents[cur].mode))
  1020. ind[0] = '/';
  1021. else if (S_ISLNK(dents[cur].mode))
  1022. ind[0] = '@';
  1023. else if (S_ISSOCK(dents[cur].mode))
  1024. ind[0] = '=';
  1025. else if (S_ISFIFO(dents[cur].mode))
  1026. ind[0] = '|';
  1027. else if (dents[cur].mode & S_IXUSR)
  1028. ind[0] = '*';
  1029. else
  1030. ind[0] = '\0';
  1031. if (!bsizeorder)
  1032. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  1033. dents[cur].name, ind);
  1034. else
  1035. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  1036. ndents, coolsize(fs_free), dents[cur].name, ind);
  1037. printmsg(cwd);
  1038. } else
  1039. printmsg("0 items");
  1040. }
  1041. }
  1042. static void
  1043. browse(char *ipath, char *ifilter)
  1044. {
  1045. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1046. static char lastdir[PATH_MAX];
  1047. static char fltr[LINE_MAX];
  1048. char *bin, *dir, *tmp, *run, *env;
  1049. struct stat sb;
  1050. regex_t re;
  1051. int r, fd;
  1052. enum action sel = SEL_RUNARG + 1;
  1053. xstrlcpy(path, ipath, sizeof(path));
  1054. xstrlcpy(lastdir, ipath, sizeof(lastdir));
  1055. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1056. oldpath[0] = '\0';
  1057. newpath[0] = '\0';
  1058. begin:
  1059. if (sel == SEL_GOIN && S_ISDIR(sb.st_mode))
  1060. r = populate(path, NULL, fltr);
  1061. else
  1062. r = populate(path, oldpath, fltr);
  1063. if (r == -1) {
  1064. printwarn();
  1065. goto nochange;
  1066. }
  1067. for (;;) {
  1068. redraw(path);
  1069. nochange:
  1070. sel = nextsel(&run, &env);
  1071. switch (sel) {
  1072. case SEL_QUIT:
  1073. dentfree(dents);
  1074. return;
  1075. case SEL_BACK:
  1076. /* There is no going back */
  1077. if (strcmp(path, "/") == 0 ||
  1078. strcmp(path, ".") == 0 ||
  1079. strchr(path, '/') == NULL) {
  1080. printmsg("You are at /");
  1081. goto nochange;
  1082. }
  1083. dir = xdirname(path);
  1084. if (canopendir(dir) == 0) {
  1085. printwarn();
  1086. goto nochange;
  1087. }
  1088. /* Save history */
  1089. xstrlcpy(oldpath, path, sizeof(oldpath));
  1090. /* Save last working directory */
  1091. xstrlcpy(lastdir, path, sizeof(lastdir));
  1092. xstrlcpy(path, dir, sizeof(path));
  1093. /* Reset filter */
  1094. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1095. goto begin;
  1096. case SEL_GOIN:
  1097. /* Cannot descend in empty directories */
  1098. if (ndents == 0)
  1099. goto nochange;
  1100. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  1101. DPRINTF_S(newpath);
  1102. /* Get path info */
  1103. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1104. if (fd == -1) {
  1105. printwarn();
  1106. goto nochange;
  1107. }
  1108. r = fstat(fd, &sb);
  1109. if (r == -1) {
  1110. printwarn();
  1111. close(fd);
  1112. goto nochange;
  1113. }
  1114. close(fd);
  1115. DPRINTF_U(sb.st_mode);
  1116. switch (sb.st_mode & S_IFMT) {
  1117. case S_IFDIR:
  1118. if (canopendir(newpath) == 0) {
  1119. printwarn();
  1120. goto nochange;
  1121. }
  1122. /* Save last working directory */
  1123. xstrlcpy(lastdir, path, sizeof(lastdir));
  1124. xstrlcpy(path, newpath, sizeof(path));
  1125. /* Reset filter */
  1126. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1127. goto begin;
  1128. case S_IFREG:
  1129. {
  1130. static char cmd[MAX_CMD_LEN];
  1131. static char *runvi = "vi";
  1132. static int status;
  1133. /* If default mime opener is set, use it */
  1134. if (opener) {
  1135. snprintf(cmd, MAX_CMD_LEN,
  1136. "%s \"%s\" > /dev/null 2>&1",
  1137. opener, newpath);
  1138. status = system(cmd);
  1139. continue;
  1140. }
  1141. /* Try custom applications */
  1142. bin = openwith(newpath);
  1143. /* If custom app doesn't exist try fallback */
  1144. snprintf(cmd, MAX_CMD_LEN, "which \"%s\"", bin);
  1145. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1146. bin = NULL;
  1147. if (bin == NULL) {
  1148. /* If a custom handler application is
  1149. not set, open plain text files with
  1150. vi, then try fallback_opener */
  1151. snprintf(cmd, MAX_CMD_LEN,
  1152. "file \"%s\"", newpath);
  1153. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1154. goto nochange;
  1155. if (strstr(cmd, "ASCII text") != NULL)
  1156. bin = runvi;
  1157. else if (fallback_opener) {
  1158. snprintf(cmd, MAX_CMD_LEN,
  1159. "%s \"%s\" > \
  1160. /dev/null 2>&1",
  1161. fallback_opener,
  1162. newpath);
  1163. status = system(cmd);
  1164. continue;
  1165. } else {
  1166. status++; /* Dummy operation */
  1167. printmsg("No association");
  1168. goto nochange;
  1169. }
  1170. }
  1171. exitcurses();
  1172. spawn(bin, newpath, NULL, 0);
  1173. initcurses();
  1174. continue;
  1175. }
  1176. default:
  1177. printmsg("Unsupported file");
  1178. goto nochange;
  1179. }
  1180. case SEL_FLTR:
  1181. /* Read filter */
  1182. printprompt("filter: ");
  1183. tmp = readln();
  1184. if (tmp == NULL)
  1185. tmp = ifilter;
  1186. /* Check and report regex errors */
  1187. r = setfilter(&re, tmp);
  1188. if (r != 0)
  1189. goto nochange;
  1190. xstrlcpy(fltr, tmp, sizeof(fltr));
  1191. DPRINTF_S(fltr);
  1192. /* Save current */
  1193. if (ndents > 0)
  1194. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1195. goto begin;
  1196. case SEL_NEXT:
  1197. if (cur < ndents - 1)
  1198. cur++;
  1199. else if (ndents)
  1200. /* Roll over, set cursor to first entry */
  1201. cur = 0;
  1202. break;
  1203. case SEL_PREV:
  1204. if (cur > 0)
  1205. cur--;
  1206. else if (ndents)
  1207. /* Roll over, set cursor to last entry */
  1208. cur = ndents - 1;
  1209. break;
  1210. case SEL_PGDN:
  1211. if (cur < ndents - 1)
  1212. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1213. break;
  1214. case SEL_PGUP:
  1215. if (cur > 0)
  1216. cur -= MIN((LINES - 4) / 2, cur);
  1217. break;
  1218. case SEL_HOME:
  1219. cur = 0;
  1220. break;
  1221. case SEL_END:
  1222. cur = ndents - 1;
  1223. break;
  1224. case SEL_CD:
  1225. /* Read target dir */
  1226. printprompt("chdir: ");
  1227. tmp = readln();
  1228. if (tmp == NULL) {
  1229. clearprompt();
  1230. goto nochange;
  1231. }
  1232. if (tmp[0] == '~') {
  1233. char *home = getenv("HOME");
  1234. if (home)
  1235. snprintf(newpath, PATH_MAX,
  1236. "%s%s", home, tmp + 1);
  1237. else
  1238. mkpath(path, tmp, newpath, sizeof(newpath));
  1239. } else if (tmp[0] == '-' && tmp[1] == '\0')
  1240. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1241. else
  1242. mkpath(path, tmp, newpath, sizeof(newpath));
  1243. if (canopendir(newpath) == 0) {
  1244. printwarn();
  1245. goto nochange;
  1246. }
  1247. /* Save last working directory */
  1248. xstrlcpy(lastdir, path, sizeof(lastdir));
  1249. xstrlcpy(path, newpath, sizeof(path));
  1250. /* Reset filter */
  1251. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1252. DPRINTF_S(path);
  1253. goto begin;
  1254. case SEL_CDHOME:
  1255. tmp = getenv("HOME");
  1256. if (tmp == NULL) {
  1257. clearprompt();
  1258. goto nochange;
  1259. }
  1260. if (canopendir(tmp) == 0) {
  1261. printwarn();
  1262. goto nochange;
  1263. }
  1264. /* Save last working directory */
  1265. xstrlcpy(lastdir, path, sizeof(lastdir));
  1266. xstrlcpy(path, tmp, sizeof(path));
  1267. /* Reset filter */
  1268. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1269. DPRINTF_S(path);
  1270. goto begin;
  1271. case SEL_LAST:
  1272. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1273. xstrlcpy(lastdir, path, sizeof(lastdir));
  1274. xstrlcpy(path, newpath, sizeof(path));
  1275. DPRINTF_S(path);
  1276. goto begin;
  1277. case SEL_TOGGLEDOT:
  1278. showhidden ^= 1;
  1279. initfilter(showhidden, &ifilter);
  1280. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1281. goto begin;
  1282. case SEL_DETAIL:
  1283. showdetail = !showdetail;
  1284. showdetail ? (printptr = &printent_long)
  1285. : (printptr = &printent);
  1286. /* Save current */
  1287. if (ndents > 0)
  1288. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1289. goto begin;
  1290. case SEL_STATS:
  1291. {
  1292. struct stat sb;
  1293. if (ndents > 0)
  1294. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1295. r = lstat(oldpath, &sb);
  1296. if (r == -1)
  1297. printerr(1, "lstat");
  1298. else
  1299. show_stats(oldpath, dents[cur].name, &sb);
  1300. goto begin;
  1301. }
  1302. case SEL_DFB:
  1303. if (!desktop_manager)
  1304. goto nochange;
  1305. exitcurses();
  1306. spawn(desktop_manager, path, path, 0);
  1307. initcurses();
  1308. goto nochange;
  1309. case SEL_FSIZE:
  1310. sizeorder = !sizeorder;
  1311. mtimeorder = 0;
  1312. bsizeorder = 0;
  1313. /* Save current */
  1314. if (ndents > 0)
  1315. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1316. goto begin;
  1317. case SEL_BSIZE:
  1318. bsizeorder = !bsizeorder;
  1319. if (bsizeorder) {
  1320. showdetail = 1;
  1321. printptr = &printent_long;
  1322. }
  1323. mtimeorder = 0;
  1324. sizeorder = 0;
  1325. /* Save current */
  1326. if (ndents > 0)
  1327. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1328. goto begin;
  1329. case SEL_MTIME:
  1330. mtimeorder = !mtimeorder;
  1331. sizeorder = 0;
  1332. bsizeorder = 0;
  1333. /* Save current */
  1334. if (ndents > 0)
  1335. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1336. goto begin;
  1337. case SEL_REDRAW:
  1338. /* Save current */
  1339. if (ndents > 0)
  1340. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1341. goto begin;
  1342. case SEL_COPY:
  1343. if (copier && ndents) {
  1344. char abspath[PATH_MAX];
  1345. if (strcmp(path, "/") == 0)
  1346. snprintf(abspath, PATH_MAX, "/%s",
  1347. dents[cur].name);
  1348. else
  1349. snprintf(abspath, PATH_MAX, "%s/%s",
  1350. path, dents[cur].name);
  1351. spawn(copier, abspath, NULL, 0);
  1352. printmsg(abspath);
  1353. } else if (!copier)
  1354. printmsg("NNN_COPIER is not set");
  1355. goto nochange;
  1356. case SEL_HELP:
  1357. show_help();
  1358. /* Save current */
  1359. if (ndents > 0)
  1360. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1361. goto begin;
  1362. case SEL_RUN:
  1363. run = xgetenv(env, run);
  1364. exitcurses();
  1365. spawn(run, NULL, path, 1);
  1366. initcurses();
  1367. /* Repopulate as directory content may have changed */
  1368. goto begin;
  1369. case SEL_RUNARG:
  1370. run = xgetenv(env, run);
  1371. exitcurses();
  1372. spawn(run, dents[cur].name, path, 0);
  1373. initcurses();
  1374. break;
  1375. }
  1376. /* Screensaver */
  1377. if (idletimeout != 0 && idle == idletimeout) {
  1378. idle = 0;
  1379. exitcurses();
  1380. spawn(idlecmd, NULL, NULL, 0);
  1381. initcurses();
  1382. }
  1383. }
  1384. }
  1385. static void
  1386. usage(void)
  1387. {
  1388. fprintf(stderr, "usage: nnn [-d] [-v] [dir]\n");
  1389. exit(1);
  1390. }
  1391. int
  1392. main(int argc, char *argv[])
  1393. {
  1394. char cwd[PATH_MAX], *ipath;
  1395. char *ifilter;
  1396. int opt = 0;
  1397. /* Confirm we are in a terminal */
  1398. if (!isatty(0) || !isatty(1)) {
  1399. fprintf(stderr, "stdin or stdout is not a tty\n");
  1400. exit(1);
  1401. }
  1402. if (argc > 3)
  1403. usage();
  1404. while ((opt = getopt(argc, argv, "dv")) != -1) {
  1405. switch (opt) {
  1406. case 'd':
  1407. /* Open in detail mode, if set */
  1408. showdetail = 1;
  1409. printptr = &printent_long;
  1410. break;
  1411. case 'v':
  1412. fprintf(stderr, "%s\n", VERSION);
  1413. return 0;
  1414. default:
  1415. usage();
  1416. }
  1417. }
  1418. if (argc == optind) {
  1419. /* Start in the current directory */
  1420. ipath = getcwd(cwd, sizeof(cwd));
  1421. if (ipath == NULL)
  1422. ipath = "/";
  1423. } else {
  1424. ipath = realpath(argv[optind], cwd);
  1425. if (!ipath) {
  1426. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1427. exit(1);
  1428. }
  1429. }
  1430. open_max = max_openfds();
  1431. if (getuid() == 0)
  1432. showhidden = 1;
  1433. initfilter(showhidden, &ifilter);
  1434. /* Get the default desktop mime opener, if set */
  1435. opener = getenv("NNN_OPENER");
  1436. /* Get the fallback desktop mime opener, if set */
  1437. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  1438. /* Get the desktop file browser, if set */
  1439. desktop_manager = getenv("NNN_DE_FILE_MANAGER");
  1440. /* Get the default copier, if set */
  1441. copier = getenv("NNN_COPIER");
  1442. signal(SIGINT, SIG_IGN);
  1443. /* Test initial path */
  1444. if (canopendir(ipath) == 0) {
  1445. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1446. exit(1);
  1447. }
  1448. /* Set locale before curses setup */
  1449. setlocale(LC_ALL, "");
  1450. initcurses();
  1451. browse(ipath, ifilter);
  1452. exitcurses();
  1453. exit(0);
  1454. }