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