zip.c 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018
  1. /*
  2. zip.c - Zip 3
  3. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  4. See the accompanying file LICENSE, version 2007-Mar-4 or later
  5. (the contents of which are also included in zip.h) for terms of use.
  6. If, for some reason, all these files are missing, the Info-ZIP license
  7. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. /*
  10. * zip.c by Mark Adler.
  11. */
  12. #define __ZIP_C
  13. #include "zip.h"
  14. #include <time.h> /* for tzset() declaration */
  15. #if defined(WIN32) || defined(WINDLL)
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <windows.h>
  18. #endif
  19. #ifdef WINDLL
  20. # include <setjmp.h>
  21. # include "windll/windll.h"
  22. #endif
  23. #define DEFCPYRT /* main module: enable copyright string defines! */
  24. #include "revision.h"
  25. #include "crc32.h"
  26. #include "crypt.h"
  27. #include "ttyio.h"
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #ifdef VMS
  31. # include <stsdef.h>
  32. # include "vms/vmsmunch.h"
  33. # include "vms/vms.h"
  34. #endif
  35. #ifdef MACOS
  36. # include "macglob.h"
  37. extern MacZipGlobals MacZip;
  38. extern int error_level;
  39. #endif
  40. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)
  41. # include <process.h>
  42. # if (!defined(P_WAIT) && defined(_P_WAIT))
  43. # define P_WAIT _P_WAIT
  44. # endif
  45. #endif
  46. #include <signal.h>
  47. #include <stdio.h>
  48. #ifdef UNICODE_TEST
  49. # ifdef WIN32
  50. # include <direct.h>
  51. # endif
  52. #endif
  53. #ifdef BZIP2_SUPPORT
  54. /* If IZ_BZIP2 is defined as the location of the bzip2 files then
  55. assume the location has been added to include path. For Unix
  56. this is done by the configure script. */
  57. /* Also do not need path for bzip2 include if OS includes support
  58. for bzip2 library. */
  59. # include "bzlib.h"
  60. #endif
  61. #define MAXCOM 256 /* Maximum one-line comment size */
  62. /* Local option flags */
  63. #ifndef DELETE
  64. #define DELETE 0
  65. #endif
  66. #define ADD 1
  67. #define UPDATE 2
  68. #define FRESHEN 3
  69. #define ARCHIVE 4
  70. local int action = ADD; /* one of ADD, UPDATE, FRESHEN, DELETE, or ARCHIVE */
  71. local int comadd = 0; /* 1=add comments for new files */
  72. local int zipedit = 0; /* 1=edit zip comment and all file comments */
  73. local int latest = 0; /* 1=set zip file time to time of latest file */
  74. local int test = 0; /* 1=test zip file with unzip -t */
  75. local char *unzip_path = NULL; /* where to find unzip */
  76. local int tempdir = 0; /* 1=use temp directory (-b) */
  77. local int junk_sfx = 0; /* 1=junk the sfx prefix */
  78. #if defined(AMIGA) || defined(MACOS)
  79. local int filenotes = 0; /* 1=take comments from AmigaDOS/MACOS filenotes */
  80. #endif
  81. #ifdef EBCDIC
  82. int aflag = __EBCDIC; /* Convert EBCDIC to ASCII or stay EBCDIC ? */
  83. #endif
  84. #ifdef CMS_MVS
  85. int bflag = 0; /* Use text mode as default */
  86. #endif
  87. #ifdef QDOS
  88. char _version[] = VERSION;
  89. #endif
  90. #ifdef WINDLL
  91. jmp_buf zipdll_error_return;
  92. #ifdef ZIP64_SUPPORT
  93. unsigned long low, high; /* returning 64 bit values for systems without an _int64 */
  94. uzoff_t filesize64;
  95. #endif
  96. #endif
  97. #if CRYPT
  98. /* Pointer to crc_table, needed in crypt.c */
  99. # if (!defined(USE_ZLIB) || defined(USE_OWN_CRCTAB))
  100. ZCONST ulg near *crc_32_tab;
  101. # else
  102. ZCONST uLongf *crc_32_tab;
  103. # endif
  104. #endif /* CRYPT */
  105. /* Local functions */
  106. local void freeup OF((void));
  107. local int finish OF((int));
  108. #if (!defined(MACOS) && !defined(WINDLL))
  109. local void handler OF((int));
  110. local void license OF((void));
  111. #ifndef VMSCLI
  112. local void help OF((void));
  113. local void help_extended OF((void));
  114. #endif /* !VMSCLI */
  115. #endif /* !MACOS && !WINDLL */
  116. /* prereading of arguments is not supported in new command
  117. line interpreter get_option() so read filters as arguments
  118. are processed and convert to expected array later */
  119. local int add_filter OF((int flag, char *pattern));
  120. local int filterlist_to_patterns OF((void));
  121. /* not used
  122. local int get_filters OF((int argc, char **argv));
  123. */
  124. /* list to store file arguments */
  125. local long add_name OF((char *filearg));
  126. local int DisplayRunningStats OF((void));
  127. local int BlankRunningStats OF((void));
  128. #if !defined(WINDLL)
  129. local void version_info OF((void));
  130. # if !defined(MACOS)
  131. local void zipstdout OF((void));
  132. # endif /* !MACOS */
  133. local int check_unzip_version OF((char *unzippath));
  134. local void check_zipfile OF((char *zipname, char *zippath));
  135. #endif /* !WINDLL */
  136. /* structure used by add_filter to store filters */
  137. struct filterlist_struct {
  138. char flag;
  139. char *pattern;
  140. struct filterlist_struct *next;
  141. };
  142. struct filterlist_struct *filterlist = NULL; /* start of list */
  143. struct filterlist_struct *lastfilter = NULL; /* last filter in list */
  144. /* structure used by add_filearg to store file arguments */
  145. struct filelist_struct {
  146. char *name;
  147. struct filelist_struct *next;
  148. };
  149. long filearg_count = 0;
  150. struct filelist_struct *filelist = NULL; /* start of list */
  151. struct filelist_struct *lastfile = NULL; /* last file in list */
  152. local void freeup()
  153. /* Free all allocations in the 'found' list, the 'zfiles' list and
  154. the 'patterns' list. */
  155. {
  156. struct flist far *f; /* steps through found list */
  157. struct zlist far *z; /* pointer to next entry in zfiles list */
  158. for (f = found; f != NULL; f = fexpel(f))
  159. ;
  160. while (zfiles != NULL)
  161. {
  162. z = zfiles->nxt;
  163. if (zfiles->zname && zfiles->zname != zfiles->name)
  164. free((zvoid *)(zfiles->zname));
  165. if (zfiles->name)
  166. free((zvoid *)(zfiles->name));
  167. if (zfiles->iname)
  168. free((zvoid *)(zfiles->iname));
  169. if (zfiles->cext && zfiles->cextra && zfiles->cextra != zfiles->extra)
  170. free((zvoid *)(zfiles->cextra));
  171. if (zfiles->ext && zfiles->extra)
  172. free((zvoid *)(zfiles->extra));
  173. if (zfiles->com && zfiles->comment)
  174. free((zvoid *)(zfiles->comment));
  175. if (zfiles->oname)
  176. free((zvoid *)(zfiles->oname));
  177. #ifdef UNICODE_SUPPORT
  178. if (zfiles->uname)
  179. free((zvoid *)(zfiles->uname));
  180. if (zfiles->zuname)
  181. free((zvoid *)(zfiles->zuname));
  182. if (zfiles->ouname)
  183. free((zvoid *)(zfiles->ouname));
  184. # ifdef WIN32
  185. if (zfiles->namew)
  186. free((zvoid *)(zfiles->namew));
  187. if (zfiles->inamew)
  188. free((zvoid *)(zfiles->inamew));
  189. if (zfiles->znamew)
  190. free((zvoid *)(zfiles->znamew));
  191. # endif
  192. #endif
  193. farfree((zvoid far *)zfiles);
  194. zfiles = z;
  195. zcount--;
  196. }
  197. if (patterns != NULL) {
  198. while (pcount-- > 0) {
  199. if (patterns[pcount].zname != NULL)
  200. free((zvoid *)(patterns[pcount].zname));
  201. }
  202. free((zvoid *)patterns);
  203. patterns = NULL;
  204. }
  205. /* close logfile */
  206. if (logfile) {
  207. fclose(logfile);
  208. }
  209. }
  210. local int finish(e)
  211. int e; /* exit code */
  212. /* Process -o and -m options (if specified), free up malloc'ed stuff, and
  213. exit with the code e. */
  214. {
  215. int r; /* return value from trash() */
  216. ulg t; /* latest time in zip file */
  217. struct zlist far *z; /* pointer into zfile list */
  218. /* If latest, set time to zip file to latest file in zip file */
  219. if (latest && zipfile && strcmp(zipfile, "-"))
  220. {
  221. diag("changing time of zip file to time of latest file in it");
  222. /* find latest time in zip file */
  223. if (zfiles == NULL)
  224. zipwarn("zip file is empty, can't make it as old as latest entry", "");
  225. else {
  226. t = 0;
  227. for (z = zfiles; z != NULL; z = z->nxt)
  228. /* Ignore directories in time comparisons */
  229. #ifdef USE_EF_UT_TIME
  230. if (z->iname[z->nam-1] != (char)0x2f) /* ascii '/' */
  231. {
  232. iztimes z_utim;
  233. ulg z_tim;
  234. z_tim = ((get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  235. unix2dostime(&z_utim.mtime) : z->tim);
  236. if (t < z_tim)
  237. t = z_tim;
  238. }
  239. #else /* !USE_EF_UT_TIME */
  240. if (z->iname[z->nam-1] != (char)0x2f /* ascii '/' */
  241. && t < z->tim)
  242. t = z->tim;
  243. #endif /* ?USE_EF_UT_TIME */
  244. /* set modified time of zip file to that time */
  245. if (t != 0)
  246. stamp(zipfile, t);
  247. else
  248. zipwarn(
  249. "zip file has only directories, can't make it as old as latest entry",
  250. "");
  251. }
  252. }
  253. if (tempath != NULL)
  254. {
  255. free((zvoid *)tempath);
  256. tempath = NULL;
  257. }
  258. if (zipfile != NULL)
  259. {
  260. free((zvoid *)zipfile);
  261. zipfile = NULL;
  262. }
  263. if (in_file != NULL)
  264. {
  265. fclose(in_file);
  266. in_file = NULL;
  267. }
  268. if (in_path != NULL)
  269. {
  270. free((zvoid *)in_path);
  271. in_path = NULL;
  272. }
  273. if (out_path != NULL)
  274. {
  275. free((zvoid *)out_path);
  276. out_path = NULL;
  277. }
  278. if (zcomment != NULL)
  279. {
  280. free((zvoid *)zcomment);
  281. zcomment = NULL;
  282. }
  283. /* If dispose, delete all files in the zfiles list that are marked */
  284. if (dispose)
  285. {
  286. diag("deleting files that were added to zip file");
  287. if ((r = trash()) != ZE_OK)
  288. ZIPERR(r, "was deleting moved files and directories");
  289. }
  290. /* Done! */
  291. freeup();
  292. return e;
  293. }
  294. void ziperr(c, h)
  295. int c; /* error code from the ZE_ class */
  296. ZCONST char *h; /* message about how it happened */
  297. /* Issue a message for the error, clean up files and memory, and exit. */
  298. {
  299. #ifndef WINDLL
  300. #ifndef MACOS
  301. static int error_level = 0;
  302. #endif
  303. if (error_level++ > 0)
  304. /* avoid recursive ziperr() printouts (his should never happen) */
  305. EXIT(ZE_LOGIC); /* ziperr recursion is an internal logic error! */
  306. #endif /* !WINDLL */
  307. if (mesg_line_started) {
  308. fprintf(mesg, "\n");
  309. mesg_line_started = 0;
  310. }
  311. if (logfile && logfile_line_started) {
  312. fprintf(logfile, "\n");
  313. logfile_line_started = 0;
  314. }
  315. if (h != NULL) {
  316. if (PERR(c))
  317. fprintf(mesg, "zip I/O error: %s", strerror(errno));
  318. /* perror("zip I/O error"); */
  319. fflush(mesg);
  320. fprintf(mesg, "\nzip error: %s (%s)\n", ZIPERRORS(c), h);
  321. #ifdef DOS
  322. check_for_windows("Zip");
  323. #endif
  324. if (logfile) {
  325. if (PERR(c))
  326. fprintf(logfile, "zip I/O error: %s\n", strerror(errno));
  327. fprintf(logfile, "\nzip error: %s (%s)\n", ZIPERRORS(c), h);
  328. logfile_line_started = 0;
  329. }
  330. }
  331. if (tempzip != NULL)
  332. {
  333. if (tempzip != zipfile) {
  334. if (current_local_file)
  335. fclose(current_local_file);
  336. if (y != current_local_file && y != NULL)
  337. fclose(y);
  338. #ifndef DEBUG
  339. destroy(tempzip);
  340. #endif
  341. free((zvoid *)tempzip);
  342. } else {
  343. /* -g option, attempt to restore the old file */
  344. /* zip64 support 09/05/2003 R.Nausedat */
  345. uzoff_t k = 0; /* keep count for end header */
  346. uzoff_t cb = cenbeg; /* get start of central */
  347. struct zlist far *z; /* steps through zfiles linked list */
  348. fprintf(mesg, "attempting to restore %s to its previous state\n",
  349. zipfile);
  350. if (logfile)
  351. fprintf(logfile, "attempting to restore %s to its previous state\n",
  352. zipfile);
  353. zfseeko(y, cenbeg, SEEK_SET);
  354. tempzn = cenbeg;
  355. for (z = zfiles; z != NULL; z = z->nxt)
  356. {
  357. putcentral(z);
  358. tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  359. k++;
  360. }
  361. putend(k, tempzn - cb, cb, zcomlen, zcomment);
  362. fclose(y);
  363. y = NULL;
  364. }
  365. }
  366. if (key != NULL) {
  367. free((zvoid *)key);
  368. key = NULL;
  369. }
  370. if (tempath != NULL) {
  371. free((zvoid *)tempath);
  372. tempath = NULL;
  373. }
  374. if (zipfile != NULL) {
  375. free((zvoid *)zipfile);
  376. zipfile = NULL;
  377. }
  378. if (out_path != NULL) {
  379. free((zvoid *)out_path);
  380. out_path = NULL;
  381. }
  382. if (zcomment != NULL) {
  383. free((zvoid *)zcomment);
  384. zcomment = NULL;
  385. }
  386. freeup();
  387. #ifndef WINDLL
  388. EXIT(c);
  389. #else
  390. longjmp(zipdll_error_return, c);
  391. #endif
  392. }
  393. void error(h)
  394. ZCONST char *h;
  395. /* Internal error, should never happen */
  396. {
  397. ziperr(ZE_LOGIC, h);
  398. }
  399. #if (!defined(MACOS) && !defined(WINDLL))
  400. local void handler(s)
  401. int s; /* signal number (ignored) */
  402. /* Upon getting a user interrupt, turn echo back on for tty and abort
  403. cleanly using ziperr(). */
  404. {
  405. #if defined(AMIGA) && defined(__SASC)
  406. _abort();
  407. #else
  408. #if !defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS)
  409. echon();
  410. putc('\n', mesg);
  411. #endif /* !MSDOS */
  412. #endif /* AMIGA && __SASC */
  413. ziperr(ZE_ABORT, "aborting");
  414. s++; /* keep some compilers happy */
  415. }
  416. #endif /* !MACOS && !WINDLL */
  417. void zipmessage_nl(a, nl)
  418. ZCONST char *a; /* message string to output */
  419. int nl; /* 1 = add nl to end */
  420. /* If nl false, print a message to mesg without new line.
  421. If nl true, print and add new line. If logfile is
  422. open then also write message to log file. */
  423. {
  424. if (noisy) {
  425. if (a && strlen(a)) {
  426. fprintf(mesg, "%s", a);
  427. mesg_line_started = 1;
  428. }
  429. if (nl) {
  430. if (mesg_line_started) {
  431. fprintf(mesg, "\n");
  432. mesg_line_started = 0;
  433. }
  434. } else if (a && strlen(a)) {
  435. mesg_line_started = 1;
  436. }
  437. fflush(mesg);
  438. }
  439. if (logfile) {
  440. if (a && strlen(a)) {
  441. fprintf(logfile, "%s", a);
  442. logfile_line_started = 1;
  443. }
  444. if (nl) {
  445. if (logfile_line_started) {
  446. fprintf(logfile, "\n");
  447. logfile_line_started = 0;
  448. }
  449. } else if (a && strlen(a)) {
  450. logfile_line_started = 1;
  451. }
  452. fflush(logfile);
  453. }
  454. }
  455. void zipmessage(a, b)
  456. ZCONST char *a, *b; /* message strings juxtaposed in output */
  457. /* Print a message to mesg and flush. Also write to log file if
  458. open. Write new line first if current line has output already. */
  459. {
  460. if (noisy) {
  461. if (mesg_line_started)
  462. fprintf(mesg, "\n");
  463. fprintf(mesg, "%s%s\n", a, b);
  464. mesg_line_started = 0;
  465. fflush(mesg);
  466. }
  467. if (logfile) {
  468. if (logfile_line_started)
  469. fprintf(logfile, "\n");
  470. fprintf(logfile, "%s%s\n", a, b);
  471. logfile_line_started = 0;
  472. fflush(logfile);
  473. }
  474. }
  475. void zipwarn(a, b)
  476. ZCONST char *a, *b; /* message strings juxtaposed in output */
  477. /* Print a warning message to mesg (usually stderr) and return. */
  478. {
  479. if (noisy) {
  480. if (mesg_line_started)
  481. fprintf(mesg, "\n");
  482. fprintf(mesg, "\tzip warning: %s%s\n", a, b);
  483. mesg_line_started = 0;
  484. fflush(mesg);
  485. }
  486. if (logfile) {
  487. if (logfile_line_started)
  488. fprintf(logfile, "\n");
  489. fprintf(logfile, "\tzip warning: %s%s\n", a, b);
  490. logfile_line_started = 0;
  491. fflush(logfile);
  492. }
  493. }
  494. #ifndef WINDLL
  495. local void license()
  496. /* Print license information to stdout. */
  497. {
  498. extent i; /* counter for copyright array */
  499. for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++)
  500. puts(swlicense[i]);
  501. }
  502. #ifdef VMSCLI
  503. void help()
  504. #else
  505. local void help()
  506. #endif
  507. /* Print help (along with license info) to stdout. */
  508. {
  509. extent i; /* counter for help array */
  510. /* help array */
  511. static ZCONST char *text[] = {
  512. #ifdef VMS
  513. "Zip %s (%s). Usage: zip == \"$ disk:[dir]zip.exe\"",
  514. #else
  515. "Zip %s (%s). Usage:",
  516. #endif
  517. #ifdef MACOS
  518. "zip [-options] [-b fm] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]",
  519. " The default action is to add or replace zipfile entries from list.",
  520. " ",
  521. " -f freshen: only changed files -u update: only changed or new files",
  522. " -d delete entries in zipfile -m move into zipfile (delete OS files)",
  523. " -r recurse into directories -j junk (don't record) directory names",
  524. " -0 store only -l convert LF to CR LF (-ll CR LF to LF)",
  525. " -1 compress faster -9 compress better",
  526. " -q quiet operation -v verbose operation/print version info",
  527. " -c add one-line comments -z add zipfile comment",
  528. " -o make zipfile as old as latest entry",
  529. " -F fix zipfile (-FF try harder) -D do not add directory entries",
  530. " -T test zipfile integrity -X eXclude eXtra file attributes",
  531. # if CRYPT
  532. " -e encrypt -n don't compress these suffixes"
  533. # else
  534. " -h show this help -n don't compress these suffixes"
  535. # endif
  536. ," -h2 show more help",
  537. " Macintosh specific:",
  538. " -jj record Fullpath (+ Volname) -N store finder-comments as comments",
  539. " -df zip only datafork of a file -S include finder invisible/system files"
  540. #else /* !MACOS */
  541. #ifdef VM_CMS
  542. "zip [-options] [-b fm] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]",
  543. #else /* !VM_CMS */
  544. "zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]",
  545. #endif /* ?VM_CMS */
  546. " The default action is to add or replace zipfile entries from list, which",
  547. " can include the special name - to compress standard input.",
  548. " If zipfile and list are omitted, zip compresses stdin to stdout.",
  549. " -f freshen: only changed files -u update: only changed or new files",
  550. " -d delete entries in zipfile -m move into zipfile (delete OS files)",
  551. " -r recurse into directories -j junk (don't record) directory names",
  552. #ifdef THEOS
  553. " -0 store only -l convert CR to CR LF (-ll CR LF to CR)",
  554. #else
  555. " -0 store only -l convert LF to CR LF (-ll CR LF to LF)",
  556. #endif
  557. " -1 compress faster -9 compress better",
  558. " -q quiet operation -v verbose operation/print version info",
  559. " -c add one-line comments -z add zipfile comment",
  560. " -@ read names from stdin -o make zipfile as old as latest entry",
  561. " -x exclude the following names -i include only the following names",
  562. #ifdef EBCDIC
  563. #ifdef CMS_MVS
  564. " -a translate to ASCII -B force binary read (text is default)",
  565. #else /* !CMS_MVS */
  566. " -a translate to ASCII",
  567. #endif /* ?CMS_MVS */
  568. #endif /* EBCDIC */
  569. #ifdef TANDEM
  570. " -Bn set Enscribe formatting options",
  571. #endif
  572. " -F fix zipfile (-FF try harder) -D do not add directory entries",
  573. " -A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)",
  574. " -T test zipfile integrity -X eXclude eXtra file attributes",
  575. #ifdef VMS
  576. " -C preserve case of file names -C- down-case all file names",
  577. " -C2 preserve case of ODS2 names -C2- down-case ODS2 file names* (*=default)",
  578. " -C5 preserve case of ODS5 names* -C5- down-case ODS5 file names",
  579. " -V save VMS file attributes (-VV also save allocated blocks past EOF)",
  580. " -w store file version numbers\
  581. -ww store file version numbers as \".nnn\"",
  582. #endif /* def VMS */
  583. #ifdef NTSD_EAS
  584. " -! use privileges (if granted) to obtain all aspects of WinNT security",
  585. #endif /* NTSD_EAS */
  586. #ifdef OS2
  587. " -E use the .LONGNAME Extended attribute (if found) as filename",
  588. #endif /* OS2 */
  589. #ifdef S_IFLNK
  590. " -y store symbolic links as the link instead of the referenced file",
  591. #endif /* !S_IFLNK */
  592. /*
  593. " -R PKZIP recursion (see manual)",
  594. */
  595. #if defined(MSDOS) || defined(OS2)
  596. " -$ include volume label -S include system and hidden files",
  597. #endif
  598. #ifdef AMIGA
  599. # if CRYPT
  600. " -N store filenotes as comments -e encrypt",
  601. " -h show this help -n don't compress these suffixes"
  602. # else
  603. " -N store filenotes as comments -n don't compress these suffixes"
  604. # endif
  605. #else /* !AMIGA */
  606. # if CRYPT
  607. " -e encrypt -n don't compress these suffixes"
  608. # else
  609. " -h show this help -n don't compress these suffixes"
  610. # endif
  611. #endif /* ?AMIGA */
  612. #ifdef RISCOS
  613. ," -h2 show more help -I don't scan thru Image files"
  614. #else
  615. ," -h2 show more help"
  616. #endif
  617. #endif /* ?MACOS */
  618. #ifdef VMS
  619. ," (Must quote upper-case options, like \"-V\", unless SET PROC/PARSE=EXTEND)"
  620. #endif /* def VMS */
  621. ," "
  622. };
  623. for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  624. {
  625. printf(copyright[i], "zip");
  626. putchar('\n');
  627. }
  628. for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  629. {
  630. printf(text[i], VERSION, REVDATE);
  631. putchar('\n');
  632. }
  633. }
  634. #ifdef VMSCLI
  635. void help_extended()
  636. #else
  637. local void help_extended()
  638. #endif
  639. /* Print extended help to stdout. */
  640. {
  641. extent i; /* counter for help array */
  642. /* help array */
  643. static ZCONST char *text[] = {
  644. "",
  645. "Extended Help for Zip",
  646. "",
  647. "See the Zip Manual for more detailed help",
  648. "",
  649. "",
  650. "Zip stores files in zip archives. The default action is to add or replace",
  651. "zipfile entries.",
  652. "",
  653. "Basic command line:",
  654. " zip options archive_name file file ...",
  655. "",
  656. "Some examples:",
  657. " Add file.txt to z.zip (create z if needed): zip z file.txt",
  658. " Zip all files in current dir: zip z *",
  659. " Zip files in current dir and subdirs also: zip -r z .",
  660. "",
  661. "Basic modes:",
  662. " External modes (selects files from file system):",
  663. " add - add new files/update existing files in archive (default)",
  664. " -u update - add new files/update existing files only if later date",
  665. " -f freshen - update existing files only (no files added)",
  666. " -FS filesync - update if date or size changed, delete if no OS match",
  667. " Internal modes (selects entries in archive):",
  668. " -d delete - delete files from archive (see below)",
  669. " -U copy - select files in archive to copy (use with --out)",
  670. "",
  671. "Basic options:",
  672. " -r recurse into directories (see Recursion below)",
  673. " -m after archive created, delete original files (move into archive)",
  674. " -j junk directory names (store just file names)",
  675. " -q quiet operation",
  676. " -v verbose operation (just \"zip -v\" shows version information)",
  677. " -c prompt for one-line comment for each entry",
  678. " -z prompt for comment for archive (end with just \".\" line or EOF)",
  679. " -@ read names to zip from stdin (one path per line)",
  680. " -o make zipfile as old as latest entry",
  681. "",
  682. "",
  683. "Syntax:",
  684. " The full command line syntax is:",
  685. "",
  686. " zip [-shortopts ...] [--longopt ...] [zipfile [path path ...]] [-xi list]",
  687. "",
  688. " Any number of short option and long option arguments are allowed",
  689. " (within limits) as well as any number of path arguments for files",
  690. " to zip up. If zipfile exists, the archive is read in. If zipfile",
  691. " is \"-\", stream to stdout. If any path is \"-\", zip stdin.",
  692. "",
  693. "Options and Values:",
  694. " For short options that take values, use -ovalue or -o value or -o=value",
  695. " For long option values, use either --longoption=value or --longoption value",
  696. " For example:",
  697. " zip -ds 10 --temp-dir=path zipfile path1 path2 --exclude pattern pattern",
  698. " Avoid -ovalue (no space between) to avoid confusion",
  699. " In particular, be aware of 2-character options. For example:",
  700. " -d -s is (delete, split size) while -ds is (dot size)",
  701. " Usually better to break short options across multiple arguments by function",
  702. " zip -r -dbdcds 10m -lilalf logfile archive input_directory -ll",
  703. "",
  704. " All args after just \"--\" arg are read verbatim as paths and not options.",
  705. " zip zipfile path path ... -- verbatimpath verbatimpath ...",
  706. " Use -nw to also disable wildcards, so paths are read literally:",
  707. " zip zipfile -nw -- \"-leadingdashpath\" \"a[path].c\" \"path*withwildcard\"",
  708. " You may still have to escape or quote arguments to avoid shell expansion",
  709. "",
  710. "Wildcards:",
  711. " Internally zip supports the following wildcards:",
  712. " ? (or %% or #, depending on OS) matches any single character",
  713. " * matches any number of characters, including zero",
  714. " [list] matches char in list (regex), can do range [ac-f], all but [!bf]",
  715. " If port supports [], must escape [ as [[] or use -nw to turn off wildcards",
  716. " For shells that expand wildcards, escape (\\* or \"*\") so zip can recurse",
  717. " zip zipfile -r . -i \"*.h\"",
  718. "",
  719. " Normally * crosses dir bounds in path, e.g. 'a*b' can match 'ac/db'. If",
  720. " -ws option used, * does not cross dir bounds but ** does",
  721. "",
  722. " For DOS and Windows, [list] is now disabled unless the new option",
  723. " -RE enable [list] (regular expression) matching",
  724. " is used to avoid problems with file paths containing \"[\" and \"]\":",
  725. " zip files_ending_with_number -RE foo[0-9].c",
  726. "",
  727. "Include and Exclude:",
  728. " -i pattern pattern ... include files that match a pattern",
  729. " -x pattern pattern ... exclude files that match a pattern",
  730. " Patterns are paths with optional wildcards and match paths as stored in",
  731. " archive. Exclude and include lists end at next option, @, or end of line.",
  732. " zip -x pattern pattern @ zipfile path path ...",
  733. "",
  734. "Case matching:",
  735. " On most OS the case of patterns must match the case in the archive, unless",
  736. " the -ic option is used.",
  737. " -ic ignore case of archive entries",
  738. " This option not available on case-sensitive file systems. On others, case",
  739. " ignored when matching files on file system but matching against archive",
  740. " entries remains case sensitive for modes -f (freshen), -U (archive copy),",
  741. " and -d (delete) because archive paths are always case sensitive. With",
  742. " -ic, all matching ignores case, but it's then possible multiple archive",
  743. " entries that differ only in case will match.",
  744. "",
  745. "End Of Line Translation (text files only):",
  746. " -l change CR or LF (depending on OS) line end to CR LF (Unix->Win)",
  747. " -ll change CR LF to CR or LF (depending on OS) line end (Win->Unix)",
  748. " If first buffer read from file contains binary the translation is skipped",
  749. "",
  750. "Recursion:",
  751. " -r recurse paths, include files in subdirs: zip -r a path path ...",
  752. " -R recurse current dir and match patterns: zip -R a ptn ptn ...",
  753. " Use -i and -x with either to include or exclude paths",
  754. " Path root in archive starts at current dir, so if /a/b/c/file and",
  755. " current dir is /a/b, 'zip -r archive .' puts c/file in archive",
  756. "",
  757. "Date filtering:",
  758. " -t date exclude before (include files modified on this date and later)",
  759. " -tt date include before (include files modified before date)",
  760. " Can use both at same time to set a date range",
  761. " Dates are mmddyyyy or yyyy-mm-dd",
  762. "",
  763. "Deletion, File Sync:",
  764. " -d delete files",
  765. " Delete archive entries matching internal archive paths in list",
  766. " zip archive -d pattern pattern ...",
  767. " Can use -t and -tt to select files in archive, but NOT -x or -i, so",
  768. " zip archive -d \"*\" -t 2005-12-27",
  769. " deletes all files from archive.zip with date of 27 Dec 2005 and later",
  770. " Note the * (escape as \"*\" on Unix) to select all files in archive",
  771. "",
  772. " -FS file sync",
  773. " Similar to update, but files updated if date or size of entry does not",
  774. " match file on OS. Also deletes entry from archive if no matching file",
  775. " on OS.",
  776. " zip archive_to_update -FS -r dir_used_before",
  777. " Result generally same as creating new archive, but unchanged entries",
  778. " are copied instead of being read and compressed so can be faster.",
  779. " WARNING: -FS deletes entries so make backup copy of archive first",
  780. "",
  781. "Compression:",
  782. " -0 store files (no compression)",
  783. " -1 to -9 compress fastest to compress best (default is 6)",
  784. " -Z cm set compression method to cm:",
  785. " store - store without compression, same as option -0",
  786. " deflate - original zip deflate, same as -1 to -9 (default)",
  787. " if bzip2 is enabled:",
  788. " bzip2 - use bzip2 compression (need modern unzip)",
  789. "",
  790. "Encryption:",
  791. " -e use standard (weak) PKZip 2.0 encryption, prompt for password",
  792. " -P pswd use standard encryption, password is pswd",
  793. "",
  794. "Splits (archives created as a set of split files):",
  795. " -s ssize create split archive with splits of size ssize, where ssize nm",
  796. " n number and m multiplier (kmgt, default m), 100k -> 100 kB",
  797. " -sp pause after each split closed to allow changing disks",
  798. " WARNING: Archives created with -sp use data descriptors and should",
  799. " work with most unzips but may not work with some",
  800. " -sb ring bell when pause",
  801. " -sv be verbose about creating splits",
  802. " Split archives CANNOT be updated, but see --out and Copy Mode below",
  803. "",
  804. "Using --out (output to new archive):",
  805. " --out oa output to new archive oa",
  806. " Instead of updating input archive, create new output archive oa.",
  807. " Result is same as without --out but in new archive. Input archive",
  808. " unchanged.",
  809. " WARNING: --out ALWAYS overwrites any existing output file",
  810. " For example, to create new_archive like old_archive but add newfile1",
  811. " and newfile2:",
  812. " zip old_archive newfile1 newfile2 --out new_archive",
  813. " Cannot update split archive, so use --out to out new archive:",
  814. " zip in_split_archive newfile1 newfile2 --out out_split_archive",
  815. " If input is split, output will default to same split size",
  816. " Use -s=0 or -s- to turn off splitting to convert split to single file:",
  817. " zip in_split_archive -s 0 --out out_single_file_archive",
  818. " WARNING: If overwriting old split archive but need less splits,",
  819. " old splits not overwritten are not needed but remain",
  820. "",
  821. "Copy Mode (copying from archive to archive):",
  822. " -U (also --copy) select entries in archive to copy (reverse delete)",
  823. " Copy Mode copies entries from old to new archive with --out and is used by",
  824. " zip when either no input files on command line or -U (--copy) used.",
  825. " zip inarchive --copy pattern pattern ... --out outarchive",
  826. " To copy only files matching *.c into new archive, excluding foo.c:",
  827. " zip old_archive --copy \"*.c\" --out new_archive -x foo.c",
  828. " If no input files and --out, copy all entries in old archive:",
  829. " zip old_archive --out new_archive",
  830. "",
  831. "Streaming and FIFOs:",
  832. " prog1 | zip -ll z - zip output of prog1 to zipfile z, converting CR LF",
  833. " zip - -R \"*.c\" | prog2 zip *.c files in current dir and stream to prog2 ",
  834. " prog1 | zip | prog2 zip in pipe with no in or out acts like zip - -",
  835. " If Zip is Zip64 enabled, streaming stdin creates Zip64 archives by default",
  836. " that need PKZip 4.5 unzipper like UnZip 6.0",
  837. " WARNING: Some archives created with streaming use data descriptors and",
  838. " should work with most unzips but may not work with some",
  839. " Can use -fz- to turn off Zip64 if input not large (< 4 GB):",
  840. " prog_with_small_output | zip archive -fz-",
  841. "",
  842. " Zip now can read Unix FIFO (named pipes). Off by default to prevent zip",
  843. " from stopping unexpectedly on unfed pipe, use -FI to enable:",
  844. " zip -FI archive fifo",
  845. "",
  846. "Dots, counts:",
  847. " -db display running count of bytes processed and bytes to go",
  848. " (uncompressed size, except delete and copy show stored size)",
  849. " -dc display running count of entries done and entries to go",
  850. " -dd display dots every 10 MB (or dot size) while processing files",
  851. " -dg display dots globally for archive instead of for each file",
  852. " zip -qdgds 10m will turn off most output except dots every 10 MB",
  853. " -ds siz each dot is siz processed where siz is nm as splits (0 no dots)",
  854. " -du display original uncompressed size for each entry as added",
  855. " -dv display volume (disk) number in format in_disk>out_disk",
  856. " Dot size is approximate, especially for dot sizes less than 1 MB",
  857. " Dot options don't apply to Scanning files dots (dot/2sec) (-q turns off)",
  858. "",
  859. "Logging:",
  860. " -lf path open file at path as logfile (overwrite existing file)",
  861. " -la append to existing logfile",
  862. " -li include info messages (default just warnings and errors)",
  863. "",
  864. "Testing archives:",
  865. " -T test completed temp archive with unzip before updating archive",
  866. " -TT cmd use command cmd instead of 'unzip -tqq' to test archive",
  867. " On Unix, to use unzip in current directory, could use:",
  868. " zip archive file1 file2 -T -TT \"./unzip -tqq\"",
  869. " In cmd, {} replaced by temp archive path, else temp appended.",
  870. " The return code is checked for success (0 on Unix)",
  871. "",
  872. "Fixing archives:",
  873. " -F attempt to fix a mostly intact archive (try this first)",
  874. " -FF try to salvage what can (may get more but less reliable)",
  875. " Fix options copy entries from potentially bad archive to new archive.",
  876. " -F tries to read archive normally and copy only intact entries, while",
  877. " -FF tries to salvage what can and may result in incomplete entries.",
  878. " Must use --out option to specify output archive:",
  879. " zip -F bad.zip --out fixed.zip",
  880. " Use -v (verbose) with -FF to see details:",
  881. " zip reallybad.zip -FF -v --out fixed.zip",
  882. " Currently neither option fixes bad entries, as from text mode ftp get.",
  883. "",
  884. "Difference mode:",
  885. " -DF (also --dif) only include files that have changed or are",
  886. " new as compared to the input archive",
  887. " Difference mode can be used to create incremental backups. For example:",
  888. " zip --dif full_backup.zip -r somedir --out diff.zip",
  889. " will store all new files, as well as any files in full_backup.zip where",
  890. " either file time or size have changed from that in full_backup.zip,",
  891. " in new diff.zip. Output archive not excluded automatically if exists,",
  892. " so either use -x to exclude it or put outside what is being zipped.",
  893. "",
  894. "DOS Archive bit (Windows only):",
  895. " -AS include only files with the DOS Archive bit set",
  896. " -AC after archive created, clear archive bit of included files",
  897. " WARNING: Once the archive bits are cleared they are cleared",
  898. " Use -T to test the archive before the bits are cleared",
  899. " Can also use -sf to save file list before zipping files",
  900. "",
  901. "Show files:",
  902. " -sf show files to operate on and exit (-sf- logfile only)",
  903. " -su as -sf but show escaped UTF-8 Unicode names also if exist",
  904. " -sU as -sf but show escaped UTF-8 Unicode names instead",
  905. " Any character not in the current locale is escaped as #Uxxxx, where x",
  906. " is hex digit, if 16-bit code is sufficient, or #Lxxxxxx if 24-bits",
  907. " are needed. If add -UN=e, Zip escapes all non-ASCII characters.",
  908. "",
  909. "Unicode:",
  910. " If compiled with Unicode support, Zip stores UTF-8 path of entries.",
  911. " This is backward compatible. Unicode paths allow better conversion",
  912. " of entry names between different character sets.",
  913. "",
  914. " New Unicode extra field includes checksum to verify Unicode path",
  915. " goes with standard path for that entry (as utilities like ZipNote",
  916. " can rename entries). If these do not match, use below options to",
  917. " set what Zip does:",
  918. " -UN=Quit - if mismatch, exit with error",
  919. " -UN=Warn - if mismatch, warn, ignore UTF-8 (default)",
  920. " -UN=Ignore - if mismatch, quietly ignore UTF-8",
  921. " -UN=No - ignore any UTF-8 paths, use standard paths for all",
  922. " An exception to -UN=N are entries with new UTF-8 bit set (instead",
  923. " of using extra fields). These are always handled as Unicode.",
  924. "",
  925. " Normally Zip escapes all chars outside current char set, but leaves",
  926. " as is supported chars, which may not be OK in path names. -UN=Escape",
  927. " escapes any character not ASCII:",
  928. " zip -sU -UN=e archive",
  929. " Can use either normal path or escaped Unicode path on command line",
  930. " to match files in archive.",
  931. "",
  932. " Zip now stores UTF-8 in entry path and comment fields on systems",
  933. " where UTF-8 char set is default, such as most modern Unix, and",
  934. " and on other systems in new extra fields with escaped versions in",
  935. " entry path and comment fields for backward compatibility.",
  936. " Option -UN=UTF8 will force storing UTF-8 in entry path and comment",
  937. " fields:",
  938. " -UN=UTF8 - store UTF-8 in entry path and comment fields",
  939. " This option can be useful for multi-byte char sets on Windows where",
  940. " escaped paths and comments can be too long to be valid as the UTF-8",
  941. " versions tend to be shorter.",
  942. "",
  943. " Only UTF-8 comments on UTF-8 native systems supported. UTF-8 comments",
  944. " for other systems planned in next release.",
  945. "",
  946. "Self extractor:",
  947. " -A Adjust offsets - a self extractor is created by prepending",
  948. " the extractor executable to archive, but internal offsets",
  949. " are then off. Use -A to fix offsets.",
  950. " -J Junk sfx - removes prepended extractor executable from",
  951. " self extractor, leaving a plain zip archive.",
  952. "",
  953. "More option highlights (see manual for additional options and details):",
  954. " -b dir when creating or updating archive, create the temp archive in",
  955. " dir, which allows using seekable temp file when writing to a",
  956. " write once CD, such archives compatible with more unzips",
  957. " (could require additional file copy if on another device)",
  958. " -MM input patterns must match at least one file and matched files",
  959. " must be readable or exit with OPEN error and abort archive",
  960. " (without -MM, both are warnings only, and if unreadable files",
  961. " are skipped OPEN error (18) returned after archive created)",
  962. " -nw no wildcards (wildcards are like any other character)",
  963. " -sc show command line arguments as processed and exit",
  964. " -sd show debugging as Zip does each step",
  965. " -so show all available options on this system",
  966. " -X default=strip old extra fields, -X- keep old, -X strip most",
  967. " -ws wildcards don't span directory boundaries in paths",
  968. ""
  969. };
  970. for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  971. {
  972. printf(text[i]);
  973. putchar('\n');
  974. }
  975. #ifdef DOS
  976. check_for_windows("Zip");
  977. #endif
  978. }
  979. /*
  980. * XXX version_info() in a separate file
  981. */
  982. local void version_info()
  983. /* Print verbose info about program version and compile time options
  984. to stdout. */
  985. {
  986. extent i; /* counter in text arrays */
  987. char *envptr;
  988. /* Bzip2 option string storage (with version). */
  989. #ifdef BZIP2_SUPPORT
  990. static char bz_opt_ver[81];
  991. static char bz_opt_ver2[81];
  992. static char bz_opt_ver3[81];
  993. #endif
  994. /* Options info array */
  995. static ZCONST char *comp_opts[] = {
  996. #ifdef ASM_CRC
  997. "ASM_CRC",
  998. #endif
  999. #ifdef ASMV
  1000. "ASMV",
  1001. #endif
  1002. #ifdef DYN_ALLOC
  1003. "DYN_ALLOC",
  1004. #endif
  1005. #ifdef MMAP
  1006. "MMAP",
  1007. #endif
  1008. #ifdef BIG_MEM
  1009. "BIG_MEM",
  1010. #endif
  1011. #ifdef MEDIUM_MEM
  1012. "MEDIUM_MEM",
  1013. #endif
  1014. #ifdef SMALL_MEM
  1015. "SMALL_MEM",
  1016. #endif
  1017. #ifdef DEBUG
  1018. "DEBUG",
  1019. #endif
  1020. #ifdef USE_EF_UT_TIME
  1021. "USE_EF_UT_TIME (store Universal Time)",
  1022. #endif
  1023. #ifdef NTSD_EAS
  1024. "NTSD_EAS (store NT Security Descriptor)",
  1025. #endif
  1026. #if defined(WIN32) && defined(NO_W32TIMES_IZFIX)
  1027. "NO_W32TIMES_IZFIX",
  1028. #endif
  1029. #ifdef VMS
  1030. #ifdef VMSCLI
  1031. "VMSCLI",
  1032. #endif
  1033. #ifdef VMS_IM_EXTRA
  1034. "VMS_IM_EXTRA",
  1035. #endif
  1036. #ifdef VMS_PK_EXTRA
  1037. "VMS_PK_EXTRA",
  1038. #endif
  1039. #endif /* VMS */
  1040. #ifdef WILD_STOP_AT_DIR
  1041. "WILD_STOP_AT_DIR (wildcards do not cross directory boundaries)",
  1042. #endif
  1043. #ifdef WIN32_OEM
  1044. "WIN32_OEM (store file paths on Windows as OEM)",
  1045. #endif
  1046. #ifdef BZIP2_SUPPORT
  1047. bz_opt_ver,
  1048. bz_opt_ver2,
  1049. bz_opt_ver3,
  1050. #endif
  1051. #ifdef S_IFLNK
  1052. # ifdef VMS
  1053. "SYMLINK_SUPPORT (symbolic links supported, if C RTL permits)",
  1054. # else
  1055. "SYMLINK_SUPPORT (symbolic links supported)",
  1056. # endif
  1057. #endif
  1058. #ifdef LARGE_FILE_SUPPORT
  1059. # ifdef USING_DEFAULT_LARGE_FILE_SUPPORT
  1060. "LARGE_FILE_SUPPORT (default settings)",
  1061. # else
  1062. "LARGE_FILE_SUPPORT (can read and write large files on file system)",
  1063. # endif
  1064. #endif
  1065. #ifdef ZIP64_SUPPORT
  1066. "ZIP64_SUPPORT (use Zip64 to store large files in archives)",
  1067. #endif
  1068. #ifdef UNICODE_SUPPORT
  1069. "UNICODE_SUPPORT (store and read UTF-8 Unicode paths)",
  1070. #endif
  1071. #ifdef UNIX
  1072. "STORE_UNIX_UIDs_GIDs (store UID/GID sizes/values using new extra field)",
  1073. # ifdef UIDGID_NOT_16BIT
  1074. "UIDGID_NOT_16BIT (old Unix 16-bit UID/GID extra field not used)",
  1075. # else
  1076. "UIDGID_16BIT (old Unix 16-bit UID/GID extra field also used)",
  1077. # endif
  1078. #endif
  1079. #if CRYPT && defined(PASSWD_FROM_STDIN)
  1080. "PASSWD_FROM_STDIN",
  1081. #endif /* CRYPT & PASSWD_FROM_STDIN */
  1082. NULL
  1083. };
  1084. static ZCONST char *zipenv_names[] = {
  1085. #ifndef VMS
  1086. # ifndef RISCOS
  1087. "ZIP"
  1088. # else /* RISCOS */
  1089. "Zip$Options"
  1090. # endif /* ?RISCOS */
  1091. #else /* VMS */
  1092. "ZIP_OPTS"
  1093. #endif /* ?VMS */
  1094. ,"ZIPOPT"
  1095. #ifdef AZTEC_C
  1096. , /* extremely lame compiler bug workaround */
  1097. #endif
  1098. #ifndef __RSXNT__
  1099. # ifdef __EMX__
  1100. ,"EMX"
  1101. ,"EMXOPT"
  1102. # endif
  1103. # if (defined(__GO32__) && (!defined(__DJGPP__) || __DJGPP__ < 2))
  1104. ,"GO32"
  1105. ,"GO32TMP"
  1106. # endif
  1107. # if (defined(__DJGPP__) && __DJGPP__ >= 2)
  1108. ,"TMPDIR"
  1109. # endif
  1110. #endif /* !__RSXNT__ */
  1111. #ifdef RISCOS
  1112. ,"Zip$Exts"
  1113. #endif
  1114. };
  1115. for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  1116. {
  1117. printf(copyright[i], "zip");
  1118. putchar('\n');
  1119. }
  1120. for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
  1121. {
  1122. printf(versinfolines[i], "Zip", VERSION, REVDATE);
  1123. putchar('\n');
  1124. }
  1125. version_local();
  1126. puts("Zip special compilation options:");
  1127. #if WSIZE != 0x8000
  1128. printf("\tWSIZE=%u\n", WSIZE);
  1129. #endif
  1130. /* Fill in bzip2 version. (32-char limit valid as of bzip 1.0.3.) */
  1131. #ifdef BZIP2_SUPPORT
  1132. sprintf( bz_opt_ver,
  1133. "BZIP2_SUPPORT (bzip2 library version %.32s)", BZ2_bzlibVersion());
  1134. sprintf( bz_opt_ver2,
  1135. " bzip2 code and library copyright (c) Julian R Seward");
  1136. sprintf( bz_opt_ver3,
  1137. " (See the bzip2 license for terms of use)");
  1138. #endif
  1139. for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
  1140. {
  1141. printf("\t%s\n",comp_opts[i]);
  1142. }
  1143. #ifdef USE_ZLIB
  1144. if (strcmp(ZLIB_VERSION, zlibVersion()) == 0)
  1145. printf("\tUSE_ZLIB [zlib version %s]\n", ZLIB_VERSION);
  1146. else
  1147. printf("\tUSE_ZLIB [compiled with version %s, using version %s]\n",
  1148. ZLIB_VERSION, zlibVersion());
  1149. i++; /* zlib use means there IS at least one compilation option */
  1150. #endif
  1151. #if CRYPT
  1152. printf("\t[encryption, version %d.%d%s of %s] (modified for Zip 3)\n\n",
  1153. CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
  1154. for (i = 0; i < sizeof(cryptnote)/sizeof(char *); i++)
  1155. {
  1156. printf(cryptnote[i]);
  1157. putchar('\n');
  1158. }
  1159. ++i; /* crypt support means there IS at least one compilation option */
  1160. #endif /* CRYPT */
  1161. if (i == 0)
  1162. puts("\t[none]");
  1163. puts("\nZip environment options:");
  1164. for (i = 0; i < sizeof(zipenv_names)/sizeof(char *); i++)
  1165. {
  1166. envptr = getenv(zipenv_names[i]);
  1167. printf("%16s: %s\n", zipenv_names[i],
  1168. ((envptr == (char *)NULL || *envptr == 0) ? "[none]" : envptr));
  1169. }
  1170. #ifdef DOS
  1171. check_for_windows("Zip");
  1172. #endif
  1173. }
  1174. #endif /* !WINDLL */
  1175. #ifndef PROCNAME
  1176. /* Default to case-sensitive matching of archive entries for the modes
  1177. that specifically operate on archive entries, as this archive may
  1178. have come from a system that allows paths in the archive to differ
  1179. only by case. Except for adding ARCHIVE (copy mode), this is how it
  1180. was done before. Note that some case-insensitive ports (WIN32, VMS)
  1181. define their own PROCNAME() in their respective osdep.h that use the
  1182. filter_match_case flag set to FALSE by the -ic option to enable
  1183. case-insensitive archive entry mathing. */
  1184. # define PROCNAME(n) procname(n, (action == ARCHIVE || action == DELETE \
  1185. || action == FRESHEN) \
  1186. && filter_match_case)
  1187. #endif /* PROCNAME */
  1188. #ifndef WINDLL
  1189. #ifndef MACOS
  1190. local void zipstdout()
  1191. /* setup for writing zip file on stdout */
  1192. {
  1193. mesg = stderr;
  1194. if (isatty(1))
  1195. ziperr(ZE_PARMS, "cannot write zip file to terminal");
  1196. if ((zipfile = malloc(4)) == NULL)
  1197. ziperr(ZE_MEM, "was processing arguments");
  1198. strcpy(zipfile, "-");
  1199. /*
  1200. if ((r = readzipfile()) != ZE_OK)
  1201. ziperr(r, zipfile);
  1202. */
  1203. }
  1204. #endif /* !MACOS */
  1205. local int check_unzip_version(unzippath)
  1206. char *unzippath;
  1207. {
  1208. #ifdef ZIP64_SUPPORT
  1209. /* Here is where we need to check for the version of unzip the user
  1210. * has. If creating a Zip64 archive need UnZip 6 or may fail.
  1211. */
  1212. char cmd[4004];
  1213. FILE *unzip_out = NULL;
  1214. char buf[1001];
  1215. float UnZip_Version = 0.0;
  1216. cmd[0] = '\0';
  1217. strncat(cmd, unzippath, 4000);
  1218. strcat(cmd, " -v");
  1219. if ((unzip_out = popen(cmd, "r")) == NULL) {
  1220. perror("unzip pipe error");
  1221. } else {
  1222. if (fgets(buf, 1000, unzip_out) == NULL) {
  1223. zipwarn("failed to get information from UnZip", "");
  1224. } else {
  1225. /* the first line should start with the version */
  1226. if (sscanf(buf, "UnZip %f ", &UnZip_Version) < 1) {
  1227. zipwarn("unexpected output of UnZip -v", "");
  1228. } else {
  1229. /* printf("UnZip %f\n", UnZip_Version); */
  1230. while (fgets(buf, 1000, unzip_out)) {
  1231. }
  1232. }
  1233. }
  1234. pclose(unzip_out);
  1235. }
  1236. if (UnZip_Version < 6.0 && zip64_archive) {
  1237. sprintf(buf, "Found UnZip version %4.2f", UnZip_Version);
  1238. zipwarn(buf, "");
  1239. zipwarn("Need UnZip 6.00 or later to test this Zip64 archive", "");
  1240. return 0;
  1241. }
  1242. #endif
  1243. return 1;
  1244. }
  1245. local void check_zipfile(zipname, zippath)
  1246. char *zipname;
  1247. char *zippath;
  1248. /* Invoke unzip -t on the given zip file */
  1249. {
  1250. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)
  1251. int status, len;
  1252. char *path, *p;
  1253. char *zipnam;
  1254. if ((zipnam = (char *)malloc(strlen(zipname) + 3)) == NULL)
  1255. ziperr(ZE_MEM, "was creating unzip zipnam");
  1256. # ifdef MSDOS
  1257. /* Add quotes for MSDOS. 8/11/04 */
  1258. strcpy(zipnam, "\""); /* accept spaces in name and path */
  1259. strcat(zipnam, zipname);
  1260. strcat(zipnam, "\"");
  1261. # else
  1262. strcpy(zipnam, zipname);
  1263. # endif
  1264. if (unzip_path) {
  1265. /* if user gave us the unzip to use go with it */
  1266. char *here;
  1267. int len;
  1268. char *cmd;
  1269. /* Replace first {} with archive name. If no {} append name to string. */
  1270. here = strstr(unzip_path, "{}");
  1271. if ((cmd = (char *)malloc(strlen(unzip_path) + strlen(zipnam) + 3)) == NULL)
  1272. ziperr(ZE_MEM, "was creating unzip cmd");
  1273. if (here) {
  1274. /* have {} so replace with temp name */
  1275. len = here - unzip_path;
  1276. strcpy(cmd, unzip_path);
  1277. cmd[len] = '\0';
  1278. strcat(cmd, " ");
  1279. strcat(cmd, zipnam);
  1280. strcat(cmd, " ");
  1281. strcat(cmd, here + 2);
  1282. } else {
  1283. /* No {} so append temp name to end */
  1284. strcpy(cmd, unzip_path);
  1285. strcat(cmd, " ");
  1286. strcat(cmd, zipnam);
  1287. }
  1288. status = system(cmd);
  1289. free(unzip_path);
  1290. unzip_path = NULL;
  1291. free(cmd);
  1292. } else {
  1293. /* Here is where we need to check for the version of unzip the user
  1294. * has. If creating a Zip64 archive need UnZip 6 or may fail.
  1295. */
  1296. if (check_unzip_version("unzip") == 0)
  1297. ZIPERR(ZE_TEST, zipfile);
  1298. status = spawnlp(P_WAIT, "unzip", "unzip", verbose ? "-t" : "-tqq",
  1299. zipnam, NULL);
  1300. # ifdef __human68k__
  1301. if (status == -1)
  1302. perror("unzip");
  1303. # else
  1304. /*
  1305. * unzip isn't in PATH range, assume an absolute path to zip in argv[0]
  1306. * and hope that unzip is in the same directory.
  1307. */
  1308. if (status == -1) {
  1309. p = MBSRCHR(zippath, '\\');
  1310. path = MBSRCHR((p == NULL ? zippath : p), '/');
  1311. if (path != NULL)
  1312. p = path;
  1313. if (p != NULL) {
  1314. len = (int)(p - zippath) + 1;
  1315. if ((path = malloc(len + sizeof("unzip.exe"))) == NULL)
  1316. ziperr(ZE_MEM, "was creating unzip path");
  1317. memcpy(path, zippath, len);
  1318. strcpy(&path[len], "unzip.exe");
  1319. if (check_unzip_version(path) == 0)
  1320. ZIPERR(ZE_TEST, zipfile);
  1321. status = spawnlp(P_WAIT, path, "unzip", verbose ? "-t" : "-tqq",
  1322. zipnam, NULL);
  1323. free(path);
  1324. }
  1325. if (status == -1)
  1326. perror("unzip");
  1327. }
  1328. }
  1329. # endif /* ?__human68k__ */
  1330. free(zipnam);
  1331. if (status != 0) {
  1332. #else /* (MSDOS && !__GO32__) || __human68k__ */
  1333. char *cmd;
  1334. int result;
  1335. /* Tell picky compilers to shut up about unused variables */
  1336. zippath = zippath;
  1337. if (unzip_path) {
  1338. /* user gave us a path to some unzip (may not be UnZip) */
  1339. char *here;
  1340. int len;
  1341. /* Replace first {} with archive name. If no {} append name to string. */
  1342. here = strstr(unzip_path, "{}");
  1343. if ((cmd = malloc(strlen(unzip_path) + strlen(zipname) + 3)) == NULL) {
  1344. ziperr(ZE_MEM, "building command string for testing archive");
  1345. }
  1346. if (here) {
  1347. /* have {} so replace with temp name */
  1348. len = here - unzip_path;
  1349. strcpy(cmd, unzip_path);
  1350. cmd[len] = '\0';
  1351. strcat(cmd, " ");
  1352. # ifdef UNIX
  1353. strcat(cmd, "'"); /* accept space or $ in name */
  1354. strcat(cmd, zipname);
  1355. strcat(cmd, "'");
  1356. # else
  1357. strcat(cmd, zipname);
  1358. # endif
  1359. strcat(cmd, " ");
  1360. strcat(cmd, here + 2);
  1361. } else {
  1362. /* No {} so append temp name to end */
  1363. strcpy(cmd, unzip_path);
  1364. strcat(cmd, " ");
  1365. # ifdef UNIX
  1366. strcat(cmd, "'"); /* accept space or $ in name */
  1367. strcat(cmd, zipname);
  1368. strcat(cmd, "'");
  1369. # else
  1370. strcat(cmd, zipname);
  1371. # endif
  1372. }
  1373. free(unzip_path);
  1374. unzip_path = NULL;
  1375. } else {
  1376. if ((cmd = malloc(20 + strlen(zipname))) == NULL) {
  1377. ziperr(ZE_MEM, "building command string for testing archive");
  1378. }
  1379. strcpy(cmd, "unzip -t ");
  1380. # ifdef QDOS
  1381. strcat(cmd, "-Q4 ");
  1382. # endif
  1383. if (!verbose) strcat(cmd, "-qq ");
  1384. if (check_unzip_version("unzip") == 0)
  1385. ZIPERR(ZE_TEST, zipfile);
  1386. # ifdef UNIX
  1387. strcat(cmd, "'"); /* accept space or $ in name */
  1388. strcat(cmd, zipname);
  1389. strcat(cmd, "'");
  1390. # else
  1391. strcat(cmd, zipname);
  1392. # endif
  1393. }
  1394. result = system(cmd);
  1395. # ifdef VMS
  1396. /* Convert success severity to 0, others to non-zero. */
  1397. result = ((result & STS$M_SEVERITY) != STS$M_SUCCESS);
  1398. # endif /* def VMS */
  1399. free(cmd);
  1400. cmd = NULL;
  1401. if (result) {
  1402. #endif /* ?((MSDOS && !__GO32__) || __human68k__) */
  1403. fprintf(mesg, "test of %s FAILED\n", zipfile);
  1404. ziperr(ZE_TEST, "original files unmodified");
  1405. }
  1406. if (noisy) {
  1407. fprintf(mesg, "test of %s OK\n", zipfile);
  1408. fflush(mesg);
  1409. }
  1410. if (logfile) {
  1411. fprintf(logfile, "test of %s OK\n", zipfile);
  1412. fflush(logfile);
  1413. }
  1414. }
  1415. #endif /* !WINDLL */
  1416. /* get_filters() is replaced by the following
  1417. local int get_filters(argc, argv)
  1418. */
  1419. /* The filter patterns for options -x, -i, and -R are
  1420. returned by get_option() one at a time, so use a linked
  1421. list to store until all args are processed. Then convert
  1422. to array for processing.
  1423. */
  1424. /* add a filter to the linked list */
  1425. local int add_filter(flag, pattern)
  1426. int flag;
  1427. char *pattern;
  1428. {
  1429. char *iname, *p = NULL;
  1430. FILE *fp;
  1431. struct filterlist_struct *filter = NULL;
  1432. /* should never happen */
  1433. if (flag != 'R' && flag != 'x' && flag != 'i') {
  1434. ZIPERR(ZE_LOGIC, "bad flag to add_filter");
  1435. }
  1436. if (pattern == NULL) {
  1437. ZIPERR(ZE_LOGIC, "null pattern to add_filter");
  1438. }
  1439. if (pattern[0] == '@') {
  1440. /* read file with 1 pattern per line */
  1441. if (pattern[1] == '\0') {
  1442. ZIPERR(ZE_PARMS, "missing file after @");
  1443. }
  1444. fp = fopen(pattern + 1, "r");
  1445. if (fp == NULL) {
  1446. sprintf(errbuf, "%c pattern file '%s'", flag, pattern);
  1447. ZIPERR(ZE_OPEN, errbuf);
  1448. }
  1449. while ((p = getnam(fp)) != NULL) {
  1450. if ((filter = (struct filterlist_struct *) malloc(sizeof(struct filterlist_struct))) == NULL) {
  1451. ZIPERR(ZE_MEM, "adding filter");
  1452. }
  1453. if (filterlist == NULL) {
  1454. /* first filter */
  1455. filterlist = filter; /* start of list */
  1456. lastfilter = filter;
  1457. } else {
  1458. lastfilter->next = filter; /* link to last filter in list */
  1459. lastfilter = filter;
  1460. }
  1461. iname = ex2in(p, 0, (int *)NULL);
  1462. free(p);
  1463. if (iname != NULL) {
  1464. lastfilter->pattern = in2ex(iname);
  1465. free(iname);
  1466. } else {
  1467. lastfilter->pattern = NULL;
  1468. }
  1469. lastfilter->flag = flag;
  1470. pcount++;
  1471. lastfilter->next = NULL;
  1472. }
  1473. fclose(fp);
  1474. } else {
  1475. /* single pattern */
  1476. if ((filter = (struct filterlist_struct *) malloc(sizeof(struct filterlist_struct))) == NULL) {
  1477. ZIPERR(ZE_MEM, "adding filter");
  1478. }
  1479. if (filterlist == NULL) {
  1480. /* first pattern */
  1481. filterlist = filter; /* start of list */
  1482. lastfilter = filter;
  1483. } else {
  1484. lastfilter->next = filter; /* link to last filter in list */
  1485. lastfilter = filter;
  1486. }
  1487. iname = ex2in(pattern, 0, (int *)NULL);
  1488. if (iname != NULL) {
  1489. lastfilter->pattern = in2ex(iname);
  1490. free(iname);
  1491. } else {
  1492. lastfilter->pattern = NULL;
  1493. }
  1494. lastfilter->flag = flag;
  1495. pcount++;
  1496. lastfilter->next = NULL;
  1497. }
  1498. return pcount;
  1499. }
  1500. /* convert list to patterns array */
  1501. local int filterlist_to_patterns()
  1502. {
  1503. unsigned i;
  1504. struct filterlist_struct *next = NULL;
  1505. if (pcount == 0) {
  1506. patterns = NULL;
  1507. return 0;
  1508. }
  1509. if ((patterns = (struct plist *) malloc((pcount + 1) * sizeof(struct plist)))
  1510. == NULL) {
  1511. ZIPERR(ZE_MEM, "was creating pattern list");
  1512. }
  1513. for (i = 0; i < pcount && filterlist != NULL; i++) {
  1514. switch (filterlist->flag) {
  1515. case 'i':
  1516. icount++;
  1517. break;
  1518. case 'R':
  1519. Rcount++;
  1520. break;
  1521. }
  1522. patterns[i].select = filterlist->flag;
  1523. patterns[i].zname = filterlist->pattern;
  1524. next = filterlist->next;
  1525. free(filterlist);
  1526. filterlist = next;
  1527. }
  1528. return pcount;
  1529. }
  1530. /* add a file argument to linked list */
  1531. local long add_name(filearg)
  1532. char *filearg;
  1533. {
  1534. char *name = NULL;
  1535. struct filelist_struct *fileentry = NULL;
  1536. if ((fileentry = (struct filelist_struct *) malloc(sizeof(struct filelist_struct))) == NULL) {
  1537. ZIPERR(ZE_MEM, "adding file");
  1538. }
  1539. if ((name = malloc(strlen(filearg) + 1)) == NULL) {
  1540. ZIPERR(ZE_MEM, "adding file");
  1541. }
  1542. strcpy(name, filearg);
  1543. fileentry->next = NULL;
  1544. fileentry->name = name;
  1545. if (filelist == NULL) {
  1546. /* first file argument */
  1547. filelist = fileentry; /* start of list */
  1548. lastfile = fileentry;
  1549. } else {
  1550. lastfile->next = fileentry; /* link to last filter in list */
  1551. lastfile = fileentry;
  1552. }
  1553. filearg_count++;
  1554. return filearg_count;
  1555. }
  1556. /* Running Stats
  1557. 10/30/04 */
  1558. local int DisplayRunningStats()
  1559. {
  1560. char tempstrg[100];
  1561. if (mesg_line_started) {
  1562. fprintf(mesg, "\n");
  1563. mesg_line_started = 0;
  1564. }
  1565. if (logfile_line_started) {
  1566. fprintf(logfile, "\n");
  1567. logfile_line_started = 0;
  1568. }
  1569. if (display_volume) {
  1570. if (noisy) {
  1571. fprintf(mesg, "%lu>%lu: ", current_in_disk + 1, current_disk + 1);
  1572. mesg_line_started = 1;
  1573. }
  1574. if (logall) {
  1575. fprintf(logfile, "%lu>%lu: ", current_in_disk + 1, current_disk + 1);
  1576. logfile_line_started = 1;
  1577. }
  1578. }
  1579. if (display_counts) {
  1580. if (noisy) {
  1581. fprintf(mesg, "%3ld/%3ld ", files_so_far, files_total - files_so_far);
  1582. mesg_line_started = 1;
  1583. }
  1584. if (logall) {
  1585. fprintf(logfile, "%3ld/%3ld ", files_so_far, files_total - files_so_far);
  1586. logfile_line_started = 1;
  1587. }
  1588. }
  1589. if (display_bytes) {
  1590. /* since file sizes can change as we go, use bytes_so_far from
  1591. initial scan so all adds up */
  1592. WriteNumString(bytes_so_far, tempstrg);
  1593. if (noisy) {
  1594. fprintf(mesg, "[%4s", tempstrg);
  1595. mesg_line_started = 1;
  1596. }
  1597. if (logall) {
  1598. fprintf(logfile, "[%4s", tempstrg);
  1599. logfile_line_started = 1;
  1600. }
  1601. if (bytes_total >= bytes_so_far) {
  1602. WriteNumString(bytes_total - bytes_so_far, tempstrg);
  1603. if (noisy)
  1604. fprintf(mesg, "/%4s] ", tempstrg);
  1605. if (logall)
  1606. fprintf(logfile, "/%4s] ", tempstrg);
  1607. } else {
  1608. WriteNumString(bytes_so_far - bytes_total, tempstrg);
  1609. if (noisy)
  1610. fprintf(mesg, "-%4s] ", tempstrg);
  1611. if (logall)
  1612. fprintf(logfile, "-%4s] ", tempstrg);
  1613. }
  1614. }
  1615. if (noisy)
  1616. fflush(mesg);
  1617. if (logall)
  1618. fflush(logfile);
  1619. return 0;
  1620. }
  1621. local int BlankRunningStats()
  1622. {
  1623. if (display_volume) {
  1624. if (noisy) {
  1625. fprintf(mesg, "%lu>%lu: ", current_in_disk + 1, current_disk + 1);
  1626. mesg_line_started = 1;
  1627. }
  1628. if (logall) {
  1629. fprintf(logfile, "%lu>%lu: ", current_in_disk + 1, current_disk + 1);
  1630. logfile_line_started = 1;
  1631. }
  1632. }
  1633. if (display_counts) {
  1634. if (noisy) {
  1635. fprintf(mesg, " / ");
  1636. mesg_line_started = 1;
  1637. }
  1638. if (logall) {
  1639. fprintf(logfile, " / ");
  1640. logfile_line_started = 1;
  1641. }
  1642. }
  1643. if (display_bytes) {
  1644. if (noisy) {
  1645. fprintf(mesg, " / ");
  1646. mesg_line_started = 1;
  1647. }
  1648. if (logall) {
  1649. fprintf(logfile, " / ");
  1650. logfile_line_started = 1;
  1651. }
  1652. }
  1653. if (noisy)
  1654. fflush(mesg);
  1655. if (logall)
  1656. fflush(logfile);
  1657. return 0;
  1658. }
  1659. #if CRYPT
  1660. #ifndef WINDLL
  1661. int encr_passwd(modeflag, pwbuf, size, zfn)
  1662. int modeflag;
  1663. char *pwbuf;
  1664. int size;
  1665. ZCONST char *zfn;
  1666. {
  1667. char *prompt;
  1668. /* Tell picky compilers to shut up about unused variables */
  1669. zfn = zfn;
  1670. prompt = (modeflag == ZP_PW_VERIFY) ?
  1671. "Verify password: " : "Enter password: ";
  1672. if (getp(prompt, pwbuf, size) == NULL) {
  1673. ziperr(ZE_PARMS, "stderr is not a tty");
  1674. }
  1675. return IZ_PW_ENTERED;
  1676. }
  1677. #endif /* !WINDLL */
  1678. #else /* !CRYPT */
  1679. int encr_passwd(modeflag, pwbuf, size, zfn)
  1680. int modeflag;
  1681. char *pwbuf;
  1682. int size;
  1683. ZCONST char *zfn;
  1684. {
  1685. /* Tell picky compilers to shut up about unused variables */
  1686. modeflag = modeflag; pwbuf = pwbuf; size = size; zfn = zfn;
  1687. return ZE_LOGIC; /* This function should never be called! */
  1688. }
  1689. #endif /* CRYPT */
  1690. /* rename a split
  1691. * A split has a tempfile name until it is closed, then
  1692. * here rename it as out_path the final name for the split.
  1693. */
  1694. int rename_split(temp_name, out_path)
  1695. char *temp_name;
  1696. char *out_path;
  1697. {
  1698. int r;
  1699. /* Replace old zip file with new zip file, leaving only the new one */
  1700. if ((r = replace(out_path, temp_name)) != ZE_OK)
  1701. {
  1702. zipwarn("new zip file left as: ", temp_name);
  1703. free((zvoid *)tempzip);
  1704. tempzip = NULL;
  1705. ZIPERR(r, "was replacing split file");
  1706. }
  1707. if (zip_attributes) {
  1708. setfileattr(out_path, zip_attributes);
  1709. }
  1710. return ZE_OK;
  1711. }
  1712. int set_filetype(out_path)
  1713. char *out_path;
  1714. {
  1715. #ifdef __BEOS__
  1716. /* Set the filetype of the zipfile to "application/zip" */
  1717. setfiletype( out_path, "application/zip" );
  1718. #endif
  1719. #ifdef __ATHEOS__
  1720. /* Set the filetype of the zipfile to "application/x-zip" */
  1721. setfiletype(out_path, "application/x-zip");
  1722. #endif
  1723. #ifdef MACOS
  1724. /* Set the Creator/Type of the zipfile to 'IZip' and 'ZIP ' */
  1725. setfiletype(out_path, 'IZip', 'ZIP ');
  1726. #endif
  1727. #ifdef RISCOS
  1728. /* Set the filetype of the zipfile to &DDC */
  1729. setfiletype(out_path, 0xDDC);
  1730. #endif
  1731. return ZE_OK;
  1732. }
  1733. /*
  1734. -------------------------------------------------------
  1735. Command Line Options
  1736. -------------------------------------------------------
  1737. Valid command line options.
  1738. The function get_option() uses this table to check if an
  1739. option is valid and if it takes a value (also called an
  1740. option argument). To add an option to zip just add it
  1741. to this table and add a case in the main switch to handle
  1742. it. If either shortopt or longopt not used set to "".
  1743. The fields:
  1744. shortopt - short option name (1 or 2 chars)
  1745. longopt - long option name
  1746. value_type - see zip.h for constants
  1747. negatable - option is negatable with trailing -
  1748. ID - unsigned long int returned for option
  1749. name - short description of option which is
  1750. returned on some errors and when options
  1751. are listed with -so option, can be NULL
  1752. */
  1753. /* Most option IDs are set to the shortopt char. For
  1754. multichar short options set to arbitrary unused constant. */
  1755. #define o_AC 0x101
  1756. #define o_AS 0x102
  1757. #define o_C2 0x103
  1758. #define o_C5 0x104
  1759. #define o_db 0x105
  1760. #define o_dc 0x106
  1761. #define o_dd 0x107
  1762. #define o_des 0x108
  1763. #define o_df 0x109
  1764. #define o_DF 0x110
  1765. #define o_dg 0x111
  1766. #define o_ds 0x112
  1767. #define o_du 0x113
  1768. #define o_dv 0x114
  1769. #define o_FF 0x115
  1770. #define o_FI 0x116
  1771. #define o_FS 0x117
  1772. #define o_h2 0x118
  1773. #define o_ic 0x119
  1774. #define o_jj 0x120
  1775. #define o_la 0x121
  1776. #define o_lf 0x122
  1777. #define o_li 0x123
  1778. #define o_ll 0x124
  1779. #define o_mm 0x125
  1780. #define o_MM 0x126
  1781. #define o_nw 0x127
  1782. #define o_RE 0x128
  1783. #define o_sb 0x129
  1784. #define o_sc 0x130
  1785. #define o_sd 0x131
  1786. #define o_sf 0x132
  1787. #define o_so 0x133
  1788. #define o_sp 0x134
  1789. #define o_su 0x135
  1790. #define o_sU 0x136
  1791. #define o_sv 0x137
  1792. #define o_tt 0x138
  1793. #define o_TT 0x139
  1794. #define o_UN 0x140
  1795. #define o_ve 0x141
  1796. #define o_VV 0x142
  1797. #define o_ws 0x143
  1798. #define o_ww 0x144
  1799. #define o_z64 0x145
  1800. #ifdef UNICODE_TEST
  1801. #define o_sC 0x146
  1802. #endif
  1803. /* the below is mainly from the old main command line
  1804. switch with a few changes */
  1805. struct option_struct far options[] = {
  1806. /* short longopt value_type negatable ID name */
  1807. #ifdef EBCDIC
  1808. {"a", "ascii", o_NO_VALUE, o_NOT_NEGATABLE, 'a', "to ascii"},
  1809. #endif /* EBCDIC */
  1810. #ifdef CMS_MVS
  1811. {"B", "binary", o_NO_VALUE, o_NOT_NEGATABLE, 'B', "binary"},
  1812. #endif /* CMS_MVS */
  1813. #ifdef TANDEM
  1814. {"B", "", o_NUMBER_VALUE, o_NOT_NEGATABLE, 'B', "nsk"},
  1815. #endif
  1816. {"0", "store", o_NO_VALUE, o_NOT_NEGATABLE, '0', "store"},
  1817. {"1", "compress-1", o_NO_VALUE, o_NOT_NEGATABLE, '1', "compress 1"},
  1818. {"2", "compress-2", o_NO_VALUE, o_NOT_NEGATABLE, '2', "compress 2"},
  1819. {"3", "compress-3", o_NO_VALUE, o_NOT_NEGATABLE, '3', "compress 3"},
  1820. {"4", "compress-4", o_NO_VALUE, o_NOT_NEGATABLE, '4', "compress 4"},
  1821. {"5", "compress-5", o_NO_VALUE, o_NOT_NEGATABLE, '5', "compress 5"},
  1822. {"6", "compress-6", o_NO_VALUE, o_NOT_NEGATABLE, '6', "compress 6"},
  1823. {"7", "compress-7", o_NO_VALUE, o_NOT_NEGATABLE, '7', "compress 7"},
  1824. {"8", "compress-8", o_NO_VALUE, o_NOT_NEGATABLE, '8', "compress 8"},
  1825. {"9", "compress-9", o_NO_VALUE, o_NOT_NEGATABLE, '9', "compress 9"},
  1826. {"A", "adjust-sfx", o_NO_VALUE, o_NOT_NEGATABLE, 'A', "adjust self extractor offsets"},
  1827. #if defined(WIN32)
  1828. {"AC", "archive-clear", o_NO_VALUE, o_NOT_NEGATABLE, o_AC, "clear DOS archive bit of included files"},
  1829. {"AS", "archive-set", o_NO_VALUE, o_NOT_NEGATABLE, o_AS, "include only files with archive bit set"},
  1830. #endif
  1831. {"b", "temp-path", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'b', "dir to use for temp archive"},
  1832. {"c", "entry-comments", o_NO_VALUE, o_NOT_NEGATABLE, 'c', "add comments for each entry"},
  1833. #ifdef VMS
  1834. {"C", "preserve-case", o_NO_VALUE, o_NEGATABLE, 'C', "Preserve (C-: down-) case all on VMS"},
  1835. {"C2", "preserve-case-2", o_NO_VALUE, o_NEGATABLE, o_C2, "Preserve (C2-: down-) case ODS2 on VMS"},
  1836. {"C5", "preserve-case-5", o_NO_VALUE, o_NEGATABLE, o_C5, "Preserve (C5-: down-) case ODS5 on VMS"},
  1837. #endif /* VMS */
  1838. {"d", "delete", o_NO_VALUE, o_NOT_NEGATABLE, 'd', "delete entries from archive"},
  1839. {"db", "display-bytes", o_NO_VALUE, o_NEGATABLE, o_db, "display running bytes"},
  1840. {"dc", "display-counts", o_NO_VALUE, o_NEGATABLE, o_dc, "display running file count"},
  1841. {"dd", "display-dots", o_NO_VALUE, o_NEGATABLE, o_dd, "display dots as process each file"},
  1842. {"dg", "display-globaldots",o_NO_VALUE, o_NEGATABLE, o_dg, "display dots for archive instead of files"},
  1843. {"ds", "dot-size", o_REQUIRED_VALUE, o_NOT_NEGATABLE, o_ds, "set progress dot size - default 10M bytes"},
  1844. {"du", "display-usize", o_NO_VALUE, o_NEGATABLE, o_du, "display uncompressed size in bytes"},
  1845. {"dv", "display-volume", o_NO_VALUE, o_NEGATABLE, o_dv, "display volume (disk) number"},
  1846. #ifdef MACOS
  1847. {"df", "datafork", o_NO_VALUE, o_NOT_NEGATABLE, o_df, "save datafork"},
  1848. #endif /* MACOS */
  1849. {"D", "no-dir-entries", o_NO_VALUE, o_NOT_NEGATABLE, 'D', "no entries for dirs themselves (-x */)"},
  1850. {"DF", "difference-archive",o_NO_VALUE, o_NOT_NEGATABLE, o_DF, "create diff archive with changed/new files"},
  1851. {"e", "encrypt", o_NO_VALUE, o_NOT_NEGATABLE, 'e', "encrypt entries, ask for password"},
  1852. #ifdef OS2
  1853. {"E", "longnames", o_NO_VALUE, o_NOT_NEGATABLE, 'E', "use OS2 longnames"},
  1854. #endif
  1855. {"F", "fix", o_NO_VALUE, o_NOT_NEGATABLE, 'F', "fix mostly intact archive (try first)"},
  1856. {"FF", "fixfix", o_NO_VALUE, o_NOT_NEGATABLE, o_FF, "try harder to fix archive (not as reliable)"},
  1857. {"FI", "fifo", o_NO_VALUE, o_NEGATABLE, o_FI, "read Unix FIFO (zip will wait on open pipe)"},
  1858. {"FS", "filesync", o_NO_VALUE, o_NOT_NEGATABLE, o_FS, "add/delete entries to make archive match OS"},
  1859. {"f", "freshen", o_NO_VALUE, o_NOT_NEGATABLE, 'f', "freshen existing archive entries"},
  1860. {"fd", "force-descriptors", o_NO_VALUE, o_NOT_NEGATABLE, o_des,"force data descriptors as if streaming"},
  1861. #ifdef ZIP64_SUPPORT
  1862. {"fz", "force-zip64", o_NO_VALUE, o_NEGATABLE, o_z64,"force use of Zip64 format, negate prevents"},
  1863. #endif
  1864. {"g", "grow", o_NO_VALUE, o_NOT_NEGATABLE, 'g', "grow existing archive instead of replace"},
  1865. #ifndef WINDLL
  1866. {"h", "help", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
  1867. {"H", "", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
  1868. {"?", "", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
  1869. {"h2", "more-help", o_NO_VALUE, o_NOT_NEGATABLE, o_h2, "extended help"},
  1870. #endif /* !WINDLL */
  1871. {"i", "include", o_VALUE_LIST, o_NOT_NEGATABLE, 'i', "include only files matching patterns"},
  1872. #if defined(VMS) || defined(WIN32)
  1873. {"ic", "ignore-case", o_NO_VALUE, o_NEGATABLE, o_ic, "ignore case when matching archive entries"},
  1874. #endif
  1875. #ifdef RISCOS
  1876. {"I", "no-image", o_NO_VALUE, o_NOT_NEGATABLE, 'I', "no image"},
  1877. #endif
  1878. {"j", "junk-paths", o_NO_VALUE, o_NOT_NEGATABLE, 'j', "strip paths and just store file names"},
  1879. #ifdef MACOS
  1880. {"jj", "absolute-path", o_NO_VALUE, o_NOT_NEGATABLE, o_jj, "MAC absolute path"},
  1881. #endif /* ?MACOS */
  1882. {"J", "junk-sfx", o_NO_VALUE, o_NOT_NEGATABLE, 'J', "strip self extractor from archive"},
  1883. {"k", "DOS-names", o_NO_VALUE, o_NOT_NEGATABLE, 'k', "force use of 8.3 DOS names"},
  1884. {"l", "to-crlf", o_NO_VALUE, o_NOT_NEGATABLE, 'l', "convert text file line ends - LF->CRLF"},
  1885. {"ll", "from-crlf", o_NO_VALUE, o_NOT_NEGATABLE, o_ll, "convert text file line ends - CRLF->LF"},
  1886. {"lf", "logfile-path",o_REQUIRED_VALUE, o_NOT_NEGATABLE, o_lf, "log to log file at path (default overwrite)"},
  1887. {"la", "log-append", o_NO_VALUE, o_NEGATABLE, o_la, "append to existing log file"},
  1888. {"li", "log-info", o_NO_VALUE, o_NEGATABLE, o_li, "include informational messages in log"},
  1889. #ifndef WINDLL
  1890. {"L", "license", o_NO_VALUE, o_NOT_NEGATABLE, 'L', "display license"},
  1891. #endif
  1892. {"m", "move", o_NO_VALUE, o_NOT_NEGATABLE, 'm', "add files to archive then delete files"},
  1893. {"mm", "", o_NO_VALUE, o_NOT_NEGATABLE, o_mm, "not used"},
  1894. {"MM", "must-match", o_NO_VALUE, o_NOT_NEGATABLE, o_MM, "error if in file not matched/not readable"},
  1895. {"n", "suffixes", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'n', "suffixes to not compress: .gz:.zip"},
  1896. {"nw", "no-wild", o_NO_VALUE, o_NOT_NEGATABLE, o_nw, "no wildcards during add or update"},
  1897. #if defined(AMIGA) || defined(MACOS)
  1898. {"N", "notes", o_NO_VALUE, o_NOT_NEGATABLE, 'N', "add notes as entry comments"},
  1899. #endif
  1900. {"o", "latest-time", o_NO_VALUE, o_NOT_NEGATABLE, 'o', "use latest entry time as archive time"},
  1901. {"O", "output-file", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'O', "set out zipfile different than in zipfile"},
  1902. {"p", "paths", o_NO_VALUE, o_NOT_NEGATABLE, 'p', "store paths"},
  1903. {"P", "password", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'P', "encrypt entries, option value is password"},
  1904. #if defined(QDOS) || defined(QLZIP)
  1905. {"Q", "Q-flag", o_NUMBER_VALUE, o_NOT_NEGATABLE, 'Q', "Q flag"},
  1906. #endif
  1907. {"q", "quiet", o_NO_VALUE, o_NOT_NEGATABLE, 'q', "quiet"},
  1908. {"r", "recurse-paths", o_NO_VALUE, o_NOT_NEGATABLE, 'r', "recurse down listed paths"},
  1909. {"R", "recurse-patterns", o_NO_VALUE, o_NOT_NEGATABLE, 'R', "recurse current dir and match patterns"},
  1910. {"RE", "regex", o_NO_VALUE, o_NOT_NEGATABLE, o_RE, "allow [list] matching (regex)"},
  1911. {"s", "split-size", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 's', "do splits, set split size (-s=0 no splits)"},
  1912. {"sp", "split-pause", o_NO_VALUE, o_NOT_NEGATABLE, o_sp, "pause while splitting to select destination"},
  1913. {"sv", "split-verbose", o_NO_VALUE, o_NOT_NEGATABLE, o_sv, "be verbose about creating splits"},
  1914. {"sb", "split-bell", o_NO_VALUE, o_NOT_NEGATABLE, o_sb, "when pause for next split ring bell"},
  1915. {"sc", "show-command",o_NO_VALUE, o_NOT_NEGATABLE, o_sc, "show command line"},
  1916. #ifdef UNICODE_TEST
  1917. {"sC", "create-files",o_NO_VALUE, o_NOT_NEGATABLE, o_sC, "create empty files using archive names"},
  1918. #endif
  1919. {"sd", "show-debug", o_NO_VALUE, o_NOT_NEGATABLE, o_sd, "show debug"},
  1920. {"sf", "show-files", o_NO_VALUE, o_NEGATABLE, o_sf, "show files to operate on and exit"},
  1921. {"so", "show-options",o_NO_VALUE, o_NOT_NEGATABLE, o_so, "show options"},
  1922. #ifdef UNICODE_SUPPORT
  1923. {"su", "show-unicode", o_NO_VALUE, o_NEGATABLE, o_su, "as -sf but also show escaped Unicode"},
  1924. {"sU", "show-just-unicode", o_NO_VALUE, o_NEGATABLE, o_sU, "as -sf but only show escaped Unicode"},
  1925. #endif
  1926. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(ATARI)
  1927. {"S", "", o_NO_VALUE, o_NOT_NEGATABLE, 'S', "include system and hidden"},
  1928. #endif /* MSDOS || OS2 || WIN32 || ATARI */
  1929. {"t", "from-date", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 't', "exclude before date"},
  1930. {"tt", "before-date", o_REQUIRED_VALUE, o_NOT_NEGATABLE, o_tt, "include before date"},
  1931. {"T", "test", o_NO_VALUE, o_NOT_NEGATABLE, 'T', "test updates before replacing archive"},
  1932. {"TT", "unzip-command", o_REQUIRED_VALUE,o_NOT_NEGATABLE,o_TT, "unzip command to use, name is added to end"},
  1933. {"u", "update", o_NO_VALUE, o_NOT_NEGATABLE, 'u', "update existing entries and add new"},
  1934. {"U", "copy-entries", o_NO_VALUE, o_NOT_NEGATABLE, 'U', "select from archive instead of file system"},
  1935. #ifdef UNICODE_SUPPORT
  1936. {"UN", "unicode", o_REQUIRED_VALUE, o_NOT_NEGATABLE, o_UN, "UN=quit, warn, ignore, no, escape"},
  1937. #endif
  1938. {"v", "verbose", o_NO_VALUE, o_NOT_NEGATABLE, 'v', "display additional information"},
  1939. {"", "version", o_NO_VALUE, o_NOT_NEGATABLE, o_ve, "(if no other args) show version information"},
  1940. #ifdef VMS
  1941. {"V", "VMS-portable", o_NO_VALUE, o_NOT_NEGATABLE, 'V', "Store VMS attributes, portable file format"},
  1942. {"VV", "VMS-specific", o_NO_VALUE, o_NOT_NEGATABLE, o_VV, "Store VMS attributes, VMS specific format"},
  1943. {"w", "VMS-versions", o_NO_VALUE, o_NOT_NEGATABLE, 'w', "store VMS versions"},
  1944. {"ww", "VMS-dot-versions", o_NO_VALUE, o_NOT_NEGATABLE, o_ww, "store VMS versions as \".nnn\""},
  1945. #endif /* VMS */
  1946. {"ws", "wild-stop-dirs", o_NO_VALUE, o_NOT_NEGATABLE, o_ws, "* stops at /, ** includes any /"},
  1947. {"x", "exclude", o_VALUE_LIST, o_NOT_NEGATABLE, 'x', "exclude files matching patterns"},
  1948. /* {"X", "no-extra", o_NO_VALUE, o_NOT_NEGATABLE, 'X', "no extra"},
  1949. */
  1950. {"X", "strip-extra", o_NO_VALUE, o_NEGATABLE, 'X', "-X- keep all ef, -X strip but critical ef"},
  1951. #ifdef S_IFLNK
  1952. {"y", "symlinks", o_NO_VALUE, o_NOT_NEGATABLE, 'y', "store symbolic links"},
  1953. #endif /* S_IFLNK */
  1954. {"z", "archive-comment", o_NO_VALUE, o_NOT_NEGATABLE, 'z', "ask for archive comment"},
  1955. {"Z", "compression-method", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'Z', "compression method"},
  1956. #if defined(MSDOS) || defined(OS2)
  1957. {"$", "volume-label", o_NO_VALUE, o_NOT_NEGATABLE, '$', "store volume label"},
  1958. #endif
  1959. #ifndef MACOS
  1960. {"@", "names-stdin", o_NO_VALUE, o_NOT_NEGATABLE, '@', "get file names from stdin, one per line"},
  1961. #endif /* !MACOS */
  1962. #ifdef NTSD_EAS
  1963. {"!", "use-privileges", o_NO_VALUE, o_NOT_NEGATABLE, '!', "use privileges"},
  1964. #endif
  1965. #ifdef RISCOS
  1966. {"/", "exts-to-swap", o_REQUIRED_VALUE, o_NOT_NEGATABLE, '/', "override Zip$Exts"},
  1967. #endif
  1968. /* the end of the list */
  1969. {NULL, NULL, o_NO_VALUE, o_NOT_NEGATABLE, 0, NULL} /* end has option_ID = 0 */
  1970. };
  1971. #ifndef USE_ZIPMAIN
  1972. int main(argc, argv)
  1973. #else
  1974. int zipmain(argc, argv)
  1975. #endif
  1976. int argc; /* number of tokens in command line */
  1977. char **argv; /* command line tokens */
  1978. /* Add, update, freshen, or delete zip entries in a zip file. See the
  1979. command help in help() above. */
  1980. {
  1981. int d; /* true if just adding to a zip file */
  1982. char *e; /* malloc'd comment buffer */
  1983. struct flist far *f; /* steps through found linked list */
  1984. int i; /* arg counter, root directory flag */
  1985. int kk; /* next arg type (formerly another re-use of "k") */
  1986. /* zip64 support 09/05/2003 R.Nausedat */
  1987. uzoff_t c; /* start of central directory */
  1988. uzoff_t t; /* length of central directory */
  1989. zoff_t k; /* marked counter, comment size, entry count */
  1990. uzoff_t n; /* total of entry len's */
  1991. int o; /* true if there were any ZE_OPEN errors */
  1992. char *p; /* steps through option arguments */
  1993. char *pp; /* temporary pointer */
  1994. int r; /* temporary variable */
  1995. int s; /* flag to read names from stdin */
  1996. uzoff_t csize; /* compressed file size for stats */
  1997. uzoff_t usize; /* uncompressed file size for stats */
  1998. ulg tf; /* file time */
  1999. int first_listarg = 0;/* index of first arg of "process these files" list */
  2000. struct zlist far *v; /* temporary variable */
  2001. struct zlist far * far *w; /* pointer to last link in zfiles list */
  2002. FILE *x /*, *y */; /* input and output zip files (y global) */
  2003. struct zlist far *z; /* steps through zfiles linked list */
  2004. int bad_open_is_error = 0; /* if read fails, 0=warning, 1=error */
  2005. #if 0
  2006. /* does not seem used */
  2007. #ifdef WINDLL
  2008. int retcode; /* return code for dll */
  2009. #endif /* WINDLL */
  2010. #endif
  2011. #if (!defined(VMS) && !defined(CMS_MVS))
  2012. char *zipbuf; /* stdio buffer for the zip file */
  2013. #endif /* !VMS && !CMS_MVS */
  2014. FILE *comment_stream; /* set to stderr if anything is read from stdin */
  2015. int all_current; /* used by File Sync to determine if all entries are current */
  2016. struct filelist_struct *filearg;
  2017. /* used by get_option */
  2018. unsigned long option; /* option ID returned by get_option */
  2019. int argcnt = 0; /* current argcnt in args */
  2020. int argnum = 0; /* arg number */
  2021. int optchar = 0; /* option state */
  2022. char *value = NULL; /* non-option arg, option value or NULL */
  2023. int negated = 0; /* 1 = option negated */
  2024. int fna = 0; /* current first non-opt arg */
  2025. int optnum = 0; /* index in table */
  2026. int show_options = 0; /* show options */
  2027. int show_what_doing = 0; /* show what doing */
  2028. int show_args = 0; /* show command line */
  2029. int seen_doubledash = 0; /* seen -- argument */
  2030. int key_needed = 0; /* prompt for encryption key */
  2031. int have_out = 0; /* if set in_path and out_path different archive */
  2032. #ifdef UNICODE_TEST
  2033. int create_files = 0;
  2034. #endif
  2035. char **args = NULL; /* could be wide argv */
  2036. #ifdef THEOS
  2037. /* the argument expansion from the standard library is full of bugs */
  2038. /* use mine instead */
  2039. _setargv(&argc, &argv);
  2040. setlocale(LC_CTYPE, "I");
  2041. #else
  2042. SETLOCALE(LC_CTYPE, "");
  2043. #endif
  2044. #ifdef UNICODE_SUPPORT
  2045. # ifdef UNIX
  2046. /* For Unix, set the locale to UTF-8. Any UTF-8 locale is
  2047. OK and they should all be the same. This allows seeing,
  2048. writing, and displaying (if the fonts are loaded) all
  2049. characters in UTF-8. */
  2050. {
  2051. char *loc;
  2052. /*
  2053. loc = setlocale(LC_CTYPE, NULL);
  2054. printf(" Initial language locale = '%s'\n", loc);
  2055. */
  2056. loc = setlocale(LC_CTYPE, "en_US.UTF-8");
  2057. /*
  2058. printf("langinfo %s\n", nl_langinfo(CODESET));
  2059. */
  2060. if (loc != NULL) {
  2061. /* using UTF-8 character set so can set UTF-8 GPBF bit 11 */
  2062. using_utf8 = 1;
  2063. /*
  2064. printf(" Locale set to %s\n", loc);
  2065. */
  2066. } else {
  2067. /*
  2068. printf(" Could not set Unicode UTF-8 locale\n");
  2069. */
  2070. }
  2071. }
  2072. # endif
  2073. #endif
  2074. #if defined(__IBMC__) && defined(__DEBUG_ALLOC__)
  2075. {
  2076. extern void DebugMalloc(void);
  2077. atexit(DebugMalloc);
  2078. }
  2079. #endif
  2080. #ifdef QDOS
  2081. {
  2082. extern void QDOSexit(void);
  2083. atexit(QDOSexit);
  2084. }
  2085. #endif
  2086. #ifdef NLM
  2087. {
  2088. extern void NLMexit(void);
  2089. atexit(NLMexit);
  2090. }
  2091. #endif
  2092. #ifdef RISCOS
  2093. set_prefix();
  2094. #endif
  2095. #ifdef __human68k__
  2096. fflush(stderr);
  2097. setbuf(stderr, NULL);
  2098. #endif
  2099. /* Re-initialize global variables to make the zip dll re-entrant. It is
  2100. * possible that we could get away with not re-initializing all of these
  2101. * but better safe than sorry.
  2102. */
  2103. #if defined(MACOS) || defined(WINDLL) || defined(USE_ZIPMAIN)
  2104. action = ADD; /* one of ADD, UPDATE, FRESHEN, DELETE, or ARCHIVE */
  2105. comadd = 0; /* 1=add comments for new files */
  2106. zipedit = 0; /* 1=edit zip comment and all file comments */
  2107. latest = 0; /* 1=set zip file time to time of latest file */
  2108. before = 0; /* 0=ignore, else exclude files before this time */
  2109. after = 0; /* 0=ignore, else exclude files newer than this time */
  2110. test = 0; /* 1=test zip file with unzip -t */
  2111. unzip_path = NULL; /* where to look for unzip command path */
  2112. tempdir = 0; /* 1=use temp directory (-b) */
  2113. junk_sfx = 0; /* 1=junk the sfx prefix */
  2114. #if defined(AMIGA) || defined(MACOS)
  2115. filenotes = 0;/* 1=take comments from AmigaDOS/MACOS filenotes */
  2116. #endif
  2117. #ifndef USE_ZIPMAIN
  2118. zipstate = -1;
  2119. #endif
  2120. tempzip = NULL;
  2121. fcount = 0;
  2122. recurse = 0; /* 1=recurse into directories; 2=match filenames */
  2123. dispose = 0; /* 1=remove files after put in zip file */
  2124. pathput = 1; /* 1=store path with name */
  2125. method = BEST; /* one of BEST, DEFLATE (only), or STORE (only) */
  2126. dosify = 0; /* 1=make new entries look like MSDOS */
  2127. verbose = 0; /* 1=report oddities in zip file structure */
  2128. fix = 0; /* 1=fix the zip file */
  2129. adjust = 0; /* 1=adjust offsets for sfx'd file (keep preamble) */
  2130. level = 6; /* 0=fastest compression, 9=best compression */
  2131. translate_eol = 0; /* Translate end-of-line LF -> CR LF */
  2132. #if defined(OS2) || defined(WIN32)
  2133. use_longname_ea = 0; /* 1=use the .LONGNAME EA as the file's name */
  2134. #endif
  2135. #ifdef NTSD_EAS
  2136. use_privileges = 0; /* 1=use security privileges overrides */
  2137. #endif
  2138. no_wild = 0; /* 1 = wildcards are disabled */
  2139. #ifdef WILD_STOP_AT_DIR
  2140. wild_stop_at_dir = 1; /* default wildcards do not include / in matches */
  2141. #else
  2142. wild_stop_at_dir = 0; /* default wildcards do include / in matches */
  2143. #endif
  2144. skip_this_disk = 0;
  2145. des_good = 0; /* Good data descriptor found */
  2146. des_crc = 0; /* Data descriptor CRC */
  2147. des_csize = 0; /* Data descriptor csize */
  2148. des_usize = 0; /* Data descriptor usize */
  2149. dot_size = 0; /* buffers processed in deflate per dot, 0 = no dots */
  2150. dot_count = 0; /* buffers seen, recyles at dot_size */
  2151. display_counts = 0; /* display running file count */
  2152. display_bytes = 0; /* display running bytes remaining */
  2153. display_globaldots = 0; /* display dots for archive instead of each file */
  2154. display_volume = 0; /* display current input and output volume (disk) numbers */
  2155. display_usize = 0; /* display uncompressed bytes */
  2156. files_so_far = 0; /* files processed so far */
  2157. bad_files_so_far = 0; /* bad files skipped so far */
  2158. files_total = 0; /* files total to process */
  2159. bytes_so_far = 0; /* bytes processed so far (from initial scan) */
  2160. good_bytes_so_far = 0; /* good bytes read so far */
  2161. bad_bytes_so_far = 0; /* bad bytes skipped so far */
  2162. bytes_total = 0; /* total bytes to process (from initial scan) */
  2163. logall = 0; /* 0 = warnings/errors, 1 = all */
  2164. logfile = NULL; /* pointer to open logfile or NULL */
  2165. logfile_append = 0; /* append to existing logfile */
  2166. logfile_path = NULL; /* pointer to path of logfile */
  2167. hidden_files = 0; /* process hidden and system files */
  2168. volume_label = 0; /* add volume label */
  2169. dirnames = 1; /* include directory entries by default */
  2170. #if defined(WIN32)
  2171. only_archive_set = 0; /* only include if DOS archive bit set */
  2172. clear_archive_bits = 0; /* clear DOS archive bit of included files */
  2173. #endif
  2174. linkput = 0; /* 1=store symbolic links as such */
  2175. noisy = 1; /* 0=quiet operation */
  2176. extra_fields = 1; /* 0=create minimum, 1=don't copy old, 2=keep old */
  2177. use_descriptors = 0; /* 1=use data descriptors 12/29/04 */
  2178. zip_to_stdout = 0; /* output zipfile to stdout 12/30/04 */
  2179. allow_empty_archive = 0;/* if no files, create empty archive anyway 12/28/05 */
  2180. copy_only = 0; /* 1=copying archive entries only */
  2181. output_seekable = 1; /* 1 = output seekable 3/13/05 EG */
  2182. #ifdef ZIP64_SUPPORT /* zip64 support 10/4/03 */
  2183. force_zip64 = -1; /* if 1 force entries to be zip64 */
  2184. /* mainly for streaming from stdin */
  2185. zip64_entry = 0; /* current entry needs Zip64 */
  2186. zip64_archive = 0; /* if 1 then at least 1 entry needs zip64 */
  2187. #endif
  2188. #ifdef UNICODE_SUPPORT
  2189. utf8_force = 0; /* 1=force storing UTF-8 as standard per AppNote bit 11 */
  2190. #endif
  2191. unicode_escape_all = 0; /* 1=escape all non-ASCII characters in paths */
  2192. unicode_mismatch = 1; /* unicode mismatch is 0=error, 1=warn, 2=ignore, 3=no */
  2193. scan_delay = 5; /* seconds before display Scanning files message */
  2194. scan_dot_time = 2; /* time in seconds between Scanning files dots */
  2195. scan_start = 0; /* start of scan */
  2196. scan_last = 0; /* time of last message */
  2197. scan_started = 0; /* scan has started */
  2198. scan_count = 0; /* Used for Scanning files ... message */
  2199. before = 0; /* 0=ignore, else exclude files before this time */
  2200. after = 0; /* 0=ignore, else exclude files newer than this time */
  2201. special = ".Z:.zip:.zoo:.arc:.lzh:.arj"; /* List of special suffixes */
  2202. key = NULL; /* Scramble password if scrambling */
  2203. key_needed = 0; /* Need scramble password */
  2204. tempath = NULL; /* Path for temporary files */
  2205. patterns = NULL; /* List of patterns to be matched */
  2206. pcount = 0; /* number of patterns */
  2207. icount = 0; /* number of include only patterns */
  2208. Rcount = 0; /* number of -R include patterns */
  2209. found = NULL; /* List of names found, or new found entry */
  2210. fnxt = &found;
  2211. /* used by get_option */
  2212. argcnt = 0; /* size of args */
  2213. argnum = 0; /* current arg number */
  2214. optchar = 0; /* option state */
  2215. value = NULL; /* non-option arg, option value or NULL */
  2216. negated = 0; /* 1 = option negated */
  2217. fna = 0; /* current first nonopt arg */
  2218. optnum = 0; /* option index */
  2219. show_options = 0; /* 1 = show options */
  2220. show_what_doing = 0; /* 1 = show what zip doing */
  2221. show_args = 0; /* 1 = show command line */
  2222. seen_doubledash = 0; /* seen -- argument */
  2223. zipfile = NULL; /* path of usual in and out zipfile */
  2224. tempzip = NULL; /* name of temp file */
  2225. y = NULL; /* output file now global so can change in splits */
  2226. in_file = NULL; /* current input file for splits */
  2227. in_split_path = NULL; /* current in split path */
  2228. in_path = NULL; /* used by splits to track changing split locations */
  2229. out_path = NULL; /* if set, use -O out_path as output */
  2230. have_out = 0; /* if set, in_path and out_path not the same archive */
  2231. total_disks = 0; /* total disks in archive */
  2232. current_in_disk = 0; /* current read split disk */
  2233. current_in_offset = 0; /* current offset in current read disk */
  2234. skip_current_disk = 0; /* if != 0 and fix then skip entries on this disk */
  2235. zip64_eocd_disk = 0; /* disk with Zip64 End Of Central Directory Record */
  2236. zip64_eocd_offset = 0; /* offset for Zip64 EOCD Record */
  2237. current_local_disk = 0; /* disk with current local header */
  2238. current_disk = 0; /* current disk number */
  2239. cd_start_disk = (ulg)-1; /* central directory start disk */
  2240. cd_start_offset = 0; /* offset of start of cd on cd start disk */
  2241. cd_entries_this_disk = 0; /* cd entries this disk */
  2242. total_cd_entries = 0; /* total cd entries in new/updated archive */
  2243. /* for split method 1 (keep split with local header open and update) */
  2244. current_local_tempname = NULL; /* name of temp file */
  2245. current_local_file = NULL; /* file pointer for current local header */
  2246. current_local_offset = 0; /* offset to start of current local header */
  2247. /* global */
  2248. bytes_this_split = 0; /* bytes written to the current split */
  2249. read_split_archive = 0; /* 1=scanzipf_reg detected spanning signature */
  2250. split_method = 0; /* 0=no splits, 1=update LHs, 2=data descriptors */
  2251. split_size = 0; /* how big each split should be */
  2252. split_bell = 0; /* when pause for next split ring bell */
  2253. bytes_prev_splits = 0; /* total bytes written to all splits before this */
  2254. bytes_this_entry = 0; /* bytes written for this entry across all splits */
  2255. noisy_splits = 0; /* be verbose about creating splits */
  2256. mesg_line_started = 0; /* 1=started writing a line to mesg */
  2257. logfile_line_started = 0; /* 1=started writing a line to logfile */
  2258. filelist = NULL;
  2259. filearg_count = 0;
  2260. allow_empty_archive = 0; /* if no files, allow creation of empty archive anyway */
  2261. bad_open_is_error = 0; /* if read fails, 0=warning, 1=error */
  2262. unicode_mismatch = 0; /* unicode mismatch is 0=error, 1=warn, 2=ignore, 3=no */
  2263. show_files = 0; /* show files to operate on and exit */
  2264. scan_delay = 5; /* seconds before display Scanning files message */
  2265. scan_dot_time = 2; /* time in seconds between Scanning files dots */
  2266. scan_started = 0; /* space at start of scan has been displayed */
  2267. scan_last = 0; /* Time last dot displayed for Scanning files message */
  2268. scan_start = 0; /* Time scanning started for Scanning files message */
  2269. #ifdef UNICODE_SUPPORT
  2270. use_wide_to_mb_default = 0;
  2271. #endif
  2272. filter_match_case = 1; /* default is to match case when matching archive entries */
  2273. allow_fifo = 0; /* 1=allow reading Unix FIFOs, waiting if pipe open */
  2274. #if !defined(MACOS) && !defined(USE_ZIPMAIN)
  2275. retcode = setjmp(zipdll_error_return);
  2276. if (retcode) {
  2277. return retcode;
  2278. }
  2279. #endif /* !MACOS */
  2280. #endif /* MACOS || WINDLL */
  2281. #if !defined(ALLOW_REGEX) && (defined(MSDOS) || defined(WIN32))
  2282. allow_regex = 0; /* 1 = allow [list] matching (regex) */
  2283. #else
  2284. allow_regex = 1;
  2285. #endif
  2286. mesg = (FILE *) stdout; /* cannot be made at link time for VMS */
  2287. comment_stream = (FILE *)stdin;
  2288. init_upper(); /* build case map table */
  2289. #ifdef LARGE_FILE_SUPPORT
  2290. /* test if we can support large files - 9/29/04 */
  2291. if (sizeof(zoff_t) < 8) {
  2292. ZIPERR(ZE_COMPERR, "LARGE_FILE_SUPPORT enabled but OS not supporting it");
  2293. }
  2294. #endif
  2295. /* test if sizes are the same - 12/30/04 */
  2296. if (sizeof(uzoff_t) != sizeof(zoff_t)){
  2297. ZIPERR(ZE_COMPERR, "uzoff_t not same size as zoff_t");
  2298. }
  2299. #if (defined(WIN32) && defined(USE_EF_UT_TIME))
  2300. /* For the Win32 environment, we may have to "prepare" the environment
  2301. prior to the tzset() call, to work around tzset() implementation bugs.
  2302. */
  2303. iz_w32_prepareTZenv();
  2304. #endif
  2305. #if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  2306. # ifndef VALID_TIMEZONE
  2307. # define VALID_TIMEZONE(tmp) \
  2308. (((tmp = getenv("TZ")) != NULL) && (*tmp != '\0'))
  2309. # endif
  2310. zp_tz_is_valid = VALID_TIMEZONE(p);
  2311. #if (defined(AMIGA) || defined(DOS))
  2312. if (!zp_tz_is_valid)
  2313. extra_fields = 0; /* disable storing "UT" time stamps */
  2314. #endif /* AMIGA || DOS */
  2315. #endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
  2316. /* For systems that do not have tzset() but supply this function using another
  2317. name (_tzset() or something similar), an appropiate "#define tzset ..."
  2318. should be added to the system specifc configuration section. */
  2319. #if (!defined(TOPS20) && !defined(VMS))
  2320. #if (!defined(RISCOS) && !defined(MACOS) && !defined(QDOS))
  2321. #if (!defined(BSD) && !defined(MTS) && !defined(CMS_MVS) && !defined(TANDEM))
  2322. tzset();
  2323. #endif
  2324. #endif
  2325. #endif
  2326. #ifdef VMSCLI
  2327. {
  2328. ulg status = vms_zip_cmdline(&argc, &argv);
  2329. if (!(status & 1))
  2330. return status;
  2331. }
  2332. #endif /* VMSCLI */
  2333. /* Substitutes the extended command line argument list produced by
  2334. * the MKS Korn Shell in place of the command line info from DOS.
  2335. */
  2336. /* extract extended argument list from environment */
  2337. expand_args(&argc, &argv);
  2338. #ifndef WINDLL
  2339. /* Process arguments */
  2340. diag("processing arguments");
  2341. /* First, check if just the help or version screen should be displayed */
  2342. if (argc == 1 && isatty(1)) /* no arguments, and output screen available */
  2343. { /* show help screen */
  2344. # ifdef VMSCLI
  2345. VMSCLI_help();
  2346. # else
  2347. help();
  2348. # endif
  2349. EXIT(ZE_OK);
  2350. }
  2351. /* Check -v here as env arg can change argc. Handle --version in main switch. */
  2352. else if (argc == 2 && strcmp(argv[1], "-v") == 0 &&
  2353. /* only "-v" as argument, and */
  2354. (isatty(1) || isatty(0)))
  2355. /* stdout or stdin is connected to console device */
  2356. { /* show diagnostic version info */
  2357. version_info();
  2358. EXIT(ZE_OK);
  2359. }
  2360. # ifndef VMS
  2361. # ifndef RISCOS
  2362. envargs(&argc, &argv, "ZIPOPT", "ZIP"); /* get options from environment */
  2363. # else /* RISCOS */
  2364. envargs(&argc, &argv, "ZIPOPT", "Zip$Options"); /* get options from environment */
  2365. getRISCOSexts("Zip$Exts"); /* get the extensions to swap from environment */
  2366. # endif /* ? RISCOS */
  2367. # else /* VMS */
  2368. envargs(&argc, &argv, "ZIPOPT", "ZIP_OPTS"); /* 4th arg for unzip compat. */
  2369. # endif /* ?VMS */
  2370. #endif /* !WINDLL */
  2371. zipfile = tempzip = NULL;
  2372. y = NULL;
  2373. d = 0; /* disallow adding to a zip file */
  2374. #if (!defined(MACOS) && !defined(WINDLL) && !defined(NLM))
  2375. signal(SIGINT, handler);
  2376. #ifdef SIGTERM /* AMIGADOS and others have no SIGTERM */
  2377. signal(SIGTERM, handler);
  2378. #endif
  2379. # if defined(SIGABRT) && !(defined(AMIGA) && defined(__SASC))
  2380. signal(SIGABRT, handler);
  2381. # endif
  2382. # ifdef SIGBREAK
  2383. signal(SIGBREAK, handler);
  2384. # endif
  2385. # ifdef SIGBUS
  2386. signal(SIGBUS, handler);
  2387. # endif
  2388. # ifdef SIGILL
  2389. signal(SIGILL, handler);
  2390. # endif
  2391. # ifdef SIGSEGV
  2392. signal(SIGSEGV, handler);
  2393. # endif
  2394. #endif /* !MACOS && !WINDLL && !NLM */
  2395. #ifdef NLM
  2396. NLMsignals();
  2397. #endif
  2398. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  2399. /* check if this Win32 OS has support for wide character calls */
  2400. has_win32_wide();
  2401. #endif
  2402. /* make copy of args that can use with insert_arg() used by get_option() */
  2403. args = copy_args(argv, 0);
  2404. kk = 0; /* Next non-option argument type */
  2405. s = 0; /* set by -@ */
  2406. /*
  2407. -------------------------------------------
  2408. Process command line using get_option
  2409. -------------------------------------------
  2410. Each call to get_option() returns either a command
  2411. line option and possible value or a non-option argument.
  2412. Arguments are permuted so that all options (-r, -b temp)
  2413. are returned before non-option arguments (zipfile).
  2414. Returns 0 when nothing left to read.
  2415. */
  2416. /* set argnum = 0 on first call to init get_option */
  2417. argnum = 0;
  2418. /* get_option returns the option ID and updates parameters:
  2419. args - usually same as argv if no argument file support
  2420. argcnt - current argc for args
  2421. value - char* to value (free() when done with it) or NULL if no value
  2422. negated - option was negated with trailing -
  2423. */
  2424. while ((option = get_option(&args, &argcnt, &argnum,
  2425. &optchar, &value, &negated,
  2426. &fna, &optnum, 0)))
  2427. {
  2428. switch (option)
  2429. {
  2430. #ifdef EBCDIC
  2431. case 'a':
  2432. aflag = ASCII;
  2433. printf("Translating to ASCII...\n");
  2434. break;
  2435. #endif /* EBCDIC */
  2436. #ifdef CMS_MVS
  2437. case 'B':
  2438. bflag = 1;
  2439. printf("Using binary mode...\n");
  2440. break;
  2441. #endif /* CMS_MVS */
  2442. #ifdef TANDEM
  2443. case 'B':
  2444. nskformatopt(value);
  2445. free(value);
  2446. break;
  2447. #endif
  2448. case '0':
  2449. method = STORE; level = 0; break;
  2450. case '1': case '2': case '3': case '4':
  2451. case '5': case '6': case '7': case '8': case '9':
  2452. /* Set the compression efficacy */
  2453. level = (int)option - '0'; break;
  2454. case 'A': /* Adjust unzipsfx'd zipfile: adjust offsets only */
  2455. adjust = 1; break;
  2456. #if defined(WIN32)
  2457. case o_AC:
  2458. clear_archive_bits = 1; break;
  2459. case o_AS:
  2460. /* Since some directories could be empty if no archive bits are
  2461. set for files in a directory, don't add directory entries (-D).
  2462. Just files with the archive bit set are added, including paths
  2463. (unless paths are excluded). All major unzips should create
  2464. directories for the paths as needed. */
  2465. dirnames = 0;
  2466. only_archive_set = 1; break;
  2467. #endif /* MSDOS || OS2 || WIN32 */
  2468. case 'b': /* Specify path for temporary file */
  2469. tempdir = 1;
  2470. tempath = value;
  2471. break;
  2472. case 'c': /* Add comments for new files in zip file */
  2473. comadd = 1; break;
  2474. /* -C, -C2, and -C5 are with -V */
  2475. case 'd': /* Delete files from zip file */
  2476. if (action != ADD) {
  2477. ZIPERR(ZE_PARMS, "specify just one action");
  2478. }
  2479. action = DELETE;
  2480. break;
  2481. #ifdef MACOS
  2482. case o_df:
  2483. MacZip.DataForkOnly = true;
  2484. break;
  2485. #endif /* MACOS */
  2486. case o_db:
  2487. if (negated)
  2488. display_bytes = 0;
  2489. else
  2490. display_bytes = 1;
  2491. break;
  2492. case o_dc:
  2493. if (negated)
  2494. display_counts = 0;
  2495. else
  2496. display_counts = 1;
  2497. break;
  2498. case o_dd:
  2499. /* display dots */
  2500. display_globaldots = 0;
  2501. if (negated) {
  2502. dot_count = 0;
  2503. } else {
  2504. /* set default dot size if dot_size not set (dot_count = 0) */
  2505. if (dot_count == 0)
  2506. /* default to 10 MB */
  2507. dot_size = 10 * 0x100000;
  2508. dot_count = -1;
  2509. }
  2510. break;
  2511. case o_dg:
  2512. /* display dots globally for archive instead of for each file */
  2513. if (negated) {
  2514. display_globaldots = 0;
  2515. } else {
  2516. display_globaldots = 1;
  2517. /* set default dot size if dot_size not set (dot_count = 0) */
  2518. if (dot_count == 0)
  2519. dot_size = 10 * 0x100000;
  2520. dot_count = -1;
  2521. }
  2522. break;
  2523. case o_ds:
  2524. /* input dot_size is now actual dot size to account for
  2525. different buffer sizes */
  2526. if (value == NULL)
  2527. dot_size = 10 * 0x100000;
  2528. else if (value[0] == '\0') {
  2529. /* default to 10 MB */
  2530. dot_size = 10 * 0x100000;
  2531. free(value);
  2532. } else {
  2533. dot_size = ReadNumString(value);
  2534. if (dot_size == (zoff_t)-1) {
  2535. sprintf(errbuf, "option -ds (--dot-size) has bad size: '%s'",
  2536. value);
  2537. free(value);
  2538. ZIPERR(ZE_PARMS, errbuf);
  2539. }
  2540. if (dot_size < 0x400) {
  2541. /* < 1 KB so there is no multiplier, assume MB */
  2542. dot_size *= 0x100000;
  2543. } else if (dot_size < 0x400L * 32) {
  2544. /* 1K <= dot_size < 32K */
  2545. sprintf(errbuf, "dot size must be at least 32 KB: '%s'", value);
  2546. free(value);
  2547. ZIPERR(ZE_PARMS, errbuf);
  2548. } else {
  2549. /* 32K <= dot_size */
  2550. }
  2551. free(value);
  2552. }
  2553. dot_count = -1;
  2554. break;
  2555. case o_du:
  2556. if (negated)
  2557. display_usize = 0;
  2558. else
  2559. display_usize = 1;
  2560. break;
  2561. case o_dv:
  2562. if (negated)
  2563. display_volume = 0;
  2564. else
  2565. display_volume = 1;
  2566. break;
  2567. case 'D': /* Do not add directory entries */
  2568. dirnames = 0; break;
  2569. case o_DF: /* Create a difference archive */
  2570. diff_mode = 1;
  2571. allow_empty_archive = 1;
  2572. break;
  2573. case 'e': /* Encrypt */
  2574. #if !CRYPT
  2575. ZIPERR(ZE_PARMS, "encryption not supported");
  2576. #else /* CRYPT */
  2577. if (key)
  2578. free(key);
  2579. key_needed = 1;
  2580. #endif /* !CRYPT */
  2581. break;
  2582. case 'F': /* fix the zip file */
  2583. fix = 1; break;
  2584. case o_FF: /* try harder to fix file */
  2585. fix = 2; break;
  2586. case o_FI:
  2587. if (negated)
  2588. allow_fifo = 0;
  2589. else
  2590. allow_fifo = 1;
  2591. break;
  2592. case o_FS: /* delete exiting entries in archive where there is
  2593. no matching file on file system */
  2594. filesync = 1; break;
  2595. case 'f': /* Freshen zip file--overwrite only */
  2596. if (action != ADD) {
  2597. ZIPERR(ZE_PARMS, "specify just one action");
  2598. }
  2599. action = FRESHEN;
  2600. break;
  2601. case 'g': /* Allow appending to a zip file */
  2602. d = 1; break;
  2603. #ifndef WINDLL
  2604. case 'h': case 'H': case '?': /* Help */
  2605. #ifdef VMSCLI
  2606. VMSCLI_help();
  2607. #else
  2608. help();
  2609. #endif
  2610. RETURN(finish(ZE_OK));
  2611. #endif /* !WINDLL */
  2612. #ifndef WINDLL
  2613. case o_h2: /* Extended Help */
  2614. help_extended();
  2615. RETURN(finish(ZE_OK));
  2616. #endif /* !WINDLL */
  2617. /* -i is with -x */
  2618. #if defined(VMS) || defined(WIN32)
  2619. case o_ic: /* Ignore case (case-insensitive matching of archive entries) */
  2620. if (negated)
  2621. filter_match_case = 1;
  2622. else
  2623. filter_match_case = 0;
  2624. break;
  2625. #endif
  2626. #ifdef RISCOS
  2627. case 'I': /* Don't scan through Image files */
  2628. scanimage = 0;
  2629. break;
  2630. #endif
  2631. #ifdef MACOS
  2632. case o_jj: /* store absolute path including volname */
  2633. MacZip.StoreFullPath = true;
  2634. break;
  2635. #endif /* ?MACOS */
  2636. case 'j': /* Junk directory names */
  2637. pathput = 0; break;
  2638. case 'J': /* Junk sfx prefix */
  2639. junk_sfx = 1; break;
  2640. case 'k': /* Make entries using DOS names (k for Katz) */
  2641. dosify = 1; break;
  2642. case 'l': /* Translate end-of-line */
  2643. translate_eol = 1; break;
  2644. case o_ll:
  2645. translate_eol = 2; break;
  2646. case o_lf:
  2647. /* open a logfile */
  2648. /* allow multiple use of option but only last used */
  2649. if (logfile_path) {
  2650. free(logfile_path);
  2651. }
  2652. logfile_path = value;
  2653. break;
  2654. case o_la:
  2655. /* append to existing logfile */
  2656. if (negated)
  2657. logfile_append = 0;
  2658. else
  2659. logfile_append = 1;
  2660. break;
  2661. case o_li:
  2662. /* log all including informational messages */
  2663. if (negated)
  2664. logall = 0;
  2665. else
  2666. logall = 1;
  2667. break;
  2668. #ifndef WINDLL
  2669. case 'L': /* Show license */
  2670. license();
  2671. RETURN(finish(ZE_OK));
  2672. #endif
  2673. case 'm': /* Delete files added or updated in zip file */
  2674. dispose = 1; break;
  2675. case o_mm: /* To prevent use of -mm for -MM */
  2676. ZIPERR(ZE_PARMS, "-mm not supported, Must_Match is -MM");
  2677. dispose = 1; break;
  2678. case o_MM: /* Exit with error if input file can't be read */
  2679. bad_open_is_error = 1; break;
  2680. case 'n': /* Don't compress files with a special suffix */
  2681. special = value;
  2682. /* special = NULL; */ /* will be set at next argument */
  2683. break;
  2684. case o_nw: /* no wildcards - wildcards are handled like other characters */
  2685. no_wild = 1;
  2686. break;
  2687. #if defined(AMIGA) || defined(MACOS)
  2688. case 'N': /* Get zipfile comments from AmigaDOS/MACOS filenotes */
  2689. filenotes = 1; break;
  2690. #endif
  2691. case 'o': /* Set zip file time to time of latest file in it */
  2692. latest = 1; break;
  2693. case 'O': /* Set output file different than input archive */
  2694. out_path = ziptyp(value);
  2695. free(value);
  2696. have_out = 1;
  2697. break;
  2698. case 'p': /* Store path with name */
  2699. break; /* (do nothing as annoyance avoidance) */
  2700. case 'P': /* password for encryption */
  2701. if (key != NULL) {
  2702. free(key);
  2703. }
  2704. #if CRYPT
  2705. key = value;
  2706. key_needed = 0;
  2707. #else
  2708. ZIPERR(ZE_PARMS, "encryption not supported");
  2709. #endif /* CRYPT */
  2710. break;
  2711. #if defined(QDOS) || defined(QLZIP)
  2712. case 'Q':
  2713. qlflag = strtol(value, NULL, 10);
  2714. /* qlflag = strtol((p+1), &p, 10); */
  2715. /* p--; */
  2716. if (qlflag == 0) qlflag = 4;
  2717. free(value);
  2718. break;
  2719. #endif
  2720. case 'q': /* Quiet operation */
  2721. noisy = 0;
  2722. #ifdef MACOS
  2723. MacZip.MacZip_Noisy = false;
  2724. #endif /* MACOS */
  2725. if (verbose) verbose--;
  2726. break;
  2727. case 'r': /* Recurse into subdirectories, match full path */
  2728. if (recurse == 2) {
  2729. ZIPERR(ZE_PARMS, "do not specify both -r and -R");
  2730. }
  2731. recurse = 1; break;
  2732. case 'R': /* Recurse into subdirectories, match filename */
  2733. if (recurse == 1) {
  2734. ZIPERR(ZE_PARMS, "do not specify both -r and -R");
  2735. }
  2736. recurse = 2; break;
  2737. case o_RE: /* Allow [list] matching (regex) */
  2738. allow_regex = 1; break;
  2739. case o_sc: /* show command line args */
  2740. show_args = 1; break;
  2741. #ifdef UNICODE_TEST
  2742. case o_sC: /* create empty files from archive names */
  2743. create_files = 1;
  2744. show_files = 1; break;
  2745. #endif
  2746. case o_sd: /* show debugging */
  2747. show_what_doing = 1; break;
  2748. case o_sf: /* show files to operate on */
  2749. if (!negated)
  2750. show_files = 1;
  2751. else
  2752. show_files = 2;
  2753. break;
  2754. case o_so: /* show all options */
  2755. show_options = 1; break;
  2756. #ifdef UNICODE_SUPPORT
  2757. case o_su: /* -sf but also show Unicode if exists */
  2758. if (!negated)
  2759. show_files = 3;
  2760. else
  2761. show_files = 4;
  2762. break;
  2763. case o_sU: /* -sf but only show Unicode if exists or normal if not */
  2764. if (!negated)
  2765. show_files = 5;
  2766. else
  2767. show_files = 6;
  2768. break;
  2769. #endif
  2770. case 's': /* enable split archives */
  2771. /* get the split size from value */
  2772. if (strcmp(value, "-") == 0) {
  2773. /* -s- do not allow splits */
  2774. split_method = -1;
  2775. } else {
  2776. split_size = ReadNumString(value);
  2777. if (split_size == (uzoff_t)-1) {
  2778. sprintf(errbuf, "bad split size: '%s'", value);
  2779. ZIPERR(ZE_PARMS, errbuf);
  2780. }
  2781. if (split_size == 0) {
  2782. /* do not allow splits */
  2783. split_method = -1;
  2784. } else {
  2785. if (split_method == 0) {
  2786. split_method = 1;
  2787. }
  2788. if (split_size < 0x400) {
  2789. /* < 1 KB there is no multiplier, assume MB */
  2790. split_size *= 0x100000;
  2791. }
  2792. /* By setting the minimum split size to 64 KB we avoid
  2793. not having enough room to write a header unsplit
  2794. which is required */
  2795. if (split_size < 0x400L * 64) {
  2796. /* split_size < 64K */
  2797. sprintf(errbuf, "minimum split size is 64 KB: '%s'", value);
  2798. free(value);
  2799. ZIPERR(ZE_PARMS, errbuf);
  2800. }
  2801. }
  2802. }
  2803. free(value);
  2804. break;
  2805. case o_sb: /* when pause for next split ring bell */
  2806. split_bell = 1;
  2807. break;
  2808. case o_sp: /* enable split select - pause splitting between splits */
  2809. use_descriptors = 1;
  2810. split_method = 2;
  2811. break;
  2812. case o_sv: /* be verbose about creating splits */
  2813. noisy_splits = 1;
  2814. break;
  2815. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(ATARI)
  2816. case 'S':
  2817. hidden_files = 1; break;
  2818. #endif /* MSDOS || OS2 || WIN32 || ATARI */
  2819. #ifdef MACOS
  2820. case 'S':
  2821. MacZip.IncludeInvisible = true; break;
  2822. #endif /* MACOS */
  2823. case 't': /* Exclude files earlier than specified date */
  2824. {
  2825. int yyyy, mm, dd; /* results of sscanf() */
  2826. /* Support ISO 8601 & American dates */
  2827. if ((sscanf(value, "%4d-%2d-%2d", &yyyy, &mm, &dd) != 3 &&
  2828. sscanf(value, "%2d%2d%4d", &mm, &dd, &yyyy) != 3) ||
  2829. mm < 1 || mm > 12 || dd < 1 || dd > 31) {
  2830. ZIPERR(ZE_PARMS,
  2831. "invalid date entered for -t option - use mmddyyyy or yyyy-mm-dd");
  2832. }
  2833. before = dostime(yyyy, mm, dd, 0, 0, 0);
  2834. }
  2835. free(value);
  2836. break;
  2837. case o_tt: /* Exclude files at or after specified date */
  2838. {
  2839. int yyyy, mm, dd; /* results of sscanf() */
  2840. /* Support ISO 8601 & American dates */
  2841. if ((sscanf(value, "%4d-%2d-%2d", &yyyy, &mm, &dd) != 3 &&
  2842. sscanf(value, "%2d%2d%4d", &mm, &dd, &yyyy) != 3) ||
  2843. mm < 1 || mm > 12 || dd < 1 || dd > 31) {
  2844. ZIPERR(ZE_PARMS,
  2845. "invalid date entered for -tt option - use mmddyyyy or yyyy-mm-dd");
  2846. }
  2847. after = dostime(yyyy, mm, dd, 0, 0, 0);
  2848. }
  2849. free(value);
  2850. break;
  2851. case 'T': /* test zip file */
  2852. test = 1; break;
  2853. case o_TT: /* command path to use instead of 'unzip -t ' */
  2854. if (unzip_path)
  2855. free(unzip_path);
  2856. unzip_path = value;
  2857. break;
  2858. case 'U': /* Select archive entries to keep or operate on */
  2859. if (action != ADD) {
  2860. ZIPERR(ZE_PARMS, "specify just one action");
  2861. }
  2862. action = ARCHIVE;
  2863. break;
  2864. #ifdef UNICODE_SUPPORT
  2865. case o_UN: /* Unicode */
  2866. if (abbrevmatch("quit", value, 0, 1)) {
  2867. /* Unicode path mismatch is error */
  2868. unicode_mismatch = 0;
  2869. } else if (abbrevmatch("warn", value, 0, 1)) {
  2870. /* warn of mismatches and continue */
  2871. unicode_mismatch = 1;
  2872. } else if (abbrevmatch("ignore", value, 0, 1)) {
  2873. /* ignore mismatches and continue */
  2874. unicode_mismatch = 2;
  2875. } else if (abbrevmatch("no", value, 0, 1)) {
  2876. /* no use Unicode path */
  2877. unicode_mismatch = 3;
  2878. } else if (abbrevmatch("escape", value, 0, 1)) {
  2879. /* escape all non-ASCII characters */
  2880. unicode_escape_all = 1;
  2881. } else if (abbrevmatch("UTF8", value, 0, 1)) {
  2882. /* force storing UTF-8 as standard per AppNote bit 11 */
  2883. utf8_force = 1;
  2884. } else {
  2885. zipwarn("-UN must be Quit, Warn, Ignore, No, Escape, or UTF8: ", value);
  2886. free(value);
  2887. ZIPERR(ZE_PARMS, "-UN (unicode) bad value");
  2888. }
  2889. free(value);
  2890. break;
  2891. #endif
  2892. case 'u': /* Update zip file--overwrite only if newer */
  2893. if (action != ADD) {
  2894. ZIPERR(ZE_PARMS, "specify just one action");
  2895. }
  2896. action = UPDATE;
  2897. break;
  2898. case 'v': /* Either display version information or */
  2899. case o_ve: /* Mention oddities in zip file structure */
  2900. if (option == o_ve || /* --version */
  2901. (argcnt == 2 && strlen(args[1]) == 2)) { /* -v only */
  2902. /* display version */
  2903. #ifndef WINDLL
  2904. version_info();
  2905. #else
  2906. zipwarn("version information not supported for dll", "");
  2907. #endif
  2908. RETURN(finish(ZE_OK));
  2909. } else {
  2910. noisy = 1;
  2911. verbose++;
  2912. }
  2913. break;
  2914. #ifdef VMS
  2915. case 'C': /* Preserve case (- = down-case) all. */
  2916. if (negated)
  2917. { /* Down-case all. */
  2918. if ((vms_case_2 > 0) || (vms_case_5 > 0))
  2919. {
  2920. ZIPERR( ZE_PARMS, "Conflicting case directives (-C-)");
  2921. }
  2922. vms_case_2 = -1;
  2923. vms_case_5 = -1;
  2924. }
  2925. else
  2926. { /* Not negated. Preserve all. */
  2927. if ((vms_case_2 < 0) || (vms_case_5 < 0))
  2928. {
  2929. ZIPERR( ZE_PARMS, "Conflicting case directives (-C)");
  2930. }
  2931. vms_case_2 = 1;
  2932. vms_case_5 = 1;
  2933. }
  2934. break;
  2935. case o_C2: /* Preserve case (- = down-case) ODS2. */
  2936. if (negated)
  2937. { /* Down-case ODS2. */
  2938. if (vms_case_2 > 0)
  2939. {
  2940. ZIPERR( ZE_PARMS, "Conflicting case directives (-C2-)");
  2941. }
  2942. vms_case_2 = -1;
  2943. }
  2944. else
  2945. { /* Not negated. Preserve ODS2. */
  2946. if (vms_case_2 < 0)
  2947. {
  2948. ZIPERR( ZE_PARMS, "Conflicting case directives (-C2)");
  2949. }
  2950. vms_case_2 = 1;
  2951. }
  2952. break;
  2953. case o_C5: /* Preserve case (- = down-case) ODS5. */
  2954. if (negated)
  2955. { /* Down-case ODS5. */
  2956. if (vms_case_5 > 0)
  2957. {
  2958. ZIPERR( ZE_PARMS, "Conflicting case directives (-C5-)");
  2959. }
  2960. vms_case_5 = -1;
  2961. }
  2962. else
  2963. { /* Not negated. Preserve ODS5. */
  2964. if (vms_case_5 < 0)
  2965. {
  2966. ZIPERR( ZE_PARMS, "Conflicting case directives (-C5)");
  2967. }
  2968. vms_case_5 = 1;
  2969. }
  2970. break;
  2971. case 'V': /* Store in VMS format. (Record multiples.) */
  2972. vms_native = 1; break;
  2973. /* below does work with new parser but doesn't allow tracking
  2974. -VV separately, like adding a separate description */
  2975. /* vms_native++; break; */
  2976. case o_VV: /* Store in VMS specific format */
  2977. vms_native = 2; break;
  2978. case 'w': /* Append the VMS version number */
  2979. vmsver |= 1; break;
  2980. case o_ww: /* Append the VMS version number as ".nnn". */
  2981. vmsver |= 3; break;
  2982. #endif /* VMS */
  2983. case o_ws: /* Wildcards do not include directory boundaries in matches */
  2984. wild_stop_at_dir = 1;
  2985. break;
  2986. case 'i': /* Include only the following files */
  2987. /* if nothing matches include list then still create an empty archive */
  2988. allow_empty_archive = 1;
  2989. case 'x': /* Exclude following files */
  2990. add_filter((int) option, value);
  2991. free(value);
  2992. break;
  2993. #ifdef S_IFLNK
  2994. case 'y': /* Store symbolic links as such */
  2995. linkput = 1; break;
  2996. #endif /* S_IFLNK */
  2997. case 'z': /* Edit zip file comment */
  2998. zipedit = 1; break;
  2999. case 'Z': /* Compression method */
  3000. if (abbrevmatch("deflate", value, 0, 1)) {
  3001. /* deflate */
  3002. method = DEFLATE;
  3003. } else if (abbrevmatch("store", value, 0, 1)) {
  3004. /* store */
  3005. method = STORE;
  3006. } else if (abbrevmatch("bzip2", value, 0, 1)) {
  3007. /* bzip2 */
  3008. #ifdef BZIP2_SUPPORT
  3009. method = BZIP2;
  3010. #else
  3011. ZIPERR(ZE_COMPERR, "Compression method bzip2 not enabled");
  3012. #endif
  3013. } else {
  3014. #ifdef BZIP2_SUPPORT
  3015. zipwarn("valid compression methods are: store, deflate, bzip2", "");
  3016. #else
  3017. zipwarn("valid compression methods are: store, deflate)", "");
  3018. #endif
  3019. zipwarn("unknown compression method found: ", value);
  3020. free(value);
  3021. ZIPERR(ZE_PARMS, "Option -Z (--compression-method): unknown method");
  3022. }
  3023. free(value);
  3024. break;
  3025. #if defined(MSDOS) || defined(OS2)
  3026. case '$': /* Include volume label */
  3027. volume_label = 1; break;
  3028. #endif
  3029. #ifndef MACOS
  3030. case '@': /* read file names from stdin */
  3031. comment_stream = NULL;
  3032. s = 1; /* defer -@ until have zipfile name */
  3033. break;
  3034. #endif /* !MACOS */
  3035. case 'X':
  3036. if (negated)
  3037. extra_fields = 2;
  3038. else
  3039. extra_fields = 0;
  3040. break;
  3041. #ifdef OS2
  3042. case 'E':
  3043. /* use the .LONGNAME EA (if any) as the file's name. */
  3044. use_longname_ea = 1;
  3045. break;
  3046. #endif
  3047. #ifdef NTSD_EAS
  3048. case '!':
  3049. /* use security privilege overrides */
  3050. use_privileges = 1;
  3051. break;
  3052. #endif
  3053. #ifdef RISCOS
  3054. case '/':
  3055. exts2swap = value; /* override Zip$Exts */
  3056. break;
  3057. #endif
  3058. case o_des:
  3059. use_descriptors = 1;
  3060. break;
  3061. #ifdef ZIP64_SUPPORT
  3062. case o_z64: /* Force creation of Zip64 entries */
  3063. if (negated) {
  3064. force_zip64 = 0;
  3065. } else {
  3066. force_zip64 = 1;
  3067. }
  3068. break;
  3069. #endif
  3070. case o_NON_OPTION_ARG:
  3071. /* not an option */
  3072. /* no more options as permuting */
  3073. /* just dash also ends up here */
  3074. if (recurse != 2 && kk == 0 && patterns == NULL) {
  3075. /* have all filters so convert filterlist to patterns array
  3076. as PROCNAME needs patterns array */
  3077. filterlist_to_patterns();
  3078. }
  3079. /* "--" stops arg processing for remaining args */
  3080. /* ignore only first -- */
  3081. if (strcmp(value, "--") == 0 && seen_doubledash == 0) {
  3082. /* -- */
  3083. seen_doubledash = 1;
  3084. if (kk == 0) {
  3085. ZIPERR(ZE_PARMS, "can't use -- before archive name");
  3086. }
  3087. /* just ignore as just marks what follows as non-option arguments */
  3088. } else if (kk == 6) {
  3089. /* value is R pattern */
  3090. add_filter((int)'R', value);
  3091. free(value);
  3092. if (first_listarg == 0) {
  3093. first_listarg = argnum;
  3094. }
  3095. } else switch (kk)
  3096. {
  3097. case 0:
  3098. /* first non-option arg is zipfile name */
  3099. #if (!defined(MACOS) && !defined(WINDLL))
  3100. if (strcmp(value, "-") == 0) { /* output zipfile is dash */
  3101. /* just a dash */
  3102. zipstdout();
  3103. } else
  3104. #endif /* !MACOS && !WINDLL */
  3105. {
  3106. /* name of zipfile */
  3107. if ((zipfile = ziptyp(value)) == NULL) {
  3108. ZIPERR(ZE_MEM, "was processing arguments");
  3109. }
  3110. /* read zipfile if exists */
  3111. /*
  3112. if ((r = readzipfile()) != ZE_OK) {
  3113. ZIPERR(r, zipfile);
  3114. }
  3115. */
  3116. free(value);
  3117. }
  3118. if (show_what_doing) {
  3119. fprintf(mesg, "sd: Zipfile name '%s'\n", zipfile);
  3120. fflush(mesg);
  3121. }
  3122. /* if in_path not set, use zipfile path as usual for input */
  3123. /* in_path is used as the base path to find splits */
  3124. if (in_path == NULL) {
  3125. if ((in_path = malloc(strlen(zipfile) + 1)) == NULL) {
  3126. ZIPERR(ZE_MEM, "was processing arguments");
  3127. }
  3128. strcpy(in_path, zipfile);
  3129. }
  3130. /* if out_path not set, use zipfile path as usual for output */
  3131. /* out_path is where the output archive is written */
  3132. if (out_path == NULL) {
  3133. if ((out_path = malloc(strlen(zipfile) + 1)) == NULL) {
  3134. ZIPERR(ZE_MEM, "was processing arguments");
  3135. }
  3136. strcpy(out_path, zipfile);
  3137. }
  3138. kk = 3;
  3139. if (s)
  3140. {
  3141. /* do -@ and get names from stdin */
  3142. /* should be able to read names from
  3143. stdin and output to stdout, but
  3144. this was not allowed in old code.
  3145. This check moved to kk = 3 case to fix. */
  3146. /* if (strcmp(zipfile, "-") == 0) {
  3147. ZIPERR(ZE_PARMS, "can't use - and -@ together");
  3148. }
  3149. */
  3150. while ((pp = getnam(stdin)) != NULL)
  3151. {
  3152. kk = 4;
  3153. if (recurse == 2) {
  3154. /* reading patterns from stdin */
  3155. add_filter((int)'R', pp);
  3156. } else {
  3157. /* file argument now processed later */
  3158. add_name(pp);
  3159. }
  3160. /*
  3161. if ((r = PROCNAME(pp)) != ZE_OK) {
  3162. if (r == ZE_MISS)
  3163. zipwarn("name not matched: ", pp);
  3164. else {
  3165. ZIPERR(r, pp);
  3166. }
  3167. }
  3168. */
  3169. free(pp);
  3170. }
  3171. s = 0;
  3172. }
  3173. if (recurse == 2) {
  3174. /* rest are -R patterns */
  3175. kk = 6;
  3176. }
  3177. break;
  3178. case 3: case 4:
  3179. /* no recurse and -r file names */
  3180. /* can't read filenames -@ and input - from stdin at
  3181. same time */
  3182. if (s == 1 && strcmp(value, "-") == 0) {
  3183. ZIPERR(ZE_PARMS, "can't read input (-) and filenames (-@) both from stdin");
  3184. }
  3185. /* add name to list for later processing */
  3186. add_name(value);
  3187. /*
  3188. if ((r = PROCNAME(value)) != ZE_OK) {
  3189. if (r == ZE_MISS)
  3190. zipwarn("name not matched: ", value);
  3191. else {
  3192. ZIPERR(r, value);
  3193. }
  3194. }
  3195. */
  3196. if (kk == 3) {
  3197. first_listarg = argnum;
  3198. kk = 4;
  3199. }
  3200. break;
  3201. } /* switch kk */
  3202. break;
  3203. default:
  3204. /* should never get here as get_option will exit if not in table */
  3205. sprintf(errbuf, "no such option ID: %ld", option);
  3206. ZIPERR(ZE_PARMS, errbuf);
  3207. } /* switch */
  3208. }
  3209. /* do processing of command line and one-time tasks */
  3210. /* Key not yet specified. If needed, get/verify it now. */
  3211. if (key_needed) {
  3212. if ((key = malloc(IZ_PWLEN+1)) == NULL) {
  3213. ZIPERR(ZE_MEM, "was getting encryption password");
  3214. }
  3215. r = encr_passwd(ZP_PW_ENTER, key, IZ_PWLEN+1, zipfile);
  3216. if (r != IZ_PW_ENTERED) {
  3217. if (r < IZ_PW_ENTERED)
  3218. r = ZE_PARMS;
  3219. ZIPERR(r, "was getting encryption password");
  3220. }
  3221. if (*key == '\0') {
  3222. ZIPERR(ZE_PARMS, "zero length password not allowed");
  3223. }
  3224. if ((e = malloc(IZ_PWLEN+1)) == NULL) {
  3225. ZIPERR(ZE_MEM, "was verifying encryption password");
  3226. }
  3227. r = encr_passwd(ZP_PW_VERIFY, e, IZ_PWLEN+1, zipfile);
  3228. if (r != IZ_PW_ENTERED && r != IZ_PW_SKIPVERIFY) {
  3229. free((zvoid *)e);
  3230. if (r < ZE_OK) r = ZE_PARMS;
  3231. ZIPERR(r, "was verifying encryption password");
  3232. }
  3233. r = ((r == IZ_PW_SKIPVERIFY) ? 0 : strcmp(key, e));
  3234. free((zvoid *)e);
  3235. if (r) {
  3236. ZIPERR(ZE_PARMS, "password verification failed");
  3237. }
  3238. }
  3239. if (key) {
  3240. /* if -P "" could get here */
  3241. if (*key == '\0') {
  3242. ZIPERR(ZE_PARMS, "zero length password not allowed");
  3243. }
  3244. }
  3245. if (show_what_doing) {
  3246. fprintf(mesg, "sd: Command line read\n");
  3247. fflush(mesg);
  3248. }
  3249. /* show command line args */
  3250. if (show_args) {
  3251. fprintf(mesg, "command line:\n");
  3252. for (i = 0; args[i]; i++) {
  3253. fprintf(mesg, "'%s' ", args[i]);
  3254. }
  3255. fprintf(mesg, "\n");
  3256. ZIPERR(ZE_ABORT, "show command line");
  3257. }
  3258. /* show all options */
  3259. if (show_options) {
  3260. printf("available options:\n");
  3261. printf(" %-2s %-18s %-4s %-3s %-30s\n", "sh", "long", "val", "neg", "description");
  3262. printf(" %-2s %-18s %-4s %-3s %-30s\n", "--", "----", "---", "---", "-----------");
  3263. for (i = 0; options[i].option_ID; i++) {
  3264. printf(" %-2s %-18s ", options[i].shortopt, options[i].longopt);
  3265. switch (options[i].value_type) {
  3266. case o_NO_VALUE:
  3267. printf("%-4s ", "");
  3268. break;
  3269. case o_REQUIRED_VALUE:
  3270. printf("%-4s ", "req");
  3271. break;
  3272. case o_OPTIONAL_VALUE:
  3273. printf("%-4s ", "opt");
  3274. break;
  3275. case o_VALUE_LIST:
  3276. printf("%-4s ", "list");
  3277. break;
  3278. case o_ONE_CHAR_VALUE:
  3279. printf("%-4s ", "char");
  3280. break;
  3281. case o_NUMBER_VALUE:
  3282. printf("%-4s ", "num");
  3283. break;
  3284. default:
  3285. printf("%-4s ", "unk");
  3286. }
  3287. switch (options[i].negatable) {
  3288. case o_NEGATABLE:
  3289. printf("%-3s ", "neg");
  3290. break;
  3291. case o_NOT_NEGATABLE:
  3292. printf("%-3s ", "");
  3293. break;
  3294. default:
  3295. printf("%-3s ", "unk");
  3296. }
  3297. if (options[i].name)
  3298. printf("%-30s\n", options[i].name);
  3299. else
  3300. printf("\n");
  3301. }
  3302. RETURN(finish(ZE_OK));
  3303. }
  3304. /* open log file */
  3305. if (logfile_path) {
  3306. char mode[10];
  3307. char *p;
  3308. char *lastp;
  3309. /* if no extension add .log */
  3310. p = logfile_path;
  3311. /* find last / */
  3312. lastp = NULL;
  3313. for (p = logfile_path; (p = MBSRCHR(p, '/')) != NULL; p++) {
  3314. lastp = p;
  3315. }
  3316. if (lastp == NULL)
  3317. lastp = logfile_path;
  3318. if (MBSRCHR(lastp, '.') == NULL) {
  3319. /* add .log */
  3320. if ((p = malloc(strlen(logfile_path) + 5)) == NULL) {
  3321. ZIPERR(ZE_MEM, "logpath");
  3322. }
  3323. strcpy(p, logfile_path);
  3324. strcat(p, ".log");
  3325. free(logfile_path);
  3326. logfile_path = p;
  3327. }
  3328. if (logfile_append) {
  3329. sprintf(mode, "a");
  3330. } else {
  3331. sprintf(mode, "w");
  3332. }
  3333. if ((logfile = zfopen(logfile_path, mode)) == NULL) {
  3334. sprintf(errbuf, "could not open logfile '%s'", logfile_path);
  3335. ZIPERR(ZE_PARMS, errbuf);
  3336. }
  3337. {
  3338. /* At top put start time and command line */
  3339. /* get current time */
  3340. struct tm *now;
  3341. time_t clocktime;
  3342. time(&clocktime);
  3343. now = localtime(&clocktime);
  3344. fprintf(logfile, "---------\n");
  3345. fprintf(logfile, "Zip log opened %s", asctime(now));
  3346. fprintf(logfile, "command line arguments:\n ");
  3347. for (i = 1; args[i]; i++) {
  3348. size_t j;
  3349. int has_space = 0;
  3350. for (j = 0; j < strlen(args[i]); j++) {
  3351. if (isspace(args[i][j])) {
  3352. has_space = 1;
  3353. break;
  3354. }
  3355. }
  3356. if (has_space)
  3357. fprintf(logfile, "\"%s\" ", args[i]);
  3358. else
  3359. fprintf(logfile, "%s ", args[i]);
  3360. }
  3361. fprintf(logfile, "\n\n");
  3362. fflush(logfile);
  3363. }
  3364. } else {
  3365. /* only set logall if logfile open */
  3366. logall = 0;
  3367. }
  3368. if (split_method && out_path) {
  3369. /* if splitting, the archive name must have .zip extension */
  3370. int plen = strlen(out_path);
  3371. char *out_path_ext;
  3372. #ifdef VMS
  3373. /* On VMS, adjust plen (and out_path_ext) to avoid the file version. */
  3374. plen -= strlen( vms_file_version( out_path));
  3375. #endif /* def VMS */
  3376. out_path_ext = out_path+ plen- 4;
  3377. if (plen < 4 ||
  3378. out_path_ext[0] != '.' ||
  3379. toupper(out_path_ext[1]) != 'Z' ||
  3380. toupper(out_path_ext[2]) != 'I' ||
  3381. toupper(out_path_ext[3]) != 'P') {
  3382. ZIPERR(ZE_PARMS, "archive name must end in .zip for splits");
  3383. }
  3384. }
  3385. if (verbose && (dot_size == 0) && (dot_count == 0)) {
  3386. /* now default to default 10 MB dot size */
  3387. dot_size = 10 * 0x100000;
  3388. /* show all dots as before if verbose set and dot_size not set (dot_count = 0) */
  3389. /* maybe should turn off dots in default verbose mode */
  3390. /* dot_size = -1; */
  3391. }
  3392. /* done getting -R filters so convert filterlist if not done */
  3393. if (pcount && patterns == NULL) {
  3394. filterlist_to_patterns();
  3395. }
  3396. #if (defined(MSDOS) || defined(OS2)) && !defined(WIN32)
  3397. if ((kk == 3 || kk == 4) && volume_label == 1) {
  3398. /* read volume label */
  3399. PROCNAME(NULL);
  3400. kk = 4;
  3401. }
  3402. #endif
  3403. if (have_out && kk == 3) {
  3404. copy_only = 1;
  3405. action = ARCHIVE;
  3406. }
  3407. if (have_out && namecmp(in_path, out_path) == 0) {
  3408. sprintf(errbuf, "--out path must be different than in path: %s", out_path);
  3409. ZIPERR(ZE_PARMS, errbuf);
  3410. }
  3411. if (fix && diff_mode) {
  3412. ZIPERR(ZE_PARMS, "can't use --diff (-DF) with fix (-F or -FF)");
  3413. }
  3414. if (action == ARCHIVE && !have_out && !show_files) {
  3415. ZIPERR(ZE_PARMS, "-U (--copy) requires -O (--out)");
  3416. }
  3417. if (fix && !have_out) {
  3418. zipwarn("fix options -F and -FF require --out:\n",
  3419. " zip -F indamagedarchive --out outfixedarchive");
  3420. ZIPERR(ZE_PARMS, "fix options require --out");
  3421. }
  3422. if (fix && !copy_only) {
  3423. ZIPERR(ZE_PARMS, "no other actions allowed when fixing archive (-F or -FF)");
  3424. }
  3425. if (!have_out && diff_mode) {
  3426. ZIPERR(ZE_PARMS, "-DF (--diff) requires -O (--out)");
  3427. }
  3428. if (diff_mode && (action == ARCHIVE || action == DELETE)) {
  3429. ZIPERR(ZE_PARMS, "can't use --diff (-DF) with -d or -U");
  3430. }
  3431. if (action != ARCHIVE && (recurse == 2 || pcount) && first_listarg == 0 &&
  3432. !filelist && (kk < 3 || (action != UPDATE && action != FRESHEN))) {
  3433. ZIPERR(ZE_PARMS, "nothing to select from");
  3434. }
  3435. /*
  3436. -------------------------------------
  3437. end of new command line code
  3438. -------------------------------------
  3439. */
  3440. #if (!defined(MACOS) && !defined(WINDLL))
  3441. if (kk < 3) { /* zip used as filter */
  3442. zipstdout();
  3443. comment_stream = NULL;
  3444. if ((r = procname("-", 0)) != ZE_OK) {
  3445. if (r == ZE_MISS) {
  3446. if (bad_open_is_error) {
  3447. zipwarn("name not matched: ", "-");
  3448. ZIPERR(ZE_OPEN, "-");
  3449. } else {
  3450. zipwarn("name not matched: ", "-");
  3451. }
  3452. } else {
  3453. ZIPERR(r, "-");
  3454. }
  3455. }
  3456. kk = 4;
  3457. if (s) {
  3458. ZIPERR(ZE_PARMS, "can't use - and -@ together");
  3459. }
  3460. }
  3461. #endif /* !MACOS && !WINDLL */
  3462. if (zipfile && !strcmp(zipfile, "-")) {
  3463. if (show_what_doing) {
  3464. fprintf(mesg, "sd: Zipping to stdout\n");
  3465. fflush(mesg);
  3466. }
  3467. zip_to_stdout = 1;
  3468. }
  3469. /* Check option combinations */
  3470. if (special == NULL) {
  3471. ZIPERR(ZE_PARMS, "missing suffix list");
  3472. }
  3473. if (level == 9 || !strcmp(special, ";") || !strcmp(special, ":"))
  3474. special = NULL; /* compress everything */
  3475. if (action == DELETE && (method != BEST || dispose || recurse ||
  3476. key != NULL || comadd || zipedit)) {
  3477. zipwarn("invalid option(s) used with -d; ignored.","");
  3478. /* reset flags - needed? */
  3479. method = BEST;
  3480. dispose = 0;
  3481. recurse = 0;
  3482. if (key != NULL) {
  3483. free((zvoid *)key);
  3484. key = NULL;
  3485. }
  3486. comadd = 0;
  3487. zipedit = 0;
  3488. }
  3489. if (action == ARCHIVE && (method != BEST || dispose || recurse ||
  3490. comadd || zipedit)) {
  3491. zipwarn("can't set method, move, recurse, or comments with copy mode.","");
  3492. /* reset flags - needed? */
  3493. method = BEST;
  3494. dispose = 0;
  3495. recurse = 0;
  3496. comadd = 0;
  3497. zipedit = 0;
  3498. }
  3499. if (linkput && dosify)
  3500. {
  3501. zipwarn("can't use -y with -k, -y ignored", "");
  3502. linkput = 0;
  3503. }
  3504. if (fix == 1 && adjust)
  3505. {
  3506. zipwarn("can't use -F with -A, -F ignored", "");
  3507. fix = 0;
  3508. }
  3509. if (fix == 2 && adjust)
  3510. {
  3511. zipwarn("can't use -FF with -A, -FF ignored", "");
  3512. fix = 0;
  3513. }
  3514. if (test && zip_to_stdout) {
  3515. test = 0;
  3516. zipwarn("can't use -T on stdout, -T ignored", "");
  3517. }
  3518. if (split_method && (fix || adjust)) {
  3519. ZIPERR(ZE_PARMS, "can't create split archive while fixing or adjusting\n");
  3520. }
  3521. if (split_method && (d || zip_to_stdout)) {
  3522. ZIPERR(ZE_PARMS, "can't create split archive with -d or -g or on stdout\n");
  3523. }
  3524. if ((action != ADD || d) && filesync) {
  3525. ZIPERR(ZE_PARMS, "can't use -d, -f, -u, -U, or -g with filesync -FS\n");
  3526. }
  3527. if ((action != ADD || d) && zip_to_stdout) {
  3528. ZIPERR(ZE_PARMS, "can't use -d, -f, -u, -U, or -g on stdout\n");
  3529. }
  3530. #if defined(EBCDIC) && !defined(OS390)
  3531. if (aflag==ASCII && !translate_eol) {
  3532. /* Translation to ASCII implies EOL translation!
  3533. * (on OS390, consistent EOL translation is controlled separately)
  3534. * The default translation mode is "UNIX" mode (single LF terminators).
  3535. */
  3536. translate_eol = 2;
  3537. }
  3538. #endif
  3539. #ifdef CMS_MVS
  3540. if (aflag==ASCII && bflag)
  3541. ZIPERR(ZE_PARMS, "can't use -a with -B");
  3542. #endif
  3543. #ifdef VMS
  3544. if (!extra_fields && vms_native)
  3545. {
  3546. zipwarn("can't use -V with -X, -V ignored", "");
  3547. vms_native = 0;
  3548. }
  3549. if (vms_native && translate_eol)
  3550. ZIPERR(ZE_PARMS, "can't use -V with -l or -ll");
  3551. #endif
  3552. if (noisy) {
  3553. if (fix == 1)
  3554. zipmessage("Fix archive (-F) - assume mostly intact archive", "");
  3555. else if (fix == 2)
  3556. zipmessage("Fix archive (-FF) - salvage what can", "");
  3557. }
  3558. /* Read old archive */
  3559. /* Now read the zip file here instead of when doing args above */
  3560. /* Only read the central directory and build zlist */
  3561. if (show_what_doing) {
  3562. fprintf(mesg, "sd: Reading archive\n");
  3563. fflush(mesg);
  3564. }
  3565. /* If -FF we do it all here */
  3566. if (fix == 2) {
  3567. /* Open zip file and temporary output file */
  3568. if (show_what_doing) {
  3569. fprintf(mesg, "sd: Open zip file and create temp file (-FF)\n");
  3570. fflush(mesg);
  3571. }
  3572. diag("opening zip file and creating temporary zip file");
  3573. x = NULL;
  3574. tempzn = 0;
  3575. if (show_what_doing) {
  3576. fprintf(mesg, "sd: Creating new zip file (-FF)\n");
  3577. fflush(mesg);
  3578. }
  3579. #if defined(UNIX) && !defined(NO_MKSTEMP)
  3580. {
  3581. int yd;
  3582. int i;
  3583. /* use mkstemp to avoid race condition and compiler warning */
  3584. if (tempath != NULL)
  3585. {
  3586. /* if -b used to set temp file dir use that for split temp */
  3587. if ((tempzip = malloc(strlen(tempath) + 12)) == NULL) {
  3588. ZIPERR(ZE_MEM, "allocating temp filename");
  3589. }
  3590. strcpy(tempzip, tempath);
  3591. if (lastchar(tempzip) != '/')
  3592. strcat(tempzip, "/");
  3593. }
  3594. else
  3595. {
  3596. /* create path by stripping name and appending template */
  3597. if ((tempzip = malloc(strlen(zipfile) + 12)) == NULL) {
  3598. ZIPERR(ZE_MEM, "allocating temp filename");
  3599. }
  3600. strcpy(tempzip, zipfile);
  3601. for(i = strlen(tempzip); i > 0; i--) {
  3602. if (tempzip[i - 1] == '/')
  3603. break;
  3604. }
  3605. tempzip[i] = '\0';
  3606. }
  3607. strcat(tempzip, "ziXXXXXX");
  3608. if ((yd = mkstemp(tempzip)) == EOF) {
  3609. ZIPERR(ZE_TEMP, tempzip);
  3610. }
  3611. if ((y = fdopen(yd, FOPW_TMP)) == NULL) {
  3612. ZIPERR(ZE_TEMP, tempzip);
  3613. }
  3614. }
  3615. #else
  3616. if ((tempzip = tempname(zipfile)) == NULL) {
  3617. ZIPERR(ZE_MEM, "allocating temp filename");
  3618. }
  3619. if ((y = zfopen(tempzip, FOPW_TMP)) == NULL) {
  3620. ZIPERR(ZE_TEMP, tempzip);
  3621. }
  3622. #endif
  3623. #if (!defined(VMS) && !defined(CMS_MVS))
  3624. /* Use large buffer to speed up stdio: */
  3625. #if (defined(_IOFBF) || !defined(BUFSIZ))
  3626. zipbuf = (char *)malloc(ZBSZ);
  3627. #else
  3628. zipbuf = (char *)malloc(BUFSIZ);
  3629. #endif
  3630. if (zipbuf == NULL) {
  3631. ZIPERR(ZE_MEM, tempzip);
  3632. }
  3633. # ifdef _IOFBF
  3634. setvbuf(y, zipbuf, _IOFBF, ZBSZ);
  3635. # else
  3636. setbuf(y, zipbuf);
  3637. # endif /* _IOBUF */
  3638. #endif /* !VMS && !CMS_MVS */
  3639. if ((r = readzipfile()) != ZE_OK) {
  3640. ZIPERR(r, zipfile);
  3641. }
  3642. /* Write central directory and end header to temporary zip */
  3643. if (show_what_doing) {
  3644. fprintf(mesg, "sd: Writing central directory (-FF)\n");
  3645. fflush(mesg);
  3646. }
  3647. diag("writing central directory");
  3648. k = 0; /* keep count for end header */
  3649. c = tempzn; /* get start of central */
  3650. n = t = 0;
  3651. for (z = zfiles; z != NULL; z = z->nxt)
  3652. {
  3653. if ((r = putcentral(z)) != ZE_OK) {
  3654. ZIPERR(r, tempzip);
  3655. }
  3656. tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  3657. n += z->len;
  3658. t += z->siz;
  3659. k++;
  3660. }
  3661. if (zcount == 0)
  3662. zipwarn("zip file empty", "");
  3663. t = tempzn - c; /* compute length of central */
  3664. diag("writing end of central directory");
  3665. if ((r = putend(k, t, c, zcomlen, zcomment)) != ZE_OK) {
  3666. ZIPERR(r, tempzip);
  3667. }
  3668. if (fclose(y)) {
  3669. ZIPERR(d ? ZE_WRITE : ZE_TEMP, tempzip);
  3670. }
  3671. if (in_file != NULL) {
  3672. fclose(in_file);
  3673. in_file = NULL;
  3674. }
  3675. /* Replace old zip file with new zip file, leaving only the new one */
  3676. if (strcmp(zipfile, "-") && !d)
  3677. {
  3678. diag("replacing old zip file with new zip file");
  3679. if ((r = replace(out_path, tempzip)) != ZE_OK)
  3680. {
  3681. zipwarn("new zip file left as: ", tempzip);
  3682. free((zvoid *)tempzip);
  3683. tempzip = NULL;
  3684. ZIPERR(r, "was replacing the original zip file");
  3685. }
  3686. free((zvoid *)tempzip);
  3687. }
  3688. tempzip = NULL;
  3689. if (zip_attributes && strcmp(zipfile, "-")) {
  3690. setfileattr(out_path, zip_attributes);
  3691. #ifdef VMS
  3692. /* If the zip file existed previously, restore its record format: */
  3693. if (x != NULL)
  3694. (void)VMSmunch(out_path, RESTORE_RTYPE, NULL);
  3695. #endif
  3696. }
  3697. set_filetype(out_path);
  3698. /* finish logfile (it gets closed in freeup() called by finish()) */
  3699. if (logfile) {
  3700. struct tm *now;
  3701. time_t clocktime;
  3702. fprintf(logfile, "\nTotal %ld entries (", files_total);
  3703. DisplayNumString(logfile, bytes_total);
  3704. fprintf(logfile, " bytes)");
  3705. /* get current time */
  3706. time(&clocktime);
  3707. now = localtime(&clocktime);
  3708. fprintf(logfile, "\nDone %s", asctime(now));
  3709. fflush(logfile);
  3710. }
  3711. RETURN(finish(ZE_OK));
  3712. }
  3713. /* read zipfile if exists */
  3714. if ((r = readzipfile()) != ZE_OK) {
  3715. ZIPERR(r, zipfile);
  3716. }
  3717. #ifndef UTIL
  3718. if (split_method == -1) {
  3719. split_method = 0;
  3720. } else if (!fix && split_method == 0 && total_disks > 1) {
  3721. /* if input archive is multi-disk and splitting has not been
  3722. enabled or disabled (split_method == -1), then automatically
  3723. set split size to same as first input split */
  3724. zoff_t size = 0;
  3725. in_split_path = get_in_split_path(in_path, 0);
  3726. if (filetime(in_split_path, NULL, &size, NULL) == 0) {
  3727. zipwarn("Could not get info for input split: ", in_split_path);
  3728. return ZE_OPEN;
  3729. }
  3730. split_method = 1;
  3731. split_size = (uzoff_t) size;
  3732. free(in_split_path);
  3733. in_split_path = NULL;
  3734. }
  3735. if (noisy_splits && split_size > 0)
  3736. zipmessage("splitsize = ", zip_fuzofft(split_size, NULL, NULL));
  3737. #endif
  3738. /* so disk display starts at 1, will be updated when entries are read */
  3739. current_in_disk = 0;
  3740. /* no input zipfile and showing contents */
  3741. if (!zipfile_exists && show_files && (kk == 3 || action == ARCHIVE)) {
  3742. ZIPERR(ZE_OPEN, zipfile);
  3743. }
  3744. if (zcount == 0 && (action != ADD || d)) {
  3745. zipwarn(zipfile, " not found or empty");
  3746. }
  3747. if (have_out && kk == 3) {
  3748. /* no input paths so assume copy mode and match everything if --out */
  3749. for (z = zfiles; z != NULL; z = z->nxt) {
  3750. z->mark = pcount ? filter(z->zname, filter_match_case) : 1;
  3751. }
  3752. }
  3753. /* Scan for new files */
  3754. /* Process file arguments from command line */
  3755. if (filelist) {
  3756. if (action == ARCHIVE) {
  3757. /* find in archive */
  3758. if (show_what_doing) {
  3759. fprintf(mesg, "sd: Scanning archive entries\n");
  3760. fflush(mesg);
  3761. }
  3762. for (; filelist; ) {
  3763. if ((r = proc_archive_name(filelist->name, filter_match_case)) != ZE_OK) {
  3764. if (r == ZE_MISS) {
  3765. char *n = NULL;
  3766. #ifdef WIN32
  3767. /* Win9x console always uses OEM character coding, and
  3768. WinNT console is set to OEM charset by default, too */
  3769. if ((n = malloc(strlen(filelist->name) + 1)) == NULL)
  3770. ZIPERR(ZE_MEM, "name not matched error");
  3771. INTERN_TO_OEM(filelist->name, n);
  3772. #else
  3773. n = filelist->name;
  3774. #endif
  3775. zipwarn("not in archive: ", n);
  3776. #ifdef WIN32
  3777. free(n);
  3778. #endif
  3779. }
  3780. else {
  3781. ZIPERR(r, filelist->name);
  3782. }
  3783. }
  3784. free(filelist->name);
  3785. filearg = filelist;
  3786. filelist = filelist->next;
  3787. free(filearg);
  3788. }
  3789. } else {
  3790. /* try find matching files on OS first then try find entries in archive */
  3791. if (show_what_doing) {
  3792. fprintf(mesg, "sd: Scanning files\n");
  3793. fflush(mesg);
  3794. }
  3795. for (; filelist; ) {
  3796. if ((r = PROCNAME(filelist->name)) != ZE_OK) {
  3797. if (r == ZE_MISS) {
  3798. if (bad_open_is_error) {
  3799. zipwarn("name not matched: ", filelist->name);
  3800. ZIPERR(ZE_OPEN, filelist->name);
  3801. } else {
  3802. zipwarn("name not matched: ", filelist->name);
  3803. }
  3804. } else {
  3805. ZIPERR(r, filelist->name);
  3806. }
  3807. }
  3808. free(filelist->name);
  3809. filearg = filelist;
  3810. filelist = filelist->next;
  3811. free(filearg);
  3812. }
  3813. }
  3814. }
  3815. /* recurse from current directory for -R */
  3816. if (recurse == 2) {
  3817. #ifdef AMIGA
  3818. if ((r = PROCNAME("")) != ZE_OK)
  3819. #else
  3820. if ((r = PROCNAME(".")) != ZE_OK)
  3821. #endif
  3822. {
  3823. if (r == ZE_MISS) {
  3824. if (bad_open_is_error) {
  3825. zipwarn("name not matched: ", "current directory for -R");
  3826. ZIPERR(ZE_OPEN, "-R");
  3827. } else {
  3828. zipwarn("name not matched: ", "current directory for -R");
  3829. }
  3830. } else {
  3831. ZIPERR(r, "-R");
  3832. }
  3833. }
  3834. }
  3835. if (show_what_doing) {
  3836. fprintf(mesg, "sd: Applying filters\n");
  3837. fflush(mesg);
  3838. }
  3839. /* Clean up selections ("3 <= kk <= 5" now) */
  3840. if (kk != 4 && first_listarg == 0 &&
  3841. (action == UPDATE || action == FRESHEN)) {
  3842. /* if -u or -f with no args, do all, but, when present, apply filters */
  3843. for (z = zfiles; z != NULL; z = z->nxt) {
  3844. z->mark = pcount ? filter(z->zname, filter_match_case) : 1;
  3845. #ifdef DOS
  3846. if (z->mark) z->dosflag = 1; /* force DOS attribs for incl. names */
  3847. #endif
  3848. }
  3849. }
  3850. if (show_what_doing) {
  3851. fprintf(mesg, "sd: Checking dups\n");
  3852. fflush(mesg);
  3853. }
  3854. if ((r = check_dup()) != ZE_OK) { /* remove duplicates in found list */
  3855. if (r == ZE_PARMS) {
  3856. ZIPERR(r, "cannot repeat names in zip file");
  3857. }
  3858. else {
  3859. ZIPERR(r, "was processing list of files");
  3860. }
  3861. }
  3862. if (zcount)
  3863. free((zvoid *)zsort);
  3864. /*
  3865. * XXX make some kind of mktemppath() function for each OS.
  3866. */
  3867. #ifndef VM_CMS
  3868. /* For CMS, leave tempath NULL. A-disk will be used as default. */
  3869. /* If -b not specified, make temporary path the same as the zip file */
  3870. #if defined(MSDOS) || defined(__human68k__) || defined(AMIGA)
  3871. if (tempath == NULL && ((p = MBSRCHR(zipfile, '/')) != NULL ||
  3872. # ifdef MSDOS
  3873. (p = MBSRCHR(zipfile, '\\')) != NULL ||
  3874. # endif /* MSDOS */
  3875. (p = MBSRCHR(zipfile, ':')) != NULL))
  3876. {
  3877. if (*p == ':')
  3878. p++;
  3879. #else
  3880. #ifdef RISCOS
  3881. if (tempath == NULL && (p = MBSRCHR(zipfile, '.')) != NULL)
  3882. {
  3883. #else
  3884. #ifdef QDOS
  3885. if (tempath == NULL && (p = LastDir(zipfile)) != NULL)
  3886. {
  3887. #else
  3888. if (tempath == NULL && (p = MBSRCHR(zipfile, '/')) != NULL)
  3889. {
  3890. #endif /* QDOS */
  3891. #endif /* RISCOS */
  3892. #endif /* MSDOS || __human68k__ || AMIGA */
  3893. if ((tempath = (char *)malloc((int)(p - zipfile) + 1)) == NULL) {
  3894. ZIPERR(ZE_MEM, "was processing arguments");
  3895. }
  3896. r = *p; *p = 0;
  3897. strcpy(tempath, zipfile);
  3898. *p = (char)r;
  3899. }
  3900. #endif /* VM_CMS */
  3901. #if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  3902. if (!zp_tz_is_valid) {
  3903. zipwarn("TZ environment variable not found, cannot use UTC times!!","");
  3904. }
  3905. #endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
  3906. /* For each marked entry, if not deleting, check if it exists, and if
  3907. updating or freshening, compare date with entry in old zip file.
  3908. Unmark if it doesn't exist or is too old, else update marked count. */
  3909. if (show_what_doing) {
  3910. fprintf(mesg, "sd: Scanning files to update\n");
  3911. fflush(mesg);
  3912. }
  3913. #ifdef MACOS
  3914. PrintStatProgress("Getting file information ...");
  3915. #endif
  3916. diag("stating marked entries");
  3917. k = 0; /* Initialize marked count */
  3918. scan_started = 0;
  3919. scan_count = 0;
  3920. all_current = 1;
  3921. for (z = zfiles; z != NULL; z = z->nxt) {
  3922. /* if already displayed Scanning files in newname() then continue dots */
  3923. if (noisy && scan_last) {
  3924. scan_count++;
  3925. if (scan_count % 100 == 0) {
  3926. time_t current = time(NULL);
  3927. if (current - scan_last > scan_dot_time) {
  3928. if (scan_started == 0) {
  3929. scan_started = 1;
  3930. fprintf(mesg, " ");
  3931. fflush(mesg);
  3932. }
  3933. scan_last = current;
  3934. fprintf(mesg, ".");
  3935. fflush(mesg);
  3936. }
  3937. }
  3938. }
  3939. z->current = 0;
  3940. if (!(z->mark)) {
  3941. /* if something excluded run through the list to catch deletions */
  3942. all_current = 0;
  3943. }
  3944. if (z->mark) {
  3945. #ifdef USE_EF_UT_TIME
  3946. iztimes f_utim, z_utim;
  3947. ulg z_tim;
  3948. #endif /* USE_EF_UT_TIME */
  3949. Trace((stderr, "zip diagnostics: marked file=%s\n", z->oname));
  3950. csize = z->siz;
  3951. usize = z->len;
  3952. if (action == DELETE) {
  3953. /* only delete files in date range */
  3954. #ifdef USE_EF_UT_TIME
  3955. z_tim = (get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  3956. unix2dostime(&z_utim.mtime) : z->tim;
  3957. #else /* !USE_EF_UT_TIME */
  3958. # define z_tim z->tim
  3959. #endif /* ?USE_EF_UT_TIME */
  3960. if (z_tim < before || (after && z_tim >= after)) {
  3961. /* include in archive */
  3962. z->mark = 0;
  3963. } else {
  3964. /* delete file */
  3965. files_total++;
  3966. /* ignore len in old archive and update to current size */
  3967. z->len = usize;
  3968. if (csize != (uzoff_t) -1 && csize != (uzoff_t) -2)
  3969. bytes_total += csize;
  3970. k++;
  3971. }
  3972. } else if (action == ARCHIVE) {
  3973. /* only keep files in date range */
  3974. #ifdef USE_EF_UT_TIME
  3975. z_tim = (get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  3976. unix2dostime(&z_utim.mtime) : z->tim;
  3977. #else /* !USE_EF_UT_TIME */
  3978. # define z_tim z->tim
  3979. #endif /* ?USE_EF_UT_TIME */
  3980. if (z_tim < before || (after && z_tim >= after)) {
  3981. /* exclude from archive */
  3982. z->mark = 0;
  3983. } else {
  3984. /* keep file */
  3985. files_total++;
  3986. /* ignore len in old archive and update to current size */
  3987. z->len = usize;
  3988. if (csize != (uzoff_t) -1 && csize != (uzoff_t) -2)
  3989. bytes_total += csize;
  3990. k++;
  3991. }
  3992. } else {
  3993. int isdirname = 0;
  3994. if (z->name && (z->name)[strlen(z->name) - 1] == '/') {
  3995. isdirname = 1;
  3996. }
  3997. # if defined(UNICODE_SUPPORT) && defined(WIN32)
  3998. if (!no_win32_wide) {
  3999. if (z->namew == NULL) {
  4000. if (z->uname != NULL)
  4001. z->namew = utf8_to_wchar_string(z->uname);
  4002. else
  4003. z->namew = local_to_wchar_string(z->name);
  4004. }
  4005. }
  4006. # endif
  4007. #ifdef USE_EF_UT_TIME
  4008. # if defined(UNICODE_SUPPORT) && defined(WIN32)
  4009. if (!no_win32_wide)
  4010. tf = filetimew(z->namew, (ulg *)NULL, (zoff_t *)&usize, &f_utim);
  4011. else
  4012. tf = filetime(z->name, (ulg *)NULL, (zoff_t *)&usize, &f_utim);
  4013. # else
  4014. tf = filetime(z->name, (ulg *)NULL, (zoff_t *)&usize, &f_utim);
  4015. # endif
  4016. #else /* !USE_EF_UT_TIME */
  4017. # if defined(UNICODE_SUPPORT) && defined(WIN32)
  4018. if (!no_win32_wide)
  4019. tf = filetimew(z->namew, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4020. else
  4021. tf = filetime(z->name, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4022. # else
  4023. tf = filetime(z->name, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4024. # endif
  4025. #endif /* ?USE_EF_UT_TIME */
  4026. if (tf == 0)
  4027. /* entry that is not on OS */
  4028. all_current = 0;
  4029. if (tf == 0 ||
  4030. tf < before || (after && tf >= after) ||
  4031. ((action == UPDATE || action == FRESHEN) &&
  4032. #ifdef USE_EF_UT_TIME
  4033. ((get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  4034. f_utim.mtime <= ROUNDED_TIME(z_utim.mtime) : tf <= z->tim)
  4035. #else /* !USE_EF_UT_TIME */
  4036. tf <= z->tim
  4037. #endif /* ?USE_EF_UT_TIME */
  4038. ))
  4039. {
  4040. z->mark = comadd ? 2 : 0;
  4041. z->trash = tf && tf >= before &&
  4042. (after ==0 || tf < after); /* delete if -um or -fm */
  4043. if (verbose)
  4044. fprintf(mesg, "zip diagnostic: %s %s\n", z->oname,
  4045. z->trash ? "up to date" : "missing or early");
  4046. if (logfile)
  4047. fprintf(logfile, "zip diagnostic: %s %s\n", z->oname,
  4048. z->trash ? "up to date" : "missing or early");
  4049. }
  4050. else if (diff_mode && tf == z->tim &&
  4051. ((isdirname && (zoff_t)usize == -1) || (usize == z->len))) {
  4052. /* if in diff mode only include if file time or size changed */
  4053. /* usize is -1 for directories */
  4054. z->mark = 0;
  4055. }
  4056. else {
  4057. /* usize is -1 for directories and -2 for devices */
  4058. if (tf == z->tim &&
  4059. ((z->len == 0 && (zoff_t)usize == -1)
  4060. || usize == z->len)) {
  4061. /* FileSync uses the current flag */
  4062. /* Consider an entry current if file time is the same
  4063. and entry size is 0 and a directory on the OS
  4064. or the entry size matches the OS size */
  4065. z->current = 1;
  4066. } else {
  4067. all_current = 0;
  4068. }
  4069. files_total++;
  4070. if (usize != (uzoff_t) -1 && usize != (uzoff_t) -2)
  4071. /* ignore len in old archive and update to current size */
  4072. z->len = usize;
  4073. else
  4074. z->len = 0;
  4075. if (usize != (uzoff_t) -1 && usize != (uzoff_t) -2)
  4076. bytes_total += usize;
  4077. k++;
  4078. }
  4079. }
  4080. }
  4081. }
  4082. /* Remove entries from found list that do not exist or are too old */
  4083. if (show_what_doing) {
  4084. fprintf(mesg, "sd: fcount = %u\n", (unsigned)fcount);
  4085. fflush(mesg);
  4086. }
  4087. diag("stating new entries");
  4088. scan_count = 0;
  4089. scan_started = 0;
  4090. Trace((stderr, "zip diagnostic: fcount=%u\n", (unsigned)fcount));
  4091. for (f = found; f != NULL;) {
  4092. Trace((stderr, "zip diagnostic: new file=%s\n", f->oname));
  4093. if (noisy) {
  4094. /* if updating archive and update was quick, scanning for new files
  4095. can still take a long time */
  4096. if (!zip_to_stdout && scan_last == 0 && scan_count % 100 == 0) {
  4097. time_t current = time(NULL);
  4098. if (current - scan_start > scan_delay) {
  4099. fprintf(mesg, "Scanning files ");
  4100. fflush(mesg);
  4101. mesg_line_started = 1;
  4102. scan_last = current;
  4103. }
  4104. }
  4105. /* if already displayed Scanning files in newname() or above then continue dots */
  4106. if (scan_last) {
  4107. scan_count++;
  4108. if (scan_count % 100 == 0) {
  4109. time_t current = time(NULL);
  4110. if (current - scan_last > scan_dot_time) {
  4111. if (scan_started == 0) {
  4112. scan_started = 1;
  4113. fprintf(mesg, " ");
  4114. fflush(mesg);
  4115. }
  4116. scan_last = current;
  4117. fprintf(mesg, ".");
  4118. fflush(mesg);
  4119. }
  4120. }
  4121. }
  4122. }
  4123. tf = 0;
  4124. if (action != DELETE && action != FRESHEN) {
  4125. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  4126. if (!no_win32_wide)
  4127. tf = filetimew(f->namew, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4128. else
  4129. tf = filetime(f->name, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4130. #else
  4131. tf = filetime(f->name, (ulg *)NULL, (zoff_t *)&usize, NULL);
  4132. #endif
  4133. }
  4134. if (action == DELETE || action == FRESHEN ||
  4135. tf == 0 ||
  4136. tf < before || (after && tf >= after) ||
  4137. (namecmp(f->zname, zipfile) == 0 && !zip_to_stdout)
  4138. )
  4139. f = fexpel(f);
  4140. else {
  4141. /* ??? */
  4142. files_total++;
  4143. f->usize = 0;
  4144. if (usize != (uzoff_t) -1 && usize != (uzoff_t) -2) {
  4145. bytes_total += usize;
  4146. f->usize = usize;
  4147. }
  4148. f = f->nxt;
  4149. }
  4150. }
  4151. if (mesg_line_started) {
  4152. fprintf(mesg, "\n");
  4153. mesg_line_started = 0;
  4154. }
  4155. #ifdef MACOS
  4156. PrintStatProgress("done");
  4157. #endif
  4158. if (show_files) {
  4159. uzoff_t count = 0;
  4160. uzoff_t bytes = 0;
  4161. if (noisy) {
  4162. fflush(mesg);
  4163. }
  4164. if (noisy && (show_files == 1 || show_files == 3 || show_files == 5)) {
  4165. /* sf, su, sU */
  4166. if (mesg_line_started) {
  4167. fprintf(mesg, "\n");
  4168. mesg_line_started = 0;
  4169. }
  4170. if (kk == 3)
  4171. /* -sf alone */
  4172. fprintf(mesg, "Archive contains:\n");
  4173. else if (action == DELETE)
  4174. fprintf(mesg, "Would Delete:\n");
  4175. else if (action == FRESHEN)
  4176. fprintf(mesg, "Would Freshen:\n");
  4177. else if (action == ARCHIVE)
  4178. fprintf(mesg, "Would Copy:\n");
  4179. else
  4180. fprintf(mesg, "Would Add/Update:\n");
  4181. fflush(mesg);
  4182. }
  4183. if (logfile) {
  4184. if (logfile_line_started) {
  4185. fprintf(logfile, "\n");
  4186. logfile_line_started = 0;
  4187. }
  4188. if (kk == 3)
  4189. /* -sf alone */
  4190. fprintf(logfile, "Archive contains:\n");
  4191. else if (action == DELETE)
  4192. fprintf(logfile, "Would Delete:\n");
  4193. else if (action == FRESHEN)
  4194. fprintf(logfile, "Would Freshen:\n");
  4195. else if (action == ARCHIVE)
  4196. fprintf(logfile, "Would Copy:\n");
  4197. else
  4198. fprintf(logfile, "Would Add/Update:\n");
  4199. fflush(logfile);
  4200. }
  4201. for (z = zfiles; z != NULL; z = z->nxt) {
  4202. if (z->mark || kk == 3) {
  4203. count++;
  4204. if ((zoff_t)z->len > 0)
  4205. bytes += z->len;
  4206. if (noisy && (show_files == 1 || show_files == 3))
  4207. /* sf, su */
  4208. fprintf(mesg, " %s\n", z->oname);
  4209. if (logfile && !(show_files == 5 || show_files == 6))
  4210. /* not sU or sU- show normal name in log */
  4211. fprintf(logfile, " %s\n", z->oname);
  4212. #ifdef UNICODE_TEST
  4213. if (create_files) {
  4214. int r;
  4215. int dir = 0;
  4216. FILE *f;
  4217. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  4218. char *fn = NULL;
  4219. wchar_t *fnw = NULL;
  4220. if (!no_win32_wide) {
  4221. if ((fnw = malloc((wcslen(z->znamew) + 120) * sizeof(wchar_t))) == NULL)
  4222. ZIPERR(ZE_MEM, "sC");
  4223. wcscpy(fnw, L"testdir/");
  4224. wcscat(fnw, z->znamew);
  4225. if (fnw[wcslen(fnw) - 1] == '/')
  4226. dir = 1;
  4227. if (dir)
  4228. r = _wmkdir(fnw);
  4229. else
  4230. f = _wfopen(fnw, L"w");
  4231. } else {
  4232. if ((fn = malloc(strlen(z->zname) + 120)) == NULL)
  4233. ZIPERR(ZE_MEM, "sC");
  4234. strcpy(fn, "testdir/");
  4235. strcat(fn, z->zname);
  4236. if (fn[strlen(fn) - 1] == '/')
  4237. dir = 1;
  4238. if (dir)
  4239. r = mkdir(fn);
  4240. else
  4241. f = fopen(fn, "w");
  4242. }
  4243. #else
  4244. char *fn = NULL;
  4245. if ((fn = malloc(strlen(z->zname) + 120)) == NULL)
  4246. ZIPERR(ZE_MEM, "sC");
  4247. strcpy(fn, "testdir/");
  4248. if (z->uname)
  4249. strcat(fn, z->uname);
  4250. else
  4251. strcat(fn, z->zname);
  4252. if (fn[strlen(fn) - 1] == '/')
  4253. dir = 1;
  4254. if (dir)
  4255. r = mkdir(fn, 0777);
  4256. else
  4257. f = fopen(fn, "w");
  4258. #endif
  4259. if (dir) {
  4260. if (r) {
  4261. if (errno != 17) {
  4262. printf(" - could not create directory testdir/%s\n", z->oname);
  4263. perror(" dir");
  4264. }
  4265. } else {
  4266. printf(" - created directory testdir/%s\n", z->oname);
  4267. }
  4268. } else {
  4269. if (f == NULL) {
  4270. printf(" - could not open testdir/%s\n", z->oname);
  4271. perror(" file");
  4272. } else {
  4273. fclose(f);
  4274. printf(" - created testdir/%s\n", z->oname);
  4275. if (z->uname)
  4276. printf(" u - created testdir/%s\n", z->uname);
  4277. }
  4278. }
  4279. }
  4280. #endif
  4281. #ifdef UNICODE_SUPPORT
  4282. if (show_files == 3 || show_files == 4) {
  4283. /* su, su- */
  4284. /* Include escaped Unicode name if exists under standard name */
  4285. if (z->ouname) {
  4286. if (noisy && show_files == 3)
  4287. fprintf(mesg, " Escaped Unicode: %s\n", z->ouname);
  4288. if (logfile)
  4289. fprintf(logfile, " Escaped Unicode: %s\n", z->ouname);
  4290. }
  4291. }
  4292. if (show_files == 5 || show_files == 6) {
  4293. /* sU, sU- */
  4294. /* Display only escaped Unicode name if exists or standard name */
  4295. if (z->ouname) {
  4296. /* Unicode name */
  4297. if (noisy && show_files == 5) {
  4298. fprintf(mesg, " %s\n", z->ouname);
  4299. }
  4300. if (logfile) {
  4301. fprintf(logfile, " %s\n", z->ouname);
  4302. }
  4303. } else {
  4304. /* No Unicode name so use standard name */
  4305. if (noisy && show_files == 5) {
  4306. fprintf(mesg, " %s\n", z->oname);
  4307. }
  4308. if (logfile) {
  4309. fprintf(logfile, " %s\n", z->oname);
  4310. }
  4311. }
  4312. }
  4313. #endif
  4314. }
  4315. }
  4316. for (f = found; f != NULL; f = f->nxt) {
  4317. count++;
  4318. if ((zoff_t)f->usize > 0)
  4319. bytes += f->usize;
  4320. #ifdef UNICODE_SUPPORT
  4321. if (unicode_escape_all) {
  4322. char *escaped_unicode;
  4323. escaped_unicode = local_to_escape_string(f->zname);
  4324. if (noisy && (show_files == 1 || show_files == 3 || show_files == 5))
  4325. /* sf, su, sU */
  4326. fprintf(mesg, " %s\n", escaped_unicode);
  4327. if (logfile)
  4328. fprintf(logfile, " %s\n", escaped_unicode);
  4329. free(escaped_unicode);
  4330. } else {
  4331. #endif
  4332. if (noisy && (show_files == 1 || show_files == 3 || show_files == 5))
  4333. /* sf, su, sU */
  4334. fprintf(mesg, " %s\n", f->oname);
  4335. if (logfile)
  4336. fprintf(logfile, " %s\n", f->oname);
  4337. #ifdef UNICODE_SUPPORT
  4338. }
  4339. #endif
  4340. }
  4341. if (noisy || logfile == NULL)
  4342. fprintf(mesg, "Total %s entries (%s bytes)\n",
  4343. zip_fuzofft(count, NULL, NULL),
  4344. zip_fuzofft(bytes, NULL, NULL));
  4345. if (logfile)
  4346. fprintf(logfile, "Total %s entries (%s bytes)\n",
  4347. zip_fuzofft(count, NULL, NULL),
  4348. zip_fuzofft(bytes, NULL, NULL));
  4349. RETURN(finish(ZE_OK));
  4350. }
  4351. /* Make sure there's something left to do */
  4352. if (k == 0 && found == NULL && !diff_mode &&
  4353. !(zfiles == NULL && allow_empty_archive) &&
  4354. !(zfiles != NULL &&
  4355. (latest || fix || adjust || junk_sfx || comadd || zipedit))) {
  4356. if (test && (zfiles != NULL || zipbeg != 0)) {
  4357. #ifndef WINDLL
  4358. check_zipfile(zipfile, argv[0]);
  4359. #endif
  4360. RETURN(finish(ZE_OK));
  4361. }
  4362. if (action == UPDATE || action == FRESHEN) {
  4363. RETURN(finish(ZE_NONE));
  4364. }
  4365. else if (zfiles == NULL && (latest || fix || adjust || junk_sfx)) {
  4366. ZIPERR(ZE_NAME, zipfile);
  4367. }
  4368. #ifndef WINDLL
  4369. else if (recurse && (pcount == 0) && (first_listarg > 0)) {
  4370. #ifdef VMS
  4371. strcpy(errbuf, "try: zip \"");
  4372. for (i = 1; i < (first_listarg - 1); i++)
  4373. strcat(strcat(errbuf, args[i]), "\" ");
  4374. strcat(strcat(errbuf, args[i]), " *.* -i");
  4375. #else /* !VMS */
  4376. strcpy(errbuf, "try: zip");
  4377. for (i = 1; i < first_listarg; i++)
  4378. strcat(strcat(errbuf, " "), args[i]);
  4379. # ifdef AMIGA
  4380. strcat(errbuf, " \"\" -i");
  4381. # else
  4382. strcat(errbuf, " . -i");
  4383. # endif
  4384. #endif /* ?VMS */
  4385. for (i = first_listarg; i < argc; i++)
  4386. strcat(strcat(errbuf, " "), args[i]);
  4387. ZIPERR(ZE_NONE, errbuf);
  4388. }
  4389. else {
  4390. ZIPERR(ZE_NONE, zipfile);
  4391. }
  4392. #endif /* !WINDLL */
  4393. }
  4394. if (filesync && all_current && fcount == 0) {
  4395. zipmessage("Archive is current", "");
  4396. RETURN(finish(ZE_OK));
  4397. }
  4398. d = (d && k == 0 && (zipbeg || zfiles != NULL)); /* d true if appending */
  4399. #if CRYPT
  4400. /* Initialize the crc_32_tab pointer, when encryption was requested. */
  4401. if (key != NULL) {
  4402. crc_32_tab = get_crc_table();
  4403. #ifdef EBCDIC
  4404. /* convert encryption key to ASCII (ISO variant for 8-bit ASCII chars) */
  4405. strtoasc(key, key);
  4406. #endif /* EBCDIC */
  4407. }
  4408. #endif /* CRYPT */
  4409. /* Just ignore the spanning signature if a multi-disk archive */
  4410. if (zfiles && total_disks != 1 && zipbeg == 4) {
  4411. zipbeg = 0;
  4412. }
  4413. /* Before we get carried away, make sure zip file is writeable. This
  4414. * has the undesired side effect of leaving one empty junk file on a WORM,
  4415. * so when the zipfile does not exist already and when -b is specified,
  4416. * the writability check is made in replace().
  4417. */
  4418. if (strcmp(zipfile, "-"))
  4419. {
  4420. if (tempdir && zfiles == NULL && zipbeg == 0) {
  4421. zip_attributes = 0;
  4422. } else {
  4423. x = (have_out || (zfiles == NULL && zipbeg == 0)) ? zfopen(out_path, FOPW) :
  4424. zfopen(out_path, FOPM);
  4425. /* Note: FOPW and FOPM expand to several parameters for VMS */
  4426. if (x == NULL) {
  4427. ZIPERR(ZE_CREAT, out_path);
  4428. }
  4429. fclose(x);
  4430. zip_attributes = getfileattr(out_path);
  4431. if (zfiles == NULL && zipbeg == 0)
  4432. destroy(out_path);
  4433. }
  4434. }
  4435. else
  4436. zip_attributes = 0;
  4437. /* Throw away the garbage in front of the zip file for -J */
  4438. if (junk_sfx) zipbeg = 0;
  4439. /* Open zip file and temporary output file */
  4440. if (show_what_doing) {
  4441. fprintf(mesg, "sd: Open zip file and create temp file\n");
  4442. fflush(mesg);
  4443. }
  4444. diag("opening zip file and creating temporary zip file");
  4445. x = NULL;
  4446. tempzn = 0;
  4447. if (strcmp(zipfile, "-") == 0)
  4448. {
  4449. #ifdef MSDOS
  4450. /* It is nonsense to emit the binary data stream of a zipfile to
  4451. * the (text mode) console. This case should already have been caught
  4452. * in a call to zipstdout() far above. Therefore, if the following
  4453. * failsafe check detects a console attached to stdout, zip is stopped
  4454. * with an "internal logic error"! */
  4455. if (isatty(fileno(stdout)))
  4456. ZIPERR(ZE_LOGIC, "tried to write binary zipfile data to console!");
  4457. /* Set stdout mode to binary for MSDOS systems */
  4458. # ifdef __HIGHC__
  4459. setmode(stdout, _BINARY);
  4460. # else
  4461. setmode(fileno(stdout), O_BINARY);
  4462. # endif
  4463. y = zfdopen(fileno(stdout), FOPW);
  4464. #else
  4465. y = stdout;
  4466. #endif
  4467. /* tempzip must be malloced so a later free won't barf */
  4468. tempzip = malloc(4);
  4469. if (tempzip == NULL) {
  4470. ZIPERR(ZE_MEM, "allocating temp filename");
  4471. }
  4472. strcpy(tempzip, "-");
  4473. }
  4474. else if (d) /* d true if just appending (-g) */
  4475. {
  4476. if (total_disks > 1) {
  4477. ZIPERR(ZE_PARMS, "cannot grow split archive");
  4478. }
  4479. if ((y = zfopen(zipfile, FOPM)) == NULL) {
  4480. ZIPERR(ZE_NAME, zipfile);
  4481. }
  4482. tempzip = zipfile;
  4483. /*
  4484. tempzf = y;
  4485. */
  4486. if (zfseeko(y, cenbeg, SEEK_SET)) {
  4487. ZIPERR(ferror(y) ? ZE_READ : ZE_EOF, zipfile);
  4488. }
  4489. bytes_this_split = cenbeg;
  4490. tempzn = cenbeg;
  4491. }
  4492. else
  4493. {
  4494. if (show_what_doing) {
  4495. fprintf(mesg, "sd: Creating new zip file\n");
  4496. fflush(mesg);
  4497. }
  4498. /* See if there is something at beginning of disk 1 to copy.
  4499. If not, do nothing as zipcopy() will open files to read
  4500. as needed. */
  4501. if (zipbeg) {
  4502. in_split_path = get_in_split_path(in_path, 0);
  4503. while ((in_file = zfopen(in_split_path, FOPR_EX)) == NULL) {
  4504. /* could not open split */
  4505. /* Ask for directory with split. Updates in_path */
  4506. if (ask_for_split_read_path(0) != ZE_OK) {
  4507. ZIPERR(ZE_ABORT, "could not open archive to read");
  4508. }
  4509. free(in_split_path);
  4510. in_split_path = get_in_split_path(in_path, 1);
  4511. }
  4512. }
  4513. #if defined(UNIX) && !defined(NO_MKSTEMP)
  4514. {
  4515. int yd;
  4516. int i;
  4517. /* use mkstemp to avoid race condition and compiler warning */
  4518. if (tempath != NULL)
  4519. {
  4520. /* if -b used to set temp file dir use that for split temp */
  4521. if ((tempzip = malloc(strlen(tempath) + 12)) == NULL) {
  4522. ZIPERR(ZE_MEM, "allocating temp filename");
  4523. }
  4524. strcpy(tempzip, tempath);
  4525. if (lastchar(tempzip) != '/')
  4526. strcat(tempzip, "/");
  4527. }
  4528. else
  4529. {
  4530. /* create path by stripping name and appending template */
  4531. if ((tempzip = malloc(strlen(zipfile) + 12)) == NULL) {
  4532. ZIPERR(ZE_MEM, "allocating temp filename");
  4533. }
  4534. strcpy(tempzip, zipfile);
  4535. for(i = strlen(tempzip); i > 0; i--) {
  4536. if (tempzip[i - 1] == '/')
  4537. break;
  4538. }
  4539. tempzip[i] = '\0';
  4540. }
  4541. strcat(tempzip, "ziXXXXXX");
  4542. if ((yd = mkstemp(tempzip)) == EOF) {
  4543. ZIPERR(ZE_TEMP, tempzip);
  4544. }
  4545. if ((y = fdopen(yd, FOPW_TMP)) == NULL) {
  4546. ZIPERR(ZE_TEMP, tempzip);
  4547. }
  4548. }
  4549. #else
  4550. if ((tempzip = tempname(zipfile)) == NULL) {
  4551. ZIPERR(ZE_MEM, "allocating temp filename");
  4552. }
  4553. if ((y = zfopen(tempzip, FOPW_TMP)) == NULL) {
  4554. ZIPERR(ZE_TEMP, tempzip);
  4555. }
  4556. #endif
  4557. }
  4558. #if (!defined(VMS) && !defined(CMS_MVS))
  4559. /* Use large buffer to speed up stdio: */
  4560. #if (defined(_IOFBF) || !defined(BUFSIZ))
  4561. zipbuf = (char *)malloc(ZBSZ);
  4562. #else
  4563. zipbuf = (char *)malloc(BUFSIZ);
  4564. #endif
  4565. if (zipbuf == NULL) {
  4566. ZIPERR(ZE_MEM, tempzip);
  4567. }
  4568. # ifdef _IOFBF
  4569. setvbuf(y, zipbuf, _IOFBF, ZBSZ);
  4570. # else
  4571. setbuf(y, zipbuf);
  4572. # endif /* _IOBUF */
  4573. #endif /* !VMS && !CMS_MVS */
  4574. /* If not seekable set some flags 3/14/05 EG */
  4575. output_seekable = 1;
  4576. if (!is_seekable(y)) {
  4577. output_seekable = 0;
  4578. use_descriptors = 1;
  4579. }
  4580. /* Not needed. Only need Zip64 when input file is larger than 2 GB or reading
  4581. stdin and writing stdout. This is set in putlocal() for each file. */
  4582. #if 0
  4583. /* If using descriptors and Zip64 enabled force Zip64 3/13/05 EG */
  4584. # ifdef ZIP64_SUPPORT
  4585. if (use_descriptors && force_zip64 != 0) {
  4586. force_zip64 = 1;
  4587. }
  4588. # endif
  4589. #endif
  4590. /* if archive exists, not streaming and not deleting or growing, copy
  4591. any bytes at beginning */
  4592. if (strcmp(zipfile, "-") != 0 && !d) /* this must go *after* set[v]buf */
  4593. {
  4594. /* copy anything before archive */
  4595. if (in_file && zipbeg && (r = bfcopy(zipbeg)) != ZE_OK) {
  4596. ZIPERR(r, r == ZE_TEMP ? tempzip : zipfile);
  4597. }
  4598. if (in_file) {
  4599. fclose(in_file);
  4600. in_file = NULL;
  4601. free(in_split_path);
  4602. }
  4603. tempzn = zipbeg;
  4604. if (split_method) {
  4605. /* add spanning signature */
  4606. if (show_what_doing) {
  4607. fprintf(mesg, "sd: Adding spanning/splitting signature at top of archive\n");
  4608. fflush(mesg);
  4609. }
  4610. /* write the spanning signature at the top of the archive */
  4611. errbuf[0] = 0x50 /*'P' except for EBCDIC*/;
  4612. errbuf[1] = 0x4b /*'K' except for EBCDIC*/;
  4613. errbuf[2] = 7;
  4614. errbuf[3] = 8;
  4615. bfwrite(errbuf, 1, 4, BFWRITE_DATA);
  4616. /* tempzn updated below */
  4617. tempzn += 4;
  4618. }
  4619. }
  4620. o = 0; /* no ZE_OPEN errors yet */
  4621. /* Process zip file, updating marked files */
  4622. #ifdef DEBUG
  4623. if (zfiles != NULL)
  4624. diag("going through old zip file");
  4625. #endif
  4626. if (zfiles != NULL && show_what_doing) {
  4627. fprintf(mesg, "sd: Going through old zip file\n");
  4628. fflush(mesg);
  4629. }
  4630. w = &zfiles;
  4631. while ((z = *w) != NULL) {
  4632. if (z->mark == 1)
  4633. {
  4634. uzoff_t len;
  4635. if ((zoff_t)z->len == -1)
  4636. /* device */
  4637. len = 0;
  4638. else
  4639. len = z->len;
  4640. /* if not deleting, zip it up */
  4641. if (action != ARCHIVE && action != DELETE)
  4642. {
  4643. struct zlist far *localz; /* local header */
  4644. if (verbose || !(filesync && z->current))
  4645. DisplayRunningStats();
  4646. if (noisy)
  4647. {
  4648. if (action == FRESHEN) {
  4649. fprintf(mesg, "freshening: %s", z->oname);
  4650. mesg_line_started = 1;
  4651. fflush(mesg);
  4652. } else if (filesync && z->current) {
  4653. if (verbose) {
  4654. fprintf(mesg, " ok: %s", z->oname);
  4655. mesg_line_started = 1;
  4656. fflush(mesg);
  4657. }
  4658. } else if (!(filesync && z->current)) {
  4659. fprintf(mesg, "updating: %s", z->oname);
  4660. mesg_line_started = 1;
  4661. fflush(mesg);
  4662. }
  4663. }
  4664. if (logall)
  4665. {
  4666. if (action == FRESHEN) {
  4667. fprintf(logfile, "freshening: %s", z->oname);
  4668. logfile_line_started = 1;
  4669. fflush(logfile);
  4670. } else if (filesync && z->current) {
  4671. if (verbose) {
  4672. fprintf(logfile, " current: %s", z->oname);
  4673. logfile_line_started = 1;
  4674. fflush(logfile);
  4675. }
  4676. } else {
  4677. fprintf(logfile, "updating: %s", z->oname);
  4678. logfile_line_started = 1;
  4679. fflush(logfile);
  4680. }
  4681. }
  4682. /* Get local header flags and extra fields */
  4683. if (readlocal(&localz, z) != ZE_OK) {
  4684. zipwarn("could not read local entry information: ", z->oname);
  4685. z->lflg = z->flg;
  4686. z->ext = 0;
  4687. } else {
  4688. z->lflg = localz->lflg;
  4689. z->ext = localz->ext;
  4690. z->extra = localz->extra;
  4691. if (localz->nam) free(localz->iname);
  4692. if (localz->nam) free(localz->name);
  4693. #ifdef UNICODE_SUPPORT
  4694. if (localz->uname) free(localz->uname);
  4695. #endif
  4696. free(localz);
  4697. }
  4698. if (!(filesync && z->current) &&
  4699. (r = zipup(z)) != ZE_OK && r != ZE_OPEN && r != ZE_MISS)
  4700. {
  4701. zipmessage_nl("", 1);
  4702. /*
  4703. if (noisy)
  4704. {
  4705. if (mesg_line_started) {
  4706. #if (!defined(MACOS) && !defined(WINDLL))
  4707. putc('\n', mesg);
  4708. fflush(mesg);
  4709. #else
  4710. fprintf(stdout, "\n");
  4711. fflush(stdout);
  4712. #endif
  4713. mesg_line_started = 0;
  4714. }
  4715. }
  4716. if (logall) {
  4717. if (logfile_line_started) {
  4718. fprintf(logfile, "\n");
  4719. logfile_line_started = 0;
  4720. fflush(logfile);
  4721. }
  4722. }
  4723. */
  4724. sprintf(errbuf, "was zipping %s", z->name);
  4725. ZIPERR(r, errbuf);
  4726. }
  4727. if (filesync && z->current)
  4728. {
  4729. /* if filesync if entry matches OS just copy */
  4730. if ((r = zipcopy(z)) != ZE_OK)
  4731. {
  4732. sprintf(errbuf, "was copying %s", z->oname);
  4733. ZIPERR(r, errbuf);
  4734. }
  4735. zipmessage_nl("", 1);
  4736. /*
  4737. if (noisy)
  4738. {
  4739. if (mesg_line_started) {
  4740. #if (!defined(MACOS) && !defined(WINDLL))
  4741. putc('\n', mesg);
  4742. fflush(mesg);
  4743. #else
  4744. fprintf(stdout, "\n");
  4745. fflush(stdout);
  4746. #endif
  4747. mesg_line_started = 0;
  4748. }
  4749. }
  4750. if (logall) {
  4751. if (logfile_line_started) {
  4752. fprintf(logfile, "\n");
  4753. logfile_line_started = 0;
  4754. fflush(logfile);
  4755. }
  4756. }
  4757. */
  4758. }
  4759. if (r == ZE_OPEN || r == ZE_MISS)
  4760. {
  4761. o = 1;
  4762. zipmessage_nl("", 1);
  4763. /*
  4764. if (noisy)
  4765. {
  4766. #if (!defined(MACOS) && !defined(WINDLL))
  4767. putc('\n', mesg);
  4768. fflush(mesg);
  4769. #else
  4770. fprintf(stdout, "\n");
  4771. #endif
  4772. mesg_line_started = 0;
  4773. }
  4774. if (logall) {
  4775. fprintf(logfile, "\n");
  4776. logfile_line_started = 0;
  4777. fflush(logfile);
  4778. }
  4779. */
  4780. if (r == ZE_OPEN) {
  4781. perror(z->oname);
  4782. zipwarn("could not open for reading: ", z->oname);
  4783. if (bad_open_is_error) {
  4784. sprintf(errbuf, "was zipping %s", z->name);
  4785. ZIPERR(r, errbuf);
  4786. }
  4787. } else {
  4788. zipwarn("file and directory with the same name: ", z->oname);
  4789. }
  4790. zipwarn("will just copy entry over: ", z->oname);
  4791. if ((r = zipcopy(z)) != ZE_OK)
  4792. {
  4793. sprintf(errbuf, "was copying %s", z->oname);
  4794. ZIPERR(r, errbuf);
  4795. }
  4796. z->mark = 0;
  4797. }
  4798. files_so_far++;
  4799. good_bytes_so_far += z->len;
  4800. bytes_so_far += len;
  4801. w = &z->nxt;
  4802. }
  4803. else if (action == ARCHIVE)
  4804. {
  4805. #ifdef DEBUG
  4806. zoff_t here = zftello(y);
  4807. #endif
  4808. DisplayRunningStats();
  4809. if (skip_this_disk - 1 != z->dsk)
  4810. /* moved to another disk so start copying again */
  4811. skip_this_disk = 0;
  4812. if (skip_this_disk - 1 == z->dsk) {
  4813. /* skipping this disk */
  4814. if (noisy) {
  4815. fprintf(mesg, " skipping: %s", z->oname);
  4816. mesg_line_started = 1;
  4817. fflush(mesg);
  4818. }
  4819. if (logall) {
  4820. fprintf(logfile, " skipping: %s", z->oname);
  4821. logfile_line_started = 1;
  4822. fflush(logfile);
  4823. }
  4824. } else {
  4825. /* copying this entry */
  4826. if (noisy) {
  4827. fprintf(mesg, " copying: %s", z->oname);
  4828. if (display_usize) {
  4829. fprintf(mesg, " (");
  4830. DisplayNumString(mesg, z->len );
  4831. fprintf(mesg, ")");
  4832. }
  4833. mesg_line_started = 1;
  4834. fflush(mesg);
  4835. }
  4836. if (logall)
  4837. {
  4838. fprintf(logfile, " copying: %s", z->oname);
  4839. if (display_usize) {
  4840. fprintf(logfile, " (");
  4841. DisplayNumString(logfile, z->len );
  4842. fprintf(logfile, ")");
  4843. }
  4844. logfile_line_started = 1;
  4845. fflush(logfile);
  4846. }
  4847. }
  4848. if (skip_this_disk - 1 == z->dsk)
  4849. /* skip entries on this disk */
  4850. z->mark = 0;
  4851. else if ((r = zipcopy(z)) != ZE_OK)
  4852. {
  4853. if (r == ZE_ABORT) {
  4854. ZIPERR(r, "user requested abort");
  4855. } else if (fix != 1) {
  4856. /* exit */
  4857. sprintf(errbuf, "was copying %s", z->oname);
  4858. zipwarn("(try -F to attempt to fix)", "");
  4859. ZIPERR(r, errbuf);
  4860. }
  4861. else /* if (r == ZE_FORM) */ {
  4862. #ifdef DEBUG
  4863. zoff_t here = zftello(y);
  4864. #endif
  4865. /* seek back in output to start of this entry so can overwrite */
  4866. if (zfseeko(y, current_local_offset, SEEK_SET) != 0){
  4867. ZIPERR(r, "could not seek in output file");
  4868. }
  4869. zipwarn("bad - skipping: ", z->oname);
  4870. #ifdef DEBUG
  4871. here = zftello(y);
  4872. #endif
  4873. tempzn = current_local_offset;
  4874. bytes_this_split = current_local_offset;
  4875. }
  4876. }
  4877. if (skip_this_disk || !(fix == 1 && r != ZE_OK))
  4878. {
  4879. if (noisy && mesg_line_started) {
  4880. fprintf(mesg, "\n");
  4881. mesg_line_started = 0;
  4882. fflush(mesg);
  4883. }
  4884. if (logall && logfile_line_started) {
  4885. fprintf(logfile, "\n");
  4886. logfile_line_started = 0;
  4887. fflush(logfile);
  4888. }
  4889. }
  4890. /* input counts */
  4891. files_so_far++;
  4892. if (r != ZE_OK)
  4893. bad_bytes_so_far += z->siz;
  4894. else
  4895. good_bytes_so_far += z->siz;
  4896. bytes_so_far += z->siz;
  4897. if (r != ZE_OK && fix == 1) {
  4898. /* remove bad entry from list */
  4899. v = z->nxt; /* delete entry from list */
  4900. free((zvoid *)(z->iname));
  4901. free((zvoid *)(z->zname));
  4902. free(z->oname);
  4903. #ifdef UNICODE_SUPPORT
  4904. if (z->uname) free(z->uname);
  4905. #endif /* def UNICODE_SUPPORT */
  4906. if (z->ext)
  4907. /* don't have local extra until zipcopy reads it */
  4908. if (z->extra) free((zvoid *)(z->extra));
  4909. if (z->cext && z->cextra != z->extra)
  4910. free((zvoid *)(z->cextra));
  4911. if (z->com)
  4912. free((zvoid *)(z->comment));
  4913. farfree((zvoid far *)z);
  4914. *w = v;
  4915. zcount--;
  4916. } else {
  4917. w = &z->nxt;
  4918. }
  4919. #ifdef WINDLL
  4920. #ifdef ZIP64_SUPPORT
  4921. /* int64 support in caller */
  4922. if (lpZipUserFunctions->ServiceApplication64 != NULL)
  4923. {
  4924. if ((*lpZipUserFunctions->ServiceApplication64)(z->zname, z->siz))
  4925. ZIPERR(ZE_ABORT, "User terminated operation");
  4926. }
  4927. else
  4928. {
  4929. /* no int64 support in caller */
  4930. filesize64 = z->siz;
  4931. low = (unsigned long)(filesize64 & 0x00000000FFFFFFFF);
  4932. high = (unsigned long)((filesize64 >> 32) & 0x00000000FFFFFFFF);
  4933. if (lpZipUserFunctions->ServiceApplication64_No_Int64 != NULL) {
  4934. if ((*lpZipUserFunctions->ServiceApplication64_No_Int64)(z->zname, low, high))
  4935. ZIPERR(ZE_ABORT, "User terminated operation");
  4936. }
  4937. }
  4938. #else
  4939. if (lpZipUserFunctions->ServiceApplication != NULL) {
  4940. if ((*lpZipUserFunctions->ServiceApplication)(z->zname, z->siz))
  4941. ZIPERR(ZE_ABORT, "User terminated operation");
  4942. }
  4943. #endif /* ZIP64_SUPPORT - I added comments around // comments - does that help below? EG */
  4944. /* strange but true: if I delete this and put these two endifs adjacent to
  4945. each other, the Aztec Amiga compiler never sees the second endif! WTF?? PK */
  4946. #endif /* WINDLL */
  4947. }
  4948. else
  4949. {
  4950. DisplayRunningStats();
  4951. if (noisy)
  4952. {
  4953. fprintf(mesg, "deleting: %s", z->oname);
  4954. if (display_usize) {
  4955. fprintf(mesg, " (");
  4956. DisplayNumString(mesg, z->len );
  4957. fprintf(mesg, ")");
  4958. }
  4959. fflush(mesg);
  4960. fprintf(mesg, "\n");
  4961. }
  4962. if (logall)
  4963. {
  4964. fprintf(logfile, "deleting: %s", z->oname);
  4965. if (display_usize) {
  4966. fprintf(logfile, " (");
  4967. DisplayNumString(logfile, z->len );
  4968. fprintf(logfile, ")");
  4969. }
  4970. fprintf(logfile, "\n");
  4971. fflush(logfile);
  4972. }
  4973. files_so_far++;
  4974. good_bytes_so_far += z->siz;
  4975. bytes_so_far += z->siz;
  4976. #ifdef WINDLL
  4977. #ifdef ZIP64_SUPPORT
  4978. /* int64 support in caller */
  4979. if (lpZipUserFunctions->ServiceApplication64 != NULL)
  4980. {
  4981. if ((*lpZipUserFunctions->ServiceApplication64)(z->zname, z->siz))
  4982. ZIPERR(ZE_ABORT, "User terminated operation");
  4983. }
  4984. else
  4985. {
  4986. /* no int64 support in caller */
  4987. filesize64 = z->siz;
  4988. low = (unsigned long)(filesize64 & 0x00000000FFFFFFFF);
  4989. high = (unsigned long)((filesize64 >> 32) & 0x00000000FFFFFFFF);
  4990. if (lpZipUserFunctions->ServiceApplication64_No_Int64 != NULL) {
  4991. if ((*lpZipUserFunctions->ServiceApplication64_No_Int64)(z->zname, low, high))
  4992. ZIPERR(ZE_ABORT, "User terminated operation");
  4993. }
  4994. }
  4995. #else
  4996. if (lpZipUserFunctions->ServiceApplication != NULL) {
  4997. if ((*lpZipUserFunctions->ServiceApplication)(z->zname, z->siz))
  4998. ZIPERR(ZE_ABORT, "User terminated operation");
  4999. }
  5000. #endif /* ZIP64_SUPPORT - I added comments around // comments - does that help below? EG */
  5001. /* strange but true: if I delete this and put these two endifs adjacent to
  5002. each other, the Aztec Amiga compiler never sees the second endif! WTF?? PK */
  5003. #endif /* WINDLL */
  5004. v = z->nxt; /* delete entry from list */
  5005. free((zvoid *)(z->iname));
  5006. free((zvoid *)(z->zname));
  5007. free(z->oname);
  5008. #ifdef UNICODE_SUPPORT
  5009. if (z->uname) free(z->uname);
  5010. #endif /* def UNICODE_SUPPORT */
  5011. if (z->ext)
  5012. /* don't have local extra until zipcopy reads it */
  5013. if (z->extra) free((zvoid *)(z->extra));
  5014. if (z->cext && z->cextra != z->extra)
  5015. free((zvoid *)(z->cextra));
  5016. if (z->com)
  5017. free((zvoid *)(z->comment));
  5018. farfree((zvoid far *)z);
  5019. *w = v;
  5020. zcount--;
  5021. }
  5022. }
  5023. else
  5024. {
  5025. if (action == ARCHIVE) {
  5026. v = z->nxt; /* delete entry from list */
  5027. free((zvoid *)(z->iname));
  5028. free((zvoid *)(z->zname));
  5029. free(z->oname);
  5030. #ifdef UNICODE_SUPPORT
  5031. if (z->uname) free(z->uname);
  5032. #endif /* def UNICODE_SUPPORT */
  5033. if (z->ext)
  5034. /* don't have local extra until zipcopy reads it */
  5035. if (z->extra) free((zvoid *)(z->extra));
  5036. if (z->cext && z->cextra != z->extra)
  5037. free((zvoid *)(z->cextra));
  5038. if (z->com)
  5039. free((zvoid *)(z->comment));
  5040. farfree((zvoid far *)z);
  5041. *w = v;
  5042. zcount--;
  5043. }
  5044. else
  5045. {
  5046. if (filesync) {
  5047. /* Delete entries if don't match a file on OS */
  5048. BlankRunningStats();
  5049. if (noisy)
  5050. {
  5051. fprintf(mesg, "deleting: %s", z->oname);
  5052. if (display_usize) {
  5053. fprintf(mesg, " (");
  5054. DisplayNumString(mesg, z->len );
  5055. fprintf(mesg, ")");
  5056. }
  5057. fflush(mesg);
  5058. fprintf(mesg, "\n");
  5059. mesg_line_started = 0;
  5060. }
  5061. if (logall)
  5062. {
  5063. fprintf(logfile, "deleting: %s", z->oname);
  5064. if (display_usize) {
  5065. fprintf(logfile, " (");
  5066. DisplayNumString(logfile, z->len );
  5067. fprintf(logfile, ")");
  5068. }
  5069. fprintf(logfile, "\n");
  5070. fflush(logfile);
  5071. logfile_line_started = 0;
  5072. }
  5073. }
  5074. /* copy the original entry */
  5075. else if (!d && !diff_mode && (r = zipcopy(z)) != ZE_OK)
  5076. {
  5077. sprintf(errbuf, "was copying %s", z->oname);
  5078. ZIPERR(r, errbuf);
  5079. }
  5080. w = &z->nxt;
  5081. }
  5082. }
  5083. }
  5084. /* Process the edited found list, adding them to the zip file */
  5085. if (show_what_doing) {
  5086. fprintf(mesg, "sd: Zipping up new entries\n");
  5087. fflush(mesg);
  5088. }
  5089. diag("zipping up new entries, if any");
  5090. Trace((stderr, "zip diagnostic: fcount=%u\n", (unsigned)fcount));
  5091. for (f = found; f != NULL; f = fexpel(f))
  5092. {
  5093. uzoff_t len;
  5094. /* add a new zfiles entry and set the name */
  5095. if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  5096. ZIPERR(ZE_MEM, "was adding files to zip file");
  5097. }
  5098. z->nxt = NULL;
  5099. z->name = f->name;
  5100. f->name = NULL;
  5101. #ifdef UNICODE_SUPPORT
  5102. z->uname = NULL; /* UTF-8 name for extra field */
  5103. z->zuname = NULL; /* externalized UTF-8 name for matching */
  5104. z->ouname = NULL; /* display version of UTF-8 name with OEM */
  5105. #if 0
  5106. /* New AppNote bit 11 allowing storing UTF-8 in path */
  5107. if (utf8_force && f->uname) {
  5108. if (f->iname)
  5109. free(f->iname);
  5110. if ((f->iname = malloc(strlen(f->uname) + 1)) == NULL)
  5111. ZIPERR(ZE_MEM, "Unicode bit 11");
  5112. strcpy(f->iname, f->uname);
  5113. # ifdef WIN32
  5114. if (f->inamew)
  5115. free(f->inamew);
  5116. f->inamew = utf8_to_wchar_string(f->iname);
  5117. # endif
  5118. }
  5119. #endif
  5120. /* Only set z->uname if have a non-ASCII Unicode name */
  5121. /* The Unicode path extra field is created if z->uname is not NULL,
  5122. unless on a UTF-8 system, then instead of creating the extra field
  5123. set bit 11 in the General Purpose Bit Flag */
  5124. {
  5125. int is_ascii = 0;
  5126. # ifdef WIN32
  5127. if (!no_win32_wide)
  5128. is_ascii = is_ascii_stringw(f->inamew);
  5129. else
  5130. is_ascii = is_ascii_string(f->uname);
  5131. # else
  5132. is_ascii = is_ascii_string(f->uname);
  5133. # endif
  5134. if (z->uname == NULL) {
  5135. if (!is_ascii)
  5136. z->uname = f->uname;
  5137. else
  5138. free(f->uname);
  5139. } else {
  5140. free(f->uname);
  5141. }
  5142. }
  5143. f->uname = NULL;
  5144. #endif
  5145. z->iname = f->iname;
  5146. f->iname = NULL;
  5147. z->zname = f->zname;
  5148. f->zname = NULL;
  5149. z->oname = f->oname;
  5150. f->oname = NULL;
  5151. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  5152. z->namew = f->namew;
  5153. f->namew = NULL;
  5154. z->inamew = f->inamew;
  5155. f->inamew = NULL;
  5156. z->znamew = f->znamew;
  5157. f->znamew = NULL;
  5158. #endif
  5159. z->ext = z->cext = z->com = 0;
  5160. z->extra = z->cextra = NULL;
  5161. z->mark = 1;
  5162. z->dosflag = f->dosflag;
  5163. /* zip it up */
  5164. DisplayRunningStats();
  5165. if (noisy)
  5166. {
  5167. fprintf(mesg, " adding: %s", z->oname);
  5168. mesg_line_started = 1;
  5169. fflush(mesg);
  5170. }
  5171. if (logall)
  5172. {
  5173. fprintf(logfile, " adding: %s", z->oname);
  5174. logfile_line_started = 1;
  5175. fflush(logfile);
  5176. }
  5177. /* initial scan */
  5178. len = f->usize;
  5179. if ((r = zipup(z)) != ZE_OK && r != ZE_OPEN && r != ZE_MISS)
  5180. {
  5181. zipmessage_nl("", 1);
  5182. /*
  5183. if (noisy)
  5184. {
  5185. #if (!defined(MACOS) && !defined(WINDLL))
  5186. putc('\n', mesg);
  5187. fflush(mesg);
  5188. #else
  5189. fprintf(stdout, "\n");
  5190. #endif
  5191. mesg_line_started = 0;
  5192. fflush(mesg);
  5193. }
  5194. if (logall) {
  5195. fprintf(logfile, "\n");
  5196. logfile_line_started = 0;
  5197. fflush(logfile);
  5198. }
  5199. */
  5200. sprintf(errbuf, "was zipping %s", z->oname);
  5201. ZIPERR(r, errbuf);
  5202. }
  5203. if (r == ZE_OPEN || r == ZE_MISS)
  5204. {
  5205. o = 1;
  5206. zipmessage_nl("", 1);
  5207. /*
  5208. if (noisy)
  5209. {
  5210. #if (!defined(MACOS) && !defined(WINDLL))
  5211. putc('\n', mesg);
  5212. fflush(mesg);
  5213. #else
  5214. fprintf(stdout, "\n");
  5215. #endif
  5216. mesg_line_started = 0;
  5217. fflush(mesg);
  5218. }
  5219. if (logall) {
  5220. fprintf(logfile, "\n");
  5221. logfile_line_started = 0;
  5222. fflush(logfile);
  5223. }
  5224. */
  5225. if (r == ZE_OPEN) {
  5226. perror("zip warning");
  5227. if (logfile)
  5228. fprintf(logfile, "zip warning: %s\n", strerror(errno));
  5229. zipwarn("could not open for reading: ", z->oname);
  5230. if (bad_open_is_error) {
  5231. sprintf(errbuf, "was zipping %s", z->name);
  5232. ZIPERR(r, errbuf);
  5233. }
  5234. } else {
  5235. zipwarn("file and directory with the same name: ", z->oname);
  5236. }
  5237. files_so_far++;
  5238. bytes_so_far += len;
  5239. bad_files_so_far++;
  5240. bad_bytes_so_far += len;
  5241. free((zvoid *)(z->name));
  5242. free((zvoid *)(z->iname));
  5243. free((zvoid *)(z->zname));
  5244. free(z->oname);
  5245. #ifdef UNICODE_SUPPORT
  5246. if (z->uname)
  5247. free(z->uname);
  5248. # ifdef WIN32
  5249. if (z->namew)
  5250. free((zvoid *)(z->namew));
  5251. if (z->inamew)
  5252. free((zvoid *)(z->inamew));
  5253. if (z->znamew)
  5254. free((zvoid *)(z->znamew));
  5255. # endif
  5256. #endif
  5257. farfree((zvoid far *)z);
  5258. }
  5259. else
  5260. {
  5261. files_so_far++;
  5262. /* current size of file (just before reading) */
  5263. good_bytes_so_far += z->len;
  5264. /* size of file on initial scan */
  5265. bytes_so_far += len;
  5266. *w = z;
  5267. w = &z->nxt;
  5268. zcount++;
  5269. }
  5270. }
  5271. if (key != NULL)
  5272. {
  5273. free((zvoid *)key);
  5274. key = NULL;
  5275. }
  5276. /* final status 3/17/05 EG */
  5277. if (noisy && bad_files_so_far)
  5278. {
  5279. char tempstrg[100];
  5280. fprintf(mesg, "\nzip warning: Not all files were readable\n");
  5281. fprintf(mesg, " files/entries read: %lu", files_total - bad_files_so_far);
  5282. WriteNumString(good_bytes_so_far, tempstrg);
  5283. fprintf(mesg, " (%s bytes)", tempstrg);
  5284. fprintf(mesg, " skipped: %lu", bad_files_so_far);
  5285. WriteNumString(bad_bytes_so_far, tempstrg);
  5286. fprintf(mesg, " (%s bytes)\n", tempstrg);
  5287. fflush(mesg);
  5288. }
  5289. if (logfile && bad_files_so_far)
  5290. {
  5291. char tempstrg[100];
  5292. fprintf(logfile, "\nzip warning: Not all files were readable\n");
  5293. fprintf(logfile, " files/entries read: %lu", files_total - bad_files_so_far);
  5294. WriteNumString(good_bytes_so_far, tempstrg);
  5295. fprintf(logfile, " (%s bytes)", tempstrg);
  5296. fprintf(logfile, " skipped: %lu", bad_files_so_far);
  5297. WriteNumString(bad_bytes_so_far, tempstrg);
  5298. fprintf(logfile, " (%s bytes)", tempstrg);
  5299. }
  5300. /* Get one line comment for each new entry */
  5301. if (show_what_doing) {
  5302. fprintf(mesg, "sd: Get comment if any\n");
  5303. fflush(mesg);
  5304. }
  5305. #if defined(AMIGA) || defined(MACOS)
  5306. if (comadd || filenotes)
  5307. {
  5308. if (comadd)
  5309. #else
  5310. if (comadd)
  5311. {
  5312. #endif
  5313. {
  5314. if (comment_stream == NULL) {
  5315. #ifndef RISCOS
  5316. comment_stream = (FILE*)fdopen(fileno(stderr), "r");
  5317. #else
  5318. comment_stream = stderr;
  5319. #endif
  5320. }
  5321. if ((e = malloc(MAXCOM + 1)) == NULL) {
  5322. ZIPERR(ZE_MEM, "was reading comment lines");
  5323. }
  5324. }
  5325. #ifdef __human68k__
  5326. setmode(fileno(comment_stream), O_TEXT);
  5327. #endif
  5328. #ifdef MACOS
  5329. if (noisy) fprintf(mesg, "\nStart commenting files ...\n");
  5330. #endif
  5331. for (z = zfiles; z != NULL; z = z->nxt)
  5332. if (z->mark)
  5333. #if defined(AMIGA) || defined(MACOS)
  5334. if (filenotes && (p = GetComment(z->zname)))
  5335. {
  5336. if (z->comment = malloc(k = strlen(p)+1))
  5337. {
  5338. z->com = k;
  5339. strcpy(z->comment, p);
  5340. }
  5341. else
  5342. {
  5343. free((zvoid *)e);
  5344. ZIPERR(ZE_MEM, "was reading filenotes");
  5345. }
  5346. }
  5347. else if (comadd)
  5348. #endif /* AMIGA || MACOS */
  5349. {
  5350. if (noisy)
  5351. fprintf(mesg, "Enter comment for %s:\n", z->oname);
  5352. if (fgets(e, MAXCOM+1, comment_stream) != NULL)
  5353. {
  5354. if ((p = malloc((extent)(k = strlen(e))+1)) == NULL)
  5355. {
  5356. free((zvoid *)e);
  5357. ZIPERR(ZE_MEM, "was reading comment lines");
  5358. }
  5359. strcpy(p, e);
  5360. if (p[k-1] == '\n')
  5361. p[--k] = 0;
  5362. z->comment = p;
  5363. /* zip64 support 09/05/2003 R.Nausedat */
  5364. z->com = (extent)k;
  5365. }
  5366. }
  5367. #ifdef MACOS
  5368. if (noisy) fprintf(mesg, "\n...done");
  5369. #endif
  5370. #if defined(AMIGA) || defined(MACOS)
  5371. if (comadd)
  5372. free((zvoid *)e);
  5373. GetComment(NULL); /* makes it free its internal storage */
  5374. #else
  5375. free((zvoid *)e);
  5376. #endif
  5377. }
  5378. /* Get multi-line comment for the zip file */
  5379. if (zipedit)
  5380. {
  5381. #ifndef WINDLL
  5382. if (comment_stream == NULL) {
  5383. #ifndef RISCOS
  5384. comment_stream = (FILE*)fdopen(fileno(stderr), "r");
  5385. #else
  5386. comment_stream = stderr;
  5387. #endif
  5388. }
  5389. if ((e = malloc(MAXCOM + 1)) == NULL) {
  5390. ZIPERR(ZE_MEM, "was reading comment lines");
  5391. }
  5392. if (noisy && zcomlen)
  5393. {
  5394. fputs("current zip file comment is:\n", mesg);
  5395. fwrite(zcomment, 1, zcomlen, mesg);
  5396. if (zcomment[zcomlen-1] != '\n')
  5397. putc('\n', mesg);
  5398. free((zvoid *)zcomment);
  5399. }
  5400. if ((zcomment = malloc(1)) == NULL)
  5401. ZIPERR(ZE_MEM, "was setting comments to null");
  5402. zcomment[0] = '\0';
  5403. if (noisy)
  5404. fputs("enter new zip file comment (end with .):\n", mesg);
  5405. #if (defined(AMIGA) && (defined(LATTICE)||defined(__SASC)))
  5406. flushall(); /* tty input/output is out of sync here */
  5407. #endif
  5408. #ifdef __human68k__
  5409. setmode(fileno(comment_stream), O_TEXT);
  5410. #endif
  5411. #ifdef MACOS
  5412. printf("\n enter new zip file comment \n");
  5413. if (fgets(e, MAXCOM+1, comment_stream) != NULL) {
  5414. if ((p = malloc((k = strlen(e))+1)) == NULL) {
  5415. free((zvoid *)e);
  5416. ZIPERR(ZE_MEM, "was reading comment lines");
  5417. }
  5418. strcpy(p, e);
  5419. if (p[k-1] == '\n') p[--k] = 0;
  5420. zcomment = p;
  5421. }
  5422. #else /* !MACOS */
  5423. while (fgets(e, MAXCOM+1, comment_stream) != NULL && strcmp(e, ".\n"))
  5424. {
  5425. if (e[(r = strlen(e)) - 1] == '\n')
  5426. e[--r] = 0;
  5427. if ((p = malloc((*zcomment ? strlen(zcomment) + 3 : 1) + r)) == NULL)
  5428. {
  5429. free((zvoid *)e);
  5430. ZIPERR(ZE_MEM, "was reading comment lines");
  5431. }
  5432. if (*zcomment)
  5433. strcat(strcat(strcpy(p, zcomment), "\r\n"), e);
  5434. else
  5435. strcpy(p, *e ? e : "\r\n");
  5436. free((zvoid *)zcomment);
  5437. zcomment = p;
  5438. }
  5439. #endif /* ?MACOS */
  5440. free((zvoid *)e);
  5441. #else /* WINDLL */
  5442. comment(zcomlen);
  5443. if ((p = malloc(strlen(szCommentBuf)+1)) == NULL) {
  5444. ZIPERR(ZE_MEM, "was setting comments to null");
  5445. }
  5446. if (szCommentBuf[0] != '\0')
  5447. lstrcpy(p, szCommentBuf);
  5448. else
  5449. p[0] = '\0';
  5450. free((zvoid *)zcomment);
  5451. GlobalUnlock(hStr);
  5452. GlobalFree(hStr);
  5453. zcomment = p;
  5454. #endif /* WINDLL */
  5455. zcomlen = strlen(zcomment);
  5456. }
  5457. if (display_globaldots) {
  5458. #ifndef WINDLL
  5459. putc('\n', mesg);
  5460. #else
  5461. fprintf(stdout,"%c",'\n');
  5462. #endif
  5463. mesg_line_started = 0;
  5464. }
  5465. /* Write central directory and end header to temporary zip */
  5466. if (show_what_doing) {
  5467. fprintf(mesg, "sd: Writing central directory\n");
  5468. fflush(mesg);
  5469. }
  5470. diag("writing central directory");
  5471. k = 0; /* keep count for end header */
  5472. c = tempzn; /* get start of central */
  5473. n = t = 0;
  5474. for (z = zfiles; z != NULL; z = z->nxt)
  5475. {
  5476. if (z->mark || !(diff_mode || filesync)) {
  5477. if ((r = putcentral(z)) != ZE_OK) {
  5478. ZIPERR(r, tempzip);
  5479. }
  5480. tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  5481. n += z->len;
  5482. t += z->siz;
  5483. k++;
  5484. }
  5485. }
  5486. if (k == 0)
  5487. zipwarn("zip file empty", "");
  5488. if (verbose) {
  5489. fprintf(mesg, "total bytes=%s, compressed=%s -> %d%% savings\n",
  5490. zip_fzofft(n, NULL, "u"), zip_fzofft(t, NULL, "u"), percent(n, t));
  5491. fflush(mesg);
  5492. }
  5493. if (logall) {
  5494. fprintf(logfile, "total bytes=%s, compressed=%s -> %d%% savings\n",
  5495. zip_fzofft(n, NULL, "u"), zip_fzofft(t, NULL, "u"), percent(n, t));
  5496. fflush(logfile);
  5497. }
  5498. t = tempzn - c; /* compute length of central */
  5499. diag("writing end of central directory");
  5500. if (show_what_doing) {
  5501. fprintf(mesg, "sd: Writing end of central directory\n");
  5502. fflush(mesg);
  5503. }
  5504. if ((r = putend(k, t, c, zcomlen, zcomment)) != ZE_OK) {
  5505. ZIPERR(r, tempzip);
  5506. }
  5507. /*
  5508. tempzf = NULL;
  5509. */
  5510. if (fclose(y)) {
  5511. ZIPERR(d ? ZE_WRITE : ZE_TEMP, tempzip);
  5512. }
  5513. y = NULL;
  5514. if (in_file != NULL) {
  5515. fclose(in_file);
  5516. in_file = NULL;
  5517. }
  5518. /*
  5519. if (x != NULL)
  5520. fclose(x);
  5521. */
  5522. /* Free some memory before spawning unzip */
  5523. #ifdef USE_ZLIB
  5524. zl_deflate_free();
  5525. #else
  5526. lm_free();
  5527. #endif
  5528. #ifdef BZIP2_SUPPORT
  5529. bz_compress_free();
  5530. #endif
  5531. #ifndef WINDLL
  5532. /* Test new zip file before overwriting old one or removing input files */
  5533. if (test)
  5534. check_zipfile(tempzip, argv[0]);
  5535. #endif
  5536. /* Replace old zip file with new zip file, leaving only the new one */
  5537. if (strcmp(zipfile, "-") && !d)
  5538. {
  5539. diag("replacing old zip file with new zip file");
  5540. if (show_what_doing) {
  5541. fprintf(mesg, "sd: Replacing old zip file\n");
  5542. fflush(mesg);
  5543. }
  5544. if ((r = replace(out_path, tempzip)) != ZE_OK)
  5545. {
  5546. zipwarn("new zip file left as: ", tempzip);
  5547. free((zvoid *)tempzip);
  5548. tempzip = NULL;
  5549. ZIPERR(r, "was replacing the original zip file");
  5550. }
  5551. free((zvoid *)tempzip);
  5552. }
  5553. tempzip = NULL;
  5554. if (zip_attributes && strcmp(zipfile, "-")) {
  5555. setfileattr(out_path, zip_attributes);
  5556. #ifdef VMS
  5557. /* If the zip file existed previously, restore its record format: */
  5558. if (x != NULL)
  5559. (void)VMSmunch(out_path, RESTORE_RTYPE, NULL);
  5560. #endif
  5561. }
  5562. if (strcmp(zipfile, "-")) {
  5563. if (show_what_doing) {
  5564. fprintf(mesg, "sd: Setting file type\n");
  5565. fflush(mesg);
  5566. }
  5567. set_filetype(out_path);
  5568. }
  5569. #if defined(WIN32)
  5570. /* All looks good so, if requested, clear the DOS archive bits */
  5571. if (clear_archive_bits) {
  5572. if (noisy)
  5573. zipmessage("Clearing archive bits...", "");
  5574. for (z = zfiles; z != NULL; z = z->nxt)
  5575. {
  5576. # ifdef UNICODE_SUPPORT
  5577. if (z->mark) {
  5578. if (!no_win32_wide) {
  5579. if (!ClearArchiveBitW(z->namew)){
  5580. zipwarn("Could not clear archive bit for: ", z->oname);
  5581. }
  5582. } else {
  5583. if (!ClearArchiveBit(z->name)){
  5584. zipwarn("Could not clear archive bit for: ", z->oname);
  5585. }
  5586. }
  5587. }
  5588. # else
  5589. if (!ClearArchiveBit(z->name)){
  5590. zipwarn("Could not clear archive bit for: ", z->oname);
  5591. }
  5592. # endif
  5593. }
  5594. }
  5595. #endif
  5596. /* finish logfile (it gets closed in freeup() called by finish()) */
  5597. if (logfile) {
  5598. struct tm *now;
  5599. time_t clocktime;
  5600. fprintf(logfile, "\nTotal %ld entries (", files_total);
  5601. if (good_bytes_so_far != bytes_total) {
  5602. fprintf(logfile, "planned ");
  5603. DisplayNumString(logfile, bytes_total);
  5604. fprintf(logfile, " bytes, actual ");
  5605. DisplayNumString(logfile, good_bytes_so_far);
  5606. fprintf(logfile, " bytes)");
  5607. } else {
  5608. DisplayNumString(logfile, bytes_total);
  5609. fprintf(logfile, " bytes)");
  5610. }
  5611. /* get current time */
  5612. time(&clocktime);
  5613. now = localtime(&clocktime);
  5614. fprintf(logfile, "\nDone %s", asctime(now));
  5615. }
  5616. /* Finish up (process -o, -m, clean up). Exit code depends on o. */
  5617. #if (!defined(VMS) && !defined(CMS_MVS))
  5618. free((zvoid *) zipbuf);
  5619. #endif /* !VMS && !CMS_MVS */
  5620. RETURN(finish(o ? ZE_OPEN : ZE_OK));
  5621. }