My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

nnn.c 57 KiB

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