My build of nnn with minor changes
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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