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

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