ckcfns.c 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167
  1. char *fnsv = "C-Kermit functions, 9.0.234, 18 September 2020";
  2. char *nm[] = { "Disabled", "Local only", "Remote only", "Enabled" };
  3. /* C K C F N S -- System-independent Kermit protocol support functions. */
  4. /* ...Part 1 (others moved to ckcfn2,3 to make this module smaller) */
  5. /*
  6. Author: Frank da Cruz <fdc@columbia.edu>,
  7. Columbia University Academic Information Systems, New York City (1974-2011)
  8. The Kermit Project, Bronx NY (2011-????)
  9. Copyright (C) 1985, 2020,
  10. Trustees of Columbia University in the City of New York.
  11. All rights reserved. See the C-Kermit COPYING.TXT file or the
  12. copyright text in the ckcmai.c module for disclaimer and permissions.
  13. */
  14. /*
  15. System-dependent primitives defined in:
  16. ck?tio.c -- terminal (communications) i/o
  17. cx?fio.c -- file i/o, directory structure
  18. */
  19. #include "ckcsym.h" /* Needed for Stratus VOS */
  20. #include "ckcasc.h" /* ASCII symbols */
  21. #include "ckcdeb.h" /* Debug formats, typedefs, etc. */
  22. #include "ckcker.h" /* Symbol definitions for Kermit */
  23. #include "ckcxla.h" /* Character set symbols */
  24. #include "ckcnet.h" /* VMS definition of TCPSOCKET */
  25. #ifdef OS2
  26. #ifdef OS2ONLY
  27. #include <os2.h>
  28. #endif /* OS2ONLY */
  29. #include "ckocon.h"
  30. #endif /* OS2 */
  31. int docrc = 0; /* Accumulate CRC for \v(crc16) */
  32. long crc16 = 0L; /* File CRC = \v(crc16) */
  33. int gnferror = 0; /* gnfile() failure reason */
  34. extern CHAR feol;
  35. extern int byteorder, xflg, what, fmask, cxseen, czseen, nscanfile, sysindex;
  36. extern int xcmdsrc, dispos, matchfifo;
  37. extern int inserver;
  38. extern int nolinks;
  39. #ifdef VMSORUNIX
  40. extern int zgfs_dir;
  41. #ifdef CKSYMLINK
  42. extern int zgfs_link;
  43. #endif /* CKSYMLINK */
  44. #endif /* VMSORUNIX */
  45. #ifndef NOXFER
  46. #ifndef NOICP
  47. #ifndef NOSPL
  48. extern char * clcmds;
  49. extern int haveurl;
  50. #ifdef CK_APC
  51. extern int apcactive, adl_ask;
  52. #endif /* CK_APC */
  53. #endif /* NOSPL */
  54. #endif /* NOICP */
  55. extern int remfile;
  56. /* (move these prototypes to the appropriate .h files...) */
  57. #ifdef COMMENT
  58. /* Not used */
  59. #ifdef VMS
  60. _PROTOTYP( int getvnum, (char *) );
  61. #endif /* VMS */
  62. #endif /* COMMENT */
  63. _PROTOTYP( static int bgetpkt, (int) );
  64. #ifndef NOCSETS
  65. _PROTOTYP( int lookup, (struct keytab[], char *, int, int *) );
  66. #endif /* NOCSETS */
  67. #ifndef NOSPL
  68. _PROTOTYP( int zzstring, (char *, char **, int *) );
  69. #endif /* NOSPL */
  70. #ifdef OS2
  71. #include <io.h>
  72. #ifdef OS2ONLY
  73. #include <os2.h>
  74. #endif /* OS2ONLY */
  75. #endif /* OS2 */
  76. #ifdef VMS
  77. #include <errno.h>
  78. #endif /* VMS */
  79. /* Externals from ckcmai.c */
  80. extern int srvcdmsg, srvidl, idletmo;
  81. extern char * cdmsgfile[];
  82. extern int spsiz, spmax, rpsiz, timint, srvtim, rtimo, npad, ebq, ebqflg,
  83. rpt, rptq, rptflg, capas, keep, fncact, pkttim, autopar, spsizr, xitsta;
  84. extern int pktnum, bctr, bctu, bctf, bctl, clfils, sbufnum, protocol,
  85. size, osize, spktl, nfils, ckwarn, timef, spsizf, sndtyp, rcvtyp, success;
  86. extern int parity, turn, network, whatru, fsecs, justone, slostart,
  87. ckdelay, displa, mypadn, moving, recursive, nettype;
  88. extern long filcnt;
  89. extern CK_OFF_T
  90. tfc, fsize, sendstart, rs_len, flci, flco, tlci, tlco, calibrate;
  91. extern long filrej, oldcps, cps, peakcps, ccu, ccp, filestatus;
  92. extern int fblksiz, frecl, frecfm, forg, fcctrl, fdispla, skipbup;
  93. extern int spackets, rpackets, timeouts, retrans, crunched, wmax, wcur;
  94. extern int hcflg, binary, fncnv, b_save, f_save, server;
  95. extern int nakstate, discard, rejection, local, xfermode, interrupted;
  96. extern int rq, rqf, sq, wslots, wslotn, wslotr, winlo, urpsiz, rln;
  97. extern int fnspath, fnrpath, eofmethod, diractive, whatru2, wearealike;
  98. extern int atcapr, atcapb, atcapu;
  99. extern int lpcapr, lpcapb, lpcapu;
  100. extern int swcapr, swcapb, swcapu;
  101. extern int lscapr, lscapb, lscapu;
  102. extern int rscapr, rscapb, rscapu;
  103. extern int rptena, rptmin;
  104. extern int sseqtbl[];
  105. extern int numerrs, nzxopts;
  106. extern long rptn;
  107. extern int maxtry;
  108. extern int stdouf;
  109. extern int sendmode;
  110. extern int carrier, ttprty;
  111. extern int g_fnrpath;
  112. #ifdef TCPSOCKET
  113. extern int ttnproto;
  114. #endif /* TCPSOCKET */
  115. #ifndef NOSPL
  116. extern int sndxin, sndxhi, sndxlo;
  117. #endif /* NOSPL */
  118. extern int g_binary, g_fncnv;
  119. #ifdef GFTIMER
  120. extern CKFLOAT fpfsecs;
  121. #endif /* GFTIMER */
  122. #ifdef OS2
  123. extern struct zattr iattr;
  124. #endif /* OS2 */
  125. #ifdef PIPESEND
  126. extern int usepipes;
  127. #endif /* PIPESEND */
  128. extern int pipesend;
  129. #ifdef STREAMING
  130. extern int streamrq, streaming, streamed, streamok;
  131. #endif /* STREAMING */
  132. extern int reliable, clearrq, cleared, urclear;
  133. extern int
  134. atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
  135. attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso;
  136. extern int bigsbsiz, bigrbsiz;
  137. extern char *versio;
  138. extern char *filefile;
  139. extern char whoareu[], * cksysid;
  140. #ifndef NOSERVER
  141. extern int ngetpath;
  142. extern char * getpath[];
  143. extern int fromgetpath;
  144. #endif /* NOSERVER */
  145. #ifdef CK_LOGIN
  146. extern int isguest;
  147. #endif /* CK_LOGIN */
  148. extern int srvcmdlen;
  149. extern CHAR *srvcmd, * epktmsg;
  150. extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate, myrptq;
  151. extern CHAR *data, padbuf[], stchr, mystch;
  152. extern CHAR *srvptr;
  153. extern CHAR *rdatap;
  154. extern char *cmarg, *cmarg2, **cmlist, filnam[], ofilnam[];
  155. extern char *rfspec, *prfspec, *rrfspec, *prrfspec, *sfspec, *psfspec, *rfspec;
  156. extern char fspec[];
  157. extern int fspeclen;
  158. #ifndef NOMSEND
  159. extern struct filelist * filehead, * filenext;
  160. extern int addlist;
  161. #endif /* NOMSEND */
  162. _PROTOTYP( int lslook, (unsigned int b) ); /* Locking Shift Lookahead */
  163. _PROTOTYP( int szeof, (CHAR *s) );
  164. _PROTOTYP( VOID fnlist, (void) );
  165. #endif /* NOXFER */
  166. extern CK_OFF_T ffc;
  167. /* Character set Translation */
  168. #ifndef NOCSETS
  169. extern int tcharset, fcharset, dcset7, dcset8;
  170. extern int fcs_save, tcs_save;
  171. extern int ntcsets, xlatype, cseqtab[];
  172. extern struct csinfo tcsinfo[], fcsinfo[];
  173. extern int r_cset, s_cset, afcset[];
  174. #ifdef UNICODE
  175. extern int ucsorder, fileorder;
  176. #endif /* UNICODE */
  177. _PROTOTYP( CHAR ident, (CHAR) ); /* Identity translation function */
  178. /* Arrays of and pointers to character translation functions */
  179. #ifdef CK_ANSIC
  180. extern CHAR (*rx)(CHAR); /* Pointer to input character translation function */
  181. extern CHAR (*sx)(CHAR); /* Pointer to output character translation function */
  182. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Send */
  183. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Recv */
  184. #ifdef UNICODE
  185. extern int (*xut)(USHORT); /* Translation function UCS to TCS */
  186. extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
  187. extern USHORT (*xtu)(CHAR); /* Translation function TCS to UCS */
  188. extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
  189. #endif /* UNICODE */
  190. #else /* The same declarations again for non-ANSI comilers... */
  191. extern CHAR (*rx)();
  192. extern CHAR (*sx)();
  193. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();
  194. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();
  195. #ifdef UNICODE
  196. extern int (*xut)();
  197. extern int (*xuf)();
  198. extern USHORT (*xtu)();
  199. extern USHORT (*xfu)();
  200. #endif /* UNICODE */
  201. #endif /* CK_ANSIC */
  202. #endif /* NOCSETS */
  203. /* (PWP) external def. of things used in buffered file input and output */
  204. #ifdef DYNAMIC
  205. extern char *zinbuffer, *zoutbuffer;
  206. #else
  207. extern char zinbuffer[], zoutbuffer[];
  208. #endif /* DYNAMIC */
  209. extern char *zinptr, *zoutptr;
  210. extern int zincnt, zoutcnt, zobufsize, xfrxla;
  211. extern long crcta[], crctb[]; /* CRC-16 generation tables */
  212. extern int rseqtbl[]; /* Rec'd-packet sequence # table */
  213. #ifndef NOXFER
  214. /* Criteria used by gnfile()... */
  215. char sndafter[19] = { NUL, NUL };
  216. char sndbefore[19] = { NUL, NUL };
  217. char sndnafter[19] = { NUL, NUL };
  218. char sndnbefore[19] = { NUL, NUL };
  219. char *sndexcept[NSNDEXCEPT] = { NULL, NULL };
  220. char *rcvexcept[NSNDEXCEPT] = { NULL, NULL };
  221. CK_OFF_T sndsmaller = (CK_OFF_T)-1;
  222. CK_OFF_T sndlarger = (CK_OFF_T)-1;
  223. /* Variables defined in this module but shared by other modules. */
  224. int xfrbel = 1;
  225. char * ofperms = ""; /* Output file permissions */
  226. int autopath = 0; /* SET RECEIVE PATHNAMES AUTO flag */
  227. #ifdef CALIBRATE
  228. #define CAL_O 3
  229. #define CAL_M 253
  230. int cal_j = 0;
  231. CHAR
  232. cal_a[] = {
  233. 16, 45, 98, 3, 52, 41, 14, 7, 76,165,122, 11,104, 77,166, 15,
  234. 160, 93, 18, 19,112, 85, 54, 23,232,213, 90, 27, 12, 81,126, 31,
  235. 4,205, 34, 35,144, 73,110, 39, 28,133,218, 43,156, 65,102, 47,
  236. 84, 61, 50, 51,208,117, 86, 55, 8,245, 74, 59, 44,125,222, 63,
  237. 80, 1,162, 67,116,105,206, 71,120, 9,250, 75, 88, 97, 6, 79,
  238. 100,221, 82, 83, 36, 89, 94, 87, 40, 21,106, 91,236,145,150, 95,
  239. 228, 33,130, 99,148,137,198,103,108,169, 42,107,184,129, 78,111,
  240. 0, 49,114,115, 32,121,254,119,172, 57,138,123,152,177, 22,127,
  241. 240,193, 2,131,176, 5, 38,135,204,229, 10,139,200,161,174,143,
  242. 128, 17,146,147, 68,153, 30,151, 72,217,170,155, 24,209, 62,159,
  243. 64,225,194,163,244,201, 70,167,216,197,234,171,188,109,230,175,
  244. 212,113,178,179,132,185,190,183,136,249,202,187, 92,241,118,191,
  245. 48,237, 66,195, 96,233,142,199,248, 37, 58,203, 60, 13,134,207,
  246. 20, 29,210,211,164,149,182,215,220, 25, 26,219,124,157,246,223,
  247. 180,141,226,227,192,101,238,231, 56, 69,154,235,252,173, 46,239,
  248. 224,253,242,243,196, 53,214,247,168,181,186,251,140,189,158,255
  249. };
  250. #endif /* CALIBRATE */
  251. char * rf_err = "Error receiving file"; /* rcvfil() error message */
  252. #ifdef CK_SPEED
  253. short ctlp[256] = { /* Control-Prefix table */
  254. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C0 */
  255. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G0 */
  256. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  257. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, /* DEL */
  258. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C1 */
  259. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G1 */
  260. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  261. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 /* 255 */
  262. };
  263. #endif /* CK_SPEED */
  264. int sndsrc; /* Flag for where to get names of files to send: */
  265. /* -1: znext() function */
  266. /* 0: stdin */
  267. /* >0: list in cmlist or other list */
  268. /* -9: calibrate */
  269. int memstr; /* Flag for input from memory string */
  270. int funcstr; /* Flag for input from function */
  271. int bestlen = 0;
  272. int maxsend = 0;
  273. int gnf_binary = 0; /* Prevailing xfer mode for gnfile */
  274. #ifdef pdp11
  275. #define MYINITLEN 32
  276. #else
  277. #define MYINITLEN 100
  278. #endif /* pdp11 */
  279. CHAR myinit[MYINITLEN]; /* Copy of my Send-Init data */
  280. /* Variables local to this module */
  281. #ifdef TLOG
  282. #ifndef XYZ_INTERNAL
  283. static
  284. #endif /* XYZ_INTERNAL */
  285. char *fncnam[] = {
  286. "rename", "replace", "backup", "append", "discard", "ask",
  287. "update", "dates-differ", ""
  288. };
  289. #endif /* TLOG */
  290. static char *memptr; /* Pointer for memory strings */
  291. #ifdef VMS
  292. extern int batch;
  293. #else
  294. extern int backgrd;
  295. #endif /* VMS */
  296. #ifdef CK_CTRLZ
  297. static int lastchar = 0;
  298. #endif /* CK_CTRLZ */
  299. #ifdef CK_ANSIC
  300. static int (*funcptr)(void); /* Pointer for function strings */
  301. #else
  302. static int (*funcptr)();
  303. #endif /* CK_ANSIC */
  304. #ifdef pdp11
  305. #define CMDSTRL 50
  306. static char cmdstr[50]; /* System command string. */
  307. #else
  308. #ifdef BIGBUFOK
  309. #define CMDSTRL 6144
  310. #else
  311. #define CMDSTRL 1024
  312. #endif /* BIGBUFOK */
  313. static char cmdstr[CMDSTRL+1];
  314. #endif /* pdp11 */
  315. static int drain; /* For draining stacked-up ACKs. */
  316. static int first; /* Flag for first char from input */
  317. static CHAR t; /* Current character */
  318. #ifdef COMMENT
  319. static CHAR next; /* Next character */
  320. #endif /* COMMENT */
  321. static int ebqsent = 0; /* 8th-bit prefix bid that I sent */
  322. static int lsstate = 0; /* Locking shift state */
  323. static int lsquote = 0; /* Locking shift quote */
  324. extern int quiet;
  325. /* E N C S T R -- Encode a string from memory. */
  326. /*
  327. Call this instead of getpkt() if source is a string, rather than a file.
  328. Note: Character set translation is never done in this case.
  329. */
  330. #ifdef COMMENT
  331. #define ENCBUFL 200
  332. #ifndef pdp11
  333. CHAR encbuf[ENCBUFL];
  334. #else
  335. /* This is gross, but the pdp11 root segment is out of space */
  336. /* Will allocate it in ckuusr.c. */
  337. extern CHAR encbuf[];
  338. #endif /* pdp11 */
  339. #endif /* COMMENT */
  340. /*
  341. Encode packet data from a string in memory rather than from a file.
  342. Returns the length of the encoded string on success, -1 if the string
  343. could not be completely encoded into the currently negotiated data
  344. field length.
  345. */
  346. int
  347. #ifdef CK_ANSIC
  348. encstr(CHAR *s)
  349. #else /* CK_ANSIC */
  350. encstr(s) CHAR* s;
  351. #endif /* CK_ANSIC */
  352. {
  353. /*
  354. Recoded 30 Jul 94 to use the regular data buffer and the negotiated
  355. maximum packet size. Previously we were limited to the length of encbuf[].
  356. Also, to return a failure code if the entire encoded string would not fit.
  357. Modified 14 Jul 98 to return length of encoded string.
  358. */
  359. int m, rc, slen; char *p;
  360. if (!data) { /* Watch out for null pointers. */
  361. debug(F100,"SERIOUS ERROR: encstr data == NULL","",0);
  362. return(-1);
  363. }
  364. if (!s) s = (CHAR *)""; /* Ditto. */
  365. slen = strlen((char *)s); /* Length of source string. */
  366. debug(F111,"encstr",s,slen);
  367. rc = 0; /* Return code. */
  368. m = memstr; p = memptr; /* Save these. */
  369. memptr = (char *)s; /* Point to the string. */
  370. /* debug(F101,"encstr memptr 1","",memptr); */
  371. memstr = 1; /* Flag memory string as source. */
  372. first = 1; /* Initialize character lookahead. */
  373. *data = NUL; /* In case s is empty */
  374. debug(F101,"encstr spsiz","",spsiz);
  375. rc = getpkt(spsiz,0); /* Fill a packet from the string. */
  376. debug(F101,"encstr getpkt rc","",rc);
  377. if (rc > -1 && memptr < (char *)(s + slen)) { /* Means we didn't encode */
  378. rc = -1; /* the whole string. */
  379. debug(F101,"encstr string too big","",size);
  380. }
  381. debug(F101,"encstr getpkt rc","",rc);
  382. memstr = m; /* Restore memory string flag */
  383. memptr = p; /* and pointer */
  384. first = 1; /* Put this back as we found it. */
  385. return(rc);
  386. }
  387. /* Output functions passed to 'decode': */
  388. int /* Put character in server command buffer */
  389. #ifdef CK_ANSIC
  390. putsrv(char c)
  391. #else
  392. putsrv(c) register char c;
  393. #endif /* CK_ANSIC */
  394. /* putsrv */ {
  395. *srvptr++ = c;
  396. *srvptr = '\0'; /* Make sure buffer is null-terminated */
  397. return(0);
  398. }
  399. int /* Output character to console. */
  400. #ifdef CK_ANSIC
  401. puttrm(char c)
  402. #else
  403. puttrm(c) register char c;
  404. #endif /* CK_ANSIC */
  405. /* puttrm */ {
  406. extern int rcdactive;
  407. #ifndef NOSPL
  408. extern char * qbufp; /* If REMOTE QUERY active, */
  409. extern int query, qbufn; /* also store response in */
  410. if (query && qbufn++ < 1024) { /* query buffer. */
  411. *qbufp++ = c;
  412. *qbufp = NUL;
  413. }
  414. if (!query || !xcmdsrc)
  415. #endif /* NOSPL */
  416. /*
  417. This routine is used (among other things) for printing the server's answer
  418. to a REMOTE command. But REMOTE CD is special because it isn't really
  419. asking for an answer from the server. Thus some people want to suppress
  420. the confirmation message (e.g. when the server sends back the actual path
  421. of the directory CD'd to), and expect to be able to do this with SET QUIET
  422. ON. But they would not want SET QUIET ON to suppress the other server
  423. replies, which are pointless without their answers. Thus the "rcdactive"
  424. flag (REMOTE CD command is active). Thu Oct 10 16:38:21 2002
  425. */
  426. if (!(quiet && rcdactive)) /* gross, yuk */
  427. conoc(c);
  428. return(0);
  429. }
  430. #endif /* NOXFER */
  431. int /* Output char to file. */
  432. #ifdef CK_ANSIC
  433. putmfil(char c) /* Just like putfil but to ZMFILE */
  434. #else /* rather than ZOFILE... */
  435. putmfil(c) register char c;
  436. #endif /* CK_ANSIC */
  437. /* putmfil */ {
  438. debug(F000,"putfil","",c);
  439. if (zchout(ZMFILE, (char) (c & fmask)) < 0) {
  440. czseen = 1;
  441. debug(F101,"putfil zchout write error, setting czseen","",1);
  442. return(-1);
  443. }
  444. return(0);
  445. }
  446. int /* Output char to nowhere. */
  447. #ifdef CK_ANSIC
  448. putnowhere(char c)
  449. #else
  450. putnowhere(c) register char c;
  451. #endif /* CK_ANSIC */
  452. /* putnowhere */ {
  453. return(0);
  454. }
  455. int /* Output char to file. */
  456. #ifdef CK_ANSIC
  457. putfil(char c)
  458. #else
  459. putfil(c) register char c;
  460. #endif /* CK_ANSIC */
  461. /* putfil */ {
  462. debug(F000,"putfil","",c);
  463. if (zchout(ZOFILE, (char) (c & fmask)) < 0) {
  464. czseen = 1; /* If write error... */
  465. debug(F101,"putfil zchout write error, setting czseen","",1);
  466. return(-1);
  467. }
  468. return(0);
  469. }
  470. /*
  471. The following function is a wrapper for putfil(). The only reason for its
  472. existence is to be passed as a function pointer to decode(), which treats
  473. putfil() itself specially -- bypassing it and using an internal macro
  474. instead to speed things up. Use zputfil() instead of putfil() in cases where
  475. we do not want this to happen, e.g. when we need to send output to the file
  476. with a mixture of zchout() and zsout()/zsoutl() calls (as is the case with
  477. incoming short-form REMOTE command replies redirected to a file), which would
  478. otherwise result in data written to the file out of order.
  479. */
  480. int
  481. #ifdef CK_ANSIC
  482. zputfil(char c)
  483. #else
  484. zputfil(c) register char c;
  485. #endif /* CK_ANSIC */
  486. /* zputfil */ {
  487. return(putfil(c));
  488. }
  489. #ifndef NOXFER
  490. /* D E C O D E -- Kermit packet decoding procedure */
  491. /*
  492. Call with string to be decoded and an output function.
  493. Returns 0 on success, -1 on failure (e.g. disk full).
  494. This is the "inner loop" when receiving files, and must be coded as
  495. efficiently as possible. Note some potential problems: if a packet
  496. is badly formed, having a prefixed sequence ending prematurely, this
  497. function, as coded, could read past the end of the packet. This has
  498. never happened, thus the additional (time-consuming) tests have not
  499. been added.
  500. */
  501. static CHAR *xdbuf; /* Global version of decode()'s buffer pointer */
  502. /* for use by translation functions. */
  503. /* Function for pushing a character onto decode()'s input stream. */
  504. VOID
  505. #ifdef CK_ANSIC
  506. zdstuff(CHAR c)
  507. #else
  508. zdstuff(c) CHAR c;
  509. #endif /* CK_ANSIC */
  510. /* zdstuff */ {
  511. xdbuf--; /* Back up the pointer. */
  512. *xdbuf = c; /* Stuff the character. */
  513. }
  514. #ifdef CKTUNING
  515. /*
  516. Trimmed-down packet decoder for binary-mode no-parity transfers.
  517. decode() is the full version.
  518. */
  519. int
  520. #ifdef CK_ANSIC
  521. bdecode(CHAR *buf, int (*fn)(char))
  522. #else
  523. bdecode(buf,fn) register CHAR *buf; register int (*fn)();
  524. #endif /* CK_ANSIC */
  525. /* bdecode */ {
  526. register unsigned int a, a7; /* Various copies of current char */
  527. int ccpflg; /* For Ctrl-unprefixing stats */
  528. int t; /* Int version of character */
  529. int len;
  530. long z; /* For CRC calculation */
  531. CHAR c; /* Current character */
  532. if (!binary || parity || fn != putfil) /* JUST IN CASE */
  533. return(decode(buf,fn,1));
  534. debug(F100,"BDECODE","",0);
  535. xdbuf = buf; /* Global copy of source pointer. */
  536. len = rln; /* Number of bytes in data field */
  537. while (len > 0) {
  538. a = *xdbuf++ & 0xff; /* Get next character */
  539. len--;
  540. rpt = 0; /* Initialize repeat count. */
  541. if (a == rptq && rptflg) { /* Got a repeat prefix? */
  542. rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
  543. rptn += rpt;
  544. a = *xdbuf++ & 0xFF; /* and get the prefixed character. */
  545. len -= 2;
  546. }
  547. ccpflg = 0; /* Control prefix flag. */
  548. if (a == ctlq) { /* If control prefix, */
  549. a = *xdbuf++ & 0xFF; /* get its operand */
  550. len--;
  551. a7 = a & 0x7F; /* and its low 7 bits. */
  552. if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
  553. a = ctl(a); /* if in control range. */
  554. a7 = a & 0x7F;
  555. ccpflg = 1; /* Note that we did this */
  556. ccp++; /* Count for stats */
  557. }
  558. } else a7 = a & 0x7f; /* Not control quote */
  559. if (a7 < 32 || a7 == 127) /* A bare control character? */
  560. if (!ccpflg) ccu++; /* Count it */
  561. if (!rpt) rpt = 1;
  562. for (; rpt > 0; rpt--) { /* Output the char RPT times */
  563. #ifdef CALIBRATE
  564. if (calibrate) {
  565. ffc++;
  566. continue;
  567. }
  568. #endif /* CALIBRATE */
  569. #ifdef OS2
  570. if (xflg && !remfile) { /* Write to virtual screen */
  571. char _a;
  572. _a = a & fmask;
  573. t = conoc(_a);
  574. if (t < 1)
  575. t = -1;
  576. } else
  577. #endif /* OS2 */
  578. t = zmchout(a & fmask); /* zmchout is a macro */
  579. if (t < 0) {
  580. debug(F101,"bdecode write error - errno","",errno);
  581. return(-1);
  582. }
  583. ffc++; /* Count the character */
  584. if (docrc && !remfile) { /* Update file CRC */
  585. c = a; /* Force conversion to unsigned char */
  586. z = crc16 ^ (long)c;
  587. crc16 = (crc16 >> 8) ^
  588. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  589. }
  590. }
  591. #ifdef CK_CTRLZ
  592. lastchar = a;
  593. #endif /* CK_CTRLZ */
  594. }
  595. return(0);
  596. }
  597. #endif /* CKTUNING */
  598. #endif /* NOXFER */
  599. /* P N B Y T E -- Output next byte to file or other destination */
  600. static CK_OFF_T offc = 0L;
  601. static int
  602. #ifdef CK_ANSIC
  603. pnbyte(CHAR c, int (*fn)(char))
  604. #else
  605. pnbyte(c,fn) CHAR c; int (*fn)();
  606. #endif /* CK_ANSIC */
  607. /* pnbyte */ {
  608. int rc;
  609. long z;
  610. #ifdef OS2
  611. #ifndef NOXFER
  612. if (xflg && !remfile) { /* Write to virtual screen */
  613. char _a;
  614. _a = c & fmask;
  615. rc = conoc(_a);
  616. if (rc < 1)
  617. return(-1);
  618. } else
  619. #endif /* NOXFER */
  620. #endif /* OS2 */
  621. {
  622. if (fn == putfil) { /* Execute output function */
  623. rc = zmchout(c); /* to-file macro (fast) */
  624. } else if (!fn) {
  625. rc = putchar(c); /* to-screen macro (fast) */
  626. } else {
  627. rc = (*fn)(c); /* function call (not as fast) */
  628. }
  629. if (rc < 0)
  630. return(rc);
  631. }
  632. /*
  633. Both xgnbyte() and xpnbyte() increment ffc (the file byte counter).
  634. During file transfer, only one of these functions is called. However,
  635. the TRANSLATE command is likely to call them both. offc, therefore,
  636. contains the output byte count, necessary for handling the UCS-2 BOM.
  637. NOTE: It might be safe to just test for W_SEND, FTP or not.
  638. */
  639. if ((what & (W_FTP|W_SEND)) != (W_FTP|W_SEND)) {
  640. offc++; /* Count the byte */
  641. ffc++; /* Count the byte */
  642. }
  643. #ifndef NOXFER
  644. if (docrc && !xflg && !remfile) { /* Update file CRC */
  645. z = crc16 ^ (long)c;
  646. crc16 = (crc16 >> 8) ^
  647. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  648. }
  649. #endif /* NOXFER */
  650. return(1);
  651. }
  652. /*
  653. X P N B Y T E -- Translate and put next byte to file.
  654. Only for Unicode. Call with next untranslated byte from incoming
  655. byte stream, which can be in any Transfer Character Set: UCS-2, UTF-8,
  656. Latin-1, Latin-Hebrew, etc. Translates to the file character set and
  657. writes bytes to the output file. Call with character to translate
  658. as an int, plus the transfer character set (to translate from) and the
  659. file character set (to translate to), or -1,0,0 to reset the UCS-2 byte
  660. number (which should be done at the beginning of a file). If the transfer
  661. (source) character-set is UCS-2, bytes MUST arrive in Big-Endian order.
  662. Returns:
  663. -1: On error
  664. 0: Nothing to write (mid-sequence)
  665. >0: Number of bytes written.
  666. */
  667. #ifdef KANJI
  668. static int jstate = 0, jx = 0; /* For outputting JIS-7 */
  669. static char jbuf[16] = { NUL, NUL };
  670. #endif /* KANJI */
  671. int
  672. #ifdef CK_ANSIC
  673. xpnbyte(int a, int tcs, int fcs, int (*fn)(char))
  674. #else
  675. xpnbyte(a,tcs,fcs,fn) int a, tcs, fcs; int (*fn)();
  676. #endif /* CK_ANSIC */
  677. /* xpnbyte */ {
  678. #ifdef UNICODE
  679. extern int ucsbom; /* Byte order */
  680. #endif /* UNICODE */
  681. /* CHAR c; */ /* Unsigned char worker */
  682. static union ck_short uc, eu, sj; /* UCS-2, EUC, and Shift-JIS workers */
  683. USHORT ch; /* ditto... */
  684. USHORT * us = NULL; /* ditto... */
  685. int c7, rc, haveuc = 0; /* Return code and UCS-2 flag */
  686. int utferror = 0; /* UTF-8 error */
  687. static int bn = 0; /* UCS-2 byte number */
  688. int swapping = 0; /* Swapping UCS bytes to output? */
  689. /* swapping must be 0 or 1 */
  690. if (a == -1 && (tcs | fcs) == 0) { /* Reset in case previous run */
  691. bn = 0; /* left bn at 1... */
  692. offc = (CK_OFF_T)0;
  693. debug(F101,"xpnbyte RESET","",bn);
  694. return(0);
  695. }
  696. debug(F001,"xpnbyte a","",a);
  697. #ifdef UNICODE
  698. /*
  699. byteorder = hardware byte order of this machine.
  700. ucsorder = override by SET FILE UCS BYTE-ORDER command.
  701. fileorder = byte order of UCS-2 input file detected from BOM.
  702. swapping applies only when output charset is UCS-2.
  703. */
  704. if (ucsorder != 1 && ucsorder != 0) /* Also just in case... */
  705. ucsorder = byteorder;
  706. if ((byteorder && !ucsorder) || (!byteorder && fileorder))
  707. swapping = 1; /* Swapping bytes to output */
  708. #ifdef COMMENT
  709. debug(F101,"xpnbyte ucsorder","",ucsorder);
  710. debug(F101,"xpnbyte swapping","",swapping);
  711. #endif /* COMMENT */
  712. if (tcs == TC_UTF8) { /* 'a' is from a UTF-8 stream */
  713. ch = a;
  714. if (fcs == TC_UTF8) /* Output is UTF-8 too */
  715. return(pnbyte(ch,fn)); /* so just copy. */
  716. rc = utf8_to_ucs2(ch,&us); /* Otherwise convert to UCS-2 */
  717. if (rc == 0) { /* Done with this sequence */
  718. uc.x_short = *us; /* We have a Unicode */
  719. haveuc = 1;
  720. } else if (rc < 0) { /* Error */
  721. debug(F101,"xpnbyte UTF-8 conversion error","",rc);
  722. haveuc = 1; /* Replace by U+FFFD */
  723. uc.x_short = *us;
  724. utferror = 1;
  725. } else /* Sequence incomplete */
  726. return(0);
  727. } else if (tcs == TC_UCS2) { /* 'a' is UCS-2 */
  728. /* Here we have incoming UCS-2 in guaranteed Big Endian order */
  729. /* so we must exchange bytes if local machine is Little Endian. */
  730. switch (bn) { /* Which byte? */
  731. case 0: /* High... */
  732. uc.x_char[byteorder] = (unsigned)a & 0xff;
  733. bn++;
  734. return(0); /* Wait for next */
  735. case 1: /* Low... */
  736. uc.x_char[1-byteorder] = (unsigned)a & 0xff;
  737. bn = 0; /* Done with sequence */
  738. haveuc = 1; /* Have a Unicode */
  739. }
  740. } else
  741. #endif /* UNICODE */
  742. #ifdef KANJI /* Whether UNICODE is defined or not */
  743. if (tcs == TC_JEUC) { /* Incoming Japanese EUC */
  744. int bad = 0;
  745. static int kanji = 0; /* Flags set in case 0 for case 1 */
  746. static int kana = 0;
  747. switch (bn) { /* Byte number */
  748. case 0: /* Byte 0 */
  749. eu.x_short = 0;
  750. if ((a & 0x80) == 0) {
  751. sj.x_short = (unsigned)a & 0xff; /* Single byte */
  752. kanji = kana = 0;
  753. } else { /* Double byte */
  754. c7 = a & 0x7f;
  755. if (c7 > 0x20 && c7 < 0x7f) { /* Kanji */
  756. eu.x_char[byteorder] = (CHAR) a; /* Store first byte */
  757. bn++; /* Set up for second byte */
  758. kanji = 1;
  759. kana = 0;
  760. return(0);
  761. } else if (a == 0x8e) { /* SS2 -- Katakana prefix */
  762. eu.x_char[byteorder] = (CHAR) a; /* Save it */
  763. bn++;
  764. kana = 1;
  765. kanji = 0;
  766. return(0);
  767. } else {
  768. bad++;
  769. }
  770. }
  771. break;
  772. case 1: /* Byte 1 */
  773. bn = 0;
  774. if (kanji) {
  775. eu.x_char[1-byteorder] = (CHAR) a;
  776. sj.x_short = eu_to_sj(eu.x_short);
  777. break;
  778. } else if (kana) {
  779. sj.x_short = (CHAR) (a | 0x80);
  780. break;
  781. } else { /* (shouldn't happen) */
  782. bad++;
  783. }
  784. }
  785. /* Come here with one Shift-JIS character */
  786. #ifdef UNICODE
  787. if (bad) {
  788. uc.x_short = 0xfffd;
  789. } else {
  790. uc.x_short = sj_to_un(sj.x_short); /* Convert to Unicode */
  791. }
  792. haveuc = 1;
  793. #endif /* UNICODE */
  794. } else
  795. #endif /* KANJI */
  796. #ifdef UNICODE
  797. uc.x_short = (unsigned)a & 0xff; /* Latin-1 or whatever... */
  798. /* Come here with uc = the character to be translated. */
  799. /* If (haveuc) it's UCS-2 in native order, otherwise it's a byte. */
  800. debug(F101,"xpnbyte haveuc","",haveuc);
  801. if (haveuc) { /* If we have a Unicode... */
  802. debug(F001,"xpnbyte uc.x_short","[A]",uc.x_short);
  803. debug(F101,"xpnbyte feol","",feol);
  804. if (what & W_XFER) { /* If transferring a file */
  805. if (feol && uc.x_short == CR) { /* handle eol conversion. */
  806. return(0);
  807. } else if (feol && uc.x_short == LF) {
  808. uc.x_short = feol;
  809. }
  810. }
  811. debug(F001,"xpnbyte uc.x_short","[B]",uc.x_short);
  812. if (fcs == FC_UCS2) { /* And FCS is UCS-2 */
  813. /* Write out the bytes in the appropriate byte order */
  814. int count = 0;
  815. #ifndef IKSDONLY
  816. #ifdef OS2
  817. extern int k95stdout,wherex[],wherey[];
  818. extern unsigned char colorcmd;
  819. union {
  820. USHORT ucs2;
  821. UCHAR bytes[2];
  822. } output;
  823. #endif /* OS2 */
  824. #endif /* IKSDONLY */
  825. if (!offc && ucsbom) { /* Beginning of file? */
  826. #ifndef IKSDONLY
  827. #ifdef OS2
  828. if (fn == NULL && !k95stdout && !inserver) {
  829. offc++;
  830. #ifdef COMMENT
  831. /* Don't print the BOM to the display */
  832. output.bytes[0] = (!ucsorder ? 0xff : 0xfe);
  833. output.bytes[1] = (!ucsorder ? 0xfe : 0xff);
  834. VscrnWrtUCS2StrAtt(VCMD,
  835. &output.ucs2,
  836. 1,
  837. wherey[VCMD],
  838. wherex[VCMD],
  839. &colorcmd
  840. );
  841. #endif /* COMMENT */
  842. } else
  843. #endif /* OS2 */
  844. #endif /* IKSDONLY */
  845. {
  846. if ((rc = pnbyte((ucsorder ? 0xff : 0xfe),fn)) < 0)
  847. return(rc); /* BOM */
  848. if ((rc = pnbyte((ucsorder ? 0xfe : 0xff),fn)) < 0)
  849. return(rc);
  850. }
  851. count += 2;
  852. }
  853. if (utferror) {
  854. #ifndef IKSDONLY
  855. #ifdef OS2
  856. if (fn == NULL && !k95stdout && !inserver) {
  857. offc++;
  858. output.bytes[0] = (!ucsorder ? 0xfd : 0xff);
  859. output.bytes[1] = (!ucsorder ? 0xff : 0xfd);
  860. VscrnWrtUCS2StrAtt(VCMD,
  861. &output.ucs2,
  862. 1,
  863. wherey[VCMD],
  864. wherex[VCMD],
  865. &colorcmd
  866. );
  867. } else
  868. #endif /* OS2 */
  869. #endif /* IKSDONLY */
  870. {
  871. if ((rc = pnbyte((ucsorder ? 0xfd : 0xff),fn)) < 0)
  872. return(rc);
  873. if ((rc = pnbyte((ucsorder ? 0xff : 0xfd),fn)) < 0)
  874. return(rc);
  875. }
  876. count += 2;
  877. }
  878. #ifndef IKSDONLY
  879. #ifdef OS2
  880. if (fn == NULL && !k95stdout && !inserver) {
  881. offc++;
  882. output.bytes[0] = uc.x_char[swapping];
  883. output.bytes[1] = uc.x_char[1-swapping];
  884. VscrnWrtUCS2StrAtt(VCMD,
  885. &output.ucs2,
  886. 1,
  887. wherey[VCMD],
  888. wherex[VCMD],
  889. &colorcmd
  890. );
  891. } else
  892. #endif /* OS2 */
  893. #endif /* IKSDONLY */
  894. {
  895. if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  896. return(rc);
  897. if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  898. return(rc);
  899. }
  900. count += 2;
  901. return(count);
  902. } else if (fcs == FC_UTF8) { /* Convert to UTF-8 */
  903. CHAR * buf = NULL;
  904. int i, count;
  905. if (utferror) {
  906. if ((rc = pnbyte((ucsorder ? 0xbd : 0xff),fn)) < 0)
  907. return(rc);
  908. if ((rc = pnbyte((ucsorder ? 0xff : 0xbd),fn)) < 0)
  909. return(rc);
  910. }
  911. if ((count = ucs2_to_utf8(uc.x_short,&buf)) < 1)
  912. return(-1);
  913. debug(F011,"xpnbyte buf",buf,count);
  914. for (i = 0; i < count; i++)
  915. if ((rc = pnbyte(buf[i],fn)) < 0)
  916. return(rc);
  917. if (utferror)
  918. count += 2;
  919. return(count);
  920. } else { /* Translate UCS-2 to byte */
  921. if (uc.x_short == 0x2028 || uc.x_short == 0x2029) {
  922. if (utferror)
  923. pnbyte(UNK,fn);
  924. if (feol)
  925. return(pnbyte((CHAR)feol,fn));
  926. if ((rc = pnbyte((CHAR)CR,fn)) < 0)
  927. return(rc);
  928. if ((rc = pnbyte((CHAR)LF,fn)) < 0)
  929. return(rc);
  930. else
  931. return(utferror ? 3 : 2);
  932. } else if (xuf) { /* UCS-to-FCS function */
  933. int x = 0;
  934. if (utferror)
  935. pnbyte(UNK,fn);
  936. if ((rc = (*xuf)(uc.x_short)) < 0) /* These can fail... */
  937. ch = UNK;
  938. else
  939. ch = (unsigned)((unsigned)rc & 0xffff);
  940. x = pnbyte(ch,fn);
  941. if (x < 0)
  942. return(x);
  943. else if (utferror)
  944. x++;
  945. return(x);
  946. #ifdef KANJI
  947. /* Also see the non-Unicode Kanji section further down in this function. */
  948. } else if (fcsinfo[fcs].alphabet == AL_JAPAN) {
  949. /* Translate UCS-2 to Japanese set */
  950. debug(F001,"xpnbyte uc","",uc.x_short);
  951. sj.x_short = un_to_sj(uc.x_short); /* First to Shift-JIS */
  952. debug(F001,"xpnbyte sj","",sj.x_short);
  953. switch (fcs) { /* File character set */
  954. case FC_SHJIS: /* Shift-JIS -- just output it */
  955. if (sj.x_char[byteorder]) /* But not high byte if zero */
  956. if ((rc = pnbyte((CHAR)sj.x_char[byteorder],fn)) < 0)
  957. return(rc);
  958. if ((rc = pnbyte((CHAR)sj.x_char[1-byteorder],fn)) < 0)
  959. return(rc);
  960. return(2);
  961. case FC_JEUC: /* EUC-JP */
  962. eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  963. debug(F001,"xpnbyte eu","",eu.x_short);
  964. if (eu.x_short == 0xffff) { /* Bad */
  965. if ((rc = pnbyte(UNK,fn)) < 0)
  966. return(rc);
  967. return(1);
  968. } else { /* Good */
  969. int count = 0; /* Write high byte if not zero */
  970. if (eu.x_char[byteorder]) {
  971. if ((rc=pnbyte((CHAR)eu.x_char[byteorder],fn)) < 0)
  972. return(rc);
  973. count++;
  974. }
  975. /* Always write low byte */
  976. if ((rc = pnbyte((CHAR)eu.x_char[1-byteorder],fn)) < 0)
  977. return(rc);
  978. count++;
  979. return(count);
  980. }
  981. break;
  982. case FC_JIS7: /* JIS-7 */
  983. case FC_JDEC: /* DEC Kanji */
  984. eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  985. if (eu.x_short == 0xffff) { /* Bad */
  986. debug(F001,"xpnbyte bad eu","",eu.x_short);
  987. if ((rc = pnbyte(UNK,fn)) < 0)
  988. return(rc);
  989. return(1);
  990. } else { /* Good */
  991. int i;
  992. /* Use another name - 'a' hides parameter */
  993. /* It's OK as is but causes compiler warnings */
  994. char a = eu.x_char[1-byteorder]; /* Low byte */
  995. debug(F001,"xpnbyte eu","",eu.x_short);
  996. if (eu.x_char[byteorder] == 0) { /* Roman */
  997. switch (jstate) {
  998. case 1: /* Current state is Katakana */
  999. jbuf[0] = 0x0f; /* SI */
  1000. jbuf[1] = a;
  1001. jx = 2;
  1002. break;
  1003. case 2: /* Current state is Kanji */
  1004. jbuf[0] = 0x1b; /* ESC */
  1005. jbuf[1] = 0x28; /* ( */
  1006. jbuf[2] = 0x4a; /* J */
  1007. jbuf[3] = a;
  1008. jx = 4;
  1009. break;
  1010. default: /* Current state is Roman */
  1011. jbuf[0] = a;
  1012. jx = 1;
  1013. break;
  1014. }
  1015. jstate = 0; /* New state is Roman */
  1016. } else if (eu.x_char[byteorder] == 0x8e) { /* Kana */
  1017. jx = 0;
  1018. switch (jstate) {
  1019. case 2: /* from Kanji */
  1020. jbuf[jx++] = 0x1b; /* ESC */
  1021. jbuf[jx++] = 0x28; /* ( */
  1022. jbuf[jx++] = 0x4a; /* J */
  1023. case 0: /* from Roman */
  1024. jbuf[jx++] = 0x0e; /* SO */
  1025. default: /* State is already Kana*/
  1026. jbuf[jx++] = (a & 0x7f); /* and the char */
  1027. break;
  1028. }
  1029. jstate = 1; /* New state is Katakana */
  1030. } else { /* Kanji */
  1031. jx = 0;
  1032. switch (jstate) {
  1033. case 1: /* Current state is Katakana */
  1034. jbuf[jx++] = 0x0f; /* SI */
  1035. case 0: /* Current state is Roman */
  1036. jbuf[jx++] = 0x1b; /* ESC */
  1037. jbuf[jx++] = 0x24; /* $ */
  1038. jbuf[jx++] = 0x42; /* B */
  1039. default: /* Current state is already Kanji */
  1040. jbuf[jx++] = eu.x_char[byteorder] & 0x7f;
  1041. jbuf[jx++] = eu.x_char[1-byteorder] & 0x7f;
  1042. break;
  1043. }
  1044. jstate = 2; /* Set state to Kanji */
  1045. }
  1046. for (i = 0; i < jx; i++) /* Output the result */
  1047. if ((rc = pnbyte(jbuf[i],fn)) < 0)
  1048. return(rc);
  1049. return(jx); /* Return its length */
  1050. }
  1051. }
  1052. #endif /* KANJI */
  1053. } else { /* No translation function */
  1054. int count = 0;
  1055. if (utferror) {
  1056. if ((rc = pnbyte((ucsorder ? 0xfd : 0xff),fn)) < 0)
  1057. return(rc);
  1058. if ((rc = pnbyte((ucsorder ? 0xff : 0xfd),fn)) < 0)
  1059. return(rc);
  1060. count += 2;
  1061. }
  1062. if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  1063. return(rc);
  1064. if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  1065. return(rc);
  1066. count += 2;
  1067. return(count);
  1068. }
  1069. }
  1070. } else { /* Byte to Unicode */
  1071. if (xtu) { /* TCS-to-UCS function */
  1072. if (((tcsinfo[tcs].size > 128) && (uc.x_short & 0x80)) ||
  1073. tcsinfo[tcs].size <= 128)
  1074. uc.x_short = (*xtu)(uc.x_short);
  1075. }
  1076. if (fcs == FC_UCS2) { /* And FCS is UCS-2 */
  1077. /* Write out the bytes in the appropriate byte order */
  1078. if (!offc && ucsbom) { /* Beginning of file? */
  1079. if ((rc = pnbyte((ucsorder ? 0xff : 0xfe),fn)) < 0) /* BOM */
  1080. return(rc);
  1081. if ((rc = pnbyte((ucsorder ? 0xfe : 0xff),fn)) < 0)
  1082. return(rc);
  1083. }
  1084. if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  1085. return(rc);
  1086. if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  1087. return(rc);
  1088. return(2);
  1089. } else if (fcs == FC_UTF8) { /* Convert to UTF-8 */
  1090. CHAR * buf = NULL;
  1091. int i, count;
  1092. if ((count = ucs2_to_utf8(uc.x_short,&buf)) < 1)
  1093. return(-1);
  1094. for (i = 0; i < count; i++)
  1095. if ((rc = pnbyte(buf[i],fn)) < 0)
  1096. return(rc);
  1097. return(count);
  1098. } else {
  1099. debug(F100,"xpnbyte impossible combination","",0);
  1100. return(-1);
  1101. }
  1102. }
  1103. #else
  1104. #ifdef KANJI
  1105. /*
  1106. This almost, but not quite, duplicates the Kanji section above.
  1107. There is no doubt a way to combine the sections more elegantly,
  1108. but probably only at the expense of additional execution overhead.
  1109. As matters stand, be careful to reflect any changes in this section
  1110. to the other Kanji section above.
  1111. */
  1112. if (tcs == TC_JEUC) { /* Incoming Japanese EUC */
  1113. int count = 0;
  1114. switch (fcs) { /* File character set */
  1115. case FC_SHJIS: /* Shift-JIS -- just output it */
  1116. if (sj.x_char[byteorder]) /* But not high byte if zero */
  1117. if ((rc = pnbyte((CHAR)sj.x_char[byteorder],fn)) < 0)
  1118. return(rc);
  1119. count++;
  1120. if ((rc = pnbyte((CHAR)sj.x_char[1-byteorder],fn)) < 0)
  1121. return(rc);
  1122. count++;
  1123. return(count);
  1124. case FC_JEUC: /* EUC-JP */
  1125. eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  1126. debug(F001,"xpnbyte FC_JEUC eu","",eu.x_short);
  1127. if (eu.x_short == 0xffff) { /* Bad */
  1128. if ((rc = pnbyte(UNK,fn)) < 0)
  1129. return(rc);
  1130. return(1);
  1131. } else { /* Good */
  1132. int count = 0; /* Write high byte if not zero */
  1133. if (eu.x_char[byteorder]) {
  1134. if ((rc = pnbyte((CHAR)eu.x_char[byteorder],fn)) < 0)
  1135. return(rc);
  1136. count++;
  1137. }
  1138. /* Always write low byte */
  1139. if ((rc = pnbyte((CHAR)eu.x_char[1-byteorder],fn)) < 0)
  1140. return(rc);
  1141. count++;
  1142. return(count);
  1143. }
  1144. break;
  1145. case FC_JIS7: /* JIS-7 */
  1146. case FC_JDEC: /* DEC Kanji */
  1147. eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  1148. if (eu.x_short == 0xffff) { /* Bad */
  1149. debug(F001,"xpnbyte FC_JIS7 bad eu","",eu.x_short);
  1150. if ((rc = pnbyte(UNK,fn)) < 0)
  1151. return(rc);
  1152. return(1);
  1153. } else { /* Good */
  1154. int i;
  1155. char a = eu.x_char[1-byteorder]; /* Low byte */
  1156. debug(F001,"xpnbyte FC_JIS7 eu","",eu.x_short);
  1157. if (eu.x_char[byteorder] == 0) { /* Roman */
  1158. switch (jstate) {
  1159. case 1: /* Current state is Katakana */
  1160. jbuf[0] = 0x0f; /* SI */
  1161. jbuf[1] = a;
  1162. jx = 2;
  1163. break;
  1164. case 2: /* Current state is Kanji */
  1165. jbuf[0] = 0x1b; /* ESC */
  1166. jbuf[1] = 0x28; /* ( */
  1167. jbuf[2] = 0x4a; /* J */
  1168. jbuf[3] = a;
  1169. jx = 4;
  1170. break;
  1171. default: /* Current state is Roman */
  1172. jbuf[0] = a;
  1173. jx = 1;
  1174. break;
  1175. }
  1176. jstate = 0; /* New state is Roman */
  1177. } else if (eu.x_char[byteorder] == 0x8e) { /* Kana */
  1178. jx = 0;
  1179. switch (jstate) {
  1180. case 2: /* from Kanji */
  1181. jbuf[jx++] = 0x1b; /* ESC */
  1182. jbuf[jx++] = 0x28; /* ( */
  1183. jbuf[jx++] = 0x4a; /* J */
  1184. case 0: /* from Roman */
  1185. jbuf[jx++] = 0x0e; /* SO */
  1186. default: /* State is already Kana*/
  1187. jbuf[jx++] = (a & 0x7f); /* and the char */
  1188. break;
  1189. }
  1190. jstate = 1; /* New state is Katakana */
  1191. } else { /* Kanji */
  1192. jx = 0;
  1193. switch (jstate) {
  1194. case 1: /* Current state is Katakana */
  1195. jbuf[jx++] = 0x0f; /* SI */
  1196. case 0: /* Current state is Roman */
  1197. jbuf[jx++] = 0x1b; /* ESC */
  1198. jbuf[jx++] = 0x24; /* $ */
  1199. jbuf[jx++] = 0x42; /* B */
  1200. default: /* Current state is already Kanji */
  1201. jbuf[jx++] = eu.x_char[byteorder] & 0x7f;
  1202. jbuf[jx++] = eu.x_char[1-byteorder] & 0x7f;
  1203. break;
  1204. }
  1205. jstate = 2; /* Set state to Kanji */
  1206. }
  1207. for (i = 0; i < jx; i++) /* Output the result */
  1208. if ((rc = pnbyte(jbuf[i],fn)) < 0)
  1209. return(rc);
  1210. return(jx); /* Return its length */
  1211. }
  1212. default:
  1213. if (sj.x_short < 0x80)
  1214. return(sj.x_short);
  1215. else
  1216. return('?');
  1217. }
  1218. }
  1219. #endif /* KANJI */
  1220. #endif /* UNICODE */
  1221. debug(F100,"xpnbyte BAD FALLTHRU","",0);
  1222. return(-1);
  1223. }
  1224. #ifndef NOXFER
  1225. /* D E C O D E -- Kermit Data-packet decoder */
  1226. int
  1227. #ifdef CK_ANSIC
  1228. decode(CHAR *buf, int (*fn)(char), int xlate)
  1229. #else
  1230. decode(buf,fn,xlate) register CHAR *buf; register int (*fn)(); int xlate;
  1231. #endif /* CK_ANSIC */
  1232. /* decode */ {
  1233. register unsigned int a, a7, a8, b8; /* Various copies of current char */
  1234. int t; /* Int version of character */
  1235. int ssflg; /* Character was single-shifted */
  1236. int ccpflg; /* For Ctrl-unprefixing stats */
  1237. int len;
  1238. long z;
  1239. CHAR c;
  1240. /*
  1241. Catch the case in which we are asked to decode into a file that is not open,
  1242. for example, if the user interrupted the transfer, but the other Kermit
  1243. keeps sending.
  1244. */
  1245. if ((cxseen || czseen || discard) && (fn == putfil))
  1246. return(0);
  1247. #ifdef COMMENT
  1248. #ifdef CKTUNING
  1249. if (binary && !parity)
  1250. return(bdecode(buf,fn));
  1251. #endif /* CKTUNING */
  1252. #endif /* COMMENT */
  1253. debug(F100,"DECODE","",0);
  1254. xdbuf = buf; /* Make global copy of pointer. */
  1255. rpt = 0; /* Initialize repeat count. */
  1256. len = rln; /* Number of bytes in data field */
  1257. while (len > 0) { /* Loop for each byte */
  1258. a = *xdbuf++ & 0xff; /* Get next character */
  1259. len--;
  1260. if (a == rptq && rptflg) { /* Got a repeat prefix? */
  1261. rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
  1262. rptn += rpt;
  1263. a = *xdbuf++ & 0xFF; /* and get the prefixed character. */
  1264. len -= 2;
  1265. }
  1266. b8 = lsstate ? 0200 : 0; /* 8th-bit value from SHIFT-STATE */
  1267. if (ebqflg && a == ebq) { /* Have 8th-bit prefix? */
  1268. b8 ^= 0200; /* Yes, invert the 8th bit's value, */
  1269. ssflg = 1; /* remember we did this, */
  1270. a = *xdbuf++ & 0xFF; /* and get the prefixed character. */
  1271. len--;
  1272. } else ssflg = 0;
  1273. ccpflg = 0;
  1274. if (a == ctlq) { /* If control prefix, */
  1275. a = *xdbuf++ & 0xFF; /* get its operand */
  1276. len--;
  1277. a7 = a & 0x7F; /* and its low 7 bits. */
  1278. if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
  1279. a = ctl(a); /* if in control range. */
  1280. a7 = a & 0x7F;
  1281. ccpflg = 1; /* Note that we did this */
  1282. ccp++; /* Count for stats */
  1283. }
  1284. } else a7 = a & 0x7f; /* Not control quote */
  1285. if (a7 < 32 || a7 == 127) { /* Control character? */
  1286. if (!ccpflg) ccu++; /* A bare one, count it */
  1287. if (lscapu) { /* If doing locking shifts... */
  1288. if (lsstate) /* If SHIFTED */
  1289. a8 = (a & ~b8) & 0xFF; /* Invert meaning of 8th bit */
  1290. else /* otherwise */
  1291. a8 = a | b8; /* OR in 8th bit */
  1292. /* If we're not in a quoted sequence */
  1293. if (!lsquote && (!lsstate || !ssflg)) {
  1294. if (a8 == DLE) { /* Check for DLE quote */
  1295. lsquote = 1; /* prefixed by single shift! */
  1296. continue;
  1297. } else if (a8 == SO) { /* Check for Shift-Out */
  1298. lsstate = 1; /* SHIFT-STATE = SHIFTED */
  1299. continue;
  1300. } else if (a8 == SI) { /* or Shift-In */
  1301. lsstate = 0; /* SHIFT-STATE = UNSHIFTED */
  1302. continue;
  1303. }
  1304. } else lsquote = 0;
  1305. }
  1306. }
  1307. a |= b8; /* OR in the 8th bit */
  1308. if (rpt == 0) rpt = 1; /* If no repeats, then one */
  1309. #ifndef NOCSETS
  1310. if (!binary) { /* If in text mode, */
  1311. if (tcharset != TC_UCS2) {
  1312. if (feol && a == CR) /* Convert CRLF to newline char */
  1313. continue;
  1314. if (feol && a == LF)
  1315. a = feol;
  1316. }
  1317. if (xlatype == XLA_BYTE) /* Byte-for-byte - do it now */
  1318. if (xlate && rx) a = (*rx)((CHAR) a);
  1319. }
  1320. #endif /* NOCSETS */
  1321. /* (PWP) Decoding speedup via buffered output and a macro... */
  1322. if (fn == putfil) {
  1323. for (; rpt > 0; rpt--) { /* Output the char RPT times */
  1324. #ifdef CALIBRATE
  1325. if (calibrate) {
  1326. ffc++;
  1327. continue;
  1328. }
  1329. #endif /* CALIBRATE */
  1330. /* Note: The Unicode and Kanji sections can probably be combined now; */
  1331. /* the Unicode method (xpnbyte()) covers Kanji too. */
  1332. #ifdef UNICODE
  1333. if (!binary && xlatype == XLA_UNICODE)
  1334. t = xpnbyte((unsigned)((unsigned)a & 0xff),
  1335. tcharset,
  1336. fcharset,
  1337. fn
  1338. );
  1339. else
  1340. #endif /* UNICODE */
  1341. #ifdef KANJI
  1342. if (!binary && tcharset == TC_JEUC &&
  1343. fcharset != FC_JEUC) { /* Translating from J-EUC */
  1344. if (!ffc) xkanjf();
  1345. if (xkanji(a,fn) < 0) /* to something else? */
  1346. return(-1);
  1347. else t = 1;
  1348. } else
  1349. #endif /* KANJI */
  1350. {
  1351. #ifdef OS2
  1352. if (xflg && !remfile) { /* Write to virtual screen */
  1353. char _a;
  1354. _a = a & fmask;
  1355. t = conoc(_a);
  1356. if (t < 1)
  1357. t = -1;
  1358. } else
  1359. #endif /* OS2 */
  1360. t = zmchout(a & fmask); /* zmchout is a macro */
  1361. }
  1362. if (t < 0) {
  1363. debug(F101,"decode write errno","",errno);
  1364. return(-1);
  1365. }
  1366. #ifdef UNICODE
  1367. if (xlatype != XLA_UNICODE || binary) {
  1368. ffc++; /* Count the character */
  1369. if (docrc && !xflg && !remfile) { /* Update file CRC */
  1370. c = a; /* Force conversion to unsigned char */
  1371. z = crc16 ^ (long)c;
  1372. crc16 = (crc16 >> 8) ^
  1373. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1374. }
  1375. }
  1376. #endif /* UNICODE */
  1377. }
  1378. } else { /* Output to something else. */
  1379. a &= fmask; /* Apply file mask */
  1380. for (; rpt > 0; rpt--) { /* Output the char RPT times */
  1381. #ifdef CALIBRATE
  1382. if (calibrate) {
  1383. ffc++;
  1384. continue;
  1385. }
  1386. #endif /* CALIBRATE */
  1387. if ((*fn)((char) a) < 0) return(-1); /* Send to output func. */
  1388. }
  1389. }
  1390. #ifdef CK_CTRLZ
  1391. lastchar = a;
  1392. #endif /* CK_CTRLZ */
  1393. }
  1394. return(0);
  1395. }
  1396. /* G E T P K T -- Fill a packet data field */
  1397. /*
  1398. Gets characters from the current source -- file or memory string.
  1399. Encodes the data into the packet, filling the packet optimally.
  1400. Set first = 1 when calling for the first time on a given input stream
  1401. (string or file).
  1402. Call with:
  1403. bufmax -- current send-packet size
  1404. xlate -- flag: 0 to skip character-set translation, 1 to translate
  1405. Uses global variables:
  1406. t -- current character.
  1407. first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
  1408. next -- next character (not used any more).
  1409. data -- pointer to the packet data buffer.
  1410. size -- number of characters in the data buffer.
  1411. memstr - flag that input is coming from a memory string instead of a file.
  1412. memptr - pointer to string in memory.
  1413. (*sx)() character set translation function
  1414. Returns:
  1415. The size as value of the function, and also sets global "size",
  1416. and fills (and null-terminates) the global data array.
  1417. Returns:
  1418. 0 on EOF.
  1419. -1 on fatal (internal) error.
  1420. -3 on timeout (e.g. when reading data from a pipe).
  1421. Rewritten by Paul W. Placeway (PWP) of Ohio State University, March 1989.
  1422. Incorporates old getchx() and encode() inline to reduce function calls,
  1423. uses buffered input for much-improved efficiency, and clears up some
  1424. confusion with line termination (CRLF vs LF vs CR).
  1425. Rewritten again by Frank da Cruz to incorporate locking shift mechanism,
  1426. May 1991. And again in 1998 for efficiency, etc, with a separate
  1427. bgetpkt() split out for binary-mode no-parity transfers.
  1428. */
  1429. /*
  1430. Note: Separate Kanji support dates from circa 1991 and now (1999) can most
  1431. likely be combined with the the Unicode support: the xgnbyte()/xpnbyte()
  1432. mechanism works for both Unicode and Kanji.
  1433. */
  1434. #ifdef KANJI
  1435. int
  1436. kgetf(
  1437. #ifdef CK_ANSIC
  1438. VOID
  1439. #endif /* CK_ANSIC */
  1440. ) {
  1441. if (funcstr)
  1442. return((*funcptr)());
  1443. else
  1444. return(zminchar());
  1445. }
  1446. int
  1447. kgetm(
  1448. #ifdef CK_ANSIC
  1449. VOID
  1450. #endif /* CK_ANSIC */
  1451. ) {
  1452. int x;
  1453. if ((x = *memptr++)) return(x);
  1454. else return(-1);
  1455. }
  1456. #endif /* KANJI */
  1457. /*
  1458. Lookahead function to decide whether locking shift is worth it. Looks at
  1459. the next four input characters to see if all of their 8th bits match the
  1460. argument. Call with 0 or 0200. Returns 1 on match, 0 if they don't match.
  1461. If we don't happen to have at least 4 more characters waiting in the input
  1462. buffer, returns 1. Note that zinptr points two characters ahead of the
  1463. current character because of repeat-count lookahead.
  1464. */
  1465. int
  1466. lslook(b) unsigned int b; { /* Locking Shift Lookahead */
  1467. int i;
  1468. if (zincnt < 3) /* If not enough chars in buffer, */
  1469. return(1); /* force shift-state switch. */
  1470. b &= 0200; /* Force argument to proper form. */
  1471. for (i = -1; i < 3; i++) /* Look at next 5 characters to */
  1472. if (((*(zinptr+i)) & 0200) != b) /* see if all their 8th bits match. */
  1473. return(0); /* They don't. */
  1474. return(1); /* They do. */
  1475. }
  1476. /* Routine to compute maximum data length for packet to be filled */
  1477. int
  1478. maxdata() { /* Get maximum data length */
  1479. int n, len;
  1480. debug(F101,"maxdata spsiz 1","",spsiz);
  1481. if (spsiz < 0) /* How could this happen? */
  1482. spsiz = DSPSIZ;
  1483. debug(F101,"maxdata spsiz 2","",spsiz);
  1484. n = spsiz - 5; /* Space for Data and Checksum */
  1485. if (n > 92 && n < 96) n = 92; /* "Short" Long packets don't pay */
  1486. if (n > 92 && lpcapu == 0) /* If long packets needed, */
  1487. n = 92; /* make sure they've been negotiated */
  1488. len = n - bctl; /* Space for data */
  1489. if (n > 92) len -= 3; /* Long packet needs header chksum */
  1490. debug(F101,"maxdata len 1","",len);
  1491. if (len < 0) len = 10;
  1492. debug(F101,"maxdata len 2","",len);
  1493. return(len);
  1494. }
  1495. static CHAR leftover[9] = { '\0','\0','\0','\0','\0','\0','\0','\0','\0' };
  1496. static int nleft = 0;
  1497. #ifdef CKTUNING
  1498. /*
  1499. When CKTUNING is defined we use this special trimmed-down version of getpkt
  1500. to speed up binary-mode no-parity transfers. When CKTUNING is not defined,
  1501. or for text-mode or parity transfers, we use the regular getpkt() function.
  1502. Call just like getpkt() but test first for transfer mode and parity. NOTE:
  1503. This routine is only to be called when sending a real file -- not for
  1504. filenames, server responses, etc, because it only reads from the input file.
  1505. See getpkt() for more detailed commentary.
  1506. */
  1507. static int
  1508. bgetpkt(bufmax) int bufmax; {
  1509. register CHAR rt = t, rnext;
  1510. register CHAR *dp, *odp, *p1, *p2;
  1511. register int x = 0, a7;
  1512. CHAR xxrc, xxcq; /* Pieces of prefixed sequence */
  1513. long z; /* A long worker (for CRC) */
  1514. if (!binary || parity || memstr) /* JUST IN CASE caller didn't test */
  1515. return(getpkt(bufmax,!binary));
  1516. if (!data) {
  1517. debug(F100,"SERIOUS ERROR: bgetpkt data == NULL","",0);
  1518. return(-1);
  1519. }
  1520. dp = data; /* Point to packet data buffer */
  1521. size = 0; /* And initialize its size */
  1522. bufmax = maxdata(); /* Get maximum data length */
  1523. #ifdef DEBUG
  1524. if (deblog)
  1525. debug(F101,"bgetpkt bufmax","",bufmax);
  1526. #endif /* DEBUG */
  1527. if (first == 1) { /* If first character of this file.. */
  1528. ffc = (CK_OFF_T)0; /* reset file character counter */
  1529. #ifdef COMMENT
  1530. /* Moved to below */
  1531. first = 0; /* Next character won't be first */
  1532. #endif /* COMMENT */
  1533. *leftover = '\0'; /* Discard any interrupted leftovers */
  1534. nleft = 0;
  1535. /* Get first character of file into rt, watching out for null file */
  1536. #ifdef CALIBRATE
  1537. if (calibrate) {
  1538. #ifdef NORANDOM
  1539. rt = 17;
  1540. #else
  1541. rt = cal_a[rand() & 0xff];
  1542. #endif /* NORANDOM */
  1543. first = 0;
  1544. } else
  1545. #endif /* CALIBRATE */
  1546. if ((x = zminchar()) < 0) { /* EOF or error */
  1547. if (x == -3) { /* Timeout. */
  1548. size = (dp - data);
  1549. debug(F101,"bgetpkt timeout size","",size);
  1550. return((size == 0) ? x : size);
  1551. }
  1552. first = -1;
  1553. size = 0;
  1554. if (x == -2) { /* Error */
  1555. debug(F100,"bgetpkt: input error","",0);
  1556. cxseen = 1; /* Interrupt the file transfer */
  1557. } else {
  1558. debug(F100,"bgetpkt empty file","",0);
  1559. }
  1560. return(0);
  1561. }
  1562. first = 0; /* Next char will not be the first */
  1563. ffc++; /* Count a file character */
  1564. rt = (CHAR) x; /* Convert int to char */
  1565. if (docrc && (what & W_SEND)) { /* Accumulate file crc */
  1566. z = crc16 ^ (long)rt;
  1567. crc16 = (crc16 >> 8) ^
  1568. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1569. }
  1570. rt &= fmask; /* Apply SET FILE BYTESIZE mask */
  1571. } else if (first == -1 && nleft == 0) { /* EOF from last time */
  1572. return(size = 0);
  1573. }
  1574. /*
  1575. Here we handle characters that were encoded for the last packet but
  1576. did not fit, and so were saved in the "leftover" array.
  1577. */
  1578. if (nleft) {
  1579. for (p1 = leftover; nleft > 0; nleft--) /* Copy leftovers */
  1580. *dp++ = *p1++;
  1581. *leftover = '\0'; /* Delete leftovers */
  1582. nleft = 0;
  1583. }
  1584. if (first == -1) /* Handle EOF */
  1585. return(size = (dp - data));
  1586. /* Now fill up the rest of the packet. */
  1587. rpt = 0; /* Initialize character repeat count */
  1588. while (first > -1) { /* Until EOF... */
  1589. #ifdef CALIBRATE
  1590. if (calibrate) { /* We generate our own "file" */
  1591. if (ffc >= calibrate) { /* EOF */
  1592. first = -1;
  1593. ffc--;
  1594. } else { /* Generate next character */
  1595. if (cal_j > CAL_M * ffc)
  1596. cal_j = cal_a[ffc & 0xff];
  1597. x = (unsigned)cal_a[(cal_j & 0xff)];
  1598. if (x == rt) x ^= 2;
  1599. }
  1600. ffc++;
  1601. cal_j += (unsigned int)(ffc + CAL_O);
  1602. } else
  1603. #endif /* CALIBRATE */
  1604. if ((x = zminchar()) < 0) { /* Check for EOF */
  1605. if (x == -3) { /* Timeout. */
  1606. t = rt;
  1607. size = (dp-data);
  1608. debug(F101,"bgetpkt timeout size","",size);
  1609. return((size == 0) ? x : size);
  1610. }
  1611. first = -1; /* Flag eof for next time. */
  1612. if (x == -2) cxseen = 1; /* If error, cancel this file. */
  1613. } else {
  1614. ffc++; /* Count the character */
  1615. if (docrc && (what & W_SEND)) { /* Accumulate file crc */
  1616. z = crc16 ^ (long)((CHAR)x & 0xff);
  1617. crc16 = (crc16 >> 8) ^
  1618. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1619. }
  1620. }
  1621. rnext = (CHAR) (x & fmask); /* Apply file mask */
  1622. /*
  1623. At this point, the character we just read is in rnext,
  1624. and the character we are about to encode into the packet is in rt.
  1625. */
  1626. odp = dp; /* Remember where we started. */
  1627. xxrc = xxcq = NUL; /* Clear these. */
  1628. /*
  1629. Now encode the character according to the options that are in effect:
  1630. ctlp[]: whether this control character needs prefixing.
  1631. rptflg: repeat counts enabled.
  1632. Other options don't apply in this routine.
  1633. */
  1634. if (rptflg && (rt == rnext) && (first == 0)) { /* Got a run... */
  1635. if (++rpt < 94) { /* Below max, just count */
  1636. continue; /* go back and get another */
  1637. } else if (rpt == 94) { /* Reached max, must dump */
  1638. xxrc = (CHAR) tochar(rpt); /* Put the repeat count here */
  1639. rptn += rpt; /* Accumulate it for statistics */
  1640. rpt = 0; /* And reset it */
  1641. }
  1642. } else if (rpt > 0) { /* End of run */
  1643. xxrc = (CHAR)tochar(++rpt); /* The count */
  1644. rptn += rpt; /* For stats */
  1645. rpt = 0; /* Reset repeat count */
  1646. }
  1647. a7 = rt & 0177; /* Get low 7 bits of character */
  1648. if (
  1649. #ifdef CK_SPEED
  1650. ctlp[(unsigned)(rt & 0xff)] /* Lop off any "sign" extension */
  1651. #else
  1652. (a7 < SP) || (a7 == DEL)
  1653. #endif /* CK_SPEED */
  1654. ) { /* Do control prefixing if necessary */
  1655. xxcq = myctlq; /* The prefix */
  1656. ccp++; /* Count it */
  1657. rt = (CHAR) ctl(rt); /* Uncontrollify the character */
  1658. }
  1659. #ifdef CK_SPEED
  1660. else if ((a7 < SP) || (a7 == DEL)) /* Count an unprefixed one */
  1661. ccu++;
  1662. #endif /* CK_SPEED */
  1663. if (a7 == myctlq) /* Always prefix the control prefix */
  1664. xxcq = myctlq;
  1665. if ((rptflg) && (a7 == rptq)) /* If it's the repeat prefix, */
  1666. xxcq = myctlq; /* prefix it if doing repeat counts */
  1667. /* Now construct the prefixed sequence */
  1668. if (xxrc) { /* Repeat count */
  1669. #ifdef COMMENT
  1670. if (xxrc == (CHAR) '"' && !xxcq) { /* 2 in a row & not prefixed */
  1671. *dp++ = rt; /* So just do this */
  1672. } else { /* More than two or prefixed */
  1673. *dp++ = (CHAR) rptq; *dp++ = xxrc; /* Emit repeat sequence */
  1674. }
  1675. #else /* CHECK THIS */
  1676. if (xxrc == (CHAR) '"' && !xxcq) { /* 2 in a row & not prefixed */
  1677. if (dp == data) {
  1678. *dp++ = rt; /* So just do this */
  1679. } else if (*(dp-1) == rt) {
  1680. *dp++ = (CHAR) rptq;
  1681. *dp++ = xxrc; /* Emit repeat sequence */
  1682. } else {
  1683. *dp++ = rt; /* So just do this */
  1684. }
  1685. } else { /* More than two or prefixed */
  1686. *dp++ = (CHAR) rptq;
  1687. *dp++ = xxrc; /* Emit repeat sequence */
  1688. }
  1689. #endif /* COMMENT */
  1690. }
  1691. if (xxcq) { *dp++ = myctlq; } /* Control prefix */
  1692. *dp++ = rt; /* Finally, the character itself */
  1693. rt = rnext; /* Next character is now current. */
  1694. /* Done encoding the character. Now take care of packet buffer overflow. */
  1695. size = dp - data; /* How many bytes we put in buffer. */
  1696. if (size >= bufmax) { /* If too big, save some for next. */
  1697. *dp = '\0'; /* Mark the end. */
  1698. if (size > bufmax) { /* if packet is overfull */
  1699. /* Copy the part that doesn't fit into the leftover buffer, */
  1700. /* taking care not to split a prefixed sequence. */
  1701. int i;
  1702. nleft = dp - odp;
  1703. p1 = leftover;
  1704. p2 = odp;
  1705. for (i = 0; i < nleft; i++)
  1706. *p1++ = *p2++;
  1707. size = odp - data; /* Return truncated packet. */
  1708. *odp = '\0'; /* Mark the new end */
  1709. }
  1710. t = rt; /* Save for next time */
  1711. return(size);
  1712. }
  1713. } /* Otherwise, keep filling. */
  1714. size = dp - data; /* End of file */
  1715. *dp = '\0'; /* Mark the end of the data. */
  1716. return(size); /* Return partially filled last packet. */
  1717. }
  1718. #endif /* CKTUNING */
  1719. VOID
  1720. dofilcrc(c) int c; { /* Accumulate file crc */
  1721. long z;
  1722. z = crc16 ^ (long)c;
  1723. crc16 = (crc16 >> 8) ^
  1724. (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1725. }
  1726. /* For SENDing from an array... */
  1727. int
  1728. agnbyte() { /* Get next byte from array */
  1729. #ifndef NOSPL
  1730. char c;
  1731. static int save = 0; /* For CRLF */
  1732. static char ** ap = NULL; /* Array pointer */
  1733. static char * p = NULL; /* Character pointer */
  1734. static int i = 0, n = 0; /* Array index and limit */
  1735. extern int a_dim[]; /* Array dimension */
  1736. if (!ap) { /* First time thru */
  1737. ap = sndarray; /* Set up array pointers */
  1738. if (!ap || (i = sndxlo) > a_dim[sndxin]) {
  1739. sndarray = NULL;
  1740. ap = NULL;
  1741. return(-1);
  1742. }
  1743. p = ap[i]; /* Point to first element in range */
  1744. n = sndxhi; /* Index of last element in range */
  1745. if (sndxhi > a_dim[sndxin]) /* Adjust if necessary */
  1746. n = a_dim[sndxin];
  1747. }
  1748. if (save) { /* If anything saved */
  1749. c = save; /* unsave it */
  1750. save = 0; /* and return it */
  1751. return(c & 0xff);
  1752. }
  1753. if (i > n) { /* No more elements */
  1754. sndarray = NULL;
  1755. ap = NULL;
  1756. return(-1);
  1757. }
  1758. if (!p) /* Source pointer is NULL */
  1759. c = NUL; /* this means an empty line */
  1760. else /* Source pointer not NULL */
  1761. c = *p++; /* Next char */
  1762. if (!c) { /* Char is empty? */
  1763. if (!binary) { /* Text: end of line. */
  1764. if (feol) { /* Supply NL */
  1765. c = feol;
  1766. } else { /* or CRLF */
  1767. save = LF;
  1768. c = CR;
  1769. }
  1770. p = ap[++i];
  1771. return(c & 0xff);
  1772. }
  1773. while (i++ < n) { /* Binary - get next element */
  1774. p = ap[i];
  1775. if (!p) /* Empty line? */
  1776. continue; /* Ignore it and get another */
  1777. c = *p++; /* Get next char */
  1778. if (!c) /* Emtpy char? */
  1779. continue; /* Ignore it and get another */
  1780. return(c & 0xff); /* Not empty - return it */
  1781. }
  1782. sndarray = NULL;
  1783. ap = NULL;
  1784. return(-1); /* Done */
  1785. }
  1786. return(c & 0xff); /* Char is not empty */
  1787. #else
  1788. sndarray = NULL;
  1789. return(-1);
  1790. #endif /* NOSPL */
  1791. }
  1792. #endif /* NOXFER */
  1793. #ifndef NOCSETS
  1794. static CHAR xlabuf[32] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  1795. static int xlacount = 0;
  1796. static int xlaptr = 0;
  1797. /* static USHORT lastucs2 = 0; */
  1798. /*
  1799. X G N B Y T E -- Get next translated byte from the input file.
  1800. Returns the next byte that is to be put into the packet, already translated.
  1801. This isolates getpkt() from having to know anything about translation,
  1802. single- vs multibyte character sets, one-to-many vs many-to-one, etc, but it
  1803. has rather high overhead, so don't call it unless you know translation is
  1804. needed to or from Unicode, Japanese, or other multibyte character set.
  1805. Call with:
  1806. fcs: File character set (source, file we are reading from)
  1807. tcs: Target character set (use an FC_xxx code, not a TC_xxx code)
  1808. Returns:
  1809. >= 0: A translated byte suitable for writing.
  1810. < 0: Fatal error (such as EOF on input source).
  1811. As of Sat Sep 7 18:37:41 2002:
  1812. When the output character-set is UCS-2, bytes are ALWAYS returned in
  1813. big-endian order (previously they could also be returned in LE order
  1814. under certain conditions, which was just way too confusing).
  1815. */
  1816. int
  1817. #ifdef CK_ANSIC
  1818. xgnbyte(int tcs, int fcs, int (*fn)(void))
  1819. #else /* CK_ANSIC */
  1820. xgnbyte(tcs,fcs,fn) int tcs, fcs, (*fn)();
  1821. #endif /* CK_ANSIC */
  1822. /* xgnbyte */ {
  1823. _PROTOTYP( int (*xx), (USHORT) ) = NULL;
  1824. int haveuc = 0; /* Flag for have Unicode character */
  1825. #ifdef KANJI
  1826. int havesj = 0; /* Have Shift-JIS character */
  1827. int haveeu = 0; /* Have EUC-JP character */
  1828. #endif /* KANJI */
  1829. int rc = -1, x = 0, flag = 0;
  1830. int utferror = 0;
  1831. int eolflag = 0;
  1832. unsigned int xc, thischar;
  1833. static int swapping = 0;
  1834. CHAR rt;
  1835. /* USHORT ch; */
  1836. #ifdef UNICODE
  1837. union ck_short uc;
  1838. #endif /* UNICODE */
  1839. #ifdef KANJI
  1840. union ck_short sj, eu; /* Shift-JIS character */
  1841. #endif /* KANJI */
  1842. #ifdef KANJI
  1843. sj.x_short = 0;
  1844. #endif /* KANJI */
  1845. #ifdef DEBUG
  1846. if (deblog && !ffc) {
  1847. debug(F101,"xgnbyte initial swap","",swapping);
  1848. }
  1849. #endif /* DEBUG */
  1850. if (xlacount-- > 0) { /* We already have some */
  1851. x = xlabuf[xlaptr++];
  1852. debug(F001,"xgnbyte from buf","",x);
  1853. return(x);
  1854. }
  1855. if (xlatype != XLA_NONE) { /* Not not translating... */
  1856. haveuc = 0;
  1857. #ifdef UNICODE
  1858. if (fcs == FC_UCS2) { /* UCS-2: Read two bytes */
  1859. if (!ffc) /* Beginning of file? */
  1860. swapping = 0; /* Reset byte-swapping flag */
  1861. uc.x_short = 0;
  1862. bomskip:
  1863. x = fn ? (*fn)() : zminchar(); /* Get first byte */
  1864. debug(F001,"zminchar swapping","",swapping);
  1865. debug(F001,"zminchar C0","",x);
  1866. flag = 1; /* Remember we called zminchar() */
  1867. if (x > -1) { /* Didn't fail */
  1868. ffc++; /* Count a file byte */
  1869. uc.x_char[swapping] = x & 0xff;
  1870. #ifndef NOXFER
  1871. if (docrc && (what & W_SEND))
  1872. dofilcrc(x);
  1873. #endif /* NOXFER */
  1874. x = fn ? (*fn)() : zminchar(); /* Get second byte */
  1875. if (x > -1) { /* If didn't fail */
  1876. debug(F001,"zminchar C1","",x);
  1877. ffc++; /* count another file byte */
  1878. uc.x_char[1-swapping] = x & 0xff;
  1879. haveuc = 1; /* And remember we have Unicode */
  1880. #ifndef NOXFER
  1881. if (docrc && (what & W_SEND))
  1882. dofilcrc(x);
  1883. #endif /* NOXFER */
  1884. if (ffc == (CK_OFF_T)2) { /* Second char of file */
  1885. debug(F001,"xgnbyte 1st UCS2","",uc.x_short);
  1886. debug(F111,"xgnbyte fileorder","A",fileorder);
  1887. if (fileorder < 0) /* Byte order of this file */
  1888. fileorder = ucsorder; /* Default is ucsorder */
  1889. if (fileorder > 1)
  1890. fileorder = 1;
  1891. debug(F111,"xgnbyte fileorder","B",fileorder);
  1892. if (uc.x_short == (USHORT)0xfeff) {
  1893. swapping = 0;
  1894. debug(F101,
  1895. "xgnbyte UCS2 goodbom swap","",swapping);
  1896. fileorder = byteorder; /* Note: NOT 0 */
  1897. goto bomskip;
  1898. } else if (uc.x_short == (USHORT)0xfffe) {
  1899. swapping = 1;
  1900. debug(F101,
  1901. "xgnbyte UCS2 badbom swap","",swapping);
  1902. fileorder = (1 - byteorder); /* Note: NOT 1 */
  1903. goto bomskip;
  1904. } else if ((byteorder && !fileorder) || /* No BOM */
  1905. (!byteorder && fileorder > 0)) {
  1906. /* fileorder might have been set by scanfile() */
  1907. CHAR c;
  1908. c = uc.x_char[0];
  1909. uc.x_char[0] = uc.x_char[1];
  1910. uc.x_char[1] = c;
  1911. swapping = 1;
  1912. debug(F111,"xgnbyte UCS2 noBOM swap","A",swapping);
  1913. } else {
  1914. swapping = 0;
  1915. debug(F111,"xgnbyte UCS2 noBOM swap","B",swapping);
  1916. }
  1917. debug(F111,"xgnbyte fileorder","C",fileorder);
  1918. }
  1919. } else
  1920. return(x);
  1921. } else
  1922. return(x);
  1923. debug(F001,"xgnbyte UCS2","",uc.x_short);
  1924. } else if (fcs == FC_UTF8) { /* File is UTF-8 */
  1925. CHAR ch = 0; /* Data types needed for API... */
  1926. USHORT * us = NULL;
  1927. uc.x_short = 0;
  1928. flag = 1; /* We (will) have called zminchar() */
  1929. /* Read source bytes */
  1930. while ((x = fn ? (*fn)() : zminchar()) > -1) {
  1931. ffc++; /* Got a byte - count it */
  1932. #ifndef NOXFER
  1933. if (docrc && (what & W_SEND))
  1934. dofilcrc(x);
  1935. #endif /* NOXFER */
  1936. ch = x;
  1937. rc = utf8_to_ucs2(ch,&us); /* Convert to UCS-2 */
  1938. if (rc == 0) { /* Done */
  1939. uc.x_short = *us;
  1940. haveuc = 1;
  1941. break;
  1942. } else if (rc < 0) { /* Error */
  1943. utferror = 1;
  1944. debug(F101,"xgnbyte UTF-8 input error","",rc);
  1945. haveuc = 1;
  1946. uc.x_short = *us;
  1947. break;
  1948. }
  1949. }
  1950. if (x < 0)
  1951. return(x);
  1952. debug(F001,"xgnbyte UTF8->UCS2","",uc.x_short);
  1953. }
  1954. #endif /* UNICODE */
  1955. #ifdef KANJI
  1956. #ifdef UNICODE
  1957. else
  1958. #endif /* UNICODE */
  1959. if (fcsinfo[fcs].alphabet == AL_JAPAN) { /* Japanese source file */
  1960. int c7, x, y;
  1961. if (fcs == FC_JIS7) { /* If file charset is JIS-7 */
  1962. if (!ffc) /* If first byte of file */
  1963. j7init(); /* Initialize JIS-7 parser */
  1964. x = getj7(); /* Get a JIS-7 byte */
  1965. } else /* Otherwise */
  1966. x = fn ? (*fn)() : zminchar(); /* Just get byte */
  1967. if (x < 0) { /* Propogate EOF or error */
  1968. debug(F100,"XGNBYTE EOF","",0);
  1969. return(x);
  1970. }
  1971. debug(F001,"XGNBYTE x","",x);
  1972. ffc++; /* Count */
  1973. #ifndef NOXFER
  1974. if (docrc && (what & W_SEND)) dofilcrc(x); /* Do CRC */
  1975. #endif /* NOXFER */
  1976. switch (fcs) { /* What next depends on charset */
  1977. case FC_SHJIS: /* Shift-JIS */
  1978. if ((x <= 0x80) || /* Any 7-bit char... */
  1979. (x >= 0xa0 && x <= 0xdf)) { /* or halfwidth Katakana */
  1980. sj.x_short = (USHORT) x; /* we read one byte. */
  1981. } else { /* Anything else */
  1982. if ((y = fn ? (*fn)() : zminchar()) < 0) /* get another */
  1983. return(y);
  1984. #ifndef NOXFER
  1985. if (docrc && (what & W_SEND)) dofilcrc(y);
  1986. #endif /* NOXFER */
  1987. ffc++;
  1988. sj.x_char[byteorder] = (CHAR) x;
  1989. sj.x_char[1-byteorder] = (CHAR) y;
  1990. }
  1991. break;
  1992. case FC_JIS7: /* JIS-7 */
  1993. case FC_JDEC: /* DEC Kanji */
  1994. case FC_JEUC: /* EUC-JP */
  1995. if ((x & 0x80) == 0) { /* Convert to Shift-JIS */
  1996. sj.x_short = (USHORT) x; /* C0 or G0: one byte */
  1997. eu.x_short = (USHORT) x;
  1998. haveeu = 1;
  1999. } else {
  2000. c7 = x & 0x7f;
  2001. if (c7 > 0x20 && c7 < 0x7f) { /* Kanji: two bytes */
  2002. if ((y = (fcs == FC_JEUC) ?
  2003. (fn ? (*fn)() : zminchar()) :
  2004. getj7() /* ^^^ */
  2005. ) < 0)
  2006. return(y);
  2007. ffc++;
  2008. #ifndef NOXFER
  2009. if (docrc && (what & W_SEND)) dofilcrc(y);
  2010. #endif /* NOXFER */
  2011. eu.x_char[byteorder] = (CHAR) x;
  2012. eu.x_char[1-byteorder] = (CHAR) y;
  2013. sj.x_short = eu_to_sj(eu.x_short);
  2014. haveeu = 1;
  2015. } else if (x == 0x8e) { /* SS2 Katakana prefix: 2 bytes */
  2016. if ((y = (fcs == FC_JIS7) ?
  2017. getj7() : /* ^^^ */
  2018. (fn ? (*fn)() : zminchar())
  2019. ) < 0)
  2020. return(y);
  2021. ffc++;
  2022. #ifndef NOXFER
  2023. if (docrc && (what & W_SEND)) dofilcrc(y);
  2024. #endif /* NOXFER */
  2025. sj.x_short = y | 0x80;
  2026. debug(F001,"XGNBYTE KANA SJ","",sj.x_short);
  2027. } else {
  2028. /* Something that translates to U+FFFD */
  2029. sj.x_short = UNKSJIS;
  2030. }
  2031. }
  2032. break;
  2033. }
  2034. havesj = 1; /* Have Shift-JIS */
  2035. #ifdef UNICODE
  2036. uc.x_short = sj_to_un(sj.x_short); /* Translate to UCS-2 */
  2037. haveuc = 1; /* Have Unicode */
  2038. #endif /* UNICODE */
  2039. flag = 1; /* Have a char */
  2040. }
  2041. #endif /* KANJI */
  2042. }
  2043. if (!flag) { /* If no character was read yet... */
  2044. if ((x = (fn ? (*fn)() : zminchar())) > -1) /* read one now */
  2045. ffc++;
  2046. debug(F101,"xgnbyte zminchar 1","",x);
  2047. if (x < 0)
  2048. return(x);
  2049. haveuc = 0;
  2050. }
  2051. #ifdef UNICODE
  2052. if (haveuc) {
  2053. thischar = uc.x_short;
  2054. /* lastucs2 = uc.x_short; */
  2055. } else
  2056. #endif /* UNICODE */
  2057. thischar = x;
  2058. debug(F001,"xgnbyte thischar",haveuc ? "[UNICODE]" : "[other]",thischar);
  2059. #ifdef CK_CTRLZ /* SET EOF CTRLZ */
  2060. if (eofmethod == XYEOF_Z && !binary && thischar == 26) {
  2061. debug(F100,"xgnbyte EOF on Ctrl-Z 1","",0);
  2062. return(-1);
  2063. }
  2064. #endif /* CK_CTRLZ */
  2065. #ifdef UNICODE
  2066. if (!haveuc) /* If not Unicode... */
  2067. #endif /* UNICODE */
  2068. x &= fmask; /* Apply SET FILE BYTESIZE mask */
  2069. switch (xlatype) { /* Translation type... */
  2070. #ifdef UNICODE
  2071. case XLA_UNICODE: { /* Unicode is involved */
  2072. xc = 0;
  2073. /*
  2074. Here we must choose the appropriate translation function. If we are being
  2075. called by getpkt() (i.e. when transferring a file), we are translating from
  2076. Unicode to the Transfer Character Set and therefore must use the function
  2077. pointed to by xut. Otherwise, e.g. during TRANSLATE, CONNECT, TRANSMIT, etc,
  2078. we are translating from Unicode to the File Character Set and so must call
  2079. the function pointed to by xuf. There might be a cleaner way to set this
  2080. up but I don't think so. For example, setxlatype() might have been called
  2081. too soon and so might not have known whether it was a file transfer or a
  2082. local operation.
  2083. */
  2084. /*
  2085. (Many years later...) In testing this code I noticed that TRANSLATE'ing
  2086. Russian text from UTF-8 to ISO Latin/Cyrillic produced all question marks.
  2087. Rereading the previous paragraph it seems to me we are (I am) overloading
  2088. this function with responsibilites, satisfying the needs of file transfer
  2089. (local file charset -> transfer charset for outbound packet) and local file
  2090. conversion. In the case of TRANSLATE, we call (xgnbyte(), xpnbyte()) in a
  2091. loop, expecting the xgnbyte() will feed UCS2 to xpnbyte(). But the
  2092. following code does what xpnbyte() is going to do, returning (in this case)
  2093. an ISO Latin/Cyrillic byte stream, which xpnbyte() believes to be UCS2, and
  2094. comes up with nonsense. Not wanting to rip the whole thing apart and start
  2095. over, I made the following change that should do no harm, upon observing
  2096. that if the input character set is UTF-8 or UCS-2, then when we get here it
  2097. has already been converted to UCS2, so if we are not transferring a file, we
  2098. don't need to do anything else except put the bytes in the right place to be
  2099. returned, which is done further along.
  2100. */
  2101. #ifdef COMMENT
  2102. /* Previous code */
  2103. xx = (what & W_SEND) ? xut : xuf;
  2104. #else
  2105. /* New code 2011-06-03 */
  2106. if (what & W_SEND) {
  2107. xx = xut;
  2108. } else {
  2109. if (fcs == FC_UCS2 || fcs == FC_UTF8)
  2110. xx = NULL;
  2111. else
  2112. xx = xuf;
  2113. }
  2114. #endif /* COMMENT */
  2115. eolflag = 0;
  2116. if (haveuc) { /* File is Unicode */
  2117. /* See Unicode TR13, "Converting to Other Character Sets" */
  2118. if (uc.x_short == 0x2028 || /* Line Separator? */
  2119. uc.x_short == 0x2029 || /* Paragraph Separator */
  2120. (feol && (uc.x_short == (USHORT)feol))
  2121. ) {
  2122. debug(F001,"xgnbyte uc eol","",uc.x_short);
  2123. rc = 0;
  2124. eolflag = 1; /* Don't translate and handle later */
  2125. }
  2126. if (xx && !eolflag) { /* UCS-to-TCS function (UCS->byte) */
  2127. rc = (*xx)(uc.x_short); /* These can fail... */
  2128. debug(F101,"xgnbyte xx rc","",rc);
  2129. if (rc < 0) /* If it can't be translated */
  2130. uc.x_short = UNK; /* Put unknown-character symbol */
  2131. else
  2132. uc.x_short = (unsigned)((unsigned)rc & 0xffff);
  2133. debug(F101,"xgnbyte xx uc","",uc.x_short);
  2134. }
  2135. #ifdef KANJI
  2136. if (tcs == FC_JEUC) { /* Translating to EUC-JP */
  2137. USHORT sj = 0;
  2138. union ck_short eu;
  2139. debug(F001,"xgnbyte UCS->EUC UCS","",uc.x_short);
  2140. if (!havesj) /* If we don't already have it */
  2141. sj = un_to_sj(uc.x_short); /* convert to Shift-JIS */
  2142. eu.x_short = sj_to_eu(sj);
  2143. debug(F001,"xgnbyte UCS->EUC EUC","",eu.x_short);
  2144. xlaptr = 0;
  2145. xlacount = 0;
  2146. if (eolflag) {
  2147. if (what & W_SEND) {
  2148. xlabuf[xlacount++] = LF;
  2149. return(CR);
  2150. } else {
  2151. return(feol);
  2152. }
  2153. }
  2154. if (eu.x_char[byteorder]) { /* Two bytes */
  2155. rc = eu.x_char[byteorder];
  2156. xlabuf[xlacount++] = eu.x_char[1-byteorder];
  2157. debug(F001,"xgnbyte UCS->EUC xlabuf[0]","",xlabuf[0]);
  2158. } else { /* One byte */
  2159. rc = eu.x_char[1-byteorder];
  2160. }
  2161. debug(F101,"xgnbyte UCS->EUC xlacount","",xlacount);
  2162. debug(F001,"xgnbyte UCS->EUC rc","",rc);
  2163. return(rc);
  2164. } else
  2165. #endif /* KANJI */
  2166. if (tcs != FC_UCS2 && tcs != FC_UTF8) {
  2167. if (uc.x_short & 0xff00) { /* Decoding error */
  2168. debug(F001,"xgnbyte decoding error","",uc.x_short);
  2169. return(-2);
  2170. } else
  2171. return((unsigned int)(uc.x_short & 0xff));
  2172. }
  2173. xc = uc.x_short;
  2174. } else { /* File is not Unicode */
  2175. USHORT ch;
  2176. /* Translate from single FCS byte to UCS-2 */
  2177. /*
  2178. This is a bit nonobvious... The blah_u() (Blah-to-Unicode) routines are
  2179. called only with pieces of character sets, in the ISO 2022 sense. So,
  2180. for example, if ch is a Latin-1 character, we call the translation
  2181. routine only if it is in the right half; if it's in the left half, it
  2182. isn't translated, and in fact will give the wrong result if sent to the
  2183. translation function. That's because those functions were designed for
  2184. use with the ISO 2022 G0..G3 sets, not for file transfer. On the other
  2185. hand, if it's a 7-bit character set, we *do* call the translation
  2186. function. (To put it another way, the left half of any 8-bit character
  2187. set is ASCII and therefore doesn't need to be translated but 7-bit sets
  2188. such as ISO 646 German do need translation).
  2189. */
  2190. ch = (unsigned)(thischar & 0xff);
  2191. if (((fcsinfo[fcs].size > 128) && (ch & 0x80)) ||
  2192. fcsinfo[fcs].size <= 128) {
  2193. if (xfu) { /* FCS-to-UCS function */
  2194. ch = (*xfu)(ch);
  2195. }
  2196. }
  2197. xc = ch;
  2198. }
  2199. /* At this point we have a UCS-2 character in native format */
  2200. /* (Big Endian or Little Endian) in xc, which is an unsigned int. */
  2201. debug(F001,"xgnbyte xc","",xc);
  2202. if (tcs == FC_UTF8) { /* Now convert to UTF-8 */
  2203. USHORT c; /* NOTE: this is FC_UTF8 on purpose! */
  2204. CHAR * buf = NULL;
  2205. int i, k = 0, x;
  2206. xlaptr = 0;
  2207. if (utferror) {
  2208. xlabuf[k++] = 0xff;
  2209. xlabuf[k++] = 0xbd;
  2210. }
  2211. if (eolflag) { /* We detected EOL in source file */
  2212. if (what & W_SEND) { /* Convert to CRLF */
  2213. xlabuf[k++] = LF;
  2214. xlacount = k;
  2215. return((unsigned int)CR);
  2216. #ifdef COMMENT
  2217. } else { /* Or to local line-end */
  2218. xlacount = k;
  2219. return((unsigned int)feol);
  2220. #endif /* COMMENT */
  2221. }
  2222. }
  2223. c = xc;
  2224. if ((x = ucs2_to_utf8(c,&buf)) < 1) {
  2225. debug(F101,"xgnbyte ucs2_to_utf8 error","",c);
  2226. return(-2);
  2227. }
  2228. debug(F101,"xgnbyte UTF8 buf[0]","",buf[0]);
  2229. for (i = 1; i < x; i++) {
  2230. xlabuf[k+i-1] = buf[i];
  2231. debug(F111,"xgnbyte UTF8 xlabuf",ckitoa(i-1),buf[i]);
  2232. }
  2233. xlaptr = 0;
  2234. xlacount = x - 1;
  2235. debug(F101,"xgnbyte UTF8 xlacount","",xlacount);
  2236. return((unsigned int)buf[0]);
  2237. } else { /* Or keep it as UCS-2 */
  2238. int k = 0;
  2239. CHAR c;
  2240. xlaptr = 0;
  2241. if (utferror) {
  2242. xlabuf[k++] = 0xff;
  2243. xlabuf[k++] = 0xfd;
  2244. debug(F101,"xgnbyte error","",k);
  2245. }
  2246. if (eolflag) { /* We detected EOL in source file */
  2247. if (what & W_SEND) { /* Convert to CRLF */
  2248. xlabuf[k++] = CR;
  2249. xlabuf[k++] = NUL;
  2250. xlabuf[k++] = LF;
  2251. xlacount = k;
  2252. debug(F101,"xgnbyte send CRLF","",k);
  2253. return(0); /* Return NUL */
  2254. } else { /* Or to local line-end */
  2255. #ifdef COMMENT
  2256. /* This bypasses byte swapping that we might need */
  2257. xlabuf[k++] = (CHAR)feol;
  2258. xlacount = k;
  2259. debug(F101,"xgnbyte send feol","",k);
  2260. return(0); /* Return NUL */
  2261. #else
  2262. xc = (CHAR)feol;
  2263. #endif /* COMMENT */
  2264. }
  2265. }
  2266. /* In which order should we return the bytes? */
  2267. #ifdef COMMENT
  2268. if ( (what & W_SEND) || (what & W_FTP) || (fileorder == 0)) {
  2269. #endif /* COMMENT */
  2270. /* ALWAYS RETURN IN BIG ENDIAN ORDER... 7 Sep 2002 */
  2271. /* xgnbyte() is almost always used to feed xpnbyte() */
  2272. /* which requires bytes in BE order. In cases where */
  2273. /* xgnbyte is used in isolation, the caller can swap */
  2274. /* bytes itself afterwards. */
  2275. xlabuf[k++] = (xc >> 8) & 0xff; /* Big Endian */
  2276. xlabuf[k++] = xc & 0xff;
  2277. debug(F001,"xgnbyte->UCS2BE",
  2278. ckitox((int)xlabuf[0]),xlabuf[1]);
  2279. #ifdef COMMENT
  2280. } else { /* Little Endian */
  2281. xlabuf[k++] = xc & 0xff;
  2282. xlabuf[k++] = (xc >> 8) & 0xff;
  2283. debug(F001,"xgnbyte->UCS2LE",
  2284. ckitox((int)xlabuf[0]),xlabuf[1]);
  2285. }
  2286. #endif /* COMMENT */
  2287. c = xlabuf[0];
  2288. xlaptr = 1;
  2289. xlacount = k-1;
  2290. debug(F101,"xgnbyte c","",c);
  2291. debug(F101,"xgnbyte xlaptr","",xlaptr);
  2292. debug(F011,"xgnbyte xlabuf",xlabuf,xlacount);
  2293. return((unsigned int)c);
  2294. }
  2295. }
  2296. #endif /* UNICODE */
  2297. case XLA_NONE:
  2298. return((fn ? (*fn)() : zminchar()));
  2299. case XLA_BYTE: /* Byte-for-Byte translation */
  2300. rt = x;
  2301. if (sx)
  2302. rt = (*sx)(rt);
  2303. #ifdef UNICODE
  2304. if (utferror) {
  2305. xlaptr = 0;
  2306. xlacount = 1;
  2307. xlabuf[0] = rt;
  2308. return(UNK);
  2309. } else
  2310. #endif /* UNICODE */
  2311. return((unsigned int)rt);
  2312. #ifdef KANJI
  2313. case XLA_JAPAN: /* Come here with Shift-JIS */
  2314. if (tcs == FC_JEUC) { /* It better be... */
  2315. xlaptr = 0;
  2316. xlacount = 0;
  2317. if (!havesj) {
  2318. printf("BAD BAD\n");
  2319. return(-2);
  2320. }
  2321. if (!haveeu) /* We might already have EUC too */
  2322. eu.x_short = sj_to_eu(sj.x_short);
  2323. if (eu.x_char[byteorder]) {
  2324. xlabuf[xlacount++] = eu.x_char[1-byteorder];
  2325. return(eu.x_char[byteorder]);
  2326. } else {
  2327. return(eu.x_char[1-byteorder]);
  2328. }
  2329. break;
  2330. }
  2331. #endif /* KANJI */
  2332. default:
  2333. debug(F101,"xgnbyte bad xlatype","",xlatype);
  2334. return(-2);
  2335. }
  2336. #ifdef COMMENT
  2337. /*
  2338. If there is a return() statement here, some compilers complain
  2339. about "statement not reached". If there is no return() statement,
  2340. other compilers complain that "Non-void function should return a value".
  2341. There is no path through this function that falls through to here.
  2342. */
  2343. debug(F100,"xgnbyte switch failure","",0);
  2344. return(-2);
  2345. #endif /* COMMENT */
  2346. }
  2347. #endif /* NOCSETS */
  2348. #ifndef NOXFER
  2349. /* G E T P K T -- Fill a packet data field from the indicated source. */
  2350. /*
  2351. Parameters:
  2352. bufmax: Maximum length of entire packet.
  2353. xlate: Flag for whether to translate charsets when in text mode.
  2354. Returns: Number of characters written to packet data field, 0 or more,
  2355. Or -1 on failure (internal error),
  2356. or -3 on timeout (e.g. when reading from a pipe).
  2357. This is the full version allowing for parity and text-mode conversions;
  2358. i.e. it works in all cases. Also see bgetpkt(), a special lean/mean/fast
  2359. packet encoder that works only for binary-mode no-parity transfers.
  2360. */
  2361. static int uflag = 0;
  2362. int
  2363. getpkt(bufmax,xlate) int bufmax, xlate; { /* Fill one packet buffer */
  2364. register CHAR rt = t, rnext = NUL; /* Register shadows of the globals */
  2365. register CHAR *dp, *odp, *odp2, *p1, *p2; /* pointers... */
  2366. register int x; /* Loop index. */
  2367. register int a7; /* Low 7 bits of character */
  2368. CHAR xxls, xxdl, xxrc, xxss, xxcq; /* Pieces of prefixed sequence */
  2369. if (binary) xlate = 0; /* We don't translate if binary */
  2370. if (!data) {
  2371. debug(F100,"SERIOUS ERROR: getpkt data == NULL","",0);
  2372. return(-1);
  2373. }
  2374. dp = data; /* Point to packet data buffer */
  2375. size = 0; /* And initialize its size */
  2376. /*
  2377. Assume bufmax is the receiver's total receive-packet buffer length.
  2378. Our whole packet has to fit into it, so we adjust the data field length.
  2379. We also decide optimally whether it is better to use a short-format or
  2380. long-format packet when we're near the borderline.
  2381. */
  2382. bufmax = maxdata(); /* Get maximum data length */
  2383. if (first == 1) { /* If first character of this file.. */
  2384. #ifdef UNICODE
  2385. /* Special end-of-line handling for Unicode */
  2386. if (tcharset == TC_UCS2 || tcharset == TC_UTF8)
  2387. uflag = 1;
  2388. #endif /* UNICODE */
  2389. debug(F101,"getpkt first uflag","",uflag);
  2390. debug(F101,"getpkt first rt","",rt);
  2391. if (!memstr && !funcstr) /* and real file... */
  2392. ffc = (CK_OFF_T)0; /* reset file character counter */
  2393. #ifdef COMMENT
  2394. /* Moved to below... */
  2395. first = 0; /* Next character won't be first */
  2396. #endif /* COMMENT */
  2397. *leftover = '\0'; /* Discard any interrupted leftovers */
  2398. nleft = 0;
  2399. #ifndef NOCSETS
  2400. setxlatype(tcharset,fcharset); /* Set up charset translations */
  2401. #endif /* NOCSETS */
  2402. /* Get first character of file into rt, watching out for null file */
  2403. #ifdef CALIBRATE
  2404. if (calibrate && !memstr) {
  2405. #ifdef NORANDOM
  2406. x = rt = 53;
  2407. #else
  2408. x = rt = cal_a[rand() & 0xff];
  2409. #endif /* NORANDOM */
  2410. first = 0;
  2411. ffc++;
  2412. } else
  2413. #endif /* CALIBRATE */
  2414. #ifdef KANJI
  2415. if (xlate && tcharset == TC_JEUC) { /* Kanji text */
  2416. x = zkanjf();
  2417. if ((x = zkanji(memstr ? kgetm : kgetf)) < 0) {
  2418. first = -1;
  2419. size = 0;
  2420. if (x == -2) {
  2421. debug(F100,"getpkt zkanji: input error","",0);
  2422. cxseen = 1;
  2423. } else debug(F100,"getpkt zkanji: empty string/file","",0);
  2424. return(0);
  2425. }
  2426. rt = x;
  2427. first = 0;
  2428. if (!memstr) {
  2429. ffc++;
  2430. if (docrc && (what & W_SEND)) /* Accumulate file crc */
  2431. dofilcrc((int)rt);
  2432. }
  2433. } else { /* Not Kanji text */
  2434. #endif /* KANJI */
  2435. if (memstr) { /* Reading data from memory string */
  2436. /* This will not be Unicode */
  2437. if ((rt = *memptr++) == '\0') { /* end of string ==> EOF */
  2438. first = -1;
  2439. size = 0;
  2440. debug(F100,"getpkt: empty string","",0);
  2441. return(0);
  2442. }
  2443. first = 0;
  2444. } else if (funcstr) { /* Reading data from a function */
  2445. /* This will not be Unicode */
  2446. if ((x = (*funcptr)()) < 0) { /* End of input */
  2447. first = -1;
  2448. size = 0; /* Empty */
  2449. return(0);
  2450. }
  2451. ffc++; /* Count a file character */
  2452. rt = (CHAR) x; /* Convert int to char */
  2453. first = 0;
  2454. debug(F000,"getpkt funcstr","",rt);
  2455. } else { /* Reading data from a file */
  2456. #ifndef NOCSETS
  2457. if (xlate && !binary) { /* Could be Unicode */
  2458. if (xlatype == XLA_UNICODE) {
  2459. /* Get next translated byte */
  2460. x = xgnbyte(cseqtab[tcharset],fcharset,NULL);
  2461. debug(F101,"getpkt xgnbyte","",x);
  2462. } else { /* Not Unicode */
  2463. x = zminchar(); /* Get next byte, translate below */
  2464. debug(F101,"getpkt zminchar A","",x);
  2465. }
  2466. } else { /* Just get next byte */
  2467. #endif /* NOCSETS */
  2468. x = zminchar();
  2469. debug(F101,"getpkt zminchar B","",x);
  2470. #ifndef NOCSETS
  2471. }
  2472. #endif /* NOCSETS */
  2473. if (x < 0) { /* End of file or input error */
  2474. if (x == -3) { /* Timeout. */
  2475. size = (dp-data);
  2476. debug(F101,"getpkt timeout size","",size);
  2477. return((size == 0) ? x : size);
  2478. }
  2479. first = -1;
  2480. size = 0;
  2481. if (x == -2) { /* Error */
  2482. debug(F100,"getpkt: input error","",0);
  2483. cxseen = 1; /* Interrupt the file transfer */
  2484. } else {
  2485. debug(F100,"getpkt empty file","",0);
  2486. }
  2487. return(0);
  2488. }
  2489. first = 0; /* Next character won't be first */
  2490. rt = (CHAR) x; /* Convert int to char */
  2491. #ifndef NOCSETS
  2492. if (xlatype != XLA_UNICODE || binary) {
  2493. ffc++;
  2494. if (sx)
  2495. rt = (*sx)(rt);
  2496. if (docrc && (what & W_SEND))
  2497. dofilcrc(x);
  2498. }
  2499. #endif /* NOCSETS */
  2500. #ifdef DEBUG
  2501. if (deblog)
  2502. debug(F101,"getpkt 1st char","",rt);
  2503. #endif /* DEBUG */
  2504. if (/* !haveuc && */ docrc && (what & W_SEND)) /* File CRC */
  2505. dofilcrc(x);
  2506. }
  2507. #ifdef KANJI
  2508. }
  2509. #endif /* KANJI */
  2510. /* PWP: handling of feol is done later (in the while loop)... */
  2511. } else if ((first == -1) && (nleft == 0)) { /* EOF from last time */
  2512. #ifdef DEBUG
  2513. if (deblog) {
  2514. debug(F101,"getpkt eof crc16","",crc16);
  2515. debug(F101,"getpkt eof ffc","",ffc);
  2516. }
  2517. #endif /* DEBUG */
  2518. return(size = 0);
  2519. }
  2520. /*
  2521. Here we handle characters that were encoded for the last packet but
  2522. did not fit, and so were saved in the "leftover" array.
  2523. */
  2524. debug(F101,"getpkt nleft","",nleft);
  2525. if (nleft) {
  2526. for (p1 = leftover; nleft > 0; nleft--) /* Copy leftovers */
  2527. *dp++ = *p1++;
  2528. *leftover = '\0'; /* Delete leftovers */
  2529. nleft = 0;
  2530. }
  2531. if (first == -1) /* Handle EOF */
  2532. return(size = (dp - data));
  2533. /* Now fill up the rest of the packet. */
  2534. rpt = 0; /* Initialize character repeat count */
  2535. while (first > -1) { /* Until EOF... */
  2536. #ifdef CALIBRATE
  2537. if (calibrate && !memstr) { /* We generate our own "file" */
  2538. if (ffc >= calibrate) { /* EOF */
  2539. first = -1;
  2540. ffc--;
  2541. } else { /* Generate next character */
  2542. if (cal_j > CAL_M * ffc)
  2543. cal_j = cal_a[ffc & 0xff];
  2544. x = (unsigned)cal_a[(cal_j & 0xff)];
  2545. if (x == rt) x ^= 2;
  2546. }
  2547. cal_j += (unsigned int)(ffc + CAL_O);
  2548. ffc++;
  2549. } else
  2550. #endif /* CALIBRATE */
  2551. #ifdef KANJI
  2552. if (xlate && tcharset == TC_JEUC) {
  2553. if ((x = zkanji(memstr ? kgetm : kgetf)) < 0) {
  2554. first = -1;
  2555. if (x == -2) cxseen = 1;
  2556. } else if (!memstr) ffc++;
  2557. rnext = (CHAR) (x & fmask);
  2558. } else {
  2559. #endif /* KANJI */
  2560. if (memstr) { /* Get next char from memory string */
  2561. if ((x = *memptr++) == '\0') /* End of string means EOF */
  2562. first = -1; /* Flag EOF for next time. */
  2563. rnext = (CHAR) (x & fmask); /* Apply file mask */
  2564. } else if (funcstr) { /* Get next char from function */
  2565. if ((x = (*funcptr)()) < 0) /* End of string means EOF */
  2566. first = -1; /* Flag EOF for next time. */
  2567. rnext = (CHAR) (x & fmask); /* Apply file mask */
  2568. } else { /* From file... */
  2569. #ifndef NOCSETS
  2570. if (xlate && !binary) { /* Could be Unicode */
  2571. if (xlatype == XLA_UNICODE) {
  2572. /* Get next translated byte */
  2573. x = xgnbyte(cseqtab[tcharset],fcharset,NULL);
  2574. } else { /* Not Unicode */
  2575. x = zminchar(); /* Get next byte, translate below */
  2576. /* debug(F101,"xgnbyte B zminchar","",x); */
  2577. }
  2578. } else { /* Just get next byte */
  2579. #endif /* NOCSETS */
  2580. x = zminchar();
  2581. /* debug(F101,"xgnbyte C zminchar","",x); */
  2582. #ifndef NOCSETS
  2583. }
  2584. #endif /* NOCSETS */
  2585. if (x < 0) { /* Check for EOF */
  2586. if (x == -3) { /* Timeout reading from pipe */
  2587. t = rt;
  2588. size = (dp-data);
  2589. debug(F101,"getpkt timeout size","",size);
  2590. return((size == 0) ? x : size);
  2591. }
  2592. first = -1; /* Flag eof for next time. */
  2593. if (x == -2) cxseen = 1; /* If error, cancel this file. */
  2594. }
  2595. rnext = (CHAR) (x & fmask); /* Apply file mask */
  2596. #ifndef NOCSETS
  2597. if (xlatype != XLA_UNICODE) {
  2598. #endif /* NOCSETS */
  2599. ffc++;
  2600. #ifndef NOCSETS
  2601. if (sx)
  2602. rt = (*sx)(rt);
  2603. #endif /* NOCSETS */
  2604. if (docrc && (what & W_SEND))
  2605. dofilcrc(x);
  2606. #ifndef NOCSETS
  2607. }
  2608. #endif /* NOCSETS */
  2609. }
  2610. #ifdef KANJI
  2611. }
  2612. #endif /* KANJI */
  2613. /*
  2614. At this point, the character we just read is in rnext,
  2615. and the character we are about to encode into the packet is in rt.
  2616. */
  2617. odp = dp; /* Remember where we started. */
  2618. xxls = xxdl = xxrc = xxss = xxcq = NUL; /* Clear these. */
  2619. /*
  2620. Now encode the character according to the options that are in effect:
  2621. ctlp[]: whether this control character needs prefixing.
  2622. binary: text or binary mode.
  2623. rptflg: repeat counts enabled.
  2624. ebqflg: 8th-bit prefixing enabled.
  2625. lscapu: locking shifts enabled.
  2626. */
  2627. if (rptflg) { /* Repeat processing is on? */
  2628. if (!uflag &&
  2629. /*
  2630. * If the next char is really CRLF, then we cannot
  2631. * be doing a repeat (unless CR,CR,LF which becomes
  2632. * "~ <n-1> CR CR LF", which is OK but not most efficient).
  2633. * I just plain don't worry about this case. The actual
  2634. * conversion from NL to CRLF is done after the rptflg if...
  2635. */
  2636. (!feol || binary || (feol && (rnext != feol))) &&
  2637. (rt == rnext) && (first == 0)) { /* Got a run... */
  2638. if (++rpt < 94) { /* Below max, just count */
  2639. continue; /* go back and get another */
  2640. } else if (rpt == 94) { /* Reached max, must dump */
  2641. xxrc = (CHAR) tochar(rpt); /* Put the repeat count here */
  2642. rptn += rpt; /* Accumulate it for statistics */
  2643. rpt = 0; /* And reset it */
  2644. }
  2645. } else if (rpt > 1) { /* More than two */
  2646. xxrc = (CHAR) tochar(++rpt); /* and count. */
  2647. rptn += rpt;
  2648. rpt = 0; /* Reset repeat counter. */
  2649. }
  2650. /*
  2651. If (rpt == 1) we must encode exactly two characters.
  2652. This is done later, after the first character is encoded.
  2653. */
  2654. }
  2655. /* If it's the newline character... */
  2656. if (!uflag && !binary && feol && (rt == feol)) {
  2657. if (lscapu && lsstate) { /* If SHIFT-STATE is SHIFTED */
  2658. if (ebqflg) { /* If single shifts enabled, */
  2659. *dp++ = (CHAR) ebq; /* insert a single shift. */
  2660. } else { /* Otherwise must shift in. */
  2661. *dp++ = myctlq; /* Insert shift-out code */
  2662. *dp++ = 'O';
  2663. lsstate = 0; /* Change shift state */
  2664. }
  2665. }
  2666. #ifdef CK_SPEED
  2667. if (ctlp[CR]) {
  2668. *dp++ = myctlq; /* Insert carriage return directly */
  2669. *dp++ = 'M';
  2670. ccp++;
  2671. } else {
  2672. *dp++ = CR; /* Perhaps literally */
  2673. ccu++;
  2674. }
  2675. #else /* !CK_SPEED */
  2676. *dp++ = myctlq; /* Insert carriage return directly */
  2677. *dp++ = 'M';
  2678. ccp++;
  2679. #endif /* CK_SPEED */
  2680. rt = LF; /* Now make next char be linefeed. */
  2681. }
  2682. /*
  2683. Now handle the 8th bit of the file character. If we have an 8-bit
  2684. connection, we preserve the 8th bit. If we have a 7-bit connection,
  2685. we employ either single or locking shifts (if they are enabled).
  2686. */
  2687. a7 = rt & 0177; /* Get low 7 bits of character */
  2688. if (rt & 0200) { /* 8-bit character? */
  2689. if (lscapu) { /* Locking shifts enabled? */
  2690. if (!lsstate) { /* Not currently shifted? */
  2691. x = lslook(0200); /* Look ahead */
  2692. if (x != 0 || ebqflg == 0) { /* Locking shift decision */
  2693. xxls = 'N'; /* Need locking shift-out */
  2694. lsstate = 1; /* and change to shifted state */
  2695. } else if (ebqflg) { /* Not worth it */
  2696. xxss = (CHAR) ebq; /* Use single shift */
  2697. }
  2698. }
  2699. rt = (CHAR) a7; /* Replace character by 7-bit value */
  2700. } else if (ebqflg) { /* 8th bit prefixing is on? */
  2701. xxss = (CHAR) ebq; /* Insert single shift */
  2702. rt = (CHAR) a7; /* Replace character by 7-bit value */
  2703. }
  2704. /*
  2705. In case we have a 7-bit connection and this is an 8-bit character, AND
  2706. neither locking shifts nor single shifts are enabled, then the character's
  2707. 8th bit will be destroyed in transmission, and a block check error will
  2708. occur.
  2709. */
  2710. } else if (lscapu) { /* 7-bit character */
  2711. if (lsstate) { /* Comes while shifted out? */
  2712. x = lslook(0); /* Yes, look ahead */
  2713. if (x || ebqflg == 0) { /* Time to shift in. */
  2714. xxls = 'O'; /* Set shift-in code */
  2715. lsstate = 0; /* Exit shifted state */
  2716. } else if (ebqflg) { /* Not worth it, stay shifted out */
  2717. xxss = (CHAR) ebq; /* Insert single shift */
  2718. }
  2719. }
  2720. }
  2721. /* If data character is significant to locking shift protocol... */
  2722. if (lscapu && (a7 == SO || a7 == SI || a7 == DLE))
  2723. xxdl = 'P'; /* Insert datalink escape */
  2724. if (
  2725. #ifdef CK_SPEED
  2726. /*
  2727. Thwart YET ANOTHER unwanted, unneeded, and unloved sign
  2728. extension. This one was particularly nasty because it prevented
  2729. 255 (Telnet IAC) from being prefixed on some platforms -- e.g.
  2730. VMS with VAX C -- but not others, thus causing file transfers to
  2731. fail on Telnet connections by sending bare IACs. Not to mention
  2732. the stray memory reference. Signed chars are a BAD idea.
  2733. */
  2734. ctlp[(unsigned)(rt & 0xff)] /* Lop off any "sign" extension */
  2735. #else
  2736. (a7 < SP) || (a7 == DEL)
  2737. #endif /* CK_SPEED */
  2738. ) { /* Do control prefixing if necessary */
  2739. xxcq = myctlq; /* The prefix */
  2740. ccp++; /* Count it */
  2741. rt = (CHAR) ctl(rt); /* Uncontrollify the character */
  2742. }
  2743. #ifdef CK_SPEED
  2744. else if ((a7 < SP) || (a7 == DEL)) /* Count an unprefixed one */
  2745. ccu++;
  2746. #endif /* CK_SPEED */
  2747. if (a7 == myctlq) /* Always prefix the control prefix */
  2748. xxcq = myctlq;
  2749. if ((rptflg) && (a7 == rptq)) /* If it's the repeat prefix, */
  2750. xxcq = myctlq; /* prefix it if doing repeat counts */
  2751. if ((ebqflg) && (a7 == ebq)) /* Prefix the 8th-bit prefix */
  2752. xxcq = myctlq; /* if doing 8th-bit prefixes */
  2753. /* Now construct the entire sequence */
  2754. if (xxls) { *dp++ = myctlq; *dp++ = xxls; } /* Locking shift */
  2755. odp2 = dp; /* (Save this place) */
  2756. if (xxdl) { *dp++ = myctlq; *dp++ = xxdl; } /* Datalink escape */
  2757. if (xxrc) { *dp++ = (CHAR) rptq; *dp++ = xxrc; } /* Repeat count */
  2758. if (xxss) { *dp++ = (CHAR) ebq; } /* Single shift */
  2759. if (xxcq) { *dp++ = myctlq; } /* Control prefix */
  2760. *dp++ = rt; /* Finally, the character itself */
  2761. if (rpt == 1) { /* Exactly two copies? */
  2762. rpt = 0;
  2763. p2 = dp; /* Save place temporarily */
  2764. for (p1 = odp2; p1 < p2; p1++) /* Copy the old chars over again */
  2765. *dp++ = *p1;
  2766. if ((p2-data) <= bufmax) odp = p2; /* Check packet bounds */
  2767. if ((p2-data) < bufmax) odp = p2; /* Check packet bounds */
  2768. }
  2769. rt = rnext; /* Next character is now current. */
  2770. /* Done encoding the character. Now take care of packet buffer overflow. */
  2771. if ((dp-data) >= bufmax) { /* If too big, save some for next. */
  2772. debug(F000,"getpkt EOP","",rt);
  2773. size = (dp-data); /* Calculate the size. */
  2774. *dp = '\0'; /* Mark the end. */
  2775. if (memstr) { /* No leftovers for memory strings */
  2776. if (rt) /* Char we didn't encode yet */
  2777. memptr--; /* (for encstr()) */
  2778. return(size);
  2779. }
  2780. if ((dp-data) > bufmax) { /* if packet is overfull */
  2781. /* copy the part that doesn't fit into the leftover buffer, */
  2782. /* taking care not to split a prefixed sequence. */
  2783. int i;
  2784. nleft = dp - odp;
  2785. for (i = 0, p1 = leftover, p2 = odp; i < nleft; i++) {
  2786. *p1++ = *p2++;
  2787. if (memstr) memptr--; /* (for encstr) */
  2788. }
  2789. debug(F111,"getpkt leftover",leftover,size);
  2790. debug(F101,"getpkt osize","",(odp-data));
  2791. size = (odp-data); /* Return truncated packet. */
  2792. *odp = '\0'; /* Mark the new end */
  2793. }
  2794. t = rt; /* Save for next time */
  2795. return(size);
  2796. }
  2797. } /* Otherwise, keep filling. */
  2798. size = (dp-data); /* End of file */
  2799. *dp = '\0'; /* Mark the end of the data. */
  2800. debug(F111,"getpkt eof/eot",data,size); /* Fell thru before packet full, */
  2801. return(size); /* return partially filled last packet. */
  2802. }
  2803. /* T I N I T -- Initialize a transaction */
  2804. int epktrcvd = 0, epktsent = 0;
  2805. /*
  2806. Call with 1 to reset everything before S/I/Y negotiation, or 0 to
  2807. reset only the things that are not set in the S/I/Y negotiation.
  2808. Returns -1 on failure (e.g. to create packet buffers), 0 on success.
  2809. */
  2810. int
  2811. tinit(flag) int flag; {
  2812. int x;
  2813. #ifdef CK_TIMERS
  2814. extern int rttflg;
  2815. #endif /* CK_TIMERS */
  2816. extern int rcvtimo;
  2817. extern int fatalio;
  2818. debug(F101,"tinit flag","",flag);
  2819. *epktmsg = NUL;
  2820. epktrcvd = 0;
  2821. epktsent = 0;
  2822. ofperms = "";
  2823. diractive = 0; /* DIR / REMOTE DIR not active */
  2824. interrupted = 0; /* Not interrupted */
  2825. fatalio = 0; /* No fatal i/o error */
  2826. if (server) {
  2827. moving = 0;
  2828. pipesend = 0; /* This takes care of multiple GETs sent to a server. */
  2829. }
  2830. bestlen = 0; /* For packet length optimization */
  2831. maxsend = 0; /* Biggest data field we can send */
  2832. #ifdef STREAMING
  2833. streamok = 0; /* Streaming negotiated */
  2834. streaming = 0; /* Streaming being done now */
  2835. #endif /* STREAMING */
  2836. binary = b_save; /* ... */
  2837. gnf_binary = binary; /* Per-file transfer mode */
  2838. retrans = 0; /* Packet retransmission count */
  2839. sndtyp = 0; /* No previous packet */
  2840. xflg = 0; /* Reset x-packet flag */
  2841. memstr = 0; /* Reset memory-string flag */
  2842. memptr = NULL; /* and buffer pointer */
  2843. funcstr = 0; /* Reset "read from function" flag */
  2844. funcptr = NULL; /* and function pointer */
  2845. autopar = 0; /* Automatic parity detection flag */
  2846. /* This stuff is only for BEFORE S/I/Y negotiation, not after */
  2847. if (flag) {
  2848. if (bctf) { /* Force Block Check 3 on all packets */
  2849. bctu = bctl = 3; /* Set block check type to 3 */
  2850. } else {
  2851. bctu = bctl = 1; /* Reset block check type to 1 */
  2852. }
  2853. myinit[0] = '\0'; /* Haven't sent init string yet */
  2854. rqf = -1; /* Reset 8th-bit-quote request flag */
  2855. ebq = MYEBQ; /* Reset 8th-bit quoting stuff */
  2856. ebqflg = 0; /* 8th bit quoting not enabled */
  2857. ebqsent = 0; /* No 8th-bit prefix bid sent yet */
  2858. sq = 'Y'; /* 8th-bit prefix bid I usually send */
  2859. spsiz = spsizr; /* Initial send-packet size */
  2860. debug(F101,"tinit spsiz","",spsiz);
  2861. wslots = 1; /* One window slot */
  2862. wslotn = 1; /* No window negotiated yet */
  2863. justone = 0; /* (should this be zero'd here?) */
  2864. whoareu[0] = NUL; /* Partner's system type */
  2865. sysindex = -1;
  2866. wearealike = 0;
  2867. what = W_INIT; /* Doing nothing so far... */
  2868. }
  2869. fncnv = f_save; /* Back to what user last said */
  2870. pktnum = 0; /* Initial packet number to send */
  2871. cxseen = czseen = discard = 0; /* Reset interrupt flags */
  2872. *filnam = '\0'; /* Clear file name */
  2873. spktl = 0; /* And its length */
  2874. nakstate = 0; /* Assume we're not in a NAK state */
  2875. numerrs = 0; /* Transmission error counter */
  2876. idletmo = 0; /* No idle timeout yet */
  2877. if (server) { /* If acting as server, */
  2878. if (srvidl > 0) /* If an idle timeout is given */
  2879. timint = srvidl;
  2880. else
  2881. timint = srvtim; /* use server timeout interval. */
  2882. } else { /* Otherwise */
  2883. timint = chktimo(rtimo,timef); /* and use local timeout value */
  2884. }
  2885. debug(F101,"tinit timint","",timint);
  2886. #ifdef CK_TIMERS
  2887. if (rttflg && timint > 0) /* Using round-trip timers? */
  2888. rttinit();
  2889. else
  2890. #endif /* CK_TIMERS */
  2891. rcvtimo = timint;
  2892. winlo = 0; /* Packet 0 is at window-low */
  2893. debug(F101,"tinit winlo","",winlo);
  2894. x = mksbuf(1); /* Make a 1-slot send-packet buffer */
  2895. if (x < 0) return(x);
  2896. x = getsbuf(0); /* Allocate first send-buffer. */
  2897. debug(F101,"tinit getsbuf","",x);
  2898. if (x < 0) return(x);
  2899. dumpsbuf();
  2900. x = mkrbuf(wslots); /* & a 1-slot receive-packet buffer. */
  2901. if (x < 0) return(x);
  2902. lsstate = 0; /* Initialize locking shift state */
  2903. if (autopath) { /* SET RECEIVE PATHNAMES AUTO fixup */
  2904. fnrpath = PATH_AUTO;
  2905. autopath = 0;
  2906. }
  2907. return(0);
  2908. }
  2909. VOID
  2910. pktinit() { /* Initialize packet sequence */
  2911. pktnum = 0; /* number & window low. */
  2912. winlo = 0;
  2913. debug(F101,"pktinit winlo","",winlo);
  2914. }
  2915. /* R I N I T -- Respond to S or I packet */
  2916. VOID
  2917. rinit(d) CHAR *d; {
  2918. char *tp = NULL;
  2919. ztime(&tp);
  2920. tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  2921. tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  2922. tlog(F110,"Collision action:", fncnam[fncact],0);
  2923. tlog(F100,"","",0);
  2924. debug(F101,"rinit fncact","",fncact);
  2925. filcnt = filrej = 0; /* Init file counters */
  2926. spar(d);
  2927. ack1(rpar());
  2928. #ifdef datageneral
  2929. if ((local) && (!quiet)) /* Only do this if local & not quiet */
  2930. consta_mt(); /* Start the asynch read task */
  2931. #endif /* datageneral */
  2932. }
  2933. /* R E S E T C -- Reset per-transaction character counters */
  2934. VOID
  2935. resetc() {
  2936. rptn = 0; /* Repeat counts */
  2937. fsecs = flci = flco = (CK_OFF_T)0; /* File chars in and out */
  2938. #ifdef GFTIMER
  2939. fpfsecs = 0.0;
  2940. #endif /* GFTIMER */
  2941. tfc = tlci = tlco = (CK_OFF_T)0; /* Total file, line chars in & out */
  2942. ccu = ccp = 0L; /* Control-char statistics */
  2943. #ifdef COMMENT
  2944. fsize = (CK_OFF_T)-1; /* File size */
  2945. #else
  2946. if (!(what & W_SEND))
  2947. fsize = (CK_OFF_T)-1;
  2948. debug(F101,"resetc fsize","",fsize);
  2949. #endif /* COMMENT */
  2950. timeouts = retrans = 0; /* Timeouts, retransmissions */
  2951. spackets = rpackets = 0; /* Packet counts out & in */
  2952. crunched = 0; /* Crunched packets */
  2953. wcur = 0; /* Current window size */
  2954. wmax = 0; /* Maximum window size used */
  2955. peakcps = 0; /* Peak chars per second */
  2956. }
  2957. /* S I N I T -- Get & verify first file name, then send Send-Init packet */
  2958. /*
  2959. Returns:
  2960. 1 if send operation begins successfully
  2961. 0 if send operation fails
  2962. */
  2963. #ifdef DYNAMIC
  2964. char *cmargbuf = NULL;
  2965. #else
  2966. char cmargbuf[CKMAXPATH+1];
  2967. #endif /* DYNAMIC */
  2968. char *cmargp[2];
  2969. VOID
  2970. fnlist() {
  2971. if (!calibrate)
  2972. sndsrc = (nfils < 0) ? -1 : nfils; /* Source for filenames */
  2973. #ifdef DYNAMIC
  2974. if (!cmargbuf && !(cmargbuf = malloc(CKMAXPATH+1)))
  2975. fatal("fnlist: no memory for cmargbuf");
  2976. #endif /* DYNAMIC */
  2977. cmargbuf[0] = NUL; /* Initialize name buffer */
  2978. debug(F101,"fnlist nfils","",nfils);
  2979. debug(F110,"fnlist cmarg",cmarg,0);
  2980. debug(F110,"fnlist cmarg2",cmarg2,0);
  2981. if (!cmarg2) cmarg2 = "";
  2982. if (nfils == 0) { /* Sending from stdin or memory. */
  2983. if ((cmarg2 != NULL) && (*cmarg2)) {
  2984. cmarg = cmarg2; /* If F packet, "as-name" is used */
  2985. cmarg2 = ""; /* if provided */
  2986. } else
  2987. cmarg = "stdin"; /* otherwise just use "stdin" */
  2988. ckstrncpy(cmargbuf,cmarg,CKMAXPATH+1);
  2989. cmargp[0] = cmargbuf;
  2990. cmargp[1] = "";
  2991. cmlist = cmargp;
  2992. nfils = 1;
  2993. }
  2994. }
  2995. int
  2996. sinit() {
  2997. int x; /* Worker int */
  2998. char *tp, *xp, *m; /* Worker string pointers */
  2999. filcnt = filrej = 0; /* Initialize file counters */
  3000. fnlist();
  3001. xp = "";
  3002. if (nfils < 0) {
  3003. #ifdef PIPESEND
  3004. if (usepipes && protocol == PROTO_K && *cmarg == '!') {
  3005. pipesend = 1;
  3006. cmarg++;
  3007. }
  3008. #endif /* PIPESEND */
  3009. xp = cmarg;
  3010. } else {
  3011. #ifndef NOMSEND
  3012. if (addlist)
  3013. xp = filehead->fl_name;
  3014. else
  3015. #endif /* NOMSEND */
  3016. if (filefile)
  3017. xp = filefile;
  3018. else if (calibrate)
  3019. xp = "Calibration";
  3020. else
  3021. xp = *cmlist;
  3022. }
  3023. debug(F110,"sinit xp",xp,0);
  3024. x = gnfile(); /* Get first filename. */
  3025. debug(F111,"sinit gnfile",ckitoa(gnferror),x);
  3026. if (x == 0) x = gnferror; /* If none, get error reason */
  3027. m = NULL; /* Error message pointer */
  3028. debug(F101,"sinit gnfil","",x);
  3029. switch (x) {
  3030. case -6: m = "No files meet selection criteria"; break;
  3031. case -5: m = "Too many files match wildcard"; break;
  3032. case -4: m = "Cancelled"; break;
  3033. case -3: m = "Read access denied"; break;
  3034. case -2: m = "File is not readable"; break;
  3035. #ifdef COMMENT
  3036. case -1: m = iswild(filnam) ? "No files match" : "File not found";
  3037. break;
  3038. case 0: m = "No filespec given!"; break;
  3039. #else
  3040. case 0:
  3041. case -1: m = iswild(filnam) ? "No files match" : "File not found";
  3042. break;
  3043. #endif /* COMMENT */
  3044. default:
  3045. break;
  3046. }
  3047. debug(F101,"sinit nfils","",nfils);
  3048. debug(F110,"sinit filnam",filnam,0);
  3049. if (x < 1) { /* Didn't get a file. */
  3050. debug(F111,"sinit msg",m,x);
  3051. if (server) { /* Doing GET command */
  3052. errpkt((CHAR *)m); /* so send Error packet. */
  3053. } else if (!local) { /* Doing SEND command */
  3054. interrupted = 1; /* (To suppress hint) */
  3055. printf("?%s\r\n",m);
  3056. } else {
  3057. xxscreen(SCR_EM,0,0l,m); /* so print message. */
  3058. }
  3059. tlog(F110,xp,m,0L); /* Make transaction log entry. */
  3060. freerbuf(rseqtbl[0]); /* Free the buffer the GET came in. */
  3061. return(0); /* Return failure code */
  3062. }
  3063. if (!local && !server && ckdelay > 0) /* OS-9 sleep(0) == infinite */
  3064. sleep(ckdelay); /* Delay if requested */
  3065. #ifdef datageneral
  3066. if ((local) && (!quiet)) /* Only do this if local & not quiet */
  3067. consta_mt(); /* Start the async read task */
  3068. #endif /* datageneral */
  3069. freerbuf(rseqtbl[0]); /* Free the buffer the GET came in. */
  3070. sipkt('S'); /* Send the Send-Init packet. */
  3071. ztime(&tp); /* Get current date/time */
  3072. tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  3073. tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  3074. tlog(F100,"","",0);
  3075. debug(F111,"sinit ok",filnam,0);
  3076. return(1);
  3077. }
  3078. int
  3079. #ifdef CK_ANSIC
  3080. sipkt(char c) /* Send S or I packet. */
  3081. #else
  3082. sipkt(c) char c;
  3083. #endif
  3084. /* sipkt */ {
  3085. CHAR *rp; int k, x;
  3086. extern int sendipkts;
  3087. debug(F101,"sipkt pktnum","",pktnum); /* (better be 0...) */
  3088. ttflui(); /* Flush pending input. */
  3089. /*
  3090. If this is an I packet and SET SEND I-PACKETS is OFF, don't send it;
  3091. set sstate to 'Y' which makes the next input() call return 'Y' as if we
  3092. had received an ACK to the I packet we didn't send. This is to work
  3093. around buggy Kermit servers that can't handle I packets.
  3094. */
  3095. if ((sendipkts == 0) && (c == 'I')) { /* I packet but don't send I pkts? */
  3096. sstate = 'Y'; /* Yikes! */
  3097. return(0); /* (see input()..)*/
  3098. }
  3099. k = sseqtbl[pktnum]; /* Find slot for this packet */
  3100. if (k < 0) { /* No slot? */
  3101. k = getsbuf(winlo = pktnum); /* Make one. */
  3102. debug(F101,"sipkt getsbuf","",k);
  3103. }
  3104. rp = rpar(); /* Get protocol parameters. */
  3105. if (!rp) rp = (CHAR *)"";
  3106. x = spack(c,pktnum,(int)strlen((char *)rp),rp); /* Send them. */
  3107. return(x);
  3108. }
  3109. /* X S I N I T -- Retransmit S-packet */
  3110. /*
  3111. For use in the GET-SEND sequence, when we start to send, but receive another
  3112. copy of the GET command because the receiver didn't get our S packet.
  3113. This retransmits the S packet and frees the receive buffer for the ACK.
  3114. This special case is necessary because packet number zero is being re-used.
  3115. */
  3116. VOID
  3117. xsinit() {
  3118. int k;
  3119. k = rseqtbl[0];
  3120. debug(F101,"xsinit k","",k);
  3121. if (k > -1)
  3122. freerbuf(k);
  3123. resend(0);
  3124. }
  3125. /* R C V F I L -- Receive a file */
  3126. /*
  3127. Incoming filename is in data field of F packet.
  3128. This function decodes it into the srvcmd buffer, substituting an
  3129. alternate "as-name", if one was given.
  3130. Then it does any requested transformations (like converting to
  3131. lowercase), and finally if a file of the same name already exists,
  3132. takes the desired collision action.
  3133. Returns:
  3134. 1 on success.
  3135. 0 on failure.
  3136. */
  3137. char ofn1[CKMAXPATH+4]; /* Buffer for output file name */
  3138. char * ofn2; /* Pointer to backup file name */
  3139. int ofn1x; /* Flag output file already exists */
  3140. CK_OFF_T ofn1len = (CK_OFF_T)0;
  3141. int opnerr; /* Flag for open error */
  3142. int /* Returns success ? 1 : 0 */
  3143. rcvfil(n) char *n; {
  3144. extern int en_cwd;
  3145. int i, skipthis;
  3146. char * n2;
  3147. char * dispo;
  3148. #ifdef OS2ONLY
  3149. char *zs, *longname, *newlongname, *pn; /* OS/2 long name items */
  3150. #endif /* OS2ONLY */
  3151. #ifdef DTILDE
  3152. char *dirp;
  3153. #endif /* DTILDE */
  3154. int dirflg, x, y;
  3155. #ifdef PIPESEND
  3156. extern char * rcvfilter;
  3157. #endif /* PIPESEND */
  3158. #ifdef CALIBRATE
  3159. extern int dest;
  3160. CK_OFF_T csave;
  3161. csave = calibrate; /* So we can decode filename */
  3162. calibrate = (CK_OFF_T)0;
  3163. #endif /* CALIBRATE */
  3164. ofperms = ""; /* Reset old-file permissions */
  3165. opnerr = 0; /* No open error (yet) */
  3166. ofn2 = NULL; /* No new name (yet) */
  3167. lsstate = 0; /* Cancel locking-shift state */
  3168. srvptr = srvcmd; /* Decode file name from packet. */
  3169. #ifdef UNICODE
  3170. xpnbyte(-1,0,0,NULL); /* Reset UCS-2 byte counter. */
  3171. #endif /* UNICODE */
  3172. debug(F110,"rcvfil rdatap",rdatap,0);
  3173. decode(rdatap,putsrv,0); /* Don't xlate charsets. */
  3174. #ifdef CALIBRATE
  3175. calibrate = csave;
  3176. if (dest == DEST_N) {
  3177. calibrate = 1;
  3178. cmarg2 = "CALIBRATE";
  3179. }
  3180. #endif /* CALIBRATE */
  3181. if (*srvcmd == '\0') /* Watch out for null F packet. */
  3182. ckstrncpy((char *)srvcmd,"NONAME",srvcmdlen);
  3183. makestr(&prrfspec,(char *)srvcmd); /* New preliminary filename */
  3184. #ifdef DTILDE
  3185. if (*srvcmd == '~') {
  3186. dirp = tilde_expand((char *)srvcmd); /* Expand tilde, if any. */
  3187. if (*dirp != '\0')
  3188. ckstrncpy((char *)srvcmd,dirp,srvcmdlen);
  3189. }
  3190. #else
  3191. #ifdef OS2
  3192. if (isalpha(*srvcmd) && srvcmd[1] == ':' && srvcmd[2] == '\0')
  3193. ckstrncat((char *)srvcmd,"NONAME",srvcmdlen);
  3194. #endif /* OS2 */
  3195. #endif /* DTILDE */
  3196. #ifndef NOICP
  3197. #ifndef NOSPL
  3198. /* File dialog when downloading... */
  3199. if (
  3200. #ifdef CK_APC
  3201. (apcactive == APC_LOCAL && adl_ask) || /* Autodownload with ASK */
  3202. #endif /* CK_APC */
  3203. (clcmds && haveurl) /* Or "kermit:" or "iksd:" URL */
  3204. ) {
  3205. int x;
  3206. char fnbuf[CKMAXPATH+1]; /* Result buffer */
  3207. char * preface;
  3208. if (clcmds && haveurl)
  3209. preface = "\r\nIncoming file from Kermit server...\r\n\
  3210. Please confirm output file specification or supply an alternative:";
  3211. else
  3212. preface = "\r\nIncoming file from remote Kermit...\r\n\
  3213. Please confirm output file specification or supply an alternative:";
  3214. x = uq_file(preface, /* Preface */
  3215. NULL, /* Prompt (let uq_file() built it) */
  3216. 3, /* Output file */
  3217. NULL, /* Help text */
  3218. (char *)srvcmd, /* Default */
  3219. fnbuf, /* Result buffer */
  3220. CKMAXPATH+1 /* Size of result buffer */
  3221. );
  3222. if (x < 1) { /* Refused */
  3223. rf_err = "Refused by user";
  3224. return(0);
  3225. }
  3226. ckstrncpy((char *)srvcmd,fnbuf,CKMAXPATH+1);
  3227. if (isabsolute((char *)srvcmd)) { /* User gave an absolute path */
  3228. g_fnrpath = fnrpath; /* Save current RECEIVE PATHNAMES */
  3229. fnrpath = PATH_ABS; /* switch to ABSOLUTE */
  3230. }
  3231. }
  3232. #endif /* NOSPL */
  3233. #endif /* NOICP */
  3234. if (!ENABLED(en_cwd)) { /* CD is disabled */
  3235. zstrip((char *)(srvcmd+2),&n2); /* and they included a pathname, */
  3236. if (strcmp((char *)(srvcmd+2),n2)) { /* so refuse. */
  3237. rf_err = "Access denied";
  3238. return(0);
  3239. }
  3240. }
  3241. #ifdef COMMENT
  3242. /* Wrong place for this -- handle cmarg2 first -- see below... */
  3243. if (zchko((char *)srvcmd) < 0) { /* Precheck for write access */
  3244. debug(F110,"rcvfil access denied",srvcmd,0);
  3245. rf_err = "Write access denied";
  3246. discard = opnerr = 1;
  3247. return(0);
  3248. }
  3249. xxscreen(SCR_FN,0,0l,(char *)srvcmd); /* Put it on screen if local */
  3250. debug(F110,"rcvfil srvcmd 1",srvcmd,0);
  3251. tlog(F110,"Receiving",(char *)srvcmd,0L); /* Transaction log entry */
  3252. #endif /* COMMENT */
  3253. skipthis = 0; /* This file in our exception list? */
  3254. for (i = 0; i < NSNDEXCEPT; i++) {
  3255. if (!rcvexcept[i]) {
  3256. break;
  3257. }
  3258. if (ckmatch(rcvexcept[i],(char *)srvcmd,filecase,1)) {
  3259. skipthis = 1;
  3260. break;
  3261. }
  3262. }
  3263. #ifdef DEBUG
  3264. if (deblog && skipthis) {
  3265. debug(F111,"rcvfil rcvexcept",rcvexcept[i],i);
  3266. debug(F110,"rcvfil skipping",srvcmd,0);
  3267. }
  3268. #endif /* DEBUG */
  3269. if (skipthis) { /* Skipping this file */
  3270. discard = 1;
  3271. rejection = 1;
  3272. rf_err = "Exception list";
  3273. debug(F101,"rcvfil discard","",discard);
  3274. tlog(F100," refused: exception list","",0);
  3275. return(1);
  3276. }
  3277. /* File is not in exception list */
  3278. if (!cmarg2) /* No core dumps please */
  3279. cmarg2 = "";
  3280. debug(F110,"rcvfil cmarg2",cmarg2,0);
  3281. if (*cmarg2) { /* Check for alternate name */
  3282. #ifndef NOSPL
  3283. int y; char *s; /* Pass it thru the evaluator */
  3284. extern int cmd_quoting;
  3285. if (cmd_quoting) {
  3286. y = MAXRP;
  3287. ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* for \v(filename) */
  3288. s = (char *)srvcmd;
  3289. zzstring(cmarg2,&s,&y);
  3290. } else
  3291. *srvcmd = NUL;
  3292. if (!*srvcmd) /* If we got something */
  3293. #endif /* NOSPL */
  3294. ckstrncpy((char *)srvcmd,cmarg2,srvcmdlen);
  3295. }
  3296. debug(F110,"rcvfil srvcmd 2",srvcmd,0);
  3297. #ifdef PIPESEND
  3298. /* If it starts with "bang", it's a pipe, not a file. */
  3299. if (usepipes && protocol == PROTO_K && *srvcmd == '!' && !rcvfilter) {
  3300. CHAR *s;
  3301. s = srvcmd+1; /* srvcmd[] is not a pointer. */
  3302. while (*s) { /* So we have to slide the contents */
  3303. *(s-1) = *s; /* over 1 space to the left. */
  3304. s++;
  3305. }
  3306. *(s-1) = NUL;
  3307. pipesend = 1;
  3308. }
  3309. #endif /* PIPESEND */
  3310. #ifdef COMMENT
  3311. /*
  3312. This is commented out because we need to know whether the name we are
  3313. using was specified by the local user as an override, or came from the
  3314. incoming packet. In the former case, we don't do stuff to it (like
  3315. strip the pathname) that we might do to it in the latter.
  3316. */
  3317. cmarg2 = ""; /* Done with alternate name */
  3318. #endif /* COMMENT */
  3319. if ((int)strlen((char *)srvcmd) > CKMAXPATH) /* Watch out for overflow */
  3320. *(srvcmd + CKMAXPATH - 1) = NUL;
  3321. /* At this point, srvcmd[] contains the incoming filename or as-name. */
  3322. /* So NOW we check for write access. */
  3323. if (zchko((char *)srvcmd) < 0) { /* Precheck for write access */
  3324. debug(F110,"rcvfil access denied",srvcmd,0);
  3325. rf_err = "Write access denied";
  3326. discard = opnerr = 1;
  3327. return(0);
  3328. }
  3329. xxscreen(SCR_FN,0,0l,(char *)srvcmd); /* Put it on screen if local */
  3330. debug(F110,"rcvfil srvcmd 1",srvcmd,0);
  3331. tlog(F110,"Receiving",(char *)srvcmd,0L); /* Transaction log entry */
  3332. #ifdef CK_LABELED
  3333. #ifdef VMS
  3334. /*
  3335. If we have an as-name, this overrides the internal name if we are doing
  3336. a labeled-mode transfer.
  3337. */
  3338. if (*cmarg2) {
  3339. extern int lf_opts;
  3340. lf_opts &= ~LBL_NAM;
  3341. }
  3342. #endif /* VMS */
  3343. #endif /* CK_LABELED */
  3344. debug(F111,"rcvfil pipesend",srvcmd,pipesend);
  3345. #ifdef PIPESEND
  3346. /* Skip all the filename manipulation and collision actions */
  3347. if (pipesend) {
  3348. dirflg = 0;
  3349. ofn1[0] = '!';
  3350. ckstrncpy(&ofn1[1],(char *)srvcmd,CKMAXPATH+1);
  3351. ckstrncpy(n,ofn1,CKMAXPATH+1);
  3352. ckstrncpy(fspec,ofn1,CKMAXPATH+1);
  3353. makestr(&prfspec,fspec); /* New preliminary filename */
  3354. debug(F110,"rcvfil pipesend",ofn1,0);
  3355. goto rcvfilx;
  3356. }
  3357. #endif /* PIPESEND */
  3358. /*
  3359. This is to avoid passing email subjects through nzrtol().
  3360. We haven't yet received the A packet so we don't yet know it's e-mail,
  3361. so in fact we go ahead and convert it anyway, but later we get the
  3362. original back from ofilnam[].
  3363. */
  3364. dispos = 0;
  3365. ckstrncpy(ofilnam,(char *)srvcmd,CKMAXPATH+1);
  3366. #ifdef NZLTOR
  3367. if (*cmarg2)
  3368. ckstrncpy((char *)ofn1,(char *)srvcmd,CKMAXPATH+1);
  3369. else
  3370. nzrtol((char *)srvcmd, /* Filename from packet */
  3371. (char *)ofn1, /* Where to put result */
  3372. fncnv, /* Filename conversion */
  3373. fnrpath, /* Pathname handling */
  3374. CKMAXPATH /* Size of result buffer */
  3375. );
  3376. #else
  3377. debug(F101,"rcvfil fnrpath","",fnrpath); /* Handle pathnames */
  3378. if (fnrpath == PATH_OFF && !*cmarg2) { /* RECEIVE PATHNAMES OFF? */
  3379. char *t; /* Yes. */
  3380. zstrip((char *)srvcmd,&t); /* If there is a pathname, strip it */
  3381. debug(F110,"rcvfil PATH_OFF zstrip",t,0);
  3382. if (!t) /* Be sure we didn't strip too much */
  3383. sprintf(ofn1,"FILE%02ld",filcnt);
  3384. else if (*t == '\0')
  3385. sprintf(ofn1,"FILE%02ld",filcnt);
  3386. else
  3387. ckstrncpy(ofn1,t,CKMAXPATH+1);
  3388. ckstrncpy((char *)srvcmd,ofn1,srvcmdlen); /* Now copy it back. */
  3389. }
  3390. /*
  3391. SET RECEIVE PATHNAMES RELATIVE...
  3392. The following doesn't belong here but doing it right would require
  3393. defining and implementing a new file routine for all ck?fio.c modules.
  3394. So for now...
  3395. */
  3396. #ifdef UNIXOROSK
  3397. else if (fnrpath == PATH_REL && !*cmarg2) {
  3398. if (isabsolute((char *)srvcmd)) {
  3399. ofn1[0] = '.';
  3400. ckstrncpy(&of1n[1],(char *)srvcmd,CKMAXPATH+1);
  3401. ckstrncpy((char *)srvcmd,ofn1,srvcmdlen);
  3402. debug(F110,"rcvfil PATH_REL",ofn1,0);
  3403. }
  3404. }
  3405. #else
  3406. #ifdef OS2
  3407. else if (fnrpath == PATH_REL && !*cmarg2) {
  3408. if (isabsolute((char *)srvcmd)) {
  3409. char *p = (char *)srvcmd;
  3410. if (isalpha(*p) && *(p+1) == ':')
  3411. p += 2;
  3412. if (*p == '\\' || *p == '/')
  3413. p++;
  3414. ckstrncpy(ofn1,p,CKMAXPATH+1);
  3415. ckstrncpy((char *)srvcmd,ofn1,srvcmdlen);
  3416. debug(F110,"rcvfil OS2 PATH_REL",ofn1,0);
  3417. }
  3418. }
  3419. #endif /* OS2 */
  3420. #endif /* UNIXOROSK */
  3421. /* Now srvcmd contains incoming filename with path possibly stripped */
  3422. if (fncnv) /* FILE NAMES CONVERTED? */
  3423. zrtol((char *)srvcmd,(char *)ofn1); /* Yes, convert to local form */
  3424. else
  3425. ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* No, copy literally. */
  3426. #endif /* NZLTOR */
  3427. #ifdef PIPESEND
  3428. if (rcvfilter) {
  3429. char * p = NULL, * q;
  3430. int nn = MAXRP;
  3431. pipesend = 1;
  3432. debug(F110,"rcvfil rcvfilter ",rcvfilter,0);
  3433. #ifndef NOSPL
  3434. if ((p = (char *) malloc(nn + 1))) {
  3435. q = p;
  3436. #ifdef COMMENT
  3437. /* We have already processed srvcmd and placed it into ofn1 */
  3438. ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* For \v(filename) */
  3439. #endif /* COMMENT */
  3440. debug(F110,"rcvfile pipesend filter",rcvfilter,0);
  3441. zzstring(rcvfilter,&p,&nn);
  3442. debug(F111,"rcvfil pipename",q,nn);
  3443. if (nn <= 0) {
  3444. printf(
  3445. "?Sorry, receive filter + filename too long, %d max.\n",
  3446. CKMAXPATH
  3447. );
  3448. rf_err = "Name too long";
  3449. free(q);
  3450. return(0);
  3451. }
  3452. ckstrncpy((char *)srvcmd,q,MAXRP);
  3453. free(q);
  3454. }
  3455. #endif /* NOSPL */
  3456. }
  3457. #endif /* PIPESEND */
  3458. /* Now the incoming filename, possibly converted, is in ofn1[]. */
  3459. #ifdef OS2
  3460. /* Don't refuse the file just because the name is illegal. */
  3461. if (!IsFileNameValid(ofn1)) { /* Name is OK for OS/2? */
  3462. #ifdef OS2ONLY
  3463. char *zs = NULL;
  3464. zstrip(ofn1, &zs); /* Not valid, strip unconditionally */
  3465. if (zs) {
  3466. if (iattr.longname.len && /* Free previous longname, if any */
  3467. iattr.longname.val)
  3468. free(iattr.longname.val);
  3469. iattr.longname.len = strlen(zs); /* Store in attribute structure */
  3470. iattr.longname.val = (char *) malloc(iattr.longname.len + 1);
  3471. if (iattr.longname.val) /* Remember this (illegal) name */
  3472. strcpy(iattr.longname.val, zs); /* safe */
  3473. }
  3474. #endif /* OS2ONLY */
  3475. debug(F110,"rcvfil: invalid file name",ofn1,0);
  3476. ChangeNameForFAT(ofn1); /* Change to an acceptable name */
  3477. debug(F110,"rcvfil: FAT file name",ofn1,0);
  3478. } else { /* Name is OK. */
  3479. debug(F110,"rcvfil: valid file name",ofn1,0);
  3480. #ifdef OS2ONLY
  3481. if (iattr.longname.len &&
  3482. iattr.longname.val) /* Free previous longname, if any */
  3483. free(iattr.longname.val);
  3484. iattr.longname.len = 0;
  3485. iattr.longname.val = NULL; /* This file doesn't need a longname */
  3486. #endif /* OS2ONLY */
  3487. }
  3488. #endif /* OS2 */
  3489. debug(F110,"rcvfil as",ofn1,0);
  3490. /* Filename collision action section. */
  3491. dirflg = /* Is it a directory name? */
  3492. #ifdef CK_TMPDIR
  3493. isdir(ofn1)
  3494. #else
  3495. 0
  3496. #endif /* CK_TMPDIR */
  3497. ;
  3498. debug(F101,"rcvfil dirflg","",dirflg);
  3499. ofn1len = zchki(ofn1); /* File already exists? */
  3500. debug(F111,"rcvfil ofn1len",ofn1,ofn1len);
  3501. ofn1x = (ofn1len != (CK_OFF_T)-1);
  3502. if ( (
  3503. #ifdef UNIX
  3504. strcmp(ofn1,"/dev/null") && /* It's not the null device? */
  3505. #else
  3506. #ifdef OSK
  3507. strcmp(ofn1,"/nil") &&
  3508. #endif /* OSK */
  3509. #endif /* UNIX */
  3510. !stdouf ) && /* Not copying to standard output? */
  3511. ofn1x || /* File of same name exists? */
  3512. dirflg ) { /* Or file is a directory? */
  3513. debug(F111,"rcvfil exists",ofn1,fncact);
  3514. #ifdef CK_PERMS
  3515. ofperms = zgperm((char *)ofn1); /* Get old file's permissions */
  3516. debug(F110,"rcvfil perms",ofperms,0);
  3517. #endif /* CK_PERMS */
  3518. debug(F101,"rcvfil fncact","",fncact);
  3519. switch (fncact) { /* Yes, do what user said. */
  3520. case XYFX_A: /* Append */
  3521. ofperms = "";
  3522. debug(F100,"rcvfil append","",0);
  3523. if (dirflg) {
  3524. rf_err = "Can't append to a directory";
  3525. tlog(F100," error - can't append to directory","",0);
  3526. discard = opnerr = 1;
  3527. return(0);
  3528. }
  3529. tlog(F110," appending to",ofn1,0);
  3530. break;
  3531. #ifdef COMMENT
  3532. case XYFX_Q: /* Query (Ask) */
  3533. break; /* not implemented */
  3534. #endif /* COMMENT */
  3535. case XYFX_B: /* Backup (rename old file) */
  3536. if (dirflg) {
  3537. rf_err = "Can't rename existing directory";
  3538. tlog(F100," error - can't rename directory","",0);
  3539. discard = opnerr = 1;
  3540. return(0);
  3541. }
  3542. znewn(ofn1,&ofn2); /* Get new unique name */
  3543. tlog(F110," backup:",ofn2,0);
  3544. debug(F110,"rcvfil backup ofn1",ofn1,0);
  3545. debug(F110,"rcvfil backup ofn2",ofn2,0);
  3546. #ifdef CK_LABELED
  3547. #ifdef OS2ONLY
  3548. /*
  3549. In case this is a FAT file system, we can't change only the FAT name, we
  3550. also have to change the longname from the extended attributes block.
  3551. Otherwise, we'll have many files with the same longname and if we copy them
  3552. to an HPFS volume, only one will survive.
  3553. */
  3554. if (os2getlongname(ofn1, &longname) > -1) {
  3555. if (strlen(longname)) {
  3556. char tmp[10];
  3557. extern int ck_znewn;
  3558. sprintf(tmp,".~%d~",ck_znewn);
  3559. newlongname =
  3560. (char *) malloc(strlen(longname) + strlen(tmp) + 1);
  3561. if (newlongname) {
  3562. strcpy(newlongname, longname); /* safe (prechecked) */
  3563. strcat(newlongname, tmp); /* safe (prechecked) */
  3564. os2setlongname(ofn1, newlongname);
  3565. free(newlongname);
  3566. newlongname = NULL;
  3567. }
  3568. }
  3569. } else debug(F100,"rcvfil os2getlongname failed","",0);
  3570. #endif /* OS2ONLY */
  3571. #endif /* CK_LABELED */
  3572. #ifdef COMMENT
  3573. /* Do this later, in opena()... */
  3574. if (zrename(ofn1,ofn2) < 0) {
  3575. rf_err = "Can't transform filename";
  3576. debug(F110,"rcvfil rename fails",ofn1,0);
  3577. discard = opnerr = 1;
  3578. return(0);
  3579. }
  3580. #endif /* COMMENT */
  3581. break;
  3582. case XYFX_D: /* Discard (refuse new file) */
  3583. ofperms = "";
  3584. discard = 1;
  3585. rejection = 1; /* Horrible hack: reason = name */
  3586. debug(F101,"rcvfil discard","",discard);
  3587. tlog(F100," refused: name","",0);
  3588. break;
  3589. case XYFX_R: /* Rename incoming file */
  3590. znewn(ofn1,&ofn2); /* Make new name for it */
  3591. #ifdef OS2ONLY
  3592. if (iattr.longname.len) {
  3593. char tmp[10];
  3594. extern int ck_znewn;
  3595. sprintf(tmp,".~%d~",ck_znewn);
  3596. newlongname =
  3597. (char *) malloc(iattr.longname.len + strlen(tmp) + 1);
  3598. if (newlongname) {
  3599. strcpy(newlongname, iattr.longname.val); /* safe */
  3600. strcat(newlongname, tmp); /* safe */
  3601. debug(F110,
  3602. "Rename Incoming: newlongname",newlongname,0);
  3603. if (iattr.longname.len &&
  3604. iattr.longname.val)
  3605. free(iattr.longname.val);
  3606. iattr.longname.len = strlen(newlongname);
  3607. iattr.longname.val = newlongname;
  3608. /* free(newlongname) here ? */
  3609. }
  3610. }
  3611. #endif /* OS2ONLY */
  3612. break;
  3613. case XYFX_X: /* Replace old file */
  3614. debug(F100,"rcvfil overwrite","",0);
  3615. if (dirflg) {
  3616. rf_err = "Can't overwrite existing directory";
  3617. tlog(F100," error - can't overwrite directory","",0);
  3618. discard = opnerr = 1;
  3619. #ifdef COMMENT
  3620. return(0);
  3621. #else
  3622. break;
  3623. #endif /* COMMENT */
  3624. }
  3625. tlog(F110,"overwriting",ofn1,0);
  3626. break;
  3627. case XYFX_U: /* Refuse if older */
  3628. debug(F110,"rcvfil update",ofn1,0);
  3629. if (dirflg) {
  3630. rf_err = "File has same name as existing directory";
  3631. tlog(F110," error - directory exists:",ofn1,0);
  3632. discard = opnerr = 1;
  3633. #ifdef COMMENT
  3634. /* Don't send an error packet, just refuse the file */
  3635. return(0);
  3636. #endif /* COMMENT */
  3637. }
  3638. break; /* Not here, we don't have */
  3639. /* the attribute packet yet. */
  3640. default:
  3641. ofperms = "";
  3642. debug(F101,"rcvfil bad collision action","",fncact);
  3643. break;
  3644. }
  3645. }
  3646. debug(F110,"rcvfil ofn1",ofn1,0);
  3647. debug(F110,"rcvfil ofn2",ofn2,0);
  3648. debug(F110,"rcvfil ofperms",ofperms,0);
  3649. if (fncact == XYFX_R && ofn1x && ofn2) { /* Renaming incoming file? */
  3650. xxscreen(SCR_AN,0,0l,ofn2); /* Display renamed name */
  3651. ckstrncpy(n, ofn2, CKMAXPATH+1); /* Return it */
  3652. } else { /* No */
  3653. xxscreen(SCR_AN,0,0l,ofn1); /* Display regular name */
  3654. ckstrncpy(n, ofn1, CKMAXPATH+1); /* and return it. */
  3655. }
  3656. #ifdef CK_MKDIR
  3657. /* Create directory(s) if necessary. */
  3658. if (!discard && fnrpath != PATH_OFF) { /* RECEIVE PATHAMES ON? */
  3659. int x;
  3660. debug(F110,"rcvfil calling zmkdir",ofn1,0); /* Yes */
  3661. if ((x = zmkdir(ofn1)) < 0) {
  3662. debug(F100,"zmkdir fails","",0);
  3663. tlog(F110," error - directory creation failure:",ofn1,0);
  3664. rf_err = "Directory creation failure.";
  3665. discard = 1;
  3666. return(0);
  3667. }
  3668. #ifdef TLOG
  3669. else if (x > 0)
  3670. tlog(F110," path created:",ofn1,0);
  3671. #endif /* TLOG */
  3672. }
  3673. #else
  3674. debug(F110,"rcvfil CK_MKDIR not defined",ofn1,0);
  3675. #endif /* CK_MKDIR */
  3676. if (calibrate)
  3677. ckstrncpy(fspec,ofn1,CKMAXPATH+1);
  3678. else
  3679. zfnqfp(ofn1,fspeclen,fspec);
  3680. debug(F110,"rcvfil fspec",fspec,0);
  3681. #ifdef COMMENT
  3682. /* See comments with VMS zfnqfp()... */
  3683. #ifdef VMS
  3684. /* zfnqfp() does not return the version number */
  3685. if (!calibrate) {
  3686. int x, v;
  3687. x = strlen(ofn1);
  3688. if (x > 0) {
  3689. if (ofn1[x-1] == ';') {
  3690. v = getvnum(ofn1);
  3691. ckstrncpy(&ofn1[x],ckitoa(v),CKMAXPATH-x);
  3692. }
  3693. }
  3694. }
  3695. #endif /* VMS */
  3696. #endif /* COMMENT */
  3697. fspec[fspeclen] = NUL;
  3698. makestr(&prfspec,fspec); /* New preliminary filename */
  3699. #ifdef PIPESEND
  3700. rcvfilx:
  3701. #endif /* PIPESEND */
  3702. debug(F110,"rcvfilx: n",n,0);
  3703. debug(F110,"rcvfilx: ofn1",ofn1,0);
  3704. ffc = (CK_OFF_T)0; /* Init per-file counters */
  3705. cps = oldcps = 0L;
  3706. rs_len = (CK_OFF_T)0;
  3707. rejection = -1;
  3708. fsecs = gtimer(); /* Time this file started */
  3709. #ifdef GFTIMER
  3710. fpfsecs = gftimer();
  3711. debug(F101,"rcvfil fpfsecs","",fpfsecs);
  3712. #endif /* GFTIMER */
  3713. filcnt++;
  3714. intmsg(filcnt);
  3715. return(1); /* Successful return */
  3716. }
  3717. /* R E O F -- Receive End Of File packet for incoming file */
  3718. /*
  3719. Closes the received file.
  3720. Returns:
  3721. 0 on success.
  3722. -1 if file could not be closed.
  3723. 2 if disposition was mail, mail was sent, but temp file not deleted.
  3724. 3 if disposition was print, file was printed, but not deleted.
  3725. -2 if disposition was mail and mail could not be sent
  3726. -3 if disposition was print and file could not be printed
  3727. -4 if MOVE-TO: failed
  3728. -5 if RENAME-TO: failed
  3729. */
  3730. int
  3731. reof(f,yy) char *f; struct zattr *yy; {
  3732. extern char * rcv_move, * rcv_rename;
  3733. extern int o_isopen;
  3734. int rc = 0; /* Return code */
  3735. char *p;
  3736. char c;
  3737. debug(F111,"reof fncact",f,fncact);
  3738. debug(F101,"reof discard","",discard);
  3739. success = 1; /* Assume status is OK */
  3740. lsstate = 0; /* Cancel locking-shift state */
  3741. if (discard) { /* Handle attribute refusals, etc. */
  3742. debug(F101,"reof discarding","",0);
  3743. success = 0; /* Status = failed. */
  3744. if (rejection == '#' || /* Unless rejection reason is */
  3745. rejection == 1 || /* date or name (SET FILE COLLISION */
  3746. rejection == '?') /* UPDATE or DISCARD) */
  3747. success = 1;
  3748. debug(F101,"reof success","",success);
  3749. filrej++; /* Count this rejection. */
  3750. discard = 0; /* We never opened the file, */
  3751. return(0); /* so we don't close it. */
  3752. }
  3753. #ifdef DEBUG
  3754. if (deblog) {
  3755. debug(F101,"reof cxseen","",cxseen);
  3756. debug(F101,"reof czseen","",czseen);
  3757. debug(F110,"reof rdatap",rdatap,0);
  3758. }
  3759. #endif /* DEBUG */
  3760. if (cxseen == 0) /* Got cancel directive? */
  3761. cxseen = (*rdatap == 'D');
  3762. if (cxseen || czseen) /* (for hints) */
  3763. interrupted = 1;
  3764. success = (cxseen || czseen) ? 0 : 1; /* Set SUCCESS flag appropriately */
  3765. if (!success) /* "Uncount" this file */
  3766. filrej++;
  3767. debug(F101,"reof o_isopen","",o_isopen);
  3768. if (o_isopen) { /* If an output file was open... */
  3769. #ifdef CK_CTRLZ
  3770. if (success) {
  3771. debug(F101,"reof lastchar","",lastchar);
  3772. if (!binary && eofmethod == XYEOF_Z && lastchar != 26 &&
  3773. (!xflg || (xflg && remfile)))
  3774. pnbyte((char)26,putfil);
  3775. }
  3776. #endif /* CK_CTRLZ */
  3777. rc = clsof(cxseen || czseen); /* Close the file (resets cxseen) */
  3778. debug(F101,"reof closf","",rc);
  3779. if (rc < 0) { /* If failure to close, FAIL */
  3780. if (success) filrej++;
  3781. success = 0;
  3782. }
  3783. if (!calibrate) {
  3784. /* Set file modification date and/or permissions */
  3785. if (success)
  3786. zstime(f,yy,0);
  3787. #ifdef OS2ONLY
  3788. #ifdef CK_LABELED
  3789. if (success && yy->longname.len)
  3790. os2setlongname(f, yy->longname.val);
  3791. #endif /* CK_LABELED */
  3792. #endif /* OS2ONLY */
  3793. }
  3794. if (success == 0) { /* And program return code */
  3795. xitsta |= W_RECV;
  3796. } else if (success == 1) { /* File rec'd successfully */
  3797. makestr(&rrfspec,prrfspec); /* Record it for wheremsg */
  3798. makestr(&rfspec,prfspec);
  3799. }
  3800. /* Handle dispositions from attribute packet... */
  3801. c = NUL;
  3802. #ifndef NOFRILLS
  3803. if (!calibrate && yy->disp.len != 0) {
  3804. p = yy->disp.val;
  3805. c = *p++;
  3806. #ifndef UNIX
  3807. /*
  3808. See ckcpro.w. In UNIX we don't use temp files any more -- we pipe the
  3809. stuff right into mail or lpr.
  3810. */
  3811. if (c == 'M') { /* Mail to user. */
  3812. rc = zmail(p,filnam); /* Do the system's mail command */
  3813. if (rc < 0) success = 0; /* Remember status */
  3814. tlog(F110,"mailed",filnam,0L);
  3815. tlog(F110," to",p,0L);
  3816. zdelet(filnam); /* Delete the file */
  3817. } else if (c == 'P') { /* Print the file. */
  3818. rc = zprint(p,filnam); /* Do the system's print command */
  3819. if (rc < 0) success = 0; /* Remember status */
  3820. tlog(F110,"printed",filnam,0L);
  3821. tlog(F110," with options",p,0L);
  3822. #ifndef VMS
  3823. #ifndef STRATUS
  3824. /* spooler deletes file after print complete in VOS & VMS */
  3825. if (zdelet(filnam) && rc == 0) rc = 3; /* Delete the file */
  3826. #endif /* STRATUS */
  3827. #endif /* VMS */
  3828. }
  3829. #endif /* UNIX */
  3830. }
  3831. #endif /* NOFRILLS */
  3832. if (success &&
  3833. !pipesend &&
  3834. !calibrate && c != 'M' && c != 'P') {
  3835. if (rcv_move) { /* If /MOVE-TO was given... */
  3836. char * p = rcv_move;
  3837. #ifdef COMMENT
  3838. /* No need for this - it's a directory name */
  3839. char tmpbuf[CKMAXPATH+1];
  3840. extern int cmd_quoting; /* for \v(filename) */
  3841. if (cmd_quoting) { /* But only if cmd_quoting is on */
  3842. int n;
  3843. n = CKMAXPATH;
  3844. p = tmpbuf;
  3845. debug(F111,"reof /move-to",rcv_move,0);
  3846. zzstring(rcv_move,&p,&n);
  3847. p = tmpbuf;
  3848. }
  3849. #endif /* COMMENT */
  3850. /*
  3851. Here we could create the directory if it didn't exist (and it was relative)
  3852. but there would have to be a user-settable option to say whether to do this.
  3853. */
  3854. rc = zrename(filnam,p);
  3855. debug(F111,"reof MOVE zrename",rcv_move,rc);
  3856. if (rc > -1) {
  3857. tlog(F110," moving received file to",rcv_move,0);
  3858. } else {
  3859. rc = -4;
  3860. tlog(F110," FAILED to move received file to",rcv_move,0);
  3861. }
  3862. } else if (rcv_rename) { /* Or /RENAME-TO: */
  3863. char *s = rcv_rename; /* This is the renaming string */
  3864. #ifndef NOSPL
  3865. char tmpnam[CKMAXPATH+16];
  3866. extern int cmd_quoting; /* for \v(filename) */
  3867. if (cmd_quoting) { /* But only if cmd_quoting is on */
  3868. int n; /* Pass it thru the evaluator */
  3869. n = CKMAXPATH;
  3870. s = (char *)tmpnam;
  3871. zzstring(rcv_rename,&s,&n);
  3872. s = (char *)tmpnam;
  3873. }
  3874. #endif /* NOSPL */
  3875. if (s) if (*s) {
  3876. rc = zrename(filnam,s);
  3877. debug(F111,"reof RENAME zrename",s,rc);
  3878. if (rc > -1) {
  3879. tlog(F110," renaming received file to",s,0);
  3880. } else {
  3881. rc = -5;
  3882. tlog(F110," FAILED to rename received file to",s,0);
  3883. }
  3884. }
  3885. }
  3886. }
  3887. }
  3888. debug(F101,"reof success","",success);
  3889. debug(F101,"reof returns","",rc);
  3890. filnam[0] = NUL; /* Erase the filename */
  3891. return(rc);
  3892. }
  3893. /* R E O T -- Receive End Of Transaction */
  3894. VOID
  3895. reot() {
  3896. cxseen = czseen = discard = 0; /* Reset interruption flags */
  3897. tstats(); /* Finalize transfer statistics */
  3898. }
  3899. /* S F I L E -- Send File header or teXt header packet */
  3900. /*
  3901. Call with x nonzero for X packet, zero for F packet.
  3902. If X == 0, filename to send is in filnam[], and if cmarg2 is not null
  3903. or empty, the file should be sent under this name rather than filnam[].
  3904. If sndfilter not NULL, it is the name of a send filter.
  3905. Returns 1 on success, 0 on failure.
  3906. */
  3907. int
  3908. sfile(x) int x; {
  3909. #ifdef pdp11
  3910. #define PKTNL 64
  3911. #else
  3912. #define PKTNL 256
  3913. #endif /* pdp11 */
  3914. char pktnam[PKTNL+1]; /* Local copy of name */
  3915. char *s;
  3916. int rc;
  3917. int notafile = 0;
  3918. extern int filepeek;
  3919. #ifdef PIPESEND
  3920. extern char * sndfilter;
  3921. if (sndfilter) {
  3922. pipesend = 1;
  3923. debug(F110,"sfile send filter ",sndfilter,0);
  3924. }
  3925. #endif /* PIPESEND */
  3926. notafile = calibrate || sndarray || pipesend || x;
  3927. debug(F101,"sfile x","",x);
  3928. debug(F101,"sfile notafile","",notafile);
  3929. #ifndef NOCSETS
  3930. if (tcs_save > -1) { /* Character sets */
  3931. tcharset = tcs_save;
  3932. tcs_save = -1;
  3933. debug(F101,"sfile restored tcharset","",tcharset);
  3934. }
  3935. if (fcs_save > -1) {
  3936. fcharset = fcs_save;
  3937. fcs_save = -1;
  3938. debug(F101,"sfile restored fcharset","",fcharset);
  3939. }
  3940. setxlatype(tcharset,fcharset); /* Translation type */
  3941. #endif /* NOCSETS */
  3942. /* cmarg2 or filnam (with that precedence) have the file's name */
  3943. lsstate = 0; /* Cancel locking-shift state */
  3944. #ifdef COMMENT
  3945. /* Do this after making sure we can open the file */
  3946. if (nxtpkt() < 0) return(0); /* Bump packet number, get buffer */
  3947. #endif /* COMMENT */
  3948. pktnam[0] = NUL; /* Buffer for name we will send */
  3949. if (x == 0) { /* F-Packet setup */
  3950. if (!cmarg2) cmarg2 = "";
  3951. #ifdef DEBUG
  3952. if (deblog) {
  3953. /* debug(F111,"sfile cmarg2",cmarg2,cmarg2); */
  3954. debug(F101,"sfile binary 1","",binary);
  3955. debug(F101,"sfile wearealike","",wearealike);
  3956. debug(F101,"sfile xfermode","",xfermode);
  3957. debug(F101,"sfile filepeek","",filepeek);
  3958. #ifndef NOCSETS
  3959. debug(F101,"sfile s_cset","",s_cset);
  3960. debug(F101,"sfile tcharset","",tcharset);
  3961. debug(F101,"sfile xfrxla","",xfrxla);
  3962. #endif /* NOCSETS */
  3963. }
  3964. #endif /* DEBUG */
  3965. if (xfermode == XMODE_A /* TRANSFER MODE AUTOMATIC */
  3966. #ifndef NOMSEND
  3967. && !addlist /* And not working from a SEND-LIST */
  3968. #endif /* NOMSEND */
  3969. ) {
  3970. /* Other Kermit is on a like system and no charset translation */
  3971. if (wearealike
  3972. #ifndef NOCSETS
  3973. && (tcharset == TC_TRANSP || xfrxla == 0)
  3974. #endif /* NOCSETS */
  3975. ) {
  3976. #ifdef VMS
  3977. if (binary != XYFT_I)
  3978. #endif /* VMS */
  3979. #ifdef CK_LABELED
  3980. if (binary != XYFT_L)
  3981. #endif /* CK_LABELED */
  3982. binary = XYFT_B; /* Send all files in binary mode */
  3983. }
  3984. /* Otherwise select transfer mode based on file info */
  3985. else if (!notafile /* but not if sending from pipe */
  3986. #ifdef CK_LABELED
  3987. && binary != XYFT_L /* and not if FILE TYPE LABELED */
  3988. #endif /* CK_LABELED */
  3989. #ifdef VMS
  3990. && binary != XYFT_I /* or FILE TYPE IMAGE */
  3991. #endif /* VMS */
  3992. ) {
  3993. #ifdef UNICODE
  3994. fileorder = -1; /* File byte order */
  3995. #endif /* UNICODE */
  3996. if (filepeek && !notafile) { /* Real file, FILE SCAN is ON */
  3997. int k, x;
  3998. k = scanfile(filnam,&x,nscanfile); /* Scan the file */
  3999. debug(F101,"sfile scanfile","",k);
  4000. switch (k) {
  4001. case FT_TEXT: /* Unspecified text */
  4002. debug(F100,"sfile scanfile text","",0);
  4003. binary = XYFT_T; /* SET FILE TYPE TEXT */
  4004. break;
  4005. #ifndef NOCSETS
  4006. case FT_7BIT: /* 7-bit text */
  4007. binary = XYFT_T; /* SET FILE TYPE TEXT */
  4008. /* If SEND CHARSET-SELECTION AUTO */
  4009. /* and SET TRANSFER TRANSLATION is ON */
  4010. debug(F100,"sfile scanfile text7","",0);
  4011. if (s_cset == XMODE_A && xfrxla) {
  4012. if (fcsinfo[fcharset].size != 128) {
  4013. fcs_save = fcharset; /* Current FCS not 7bit */
  4014. fcharset = dcset7; /* Use default 7bit set */
  4015. debug(F101,"sfile scanfile 7 fcharset",
  4016. "",fcharset);
  4017. }
  4018. /* And also switch to appropriate TCS */
  4019. if (afcset[fcharset] > -1 &&
  4020. afcset[fcharset] <= MAXTCSETS) {
  4021. tcs_save = tcharset;
  4022. tcharset = afcset[fcharset];
  4023. debug(F101,"sfile scanfile 7 tcharset","",
  4024. tcharset);
  4025. }
  4026. setxlatype(tcharset,fcharset);
  4027. }
  4028. break;
  4029. case FT_8BIT: /* 8-bit text */
  4030. binary = XYFT_T; /* SET FILE TYPE TEXT */
  4031. /* If SEND CHARSET-SELEC AUTO */
  4032. /* and SET TRANSFER TRANSLATION is ON */
  4033. debug(F100,"sfile scanfile text8","",0);
  4034. if (s_cset == XMODE_A && xfrxla) {
  4035. if (fcsinfo[fcharset].size != 256) {
  4036. fcs_save = fcharset; /* Current FCS not 8bit */
  4037. fcharset = dcset8; /* Use default 8bit set */
  4038. debug(F101,"sfile scanfile 8 fcharset",
  4039. "",fcharset);
  4040. }
  4041. /* Switch to corresponding transfer charset */
  4042. if (afcset[fcharset] > -1 &&
  4043. afcset[fcharset] <= MAXTCSETS) {
  4044. tcs_save = tcharset;
  4045. tcharset = afcset[fcharset];
  4046. debug(F101,"sfile scanfile 8 tcharset","",
  4047. tcharset);
  4048. }
  4049. setxlatype(tcharset,fcharset);
  4050. }
  4051. break;
  4052. #ifdef UNICODE
  4053. case FT_UTF8: /* UTF-8 text */
  4054. case FT_UCS2: /* UCS-2 text */
  4055. debug(F101,"sfile scanfile Unicode","",k);
  4056. binary = XYFT_T;
  4057. /* If SEND CHARSET-SELEC AUTO */
  4058. /* and SET TRANSFER TRANSLATION is ON */
  4059. if (s_cset == XMODE_A && xfrxla) {
  4060. fcs_save = fcharset;
  4061. tcs_save = tcharset;
  4062. fcharset = (k == FT_UCS2) ? FC_UCS2 : FC_UTF8;
  4063. if (k == FT_UCS2 && x > -1)
  4064. fileorder = x;
  4065. }
  4066. /* Switch to associated transfer charset if any */
  4067. if (afcset[fcharset] > -1 &&
  4068. afcset[fcharset] <= MAXTCSETS)
  4069. tcharset = afcset[fcharset];
  4070. if (tcharset == TC_TRANSP) /* If none */
  4071. tcharset = TC_UTF8; /* use UTF-8 */
  4072. setxlatype(tcharset,fcharset);
  4073. debug(F101,"sfile Unicode tcharset","",tcharset);
  4074. break;
  4075. #endif /* UNICODE */
  4076. #endif /* NOCSETS */
  4077. case FT_BIN:
  4078. debug(F101,"sfile scanfile binary","",k);
  4079. binary = XYFT_B;
  4080. break;
  4081. /* Default: Don't change anything */
  4082. }
  4083. }
  4084. }
  4085. debug(F101,"sfile binary 2","",binary);
  4086. debug(F101,"sfile sendmode","",sendmode);
  4087. }
  4088. if (*cmarg2) { /* If we have a send-as name... */
  4089. int y; char *s;
  4090. #ifndef NOSPL /* and a script programming language */
  4091. extern int cmd_quoting;
  4092. if (cmd_quoting) { /* and it's not turned off */
  4093. y = PKTNL; /* pass as-name thru the evaluator */
  4094. s = pktnam;
  4095. zzstring(cmarg2,&s,&y);
  4096. #ifdef COMMENT
  4097. /* This ruins macros like BSEND */
  4098. if (!pktnam[0]) /* and make sure result is not empty */
  4099. sprintf(pktnam,"FILE%02ld",filcnt);
  4100. #endif /* COMMENT */
  4101. } else
  4102. #endif /* NOSPL */
  4103. ckstrncpy(pktnam,cmarg2,PKTNL); /* copy it literally, */
  4104. debug(F110,"sfile pktnam",pktnam,0);
  4105. #ifdef COMMENT
  4106. /* We don't do this any more because now we have filename templates */
  4107. cmarg2 = ""; /* and blank it out for next time. */
  4108. #endif /* COMMENT */
  4109. }
  4110. if (!*pktnam) { /* No as-name... */
  4111. #ifdef NZLTOR
  4112. int xfncnv, xpath;
  4113. debug(F101,"sfile fnspath","",fnspath);
  4114. debug(F101,"sfile fncnv","",fncnv);
  4115. xfncnv = fncnv;
  4116. xpath = fnspath;
  4117. if (notafile) { /* If not an actual file */
  4118. xfncnv = 0; /* Don't convert name */
  4119. xpath = PATH_OFF; /* Leave path off */
  4120. } else if (xfncnv &&
  4121. (!strcmp(whoareu,"U1") || !strcmp(whoareu,"UN"))
  4122. ) {
  4123. /* Less-strict conversion if partner is UNIX or Win32 */
  4124. xfncnv = -1;
  4125. }
  4126. debug(F101,"sfile xpath","",xpath);
  4127. debug(F101,"sfile xfncnv","",xfncnv);
  4128. nzltor(filnam,pktnam,xfncnv,xpath,PKTNL);
  4129. #else /* Not NZLTOR */
  4130. debug(F101,"sfile fnspath","",fnspath);
  4131. if (fnspath == PATH_OFF /* Stripping path names? */
  4132. && (!notafile) /* (of actual files) */
  4133. ) {
  4134. char *t; /* Yes. */
  4135. zstrip(filnam,&t); /* Strip off the path. */
  4136. debug(F110,"sfile zstrip",t,0);
  4137. if (!t) t = "UNKNOWN"; /* Be cautious... */
  4138. else if (*t == '\0')
  4139. t = "UNKNOWN";
  4140. ckstrncpy(pktnam,t,PKTNL); /* Copy stripped name literally. */
  4141. } else if (fnspath == PATH_ABS && !notafile) {
  4142. /* Converting to absolute form */
  4143. zfnqfp(filnam,PKTNL,pktnam);
  4144. } else
  4145. ckstrncpy(pktnam,filnam,PKTNL);
  4146. /* pktnam[] has the packet name, filnam[] has the original name. */
  4147. /* But we still need to convert pktnam if FILE NAMES CONVERTED. */
  4148. debug(F101,"sfile fncnv","",fncnv);
  4149. if (fncnv && !notafile) { /* If converting names of files */
  4150. zltor(pktnam,(char *)srvcmd); /* convert it to common form, */
  4151. ckstrncpy(pktnam,(char *)srvcmd,PKTNL);
  4152. *srvcmd = NUL;
  4153. }
  4154. #endif /* NZLTOR */
  4155. }
  4156. if (!*pktnam) /* Failsafe... */
  4157. sprintf(pktnam,"FILE%02ld",filcnt);
  4158. debug(F110,"sfile filnam 1",filnam,0);
  4159. debug(F110,"sfile pktnam 1",pktnam,0);
  4160. #ifdef PIPESEND
  4161. /* If we have a send filter, substitute the current filename into it */
  4162. if (sndfilter) {
  4163. char * p = NULL, * q;
  4164. int n = CKMAXPATH;
  4165. #ifndef NOSPL
  4166. if ((p = (char *) malloc(n + 1))) {
  4167. q = p;
  4168. debug(F110,"sfile pipesend filter",sndfilter,0);
  4169. zzstring(sndfilter,&p,&n);
  4170. debug(F111,"sfile pipename",q,n);
  4171. if (n <= 0) {
  4172. printf(
  4173. "?Sorry, send filter + filename too long, %d max.\n",
  4174. CKMAXPATH
  4175. );
  4176. free(q);
  4177. return(0);
  4178. }
  4179. ckstrncpy(filnam,q,CKMAXPATH+1);
  4180. free(q);
  4181. }
  4182. #endif /* NOSPL */
  4183. }
  4184. #endif /* PIPESEND */
  4185. debug(F110,"sfile filnam 2",filnam,0); /* Log debugging info */
  4186. debug(F110,"sfile pktnam 2",pktnam,0);
  4187. if (openi(filnam) == 0) /* Try to open the input file */
  4188. return(0);
  4189. #ifdef CK_RESEND
  4190. /*
  4191. The following check is done after openi() is called, since openi() itself
  4192. can change the transfer mode (as in VMS).
  4193. */
  4194. if ((binary == XYFT_T
  4195. #ifdef VMS
  4196. || binary == XYFT_L
  4197. #endif /* VMS */
  4198. ) && sendmode == SM_RESEND) {
  4199. /* Trying to RESEND/REGET a file first sent in TEXT mode. */
  4200. debug(F111,"sfile error - Recover vs Text",filnam,binary);
  4201. /* Set appropriate error messages and make log entries here */
  4202. #ifdef VMS
  4203. if (binary == XYFT_L)
  4204. ckmakmsg((char *)epktmsg,
  4205. PKTMSGLEN,
  4206. "Recovery attempted in LABELED mode: ",
  4207. filnam,
  4208. NULL,
  4209. NULL
  4210. );
  4211. else
  4212. #endif /* VMS */
  4213. ckmakmsg((char *)epktmsg,
  4214. PKTMSGLEN,
  4215. "Recovery attempted in TEXT mode: ",
  4216. filnam,
  4217. NULL,
  4218. NULL
  4219. );
  4220. return(0);
  4221. }
  4222. if (sendmode == SM_PSEND) /* PSENDing? */
  4223. if (sendstart > (CK_OFF_T)0) /* Starting position */
  4224. if (zfseek(sendstart) < 0) /* seek to it... */
  4225. return(0);
  4226. #endif /* CK_RESEND */
  4227. s = pktnam; /* Name for packet data field */
  4228. #ifdef OS2
  4229. /* Never send a disk letter. */
  4230. if (isalpha(*s) && (*(s+1) == ':'))
  4231. s += 2;
  4232. #endif /* OS2 */
  4233. } else { /* X-packet setup, not F-packet. */
  4234. binary = XYFT_T; /* Text always */
  4235. debug(F110,"sfile X packet",cmdstr,0); /* Log debugging info */
  4236. s = cmdstr; /* Name for data field */
  4237. }
  4238. /* Now s points to the string that goes in the packet data field. */
  4239. debug(F101,"sfile binary","",binary); /* Log debugging info */
  4240. encstr((CHAR *)s); /* Encode the name. */
  4241. /* Send the F or X packet */
  4242. /* If the encoded string did not fit into the packet, it was truncated. */
  4243. if (nxtpkt() < 0) return(0); /* Bump packet number, get buffer */
  4244. rc = spack((char) (x ? 'X' : 'F'), pktnum, size, data);
  4245. if (rc < 0)
  4246. return(rc);
  4247. #ifndef NOCSETS
  4248. setxlatype(tcharset,fcharset); /* Set up charset translations */
  4249. #endif /* NOCSETS */
  4250. if (x == 0) { /* Display for F packet */
  4251. if (displa) { /* Screen */
  4252. xxscreen(SCR_FN,'F',(long)pktnum,filnam);
  4253. xxscreen(SCR_AN,0,0L,pktnam);
  4254. xxscreen(SCR_FS,0,calibrate ? calibrate : fsize,"");
  4255. }
  4256. #ifdef pdp11
  4257. tlog(F110,"Sending",filnam,0L); /* Transaction log entry */
  4258. makestr(&psfspec,filnam); /* New filename */
  4259. #else
  4260. #ifndef ZFNQFP
  4261. tlog(F110,"Sending",filnam,0L);
  4262. makestr(&psfspec,filnam); /* New filename */
  4263. #else
  4264. if (notafile) { /* If not a file log simple name */
  4265. tlog(F110,"Sending", filnam, 0L);
  4266. } else { /* Log fully qualified filename */
  4267. #ifdef COMMENT
  4268. /* This section generates bad code in SCO 3.2v5.0.5's cc */
  4269. char *p = NULL, *q = filnam;
  4270. debug(F101,"sfile CKMAXPATH","",CKMAXPATH);
  4271. if ((p = malloc(CKMAXPATH+1))) {
  4272. debug(F111,"sfile calling zfnqfp",filnam,strlen(filnam));
  4273. if (zfnqfp(filnam, CKMAXPATH, p)) {
  4274. debug(F111,"sfile zfnqfp ok",p,strlen(p));
  4275. q = p;
  4276. }
  4277. }
  4278. #else
  4279. char tmpbuf[CKMAXPATH+1];
  4280. char *p = tmpbuf, *q = filnam;
  4281. if (zfnqfp(filnam, CKMAXPATH, p))
  4282. q = p;
  4283. #endif /* COMMENT */
  4284. debug(F111,"sfile q",q,strlen(q));
  4285. tlog(F110,"Sending",q,0L);
  4286. makestr(&psfspec,q); /* New preliminary filename */
  4287. #ifdef COMMENT
  4288. if (p) free(p);
  4289. #endif /* COMMENT */
  4290. }
  4291. #endif /* ZFNQFP */
  4292. #endif /* pdp11 */
  4293. tlog(F110," as",pktnam,0L);
  4294. if (binary) { /* Log file mode in transaction log */
  4295. tlog(F101," mode: binary","",(long) binary);
  4296. } else { /* If text mode, check character set */
  4297. tlog(F100," mode: text","",0L);
  4298. #ifndef NOCSETS
  4299. if (tcharset == TC_TRANSP || xfrxla == 0) {
  4300. tlog(F110," character set","transparent",0L);
  4301. } else {
  4302. tlog(F110," xfer character set",tcsinfo[tcharset].name,0L);
  4303. tlog(F110," file character set",fcsinfo[fcharset].name,0L);
  4304. }
  4305. #endif /* NOCSETS */
  4306. }
  4307. } else { /* Display for X-packet */
  4308. xxscreen(SCR_XD,'X',(long)pktnum,cmdstr); /* Screen */
  4309. tlog(F110,"Sending from:",cmdstr,0L); /* Transaction log */
  4310. }
  4311. intmsg(++filcnt); /* Count file, give interrupt msg */
  4312. first = 1; /* Init file character lookahead. */
  4313. ffc = (CK_OFF_T)0; /* Init file character counter. */
  4314. cps = oldcps = 0L; /* Init cps statistics */
  4315. rejection = -1;
  4316. fsecs = gtimer(); /* Time this file started */
  4317. #ifdef GFTIMER
  4318. fpfsecs = gftimer();
  4319. debug(F101,"SFILE fpfsecs","",fpfsecs);
  4320. #endif /* GFTIMER */
  4321. debug(F101,"SFILE fsecs","",fsecs);
  4322. return(1);
  4323. }
  4324. /* S D A T A -- Send a data packet */
  4325. /*
  4326. Returns -1 if no data to send (end of file), -2 if connection is broken.
  4327. If there is data, a data packet is sent, and sdata() returns 1.
  4328. In the streaming case, the window is regarded as infinite and we keep
  4329. sending data packets until EOF or there appears to be a Kermit packet on the
  4330. reverse channel. When not streaming and the window size is greater than 1,
  4331. we keep sending data packets until window is full or characters start to
  4332. appear from the other Kermit.
  4333. In the windowing or streaming case, when there is no more data left to send
  4334. (or when sending has been interrupted), sdata() does nothing and returns 0
  4335. each time it is called until the acknowledgement sequence number catches up
  4336. to the last data packet that was sent.
  4337. */
  4338. int
  4339. sdata() {
  4340. int i, x, len;
  4341. char * s;
  4342. debug(F101,"sdata entry, first","",first);
  4343. debug(F101,"sdata drain","",drain);
  4344. /*
  4345. The "drain" flag is used with window size > 1. It means we have sent
  4346. our last data packet. If called and drain is not zero, then we return
  4347. 0 as if we had sent an empty data packet, until all data packets have
  4348. been ACK'd, then then we can finally return -1 indicating EOF, so that
  4349. the protocol can switch to seof state. This is a kludge, but at least
  4350. it's localized...
  4351. */
  4352. if (first == 1) drain = 0; /* Start of file, init drain flag. */
  4353. if (drain) { /* If draining... */
  4354. debug(F101,"sdata draining, winlo","",winlo);
  4355. if (winlo == pktnum) /* If all data packets are ACK'd */
  4356. return(-1); /* return EOF indication */
  4357. else /* otherwise */
  4358. return(0); /* pretend we sent a data packet. */
  4359. }
  4360. debug(F101,"sdata sbufnum","",sbufnum);
  4361. for (i = sbufnum;
  4362. i > 0
  4363. #ifdef STREAMING
  4364. || streaming
  4365. #endif /* STREAMING */
  4366. ;
  4367. i--) {
  4368. if (i < 0)
  4369. break;
  4370. debug(F101,"sdata countdown","",i);
  4371. #ifdef STREAMING
  4372. if (streaming) {
  4373. pktnum = (pktnum + 1) % 64;
  4374. winlo = pktnum;
  4375. debug(F101,"sdata streaming pktnum","",pktnum);
  4376. } else {
  4377. #endif /* STREAMING */
  4378. x = nxtpkt(); /* Get next pkt number and buffer */
  4379. debug(F101,"sdata nxtpkt pktnum","",pktnum);
  4380. if (x < 0) return(0);
  4381. #ifdef STREAMING
  4382. }
  4383. #endif /* STREAMING */
  4384. debug(F101,"sdata packet","",pktnum);
  4385. if (chkint() < 0) /* Especially important if streaming */
  4386. return(-9);
  4387. if (cxseen || czseen) { /* If interrupted, done. */
  4388. if (wslots > 1) {
  4389. drain = 1;
  4390. debug(F100,"sdata cx/zseen windowing","",0);
  4391. return(0);
  4392. } else {
  4393. debug(F100,"sdata cx/zseen nonwindowing","",0);
  4394. return(-1);
  4395. }
  4396. }
  4397. #ifdef DEBUG
  4398. if (deblog) {
  4399. debug(F101,"sdata spsiz","",spsiz);
  4400. debug(F101,"sdata binary","",binary);
  4401. debug(F101,"sdata parity","",parity);
  4402. }
  4403. #endif /* DEBUG */
  4404. #ifdef CKTUNING
  4405. if (binary && !parity && !memstr && !funcstr)
  4406. len = bgetpkt(spsiz);
  4407. else
  4408. len = getpkt(spsiz,1);
  4409. #else
  4410. len = getpkt(spsiz,1);
  4411. #endif /* CKTUNING */
  4412. s = (char *)data;
  4413. if (len == -3) { /* Timed out (e.g.reading from pipe) */
  4414. s = ""; /* Send an empty data packet. */
  4415. len = 0;
  4416. } else if (len == 0) { /* Done if no data. */
  4417. if (pktnum == winlo)
  4418. return(-1);
  4419. drain = 1; /* But can't return -1 until all */
  4420. debug(F101,"sdata eof, drain","",drain);
  4421. return(0); /* ACKs are drained. */
  4422. }
  4423. debug(F101,"sdata pktnum","",pktnum);
  4424. debug(F101,"sdata len","",len);
  4425. debug(F011,"sdata data",data,52);
  4426. x = spack('D',pktnum,len,(CHAR *)s); /* Send the data packet. */
  4427. debug(F101,"sdata spack","",x);
  4428. if (x < 0) { /* Error */
  4429. ttchk(); /* See if connection is still there */
  4430. return(-2);
  4431. }
  4432. #ifdef STREAMING
  4433. if (streaming) /* What an ACK would do. */
  4434. winlo = pktnum;
  4435. #endif /* STREAMING */
  4436. x = ttchk(); /* Peek at input buffer. */
  4437. debug(F101,"sdata ttchk","",x); /* ACKs waiting, maybe? */
  4438. if (x < 0) /* Or connection broken? */
  4439. return(-2);
  4440. /*
  4441. Here we check to see if any ACKs or NAKs have arrived, in which case we
  4442. break out of the D-packet-sending loop and return to the state switcher
  4443. to process them. This is what makes our windows slide instead of lurch.
  4444. */
  4445. if (
  4446. #ifdef GEMDOS
  4447. /*
  4448. In the Atari ST version, ttchk() can only return 0 or 1. But note: x will
  4449. probably always be > 0, since the as-yet-unread packet terminator from the
  4450. last packet is probably still in the buffer, so sliding windows will
  4451. probably never happen when the Atari ST is the file sender. The alternative
  4452. is to say "if (0)", in which case the ST will always send a window full of
  4453. packets before reading any ACKs or NAKs.
  4454. */
  4455. x > 0
  4456. #else /* !GEMDOS */
  4457. /*
  4458. In most other versions, ttchk() returns the actual count.
  4459. It can't be a Kermit packet if it's less than five bytes long.
  4460. */
  4461. x > 4 + bctu
  4462. #endif /* GEMDOS */
  4463. )
  4464. return(1); /* Yes, stop sending data packets */
  4465. } /* and go try to read the ACKs. */
  4466. return(1);
  4467. }
  4468. /* S E O F -- Send an End-Of-File packet */
  4469. /* Call with a string pointer to character to put in the data field, */
  4470. /* or else a null pointer or "" for no data. */
  4471. /*
  4472. There are two "send-eof" functions. seof() is used to send the normal eof
  4473. packet at the end of a file's data (even if the file has no data), or when
  4474. a file transfer is interrupted. sxeof() is used to send an EOF packet that
  4475. occurs because of attribute refusal or interruption prior to entering data
  4476. state. The difference is purely a matter of buffer allocation and packet
  4477. sequence number management. Both functions act as "front ends" to the
  4478. common send-eof function, szeof().
  4479. */
  4480. /* Code common to both seof() and sxeof() */
  4481. int
  4482. szeof(s) CHAR *s; {
  4483. int x;
  4484. lsstate = 0; /* Cancel locking-shift state */
  4485. if (!s) s = (CHAR *)"";
  4486. debug(F111,"szeof",s,pktnum);
  4487. if (*s) {
  4488. x = spack('Z',pktnum,1,s);
  4489. xitsta |= W_SEND;
  4490. #ifdef COMMENT
  4491. tlog(F100," *** interrupted, sending discard request","",0L);
  4492. #endif /* COMMENT */
  4493. filrej++;
  4494. } else {
  4495. x = spack('Z',pktnum,0,(CHAR *)"");
  4496. }
  4497. if (x < 0)
  4498. return(x);
  4499. #ifdef COMMENT
  4500. /* No, too soon */
  4501. discard = 0; /* Turn off per-file discard flag */
  4502. #endif /* COMMENT */
  4503. /* If we were sending from a pipe, we're not any more... */
  4504. pipesend = 0;
  4505. return(0);
  4506. }
  4507. int
  4508. seof(x) int x; {
  4509. char * s;
  4510. /*
  4511. ckcpro.w, before calling seof(), sets window size back to 1 and then calls
  4512. window(), which clears out the old buffers. This is OK because the final
  4513. data packet for the file has been ACK'd. However, sdata() has already
  4514. called nxtpkt(), which set the new value of pktnum which seof() will use.
  4515. So all we need to do here is is allocate a new send-buffer.
  4516. */
  4517. s = x ? "D" : "";
  4518. debug(F111,"seof",s,pktnum);
  4519. if (getsbuf(pktnum) < 0) { /* Get a buffer for packet n */
  4520. debug(F101,"seof can't get s-buffer","",pktnum);
  4521. return(-1);
  4522. }
  4523. return(szeof((CHAR *)s));
  4524. }
  4525. /*
  4526. Version of seof() to be called when sdata() has not been called before. The
  4527. difference is that this version calls nxtpkt() to allocate a send-buffer and
  4528. get the next packet number.
  4529. */
  4530. int
  4531. sxeof(x) int x; {
  4532. char * s;
  4533. s = x ? "D" : "";
  4534. if (nxtpkt() < 0) /* Get next pkt number and buffer */
  4535. debug(F101,"sxeof nxtpkt fails","",pktnum);
  4536. else
  4537. debug(F101,"sxeof packet","",pktnum);
  4538. return(szeof((CHAR *)s));
  4539. }
  4540. /* S E O T -- Send an End-Of-Transaction packet */
  4541. int
  4542. seot() {
  4543. int x;
  4544. x = nxtpkt();
  4545. debug(F101,"seot nxtpkt","",x);
  4546. if (x < 0) return(-1); /* Bump packet number, get buffer */
  4547. x = spack('B',pktnum,0,(CHAR *)""); /* Send the EOT packet */
  4548. if (x < 0)
  4549. return(x);
  4550. cxseen = czseen = discard = 0; /* Reset interruption flags */
  4551. tstats(); /* Log timing info */
  4552. return(0);
  4553. }
  4554. /* R P A R -- Fill the data array with my send-init parameters */
  4555. int q8flag = 0;
  4556. CHAR dada[32]; /* Use this instead of data[]. */
  4557. /* To avoid some kind of wierd */
  4558. /* addressing foulup in spack()... */
  4559. /* (which might be fixed now...) */
  4560. CHAR *
  4561. rpar() {
  4562. char *p;
  4563. int i, x, max;
  4564. extern int sprmlen;
  4565. max = maxdata(); /* Biggest data field I can send */
  4566. debug(F101, "rpar max 1","",max);
  4567. debug(F101, "rpar sprmlen","",sprmlen);
  4568. if (sprmlen > 1 && sprmlen < max) /* User override */
  4569. max = sprmlen;
  4570. debug(F101, "rpar max 2","",max);
  4571. if (rpsiz > MAXPACK) /* Biggest normal packet I want. */
  4572. dada[0] = (char) tochar(MAXPACK); /* If > 94, use 94, but specify */
  4573. else /* extended packet length below... */
  4574. dada[0] = (char) tochar(rpsiz); /* else use what the user said. */
  4575. dada[1] = (char) tochar(chktimo(pkttim,0)); /* When to time me out */
  4576. dada[2] = (char) tochar(mypadn); /* How much padding I need (none) */
  4577. dada[3] = (char) ctl(mypadc); /* Padding character I want */
  4578. dada[4] = (char) tochar(eol); /* End-Of-Line character I want */
  4579. dada[5] = myctlq; /* Control-Quote character I send */
  4580. if (max < 6) { dada[6] = NUL; rqf = 0; ebq = sq = NUL; return(dada); }
  4581. switch (rqf) { /* 8th-bit prefix (single-shift) */
  4582. case -1: /* I'm opening the bids */
  4583. case 1: /* Other Kermit already bid 'Y' */
  4584. if (parity) ebq = sq = MYEBQ; /* So I reply with '&' if parity */
  4585. break; /* otherwise with 'Y'. */
  4586. case 2: /* Other Kermit sent a valid prefix */
  4587. if (q8flag)
  4588. sq = ebq; /* Fall through on purpose */
  4589. case 0: /* Other Kermit bid nothing */
  4590. break; /* So I reply with 'Y'. */
  4591. }
  4592. debug(F000,"rpar 8bq sq","",sq);
  4593. debug(F000,"rpar 8bq ebq","",ebq);
  4594. if (lscapu == 2) /* LOCKING-SHIFT FORCED */
  4595. dada[6] = 'N'; /* requires no single-shift */
  4596. else /* otherwise send prefix or 'Y' */
  4597. dada[6] = (char) sq;
  4598. ebqsent = dada[6]; /* And remember what I really sent */
  4599. if (max < 7) { dada[7] = NUL; bctr = 1; return(dada); }
  4600. dada[7] = (char) (bctr == 4) ? 'B' : bctr + '0'; /* Block check type */
  4601. if (max < 8) { dada[8] = NUL; rptflg = 0; return(dada); }
  4602. if (rptena) {
  4603. if (rptflg) /* Run length encoding */
  4604. dada[8] = (char) rptq; /* If receiving, agree */
  4605. else /* by replying with same character. */
  4606. dada[8] = (char) (rptq = myrptq); /* When sending use this. */
  4607. } else dada[8] = SP; /* Not enabled, put a space here. */
  4608. /* CAPAS mask */
  4609. if (max < 9) {
  4610. dada[9] = NUL;
  4611. atcapr = 0;
  4612. lpcapr = 0;
  4613. swcapr = 0;
  4614. rscapr = 0;
  4615. return(dada);
  4616. }
  4617. dada[9] = (char) tochar((lscapr ? lscapb : 0) | /* Locking shifts */
  4618. (atcapr ? atcapb : 0) | /* Attribute packets */
  4619. (lpcapr ? lpcapb : 0) | /* Long packets */
  4620. (swcapr ? swcapb : 0) | /* Sliding windows */
  4621. (rscapr ? rscapb : 0)); /* RESEND */
  4622. if (max < 10) { wslotr = 1; return(dada); }
  4623. dada[10] = (char) tochar(swcapr ? wslotr : 1); /* CAPAS+1 = Window size */
  4624. if (max < 12) { rpsiz = 80; return(dada); }
  4625. if (urpsiz > 94)
  4626. rpsiz = urpsiz - 1; /* Long packets ... */
  4627. dada[11] = (char) tochar(rpsiz / 95); /* Long packet size, big part */
  4628. dada[12] = (char) tochar(rpsiz % 95); /* Long packet size, little part */
  4629. if (max < 16) return(dada);
  4630. dada[13] = '0'; /* CAPAS+4 = WONT CHKPNT */
  4631. dada[14] = '_'; /* CAPAS+5 = CHKINT (reserved) */
  4632. dada[15] = '_'; /* CAPAS+6 = CHKINT (reserved) */
  4633. dada[16] = '_'; /* CAPAS+7 = CHKINT (reserved) */
  4634. if (max < 17) return(dada);
  4635. #ifndef WHATAMI
  4636. dada[17] = ' ';
  4637. #else
  4638. x = 0;
  4639. if (server) x |= WMI_SERVE; /* Whether I am a server */
  4640. if (binary) x |= WMI_FMODE; /* My file transfer mode is ... */
  4641. if (fncnv) x |= WMI_FNAME; /* My filename conversion is ... */
  4642. #ifdef STREAMING
  4643. if (streamrq == SET_ON)
  4644. x |= WMI_STREAM;
  4645. else if (streamrq == SET_AUTO && reliable == SET_ON)
  4646. x |= WMI_STREAM;
  4647. /*
  4648. Always offer to stream when in remote mode and STREAMING is AUTO
  4649. and RELIABLE is not OFF (i.e. is ON or AUTO).
  4650. */
  4651. else if (!local && streamrq == SET_AUTO && reliable != SET_OFF)
  4652. x |= WMI_STREAM;
  4653. #endif /* STREAMING */
  4654. #ifdef TCPSOCKET
  4655. if (clearrq == SET_ON)
  4656. x |= WMI_CLEAR;
  4657. else if (clearrq == SET_AUTO && /* SET CLEAR-CHANNEL AUTO */
  4658. ((network && nettype == NET_TCPB /* TCP/IP */
  4659. #ifdef RLOGCODE
  4660. && ttnproto != NP_RLOGIN/* Rlogin is not clear */
  4661. && !(ttnproto >= NP_K4LOGIN && ttnproto <= NP_EK5LOGIN)
  4662. #endif /* RLOGCODE */
  4663. )
  4664. #ifdef SSHBUILTIN
  4665. || (network && nettype == NET_SSH)
  4666. #endif /* SSHBUILTIN */
  4667. #ifdef IKSD
  4668. || inserver /* We are IKSD */
  4669. #endif /* IKSD */
  4670. ))
  4671. x |= WMI_CLEAR;
  4672. #endif /* TCPSOCKET */
  4673. x |= WMI_FLAG;
  4674. dada[17] = (char) tochar(x);
  4675. #endif /* WHATAMI */
  4676. i = 18; /* Position of next field */
  4677. p = cksysid; /* WHOAMI (my system ID) */
  4678. x = strlen(p);
  4679. if (max - i < x + 1) return(dada);
  4680. if (x > 0) {
  4681. dada[i++] = (char) tochar(x);
  4682. while (*p)
  4683. dada[i++] = *p++;
  4684. }
  4685. if (max < i+1) return(dada);
  4686. #ifndef WHATAMI /* WHATAMI2 */
  4687. dada[i++] = ' ';
  4688. #else
  4689. debug(F101,"rpar xfermode","",xfermode);
  4690. x = WMI2_FLAG; /* Is-Valid flag */
  4691. if (xfermode != XMODE_A) /* If TRANSFER MODE is MANUAL */
  4692. x |= WMI2_XMODE; /* set the XFERMODE bit */
  4693. if (recursive > 0) /* If this is a recursive transfer */
  4694. x |= WMI2_RECU; /* set the RECURSIVE bit */
  4695. dada[i++] = tochar(x);
  4696. debug(F101,"rpar whatami2","",x);
  4697. #endif /* WHATAMI */
  4698. dada[i] = '\0'; /* Terminate the init string */
  4699. #ifdef DEBUG
  4700. if (deblog) {
  4701. debug(F110,"rpar",dada,0);
  4702. rdebu(dada,(int)strlen((char *)dada));
  4703. }
  4704. #endif /* DEBUG */
  4705. ckstrncpy((char *)myinit,(char *)dada,MYINITLEN);
  4706. return(dada); /* Return pointer to string. */
  4707. }
  4708. int
  4709. spar(s) CHAR *s; { /* Set parameters */
  4710. int x, y, lpsiz, biggest;
  4711. extern int rprmlen, lastspmax;
  4712. extern struct sysdata sysidlist[];
  4713. whatru = 0;
  4714. whoareu[0] = NUL;
  4715. #ifdef STREAMING
  4716. streamok = 0;
  4717. streaming = 0;
  4718. #endif /* STREAMING */
  4719. biggest = rln;
  4720. debug(F101, "spar biggest 1","",biggest);
  4721. debug(F101, "spar rprmlen","",rprmlen);
  4722. if (rprmlen > 1 && rprmlen < biggest)
  4723. biggest = rprmlen;
  4724. debug(F101, "rpar biggest 2","",biggest);
  4725. debug(F110,"spar packet",s,0);
  4726. s--; /* Line up with field numbers. */
  4727. /* Limit on size of outbound packets */
  4728. x = (biggest >= 1) ? xunchar(s[1]) : 80;
  4729. lpsiz = spsizr; /* Remember what they SET. */
  4730. if (spsizf) { /* SET-command override? */
  4731. if (x < spsizr) spsiz = x; /* Ignore LEN unless smaller */
  4732. } else { /* otherwise */
  4733. spsiz = (x < 10) ? 80 : x; /* believe them if reasonable */
  4734. }
  4735. spmax = spsiz; /* Remember maximum size */
  4736. /* Timeout on inbound packets */
  4737. if (timef) {
  4738. timint = rtimo; /* SET SEND TIMEOUT value overrides */
  4739. } else { /* Otherwise use requested value, */
  4740. x = (biggest >= 2) ? xunchar(s[2]) : rtimo; /* if it is legal. */
  4741. timint = (x < 0) ? rtimo : x;
  4742. }
  4743. timint = chktimo(timint,timef); /* Adjust if necessary */
  4744. /* Outbound Padding */
  4745. npad = 0; padch = '\0';
  4746. if (biggest >= 3) {
  4747. npad = xunchar(s[3]);
  4748. if (biggest >= 4) padch = (CHAR) ctl(s[4]); else padch = 0;
  4749. }
  4750. if (npad) {
  4751. int i;
  4752. for (i = 0; i < npad; i++) padbuf[i] = dopar(padch);
  4753. }
  4754. /* Outbound Packet Terminator */
  4755. seol = (CHAR) (biggest >= 5) ? xunchar(s[5]) : CR;
  4756. if ((seol < 1) || (seol > 31)) seol = CR;
  4757. /* Control prefix that the other Kermit is sending */
  4758. x = (biggest >= 6) ? s[6] : '#';
  4759. ctlq = (CHAR) (((x > 32 && x < 63) || (x > 95 && x < 127)) ? x : '#');
  4760. /* 8th-bit prefix */
  4761. /*
  4762. NOTE: Maybe this could be simplified using rcvtyp.
  4763. If rcvtyp == 'Y' then we're reading the ACK,
  4764. otherwise we're reading the other Kermit's initial bid.
  4765. But his horrendous code has been working OK for years, so...
  4766. */
  4767. rq = (biggest >= 7) ? s[7] : 0;
  4768. if (rq == 'Y') rqf = 1;
  4769. else if ((rq > 32 && rq < 63) || (rq > 95 && rq < 127)) rqf = 2;
  4770. else rqf = 0;
  4771. debug(F000,"spar 8bq rq","",rq);
  4772. debug(F000,"spar 8bq sq","",sq);
  4773. debug(F000,"spar 8bq ebq","",ebq);
  4774. debug(F101,"spar 8bq rqf","",rqf);
  4775. switch (rqf) {
  4776. case 0: /* Field is missing from packet. */
  4777. ebqflg = 0; /* So no 8th-bit prefixing. */
  4778. break;
  4779. case 1: /* Other Kermit sent 'Y' = Will Do. */
  4780. /*
  4781. When I am the file receiver, ebqsent is 0 because I didn't send a
  4782. negotiation yet. If my parity is set to anything other than NONE,
  4783. either because my user SET PARITY or because I detected parity bits
  4784. on this packet, I reply with '&', otherwise 'Y'.
  4785. When I am the file sender, ebqsent is what I just sent in rpar(),
  4786. which can be 'Y', 'N', or '&'. If I sent '&', then this 'Y' means
  4787. the other Kermit agrees to do 8th-bit prefixing.
  4788. If I sent 'Y' or 'N', but then detected parity on the ACK packet
  4789. that came back, then it's too late: there is no longer any way for
  4790. me to tell the other Kermit that I want to do 8th-bit prefixing, so
  4791. I must not do it, and in that case, if there is any 8-bit data in
  4792. the file to be transferred, the transfer will fail because of block
  4793. check errors.
  4794. The following clause covers all of these situations:
  4795. */
  4796. if (parity && (ebqsent == 0 || ebqsent == '&')) {
  4797. ebqflg = 1;
  4798. ebq = MYEBQ;
  4799. }
  4800. break;
  4801. case 2: /* Other Kermit sent a valid prefix */
  4802. ebqflg = (ebq == sq || sq == 'Y');
  4803. if (ebqflg) {
  4804. ebq = rq;
  4805. debug(F101,"spar setting parity to space","",ebq);
  4806. if (!parity) parity = ttprty = 's';
  4807. }
  4808. }
  4809. if (lscapu == 2) { /* But no single-shifts if LOCKING-SHIFT FORCED */
  4810. ebqflg = 0;
  4811. ebq = 'N';
  4812. }
  4813. /* Block check */
  4814. x = 1;
  4815. if (biggest >= 8) {
  4816. if (s[8] == 'B') x = 4;
  4817. else x = s[8] - '0';
  4818. if ((x < 1) || (x > 5)) x = 1; /* "5" 20110605 */
  4819. }
  4820. bctr = x;
  4821. /* Repeat prefix */
  4822. rptflg = 0; /* Assume no repeat-counts */
  4823. if (biggest >= 9) { /* Is there a repeat-count field? */
  4824. char t; /* Yes. */
  4825. t = s[9]; /* Get its contents. */
  4826. /*
  4827. If I'm sending files, then I'm reading these parameters from an ACK, and so
  4828. this character must agree with what I sent.
  4829. */
  4830. if (rptena) { /* If enabled ... */
  4831. if ((char) rcvtyp == 'Y') { /* Sending files, reading ACK. */
  4832. if (t == myrptq) rptflg = 1;
  4833. } else { /* I'm receiving files */
  4834. if ((t > 32 && t < 63) || (t > 95 && t < 127)) {
  4835. rptflg = 1;
  4836. rptq = t;
  4837. }
  4838. }
  4839. } else rptflg = 0;
  4840. }
  4841. /* Capabilities */
  4842. atcapu = lpcapu = swcapu = rscapu = 0; /* Assume none of these. */
  4843. if (lscapu != 2) lscapu = 0; /* Assume no LS unless forced. */
  4844. y = 11; /* Position of next field, if any */
  4845. if (biggest >= 10) {
  4846. x = xunchar(s[10]);
  4847. debug(F101,"spar capas","",x);
  4848. atcapu = (x & atcapb) && atcapr; /* Attributes */
  4849. lpcapu = (x & lpcapb) && lpcapr; /* Long packets */
  4850. swcapu = (x & swcapb) && swcapr; /* Sliding windows */
  4851. rscapu = (x & rscapb) && rscapr; /* RESEND */
  4852. debug(F101,"spar lscapu","",lscapu);
  4853. debug(F101,"spar lscapr","",lscapr);
  4854. debug(F101,"spar ebqflg","",ebqflg);
  4855. if (lscapu != 2) lscapu = ((x & lscapb) && lscapr && ebqflg) ? 1 : 0;
  4856. debug(F101,"spar swcapr","",swcapr);
  4857. debug(F101,"spar swcapu","",swcapu);
  4858. debug(F101,"spar lscapu","",lscapu);
  4859. for (y = 10; (xunchar(s[y]) & 1) && (biggest >= y); y++);
  4860. debug(F101,"spar y","",y);
  4861. }
  4862. /* Long Packets */
  4863. debug(F101,"spar lpcapu","",lpcapu);
  4864. if (lpcapu) {
  4865. if (biggest > y+1) {
  4866. x = xunchar(s[y+2]) * 95 + xunchar(s[y+3]);
  4867. debug(F101,"spar lp len","",x);
  4868. if (spsizf) { /* If overriding negotiations */
  4869. spsiz = (x < lpsiz) ? x : lpsiz; /* do this, */
  4870. } else { /* otherwise */
  4871. spsiz = (x > MAXSP) ? MAXSP : x; /* do this. */
  4872. }
  4873. if (spsiz < 10) spsiz = 80; /* Be defensive... */
  4874. }
  4875. }
  4876. /* (PWP) save current send packet size for optimal packet size calcs */
  4877. spmax = spsiz; /* Maximum negotiated length */
  4878. lastspmax = spsiz; /* For stats */
  4879. if (slostart && spsiz > 499) /* Slow start length */
  4880. spsiz = 244;
  4881. debug(F101,"spar slow-start spsiz","",spsiz);
  4882. debug(F101,"spar lp spmax","",spmax);
  4883. timint = chktimo(timint,timef); /* Recalculate the packet timeout */
  4884. /* Sliding Windows... */
  4885. if (swcapr) { /* Only if requested... */
  4886. if (biggest > y) { /* See what other Kermit says */
  4887. x = xunchar(s[y+1]);
  4888. debug(F101,"spar window","",x);
  4889. wslotn = (x > MAXWS) ? MAXWS : x;
  4890. /*
  4891. wslotn = negotiated size (from other Kermit's S or I packet).
  4892. wslotr = requested window size (from this Kermit's SET WINDOW command).
  4893. */
  4894. if (wslotn > wslotr) /* Use the smaller of the two */
  4895. wslotn = wslotr;
  4896. if (wslotn < 1) /* Watch out for bad negotiation */
  4897. wslotn = 1;
  4898. if (wslotn > 1) {
  4899. swcapu = 1; /* We do windows... */
  4900. if (wslotn > maxtry) /* Retry limit must be greater */
  4901. maxtry = wslotn + 1; /* than window size. */
  4902. }
  4903. debug(F101,"spar window after adjustment","",x);
  4904. } else { /* No window size specified. */
  4905. wslotn = 1; /* We don't do windows... */
  4906. debug(F101,"spar window","",x);
  4907. swcapu = 0;
  4908. debug(F101,"spar no windows","",wslotn);
  4909. }
  4910. }
  4911. /* Now recalculate packet length based on number of windows. */
  4912. /* The nogotiated number of window slots will be allocated, */
  4913. /* and the maximum packet length will be reduced if necessary, */
  4914. /* so that a windowful of packets can fit in the big buffer. */
  4915. if (wslotn > 1) { /* Shrink to fit... */
  4916. x = adjpkl(spmax,wslotn,bigsbsiz);
  4917. if (x < spmax) {
  4918. spmax = x;
  4919. lastspmax = spsiz;
  4920. if (slostart && spsiz > 499) spsiz = 244; /* Slow start again */
  4921. debug(F101,"spar sending, redefine spmax","",spmax);
  4922. }
  4923. }
  4924. #ifdef WHATAMI
  4925. debug(F101,"spar biggest","",biggest);
  4926. if (biggest > y+7) { /* Get WHATAMI info if any */
  4927. whatru = xunchar(s[y+8]);
  4928. debug(F101,"spar whatru","",whatru);
  4929. }
  4930. if (whatru & WMI_FLAG) { /* Only valid if this bit is set */
  4931. #ifdef STREAMING
  4932. if (whatru & WMI_STREAM) {
  4933. if (streamrq == SET_ON ||
  4934. (streamrq == SET_AUTO &&
  4935. (reliable == SET_ON || (reliable == SET_AUTO && !local)
  4936. #ifdef TN_COMPORT
  4937. && !istncomport()
  4938. #endif /* TN_COMPORT */
  4939. #ifdef IKSD
  4940. || inserver
  4941. #endif /* IKSD */
  4942. ))) {
  4943. streamok = 1; /* Streaming negotiated */
  4944. slostart = 0; /* Undo slow-start machinations */
  4945. spsiz = lastspmax;
  4946. }
  4947. }
  4948. streamed = streamok;
  4949. debug(F101,"spar streamok","",streamok);
  4950. debug(F101,"spar clearrq","",clearrq);
  4951. if (clearrq == SET_ON ||
  4952. (clearrq == SET_AUTO &&
  4953. ((network && nettype == NET_TCPB
  4954. #ifdef RLOGCODE
  4955. && ttnproto != NP_RLOGIN/* Rlogin is not clear */
  4956. && !(ttnproto >= NP_K4LOGIN && ttnproto <= NP_EK5LOGIN)
  4957. #endif /* RLOGCODE */
  4958. #ifdef TN_COMPORT
  4959. && !istncomport()
  4960. #endif /* TN_COMPORT */
  4961. )
  4962. #ifdef SSHBUILTIN
  4963. || (network && nettype == NET_SSH)
  4964. #endif /* SSHBUILTIN */
  4965. #ifdef IKSD
  4966. || inserver
  4967. #endif /* IKSD */
  4968. )))
  4969. urclear = (whatru & WMI_CLEAR);
  4970. debug(F101,"spar urclear","",urclear);
  4971. #ifdef CK_SPEED
  4972. if (urclear)
  4973. setprefix(PX_NON);
  4974. #endif /* CK_SPEED */
  4975. cleared = urclear;
  4976. #endif /* STREAMING */
  4977. }
  4978. #endif /* WHATAMI */
  4979. if (biggest > y+8) { /* Get WHOAREYOU info if any */
  4980. int x, z;
  4981. x = xunchar(s[y+9]); /* Length of it */
  4982. z = y;
  4983. y += (9 + x);
  4984. debug(F101,"spar sysindex x","",x);
  4985. debug(F101,"spar sysindex y","",y);
  4986. debug(F101,"spar sysindex biggest","",biggest);
  4987. if (x > 0 && x < 16 && biggest >= y) {
  4988. strncpy(whoareu,(char *)s+z+10,x); /* Other Kermit's system ID */
  4989. debug(F111,"spar whoareyou",whoareu,whoareu[0]);
  4990. if (whoareu[0]) { /* Got one? */
  4991. sysindex = getsysix((char *)whoareu);
  4992. debug(F101,"spar sysindex",whoareu,sysindex);
  4993. }
  4994. }
  4995. } else
  4996. goto xspar;
  4997. #ifdef WHATAMI
  4998. y++; /* Advance pointer */
  4999. if (biggest >= y) {
  5000. whatru2 = xunchar(s[y]); /* Next field is WHATAMI2 */
  5001. debug(F101,"spar whatru2","",whatru2);
  5002. if (whatru2 & WMI2_FLAG) { /* Valid only if this bit is set */
  5003. if (server) { /* Server obeys client's xfer mode */
  5004. xfermode = (whatru2 & WMI2_XMODE) ? XMODE_M : XMODE_A;
  5005. debug(F101,"spar whatru2 xfermode","",xfermode);
  5006. }
  5007. if (whatru2 & WMI2_RECU) { /* RECURSIVE transfer */
  5008. if (fnrpath == PATH_AUTO) { /* If REC PATH AUTO */
  5009. fnrpath = PATH_REL; /* Set it to RELATIVE */
  5010. autopath = 1; /* and remember we did this */
  5011. }
  5012. }
  5013. }
  5014. }
  5015. #endif /* WHATAMI */
  5016. xspar:
  5017. if (sysindex > -1) {
  5018. char * p;
  5019. p = sysidlist[sysindex].sid_name;
  5020. tlog(F110,"Remote system type: ",p,0L);
  5021. if (sysindex > 0) { /* If partnet's system type known */
  5022. whoarewe(); /* see if we are a match. */
  5023. #ifdef CK_SPEED
  5024. /* Never unprefix XON and XOFF when sending to VMS */
  5025. debug(F111,"proto whoareu",whoareu,sysindex);
  5026. if (!strcmp((char *)whoareu,"D7")) {
  5027. debug(F111,"proto special VMS prefixing","",0);
  5028. ctlp[XON] = ctlp[XOFF] = 1;
  5029. ctlp[XON+128] = ctlp[XOFF+128] = 1;
  5030. ctlp[3] = 1; /* Ctrl-C might be dangerous too */
  5031. ctlp[14] = ctlp[15] = 1; /* And SO/SI */
  5032. ctlp[24] = ctlp[25] = 1; /* And ^X/^Y */
  5033. ctlp[141] = 1; /* And CR+128 */
  5034. }
  5035. #endif /* CK_SPEED */
  5036. }
  5037. }
  5038. /* Record parameters in debug log */
  5039. #ifdef DEBUG
  5040. if (deblog) sdebu(biggest);
  5041. #endif /* DEBUG */
  5042. numerrs = 0; /* Start counting errors here. */
  5043. return(0);
  5044. }
  5045. /* G N F I L E -- Get name of next file to send */
  5046. /*
  5047. Expects global sndsrc to be:
  5048. -9: if we are generating a file internally for calibration.
  5049. -1: next filename to be obtained by calling znext().
  5050. 0: no next file name
  5051. 1: (or greater) next filename to be obtained from **cmlist,
  5052. or if addlist != 0, from the "filehead" linked list,
  5053. or if filefile pointer not null from that file (which is already open).
  5054. Returns:
  5055. 1, with name of next file in filnam.
  5056. 0, no more files, with filnam set to empty string.
  5057. -1, file not found
  5058. -2, file is not readable (but then we just skip to the next one if any)
  5059. -3, read access denied
  5060. -4, cancelled
  5061. -5, too many files match wildcard
  5062. -6, no files selected
  5063. NOTE:
  5064. If gnfile() returns 0, then the global variable gnferror should be checked
  5065. to find out the most recent gnfile() error, and use that instead of the
  5066. return code (for reasons too hard to explain).
  5067. */
  5068. int
  5069. gnfile() {
  5070. int i = 0, x = 0;
  5071. CK_OFF_T filesize = 0;
  5072. int dodirstoo = 0;
  5073. int retcode = 0;
  5074. char fullname[CKMAXPATH+1];
  5075. dodirstoo = ((what & (W_FTP|W_SEND)) == (W_FTP|W_SEND)) && recursive;
  5076. debug(F101,"gnfile sndsrc","",sndsrc);
  5077. debug(F101,"gnfile filcnt","",filcnt);
  5078. debug(F101,"gnfile what","",what);
  5079. debug(F101,"gnfile recursive","",recursive);
  5080. debug(F101,"gnfile dodirstoo","",dodirstoo);
  5081. gnferror = 0;
  5082. fsize = (CK_OFF_T)-1; /* Initialize file size */
  5083. fullname[0] = NUL;
  5084. #ifdef VMS
  5085. /*
  5086. In VMS, zopeni() sets binary 0/1 automatically from the file
  5087. attributes. Don't undo it here.
  5088. */
  5089. debug(F101,"gnfile VMS binary","",binary);
  5090. #else /* VMS */
  5091. if (!(what & W_REMO) && (xfermode == XMODE_A)
  5092. #ifndef NOMSEND
  5093. && !addlist
  5094. #endif /* NOMSEND */
  5095. ) {
  5096. extern int stdinf;
  5097. if (!stdinf) /* Not if sending from stdin */
  5098. #ifdef WHATAMI
  5099. /* We don't do this in server mode because it undoes WHATAMI */
  5100. if (!server || (server && ((whatru & WMI_FLAG) == 0)))
  5101. #endif /* WHATAMI */
  5102. binary = gnf_binary; /* Restore prevailing transfer mode */
  5103. debug(F101,"gnfile binary = gnf_binary","",gnf_binary);
  5104. }
  5105. #endif /* VMS */
  5106. #ifdef PIPESEND
  5107. debug(F101,"gnfile pipesend","",pipesend);
  5108. if (pipesend) { /* First one */
  5109. if (filcnt == 0) {
  5110. ckstrncpy(filnam,cmarg,CKMAXPATH+1);
  5111. return(1);
  5112. } else { /* There's only one... */
  5113. *filnam = NUL;
  5114. pipesend = 0;
  5115. return(0);
  5116. }
  5117. }
  5118. #endif /* PIPESEND */
  5119. #ifdef CALIBRATE
  5120. if (sndsrc == -9) {
  5121. debug(F100,"gnfile calibrate","",0);
  5122. ckstrncpy(filnam,"CALIBRATION",CKMAXPATH);
  5123. nfils = 0;
  5124. cal_j = 0;
  5125. fsize = calibrate;
  5126. sndsrc = 0; /* For next time */
  5127. nfils = 0;
  5128. return(1);
  5129. }
  5130. #endif /* CALIBRATE */
  5131. #ifndef NOSPL
  5132. if (sndarray) { /* Sending from an array */
  5133. extern char sndxnam[]; /* Pseudo filename */
  5134. debug(F100,"gnfile array","",0);
  5135. nfils = 0;
  5136. fsize = (CK_OFF_T)-1; /* Size unknown */
  5137. sndsrc = 0;
  5138. ckstrncpy(filnam,sndxnam,CKMAXPATH);
  5139. return(1);
  5140. }
  5141. #endif /* NOSPL */
  5142. if (sndsrc == 0) { /* It's not really a file */
  5143. if (nfils > 0) { /* It's a pipe, or stdin */
  5144. ckstrncpy(filnam,*cmlist,CKMAXPATH+1); /* Copy its "name" */
  5145. nfils = 0; /* There is no next file */
  5146. return(1); /* OK this time */
  5147. } else return(0); /* but not next time */
  5148. }
  5149. /* If file group interruption (C-Z) occurred, fail. */
  5150. if (czseen) {
  5151. tlog(F100,"Transaction cancelled","",0L);
  5152. debug(F100,"gnfile czseen","",0);
  5153. return(-4);
  5154. }
  5155. /* Loop through file list till we find a readable, sendable file */
  5156. filesize = (CK_OFF_T)-1; /* Loop exit (file size) variable */
  5157. while (filesize < 0) { /* Keep trying till we get one... */
  5158. retcode = 0;
  5159. if (sndsrc > 0) { /* File list in cmlist or file */
  5160. if (filefile) { /* Reading list from file... */
  5161. if (zsinl(ZMFILE,filnam,CKMAXPATH) < 0) { /* Read a line */
  5162. zclose(ZMFILE); /* Failed */
  5163. debug(F110,"gnfile filefile EOF",filefile,0);
  5164. makestr(&filefile,NULL);
  5165. return(0);
  5166. }
  5167. debug(F110,"gnfile filefile filnam",filnam,0);
  5168. }
  5169. debug(F101,"gnfile nfils","",nfils);
  5170. if (nfils-- > 0 || filefile) { /* Still some left? */
  5171. #ifndef NOMSEND
  5172. if (addlist) {
  5173. if (filenext && filenext->fl_name) {
  5174. ckstrncpy(filnam,filenext->fl_name,CKMAXPATH+1);
  5175. cmarg2 =
  5176. filenext->fl_alias ?
  5177. filenext->fl_alias :
  5178. "";
  5179. binary = filenext->fl_mode;
  5180. } else {
  5181. printf("?Internal error expanding ADD list\n");
  5182. return(-5);
  5183. }
  5184. filenext = filenext->fl_next;
  5185. debug(F111,"gnfile addlist filnam",filnam,nfils);
  5186. } else if (sndsrc > 0 && !filefile) {
  5187. #endif /* NOMSEND */
  5188. ckstrncpy(filnam,*cmlist++,CKMAXPATH+1);
  5189. debug(F111,"gnfile cmlist filnam",filnam,nfils);
  5190. #ifndef NOMSEND
  5191. }
  5192. #endif /* NOMSEND */
  5193. i = 0;
  5194. #ifndef NOSERVER
  5195. debug(F101,"gnfile ngetpath","",ngetpath);
  5196. #endif /* NOSERVER */
  5197. nextinpath:
  5198. #ifndef NOSERVER
  5199. fromgetpath = 0;
  5200. if (server && !isabsolute(filnam) && (ngetpath > i)) {
  5201. ckstrncpy(fullname,getpath[i],CKMAXPATH+1);
  5202. ckstrncat(fullname,filnam,CKMAXPATH);
  5203. debug(F111,"gnfile getpath",fullname,i);
  5204. fromgetpath = 1;
  5205. i++;
  5206. } else {
  5207. i = ngetpath + 1;
  5208. #else
  5209. i = 1; /* ? */
  5210. #endif /* NOSERVER */
  5211. ckstrncpy(fullname,filnam,CKMAXPATH+1);
  5212. debug(F110,"gnfile absolute",fullname,0);
  5213. #ifndef NOSERVER
  5214. }
  5215. #endif /* NOSERVER */
  5216. if (iswild(fullname)
  5217. #ifdef RECURSIVE
  5218. || recursive > 0 || !strcmp(fullname,".")
  5219. #endif /* RECURSIVE */
  5220. ) { /* It looks wild... */
  5221. /* First check if a file with this name exists */
  5222. debug(F110,"gnfile wild",fullname,0);
  5223. if (zchki(fullname) >= 0) {
  5224. /*
  5225. Here we have a file whose name actually
  5226. contains wildcard characters.
  5227. */
  5228. goto gotnam;
  5229. }
  5230. #ifdef COMMENT
  5231. nzxopts = ZX_FILONLY; /* (was 0: 25 Jul 2001 fdc) */
  5232. #else
  5233. nzxopts = recursive ? 0 : ZX_FILONLY; /* 30 Jul 2001 */
  5234. #endif /* COMMENT */
  5235. if (nolinks) nzxopts |= ZX_NOLINKS; /* (26 Jul 2001 fdc) */
  5236. #ifdef UNIXOROSK
  5237. if (matchdot) nzxopts |= ZX_MATCHDOT;
  5238. #endif /* UNIXOROSK */
  5239. if (recursive) nzxopts |= ZX_RECURSE;
  5240. x = nzxpand(fullname,nzxopts); /* Expand wildcards */
  5241. debug(F101,"gnfile nzxpand","",x);
  5242. if (x == 1) {
  5243. int xx;
  5244. xx = znext(fullname);
  5245. debug(F111,"gnfile znext A",fullname,xx);
  5246. goto gotnam;
  5247. }
  5248. if (x == 0) { /* None match */
  5249. #ifndef NOSERVER
  5250. if (server && ngetpath > i)
  5251. goto nextinpath;
  5252. #endif /* NOSERVER */
  5253. retcode = -1;
  5254. debug(F101,"gnfile gnferror A","",gnferror);
  5255. gnferror = -1;
  5256. continue;
  5257. }
  5258. if (x < 0) { /* Too many to expand */
  5259. debug(F101,"gnfile gnferror B","",gnferror);
  5260. gnferror = -5;
  5261. return(-5);
  5262. }
  5263. sndsrc = -1; /* Change send-source to znext() */
  5264. }
  5265. } else { /* We're out of files. */
  5266. debug(F111,"gnfile done",ckitoa(gnferror),nfils);
  5267. *filnam = '\0';
  5268. return(0);
  5269. }
  5270. }
  5271. /* Otherwise, step to next element of internal wildcard expansion list. */
  5272. if (sndsrc == -1) {
  5273. int xx = 0;
  5274. while (1) {
  5275. debug(F111,"gnfile znext X",filnam,xx);
  5276. xx = znext(filnam);
  5277. debug(F111,"gnfile znext B",filnam,xx);
  5278. if (!filnam[0])
  5279. break;
  5280. if (dodirstoo) {
  5281. debug(F111,"gnfile FTP MPUT /RECURSIVE",filnam,xx);
  5282. break;
  5283. }
  5284. if (!isdir(filnam))
  5285. break;
  5286. }
  5287. debug(F111,"gnfile znext C",filnam,x);
  5288. if (!filnam[0]) { /* If no more, */
  5289. sndsrc = 1; /* go back to previous list */
  5290. debug(F101,"gnfile setting sndsrc back","",sndsrc);
  5291. continue;
  5292. } else
  5293. ckstrncpy(fullname,filnam,CKMAXPATH+1);
  5294. }
  5295. /* Get here with a filename. */
  5296. gotnam:
  5297. debug(F110,"gnfile fullname",fullname,0);
  5298. if (fullname[0]) {
  5299. #ifdef DTILDE
  5300. char * dirp = "";
  5301. if (fullname[0] == '~') {
  5302. dirp = tilde_expand((char *)fullname);
  5303. if (*dirp) ckstrncpy(fullname,dirp,CKMAXPATH+1);
  5304. }
  5305. #endif /* DTILDE */
  5306. filesize = zchki(fullname); /* Check if file readable */
  5307. debug(F111,"gnfile zchki",fullname,filesize);
  5308. retcode = filesize; /* Possible return code */
  5309. if (filesize == (CK_OFF_T)-2 && dodirstoo) {
  5310. filesize = 0;
  5311. }
  5312. if (filesize < 0) {
  5313. gnferror = (int)filesize;
  5314. debug(F101,"gnfile gnferror C","",gnferror);
  5315. }
  5316. if (filesize == (CK_OFF_T)-1) { /* If not found */
  5317. debug(F100,"gnfile -1","",0);
  5318. #ifndef NOSERVER
  5319. if (server && ngetpath > i)
  5320. goto nextinpath;
  5321. #endif /* NOSERVER */
  5322. debug(F110,"gnfile skipping:",fullname,0);
  5323. tlog(F110,fullname,": open failure - skipped",0);
  5324. xxscreen(SCR_FN,0,0l,fullname);
  5325. xxscreen(SCR_ST,ST_SKIP,SKP_ACC,fullname);
  5326. #ifdef TLOG
  5327. if (tralog && !tlogfmt)
  5328. doxlog(what,fullname,fsize,binary,1,"Skipped");
  5329. #endif /* TLOG */
  5330. continue;
  5331. } else if (filesize < 0) {
  5332. if (filesize == (CK_OFF_T)-3) { /* Exists but not readable */
  5333. debug(F100,"gnfile -3","",0);
  5334. filrej++; /* Count this one as not sent */
  5335. tlog(F110,"Read access denied",fullname,0); /* Log this */
  5336. xxscreen(SCR_FN,0,0l,fullname);
  5337. xxscreen(SCR_ST,ST_SKIP,SKP_ACC,fullname); /* Display it */
  5338. #ifdef TLOG
  5339. if (tralog && !tlogfmt)
  5340. doxlog(what,fullname,fsize,binary,1,"Skipped");
  5341. #endif /* TLOG */
  5342. }
  5343. continue;
  5344. } else {
  5345. int xx;
  5346. fsize = filesize;
  5347. /* +++ */
  5348. debug(F111,"gnfile sndsmaller",ckfstoa(sndsmaller),sndsmaller);
  5349. debug(F111,"gnfile sndlarger",ckfstoa(sndlarger),sndlarger);
  5350. debug(F111,"gnfile (CK_OFF_T)-1",ckfstoa((CK_OFF_T)-1),(CK_OFF_T)-1);
  5351. xx = fileselect(fullname,
  5352. sndafter, sndbefore,
  5353. sndnafter,sndnbefore,
  5354. sndsmaller,sndlarger,
  5355. skipbup,
  5356. NSNDEXCEPT,sndexcept);
  5357. debug(F111,"gnfile fileselect",fullname,xx);
  5358. if (!xx) {
  5359. filesize = (CK_OFF_T)-1;
  5360. gnferror = -6;
  5361. debug(F101,"gnfile gnferror D","",gnferror);
  5362. continue;
  5363. }
  5364. ckstrncpy(filnam,fullname,CKMAXPATH+1);
  5365. return(1);
  5366. }
  5367. #ifdef COMMENT
  5368. /* This can't be right! */
  5369. } else { /* sndsrc is 0... */
  5370. if (!fileselect(fullname,
  5371. sndafter, sndbefore,
  5372. sndnafter,sndnbefore,
  5373. sndsmaller,sndlarger,
  5374. skipbup,
  5375. NSNDEXCEPT,sndexcept)) {
  5376. gnferror = -6;
  5377. debug(F111,"gnfile fileselect",fullname,gnferror);
  5378. filesize = (CK_OFF_T)-1;
  5379. continue;
  5380. }
  5381. ckstrncpy(filnam,fullname,CKMAXPATH+1);
  5382. return(1);
  5383. #endif /* COMMENT */
  5384. }
  5385. }
  5386. debug(F101,"gnfile result","",retcode);
  5387. *filnam = '\0';
  5388. return(0);
  5389. }
  5390. /*
  5391. The following bunch of routines feed internally generated data to the server
  5392. to send to the client in response to REMOTE commands like DIRECTORY, DELETE,
  5393. and so on. We have to write these lines in the format appropriate to our
  5394. platform, so they can be converted to generic (CRLF) text format by the
  5395. packetizer.
  5396. */
  5397. #ifdef UNIX
  5398. char * endline = "\12";
  5399. #else
  5400. #ifdef datageneral
  5401. char * endline = "\12";
  5402. #else
  5403. #ifdef MAC
  5404. char * endline = "\15";
  5405. #else
  5406. #ifdef OSK
  5407. char * endline = "\15";
  5408. #else
  5409. char * endline = "\15\12";
  5410. #endif /* OSK */
  5411. #endif /* MAC */
  5412. #endif /* datageneral */
  5413. #endif /* UNIX */
  5414. #ifdef MAC
  5415. #define FNCBUFL 256
  5416. #else
  5417. #ifdef CKSYMLINK
  5418. #define FNCBUFL (CKMAXPATH + CKMAXPATH + 64)
  5419. #else
  5420. #define FNCBUFL (CKMAXPATH + 64)
  5421. #endif /* CKSYMLINK */
  5422. #endif /* MAC */
  5423. /* NB: The minimum FNCBUFL is 255 */
  5424. static CHAR funcbuf[FNCBUFL];
  5425. static int funcnxt = 0;
  5426. static int funclen = 0;
  5427. static int nxpnd = -1;
  5428. static long ndirs = 0;
  5429. static long nfiles = 0;
  5430. static CK_OFF_T nbytes = 0;
  5431. int
  5432. sndstring(p) char * p; {
  5433. #ifndef NOSERVER
  5434. nfils = 0; /* No files, no lists. */
  5435. xflg = 1; /* Flag we must send X packet. */
  5436. ckstrncpy(cmdstr,versio,CMDSTRL); /* Data for X packet. */
  5437. first = 1; /* Init getchx lookahead */
  5438. memstr = 1; /* Just set the flag. */
  5439. memptr = p; /* And the pointer. */
  5440. binary = XYFT_T; /* Text mode for this. */
  5441. return(sinit());
  5442. #else
  5443. return(0);
  5444. #endif /* NOSERVER */
  5445. }
  5446. /* S N D H L P -- Routine to send builtin help */
  5447. static int srvhlpnum = 0;
  5448. #ifdef IKSD
  5449. static char *nmx[] = { "Disabled", "Disabled", "Enabled", "Enabled" };
  5450. #endif /* IKSD */
  5451. static char *
  5452. xnm(x) int x; {
  5453. #ifdef IKSD
  5454. if (inserver)
  5455. return(nmx[x]);
  5456. else
  5457. #endif /* IKSD */
  5458. return(nm[x]);
  5459. }
  5460. static int
  5461. nxthlp(
  5462. #ifdef CK_ANSIC
  5463. void
  5464. #endif /* CK_ANSIC */
  5465. ) {
  5466. int x = 0;
  5467. extern int
  5468. en_cpy, en_cwd, en_del, en_dir, en_fin, en_get, en_bye, en_mai,
  5469. en_pri, en_hos, en_ren, en_sen, en_spa, en_set, en_typ, en_who,
  5470. /* en_ret, */ en_mkd, en_rmd, en_asg, en_que, en_xit, x_login, x_logged,
  5471. xfinish;
  5472. extern char * ckxsys;
  5473. if (funcnxt < funclen)
  5474. return (funcbuf[funcnxt++]);
  5475. switch (srvhlpnum++) {
  5476. case 0:
  5477. x = ckstrncpy((char *)funcbuf,
  5478. " Client Command Status Description\n",
  5479. FNCBUFL
  5480. );
  5481. if (x_login && !x_logged) {
  5482. x += ckstrncat((char *)funcbuf,
  5483. " REMOTE LOGIN required\n",
  5484. FNCBUFL
  5485. );
  5486. }
  5487. if (FNCBUFL - x > 74)
  5488. sprintf((char *)(funcbuf+x)," GET %-14s%s\n",
  5489. xnm(en_get),
  5490. "Transfer file(s) from server to client."
  5491. );
  5492. break;
  5493. /* NOTE: The minimum funcbuf[] size is 255; all of the following are safe. */
  5494. case 1:
  5495. sprintf((char *)funcbuf," SEND %-14s%s\n",
  5496. xnm(en_sen),
  5497. "Transfer file(s) from client to server."
  5498. );
  5499. break;
  5500. case 2:
  5501. sprintf((char *)funcbuf," MAIL %-14s%s\n",
  5502. xnm(inserver ? 0 : en_mai),
  5503. "Send file(s) as e-mail."
  5504. );
  5505. break;
  5506. case 3:
  5507. #ifndef NOSPL
  5508. sprintf((char *)funcbuf," REMOTE ASSIGN %-14s%s\n",
  5509. xnm(en_asg),
  5510. "Assign value to server variable or macro."
  5511. );
  5512. #else
  5513. sprintf((char *)funcbuf," REMOTE ASSIGN not configured\n");
  5514. #endif /* NOSPL */
  5515. break;
  5516. case 4:
  5517. sprintf((char *)funcbuf," REMOTE CD %-14s%s\n",
  5518. xnm(en_cwd),
  5519. "Change server's directory."
  5520. );
  5521. break;
  5522. case 5:
  5523. #ifdef ZCOPY
  5524. sprintf((char *)funcbuf," REMOTE COPY %-14s%s\n",
  5525. xnm(en_cpy),
  5526. "Copy a file on the server."
  5527. );
  5528. #else
  5529. sprintf((char *)funcbuf," REMOTE COPY not configured\n");
  5530. #endif /* ZCOPY */
  5531. break;
  5532. case 6:
  5533. sprintf((char *)funcbuf," REMOTE DELETE %-14s%s\n",
  5534. xnm(en_del),
  5535. "Delete a file on the server."
  5536. );
  5537. break;
  5538. case 7:
  5539. sprintf((char *)funcbuf," REMOTE DIRECTORY %-14s%s\n",
  5540. xnm(en_dir),
  5541. "List files on the server."
  5542. );
  5543. break;
  5544. case 8:
  5545. sprintf((char *)funcbuf," REMOTE EXIT %-14s%s\n",
  5546. xnm(en_xit),
  5547. "Exit from Kermit server program."
  5548. );
  5549. break;
  5550. case 9:
  5551. sprintf((char *)funcbuf," REMOTE HOST %-14s%s\n",
  5552. xnm(inserver ? 0 : en_hos),
  5553. #ifdef datageneral
  5554. "Execute a CLI command on the server."
  5555. #else
  5556. #ifdef VMS
  5557. "Execute a DCL command on the server."
  5558. #else
  5559. "Execute a shell command on the server."
  5560. #endif /* VMS */
  5561. #endif /* datageneral */
  5562. );
  5563. break;
  5564. case 10:
  5565. sprintf((char *)funcbuf," REMOTE PRINT %-14s%s\n",
  5566. xnm(inserver ? 0 : en_pri),
  5567. "Send a file to the server for printing."
  5568. );
  5569. break;
  5570. case 11:
  5571. #ifndef NOSPL
  5572. sprintf((char *)funcbuf," REMOTE QUERY %-14s%s\n",
  5573. xnm(en_que),
  5574. "Get value of server variable or macro."
  5575. );
  5576. #else
  5577. sprintf((char *)funcbuf," REMOTE QUERY not configured\n");
  5578. #endif /* NOSPL */
  5579. break;
  5580. case 12:
  5581. sprintf((char *)funcbuf," REMOTE MKDIR %-14s%s\n",
  5582. xnm(en_mkd),
  5583. "Create a directory on the server."
  5584. );
  5585. break;
  5586. case 13:
  5587. sprintf((char *)funcbuf," REMOTE RMDIR %-14s%s\n",
  5588. xnm(en_rmd),
  5589. "Remove a directory on the server."
  5590. );
  5591. break;
  5592. case 14:
  5593. sprintf((char *)funcbuf," REMOTE RENAME %-14s%s\n",
  5594. xnm(en_ren),
  5595. "Rename a file on the server."
  5596. );
  5597. break;
  5598. case 15:
  5599. sprintf((char *)funcbuf," REMOTE SET %-14s%s\n",
  5600. xnm(en_set),
  5601. "Set a parameter on the server"
  5602. );
  5603. break;
  5604. case 16:
  5605. sprintf((char *)funcbuf," REMOTE SPACE %-14s%s\n",
  5606. xnm(en_spa),
  5607. "Inquire about disk space on the server."
  5608. );
  5609. break;
  5610. case 17:
  5611. sprintf((char *)funcbuf," REMOTE TYPE %-14s%s\n",
  5612. xnm(en_typ),
  5613. "Display a server file on your screen."
  5614. );
  5615. break;
  5616. case 18:
  5617. sprintf((char *)funcbuf," REMOTE WHO %-14s%s\n",
  5618. xnm(inserver ? 0 : en_who),
  5619. "List who is logged in to the server."
  5620. );
  5621. break;
  5622. case 19:
  5623. sprintf((char *)funcbuf," FINISH %-14s%s\n",
  5624. xnm(en_fin),
  5625. xfinish ?
  5626. "Exit from Kermit server program." :
  5627. "Return the server to its command prompt."
  5628. );
  5629. break;
  5630. case 20:
  5631. sprintf((char *)funcbuf," BYE %-14s%s\n\n",
  5632. xnm(en_bye),
  5633. "Log the server out and disconnect."
  5634. );
  5635. break;
  5636. default:
  5637. return(-1);
  5638. }
  5639. funcnxt = 0;
  5640. funclen = strlen((char *)funcbuf);
  5641. return(funcbuf[funcnxt++]);
  5642. }
  5643. int
  5644. sndhlp() {
  5645. #ifndef NOSERVER
  5646. extern char * ckxsys;
  5647. first = 1; /* Init getchx lookahead */
  5648. nfils = 0; /* No files, no lists. */
  5649. xflg = 1; /* Flag we must send X packet. */
  5650. ckstrncpy(cmdstr,"REMOTE HELP",CMDSTRL); /* Data for X packet. */
  5651. /*sprintf((char *)funcbuf, "C-Kermit %s,%s\n\n", versio, ckxsys);*/
  5652. /*funclen = strlen((char *)funcbuf);*/
  5653. #ifdef IKSD
  5654. if (inserver) {
  5655. sprintf((char *)(funcbuf+funclen),
  5656. "Internet Kermit Service\n\n");
  5657. funclen = strlen((char *)funcbuf);
  5658. }
  5659. #endif /* IKSD */
  5660. funcnxt = 0;
  5661. funcptr = nxthlp;
  5662. funcstr = 1;
  5663. srvhlpnum = 0;
  5664. binary = XYFT_T; /* Text mode for this. */
  5665. return(sinit());
  5666. #else
  5667. return(0);
  5668. #endif /* NOSERVER */
  5669. }
  5670. /*
  5671. Returns the next available character,
  5672. -1 if no more data.
  5673. */
  5674. static int
  5675. nxttype(
  5676. #ifdef CK_ANSIC
  5677. void
  5678. #endif /* CK_ANSIC */
  5679. ) {
  5680. int c;
  5681. if (zchin(ZIFILE,&c) < 0) {
  5682. zclose(ZIFILE);
  5683. return(-1);
  5684. } else {
  5685. return((unsigned)c);
  5686. }
  5687. }
  5688. /* S N D T Y P -- TYPE a file to remote client */
  5689. int
  5690. sndtype(file) char * file; {
  5691. #ifndef NOSERVER
  5692. char name[CKMAXPATH+1];
  5693. #ifdef OS2
  5694. char * p = NULL;
  5695. if (*file) {
  5696. ckstrncpy(name, file, CKMAXPATH+1);
  5697. /* change / to \. */
  5698. p = name;
  5699. while (*p) { /* Change them back to \ */
  5700. if (*p == '/') *p = '\\';
  5701. p++;
  5702. }
  5703. } else
  5704. return(0);
  5705. #else
  5706. ckstrncpy(name, file, CKMAXPATH+1);
  5707. #endif /* OS2 */
  5708. funcnxt = 0;
  5709. funclen = strlen((char *)funcbuf);
  5710. if (zchki(name) == -2) {
  5711. /* Found a directory */
  5712. return(0);
  5713. }
  5714. if (!zopeni(ZIFILE,name))
  5715. return(0);
  5716. nfils = 0; /* No files, no lists. */
  5717. xflg = 1; /* Flag we must send X packet. */
  5718. ckstrncpy(cmdstr,"type",CMDSTRL); /* Data for X packet. */
  5719. first = 1; /* Init getchx lookahead */
  5720. funcstr = 1; /* Just set the flag. */
  5721. funcptr = nxttype; /* And the pointer. */
  5722. binary = XYFT_T; /* Text mode for this */
  5723. return(sinit());
  5724. #else
  5725. return(0);
  5726. #endif /* NOSERVER */
  5727. }
  5728. /*
  5729. N X T D I R -- Provide data for senddir()
  5730. Returns the next available character or -1 if no more data.
  5731. */
  5732. #ifndef NOICP
  5733. /* Directory listing parameters set by the user interface, if any. */
  5734. extern int dir_head, dir_dots, dir_back;
  5735. #endif /* NOICP */
  5736. static int sd_hdg, sd_bkp, sd_dot; /* Local listing parameters */
  5737. static int
  5738. nxtdir(
  5739. #ifdef CK_ANSIC
  5740. void
  5741. #endif /* CK_ANSIC */
  5742. ) {
  5743. char name[CKMAXPATH+1], dbuf[24], *p = NULL;
  5744. char *dstr = NULL, * lnk = "";
  5745. CHAR c, * linebuf = funcbuf;
  5746. #ifdef OSK
  5747. /* Work around bugs in OSK compiler */
  5748. char *dirtag = "directories";
  5749. char *filetag = "files";
  5750. char *bytetag = "bytes";
  5751. #endif /* OSK */
  5752. CK_OFF_T len = 0;
  5753. int x, itsadir = 0, gotone = 0;
  5754. #ifdef DEBUG
  5755. if (deblog) {
  5756. debug(F101,"nxtdir funcnxt","",funcnxt);
  5757. debug(F101,"nxtdir funclen","",funclen);
  5758. debug(F110,"nxtdir funcbuf",funcbuf+funcnxt,0);
  5759. }
  5760. #endif /* DEBUG */
  5761. if (funcnxt < funclen) { /* Return next character from buffer */
  5762. c = funcbuf[funcnxt++];
  5763. debug(F000,"nxtdir return 1","",(unsigned)(c & 0xff));
  5764. return((unsigned)(c & 0xff));
  5765. }
  5766. while (nxpnd > 0) { /* Buffer needs refill */
  5767. nxpnd--;
  5768. znext(name); /* Get next filename */
  5769. if (!name[0]) { /* None left - done */
  5770. nxpnd = 0;
  5771. return(nxtdir());
  5772. }
  5773. if (sd_bkp) { /* Showing backup files? */
  5774. gotone = 1; /* Yes, no need to check. */
  5775. break;
  5776. }
  5777. x = ckmatch( /* No - see if this is one */
  5778. #ifdef CKREGEX
  5779. "*.~[0-9]*~" /* Not perfect but close enough. */
  5780. #else
  5781. "*.~*~" /* Less close. */
  5782. #endif /* CKREGEX */
  5783. ,name,filecase,1);
  5784. debug(F111,"nxtdir ckmatch",name,x);
  5785. if (x) {
  5786. continue; /* It's a backup file - skip it */
  5787. } else {
  5788. gotone = 1; /* It's not, break from loop. */
  5789. break;
  5790. }
  5791. }
  5792. if (gotone) {
  5793. len = zgetfs(name); /* Get file size */
  5794. debug(F111,"nxtdir zgetfs",name,len);
  5795. #ifdef VMSORUNIX
  5796. itsadir = zgfs_dir; /* See if it's a directory */
  5797. #else
  5798. itsadir = (len == -2 || isdir(name));
  5799. #endif /* VMSORUNIX */
  5800. dstr = zfcdat(name);
  5801. debug(F111,"nxtdir zcfdat",dstr,0);
  5802. if (!dstr)
  5803. dstr = "0000-00-00 00:00:00";
  5804. if (!*dstr) {
  5805. dstr = "0000-00-00 00:00:00";
  5806. } else {
  5807. dbuf[0] = dstr[0];
  5808. dbuf[1] = dstr[1];
  5809. dbuf[2] = dstr[2];
  5810. dbuf[3] = dstr[3];
  5811. dbuf[4] = '-';
  5812. dbuf[5] = dstr[4];
  5813. dbuf[6] = dstr[5];
  5814. dbuf[7] = '-';
  5815. dbuf[8] = dstr[6];
  5816. dbuf[9] = dstr[7];
  5817. strcpy(dbuf+10,dstr+8);
  5818. dstr = dbuf;
  5819. }
  5820. #ifdef CK_PERMS
  5821. #ifdef VMSORUNIX
  5822. p = ziperm(name); /* Get permissions */
  5823. #else
  5824. p = zgperm(name);
  5825. #endif /* VMSORUNIX */
  5826. #else
  5827. p = NULL;
  5828. #endif /* CK_PERMS */
  5829. debug(F110,"domydir perms",p,0);
  5830. #ifdef VMS
  5831. /* Make name relative */
  5832. ckstrncpy(name,zrelname(name,zgtdir()),CKMAXPATH+1);
  5833. #endif /* VMS */
  5834. if (itsadir) {
  5835. ndirs++;
  5836. } else {
  5837. nfiles++;
  5838. nbytes += len;
  5839. }
  5840. lnk = "";
  5841. #ifdef UNIX
  5842. #ifdef CKSYMLINK
  5843. if (zgfs_link) {
  5844. extern char linkname[];
  5845. lnk = linkname;
  5846. }
  5847. debug(F111,"nxtdir linkname",lnk,zgfs_link);
  5848. #endif /* CKSYMLINK */
  5849. #endif /* UNIX */
  5850. /*
  5851. The following sprintf's are safe; linebuf is a pointer to funcbuf,
  5852. which is 64 bytes larger than CKMAXPATH (or double CKMAXPATH when
  5853. symlinks are possible). 64 allows for the fixed-field portions of
  5854. the file listing line: permissions, size, and date. CKMAXPATH allows
  5855. for the longest possible pathname.
  5856. */
  5857. if (itsadir && len < 0) { /* Directory */
  5858. #ifdef VMS
  5859. sprintf((char *)linebuf,
  5860. "%-22s%-10s %s %s\n",p,"<DIR>",dstr,name);
  5861. #else
  5862. if (p)
  5863. sprintf((char *)linebuf,
  5864. "%10s%-10s %s %s\n",p,"<DIR>",dstr,name);
  5865. else
  5866. sprintf((char *)linebuf,
  5867. "%-10s %s %s\n", "<DIR>", dstr, name);
  5868. #endif /* VMS */
  5869. } else { /* Regular file */
  5870. #ifdef VMS
  5871. sprintf((char *)linebuf,
  5872. "%-22s%10s %s %s\n", p, ckfstoa(len), dstr, name);
  5873. #else
  5874. if (p)
  5875. sprintf((char *)linebuf,
  5876. "%10s%10s %s %s%s%s\n",
  5877. p, ckfstoa(len), dstr, name,
  5878. *lnk ? " -> " : "",
  5879. lnk
  5880. );
  5881. else
  5882. sprintf((char *)linebuf,
  5883. "%10s %s %s%s%s\n",
  5884. ckfstoa(len), dstr, name,
  5885. *lnk ? " -> " : "",
  5886. lnk
  5887. );
  5888. #endif /* VMS */
  5889. }
  5890. funcnxt = 0;
  5891. funclen = strlen((char *)funcbuf);
  5892. } else if (sd_hdg && nxpnd == 0) { /* Done, send summary */
  5893. char *blankline = ""; /* At beginning of summary */
  5894. /*
  5895. The idea is to prevent (a) unnecessary multiple blanklines, and (b)
  5896. prompt-stomping. Preventing (b) is practically impossible, because it
  5897. depends on the client so for now always include that final CRLF.
  5898. */
  5899. if (!ndirs || !nbytes || !nfiles)
  5900. blankline = endline;
  5901. #ifdef OSK
  5902. /* Workaround bugs in OS-9 compiler... */
  5903. if (ndirs == 1)
  5904. dirtag = "directory";
  5905. if (nfiles == 1)
  5906. filetag = "file";
  5907. if (nbytes == (CK_OFF_T)1)
  5908. bytetag = "byte";
  5909. sprintf((char *)funcbuf,
  5910. "%sSummary: %ld %s, %ld %s, %s %s%s",
  5911. blankline,
  5912. ndirs,
  5913. dirtag,
  5914. nfiles,
  5915. filetag,
  5916. ckfstoa(nbytes),
  5917. bytetag,
  5918. endline);
  5919. #else
  5920. sprintf((char *)funcbuf,
  5921. "%sSummary: %ld director%s, %ld file%s, %s byte%s%s",
  5922. blankline,
  5923. ndirs,
  5924. (ndirs == 1) ? "y" : "ies",
  5925. nfiles,
  5926. (nfiles == 1) ? "" : "s",
  5927. ckfstoa(nbytes),
  5928. (nbytes == (CK_OFF_T)1) ? "" : "s",
  5929. endline
  5930. );
  5931. #endif /* OSK */
  5932. nxpnd--;
  5933. funcnxt = 0;
  5934. funclen = strlen((char *)funcbuf);
  5935. } else {
  5936. funcbuf[0] = '\0';
  5937. funcnxt = 0;
  5938. funclen = 0;
  5939. }
  5940. debug(F101,"nxtdir funclen","",funclen);
  5941. if (funcnxt < funclen) { /* If we have data to send... */
  5942. c = funcbuf[funcnxt++];
  5943. debug(F000,"nxtdir return 2","",(unsigned)(c & 0xff));
  5944. return((unsigned)(c & 0xff));
  5945. } else
  5946. return(-1); /* Nothing left, done. */
  5947. }
  5948. /* S N D D I R -- send directory listing */
  5949. int
  5950. snddir(spec) char * spec; {
  5951. #ifndef NOSERVER
  5952. char * p = NULL, name[CKMAXPATH+1];
  5953. int t = 0, rc = 0;
  5954. char fnbuf[CKMAXPATH+1];
  5955. debug(F111,"snddir matchdot",spec,matchdot);
  5956. #ifndef NOICP
  5957. debug(F111,"snddir dir_dots",spec,dir_dots);
  5958. sd_hdg = dir_head > 0; /* Import listing parameters if any */
  5959. sd_bkp = dir_back > 0;
  5960. if (dir_dots > -1)
  5961. sd_dot = dir_dots;
  5962. else
  5963. sd_dot = matchdot;
  5964. #else
  5965. sd_hdg = 1; /* Or use hardwired defaults */
  5966. sd_bkp = 1;
  5967. sd_dot = matchdot;
  5968. #endif /* NOICP */
  5969. if (!spec) spec = "";
  5970. debug(F111,"snddir sd_dot",spec,sd_dot);
  5971. if (*spec) {
  5972. #ifdef COMMENT
  5973. zfnqfp(spec,CKMAXPATH,name);
  5974. debug(F110,"snddir zfnqfp",name,0);
  5975. #else
  5976. ckstrncpy(name,spec,CKMAXPATH+1);
  5977. debug(F110,"snddir name",name,0);
  5978. #endif /* COMMENT */
  5979. } else {
  5980. #ifdef OS2
  5981. strcpy(name, "*");
  5982. #else
  5983. #ifdef UNIXOROSK
  5984. strcpy(name, "./*");
  5985. #else
  5986. #ifdef VMS
  5987. strcpy(name, "*.*");
  5988. #else
  5989. #ifdef datageneral
  5990. strcpy(name, "+");
  5991. #else
  5992. debug(F101,"snddir quit (no filespec)","",0);
  5993. return(0);
  5994. #endif /* datageneral */
  5995. #endif /* VMS */
  5996. #endif /* UNIX */
  5997. #endif /* OS2 */
  5998. }
  5999. debug(F110,"snddir name 1",name,0);
  6000. ndirs = 0L;
  6001. nfiles = 0L;
  6002. nbytes = (CK_OFF_T)0;
  6003. if (zfnqfp(name,CKMAXPATH,fnbuf))
  6004. debug(F110,"snddir name 2",name,0);
  6005. p = name + strlen(name); /* Move it to end of list */
  6006. /* sprintf safe because funcbuf size >= max path len + 64 */
  6007. if (sd_hdg) {
  6008. sprintf((char *)funcbuf,"Listing files: %s%s%s",fnbuf,endline,endline);
  6009. funcnxt = 0;
  6010. funclen = strlen((char *)funcbuf);
  6011. }
  6012. diractive = 1;
  6013. #ifdef OS2
  6014. if (zchki(name) == -2) { /* Found a directory */
  6015. p--;
  6016. if (*p == '\\' || *p == '/')
  6017. ckstrncat(name, "*", CKMAXPATH);
  6018. else if (*p == ':')
  6019. ckstrncat(name, ".", CKMAXPATH);
  6020. else
  6021. ckstrncat(name, "\\*", CKMAXPATH);
  6022. debug(F110,"snddir directory",name,0);
  6023. }
  6024. #else
  6025. if (!iswild(name) && isdir(name)) {
  6026. char * s = name;
  6027. p--;
  6028. #ifdef UNIXOROSK
  6029. if (*p == '/') /* So append wildcard to it */
  6030. ckstrncat(s, "*", CKMAXPATH);
  6031. else
  6032. ckstrncat(s, "/*", CKMAXPATH);
  6033. #else
  6034. #ifdef VMS
  6035. if (*p == ']' || *p == '>' || *p == ':')
  6036. ckstrncat(s, "*.*", CKMAXPATH);
  6037. #else
  6038. #ifdef datageneral
  6039. if (*p == ':')
  6040. ckstrncat(s, "+", CKMAXPATH);
  6041. else
  6042. ckstrncat(s, ":+", CKMAXPATH);
  6043. #else
  6044. #ifdef VOS
  6045. if (*p == '>')
  6046. ckstrncat(s, "*", CKMAXPATH);
  6047. else
  6048. ckstrncat(s, ">*", CKMAXPATH);
  6049. #endif /* VOS */
  6050. #endif /* datageneral */
  6051. #endif /* VMS */
  6052. #endif /* UNIXOROSK */
  6053. debug(F110,"snddir directory",name,0);
  6054. }
  6055. #endif /* OS2 */
  6056. nzxopts = 0;
  6057. #ifdef UNIX
  6058. {
  6059. extern char ** mtchs;
  6060. debug(F111,"snddir sd_dot",spec,sd_dot);
  6061. if (sd_dot > 0)
  6062. nzxopts |= ZX_MATCHDOT;
  6063. if (recursive)
  6064. nzxopts |= ZX_RECURSE;
  6065. debug(F111,"snddir nzxopts",spec,nzxopts);
  6066. nxpnd = nzxpand(name,nzxopts); /* Get the array of names */
  6067. sh_sort(mtchs,NULL,nxpnd,0,0,1); /* Sort the array */
  6068. }
  6069. #else
  6070. if (recursive) nzxopts |= ZX_RECURSE;
  6071. nxpnd = nzxpand(name,nzxopts);
  6072. #endif /* UNIX */
  6073. debug(F101,"snddir nzxpand nxpnd","",nxpnd);
  6074. if (nxpnd < 1)
  6075. return(-1);
  6076. nfils = 0; /* No files, no lists. */
  6077. xflg = 1; /* Flag we must send X packet. */
  6078. if ((int)strlen(name) < CMDSTRL - 11) /* Data for X packet. */
  6079. sprintf(cmdstr,"DIRECTORY %s",name); /* safe */
  6080. else
  6081. ckstrncpy(cmdstr,"DIRECTORY",CMDSTRL);
  6082. first = 1; /* Init getchx lookahead */
  6083. funcstr = 1; /* Just set the flag. */
  6084. funcptr = nxtdir; /* And the pointer. */
  6085. binary = XYFT_T; /* Text mode for this */
  6086. rc = sinit(); /* 26 Aug 2005 */
  6087. debug(F111,"snddir","sinit()",rc);
  6088. return(rc);
  6089. #else
  6090. return(0);
  6091. #endif /* NOSERVER */
  6092. }
  6093. /* N X T D E L -- provide data for delete */
  6094. /* Returns the next available character or -1 if no more data */
  6095. static int
  6096. nxtdel(
  6097. #ifdef CK_ANSIC
  6098. void
  6099. #endif /* CK_ANSIC */
  6100. ) {
  6101. char name[257], *p = NULL;
  6102. int len = 0;
  6103. if (funcnxt < funclen)
  6104. return ((unsigned)funcbuf[funcnxt++]);
  6105. if (nxpnd > 0) {
  6106. nxpnd--;
  6107. znext(name);
  6108. if (!name[0]) {
  6109. nxpnd = 0;
  6110. return(nxtdel());
  6111. }
  6112. len = zchki(name);
  6113. /* Find just the name of the file */
  6114. for (p = name + strlen(name); p != name && *p != '/' ; p--) ;
  6115. if (*p == '/') p++;
  6116. /* sprintf's safe because size of funcbuf >= 64 + maxpathlen */
  6117. if (len > -1L) {
  6118. if (zdelet(name)) {
  6119. sprintf((char *)funcbuf," %10s: %s%s","skipping",p,endline);
  6120. } else {
  6121. nfiles++;
  6122. nbytes += len;
  6123. sprintf((char *)funcbuf," %10s: %s%s","deleted",p,endline);
  6124. }
  6125. } else
  6126. sprintf((char *)funcbuf," directory: %s%s", p, endline);
  6127. funcnxt = 0;
  6128. funclen = strlen((char *)funcbuf);
  6129. } else
  6130. /* If done processing the expanded entries send a summary statement */
  6131. if (nxpnd == 0) {
  6132. sprintf((char *)funcbuf,
  6133. "%s%ld file%s deleted, %s byte%s freed%s",
  6134. endline,
  6135. nfiles,
  6136. (nfiles == 1) ? "" : "s",
  6137. ckfstoa(nbytes),
  6138. (nbytes == (CK_OFF_T)1) ? "" : "s",
  6139. endline
  6140. );
  6141. nxpnd--;
  6142. funcnxt = 0;
  6143. funclen = strlen((char *)funcbuf);
  6144. } else {
  6145. funcbuf[0] = '\0';
  6146. funcnxt = 0;
  6147. funclen = 0;
  6148. }
  6149. /* If we have data to send */
  6150. if (funcnxt < funclen)
  6151. return ((unsigned)funcbuf[funcnxt++]); /* Return a character */
  6152. else
  6153. return(-1); /* No more input */
  6154. }
  6155. /* S N D D E L -- Send delete message */
  6156. int
  6157. snddel(spec) char * spec; {
  6158. #ifndef NOSERVER
  6159. char name[CKMAXPATH+1];
  6160. #ifdef OS2
  6161. char * p = NULL;
  6162. #endif /* #ifdef OS2 */
  6163. if (!*spec)
  6164. return(0);
  6165. ckstrncpy(name, spec, CKMAXPATH+1);
  6166. #ifdef OS2
  6167. /* change / to \. */
  6168. p = name;
  6169. while (*p) { /* Change them back to \ */
  6170. if (*p == '/') *p = '\\';
  6171. p++;
  6172. }
  6173. #endif /* OS2 */
  6174. nfiles = 0L;
  6175. nbytes = (CK_OFF_T)0;
  6176. sprintf((char *)funcbuf,"Deleting \"%s\"%s",name,endline);
  6177. funcnxt = 0;
  6178. funclen = strlen((char *)funcbuf);
  6179. nzxopts = ZX_FILONLY; /* Files only */
  6180. #ifdef UNIXOROSK
  6181. if (matchdot) nzxopts |= ZX_MATCHDOT;
  6182. #endif /* UNIXOROSK */
  6183. #ifdef COMMENT
  6184. /* Recursive deleting not supported yet */
  6185. if (recursive) nzxopts |= ZX_RECURSE;
  6186. #endif /* COMMENT */
  6187. nxpnd = nzxpand(name,nzxopts);
  6188. if (nxpnd < 1)
  6189. return(-1);
  6190. nfils = 0; /* No files, no lists. */
  6191. xflg = 1; /* Flag we must send X packet. */
  6192. ckstrncpy(cmdstr,"REMOTE DELETE",CMDSTRL); /* Data for X packet. */
  6193. first = 1; /* Init getchx lookahead */
  6194. funcstr = 1; /* Just set the flag. */
  6195. funcptr = nxtdel; /* And the pointer. */
  6196. binary = XYFT_T; /* Use text mode for this, */
  6197. return(sinit());
  6198. #else
  6199. return(0);
  6200. #endif /* NOSERVER */
  6201. }
  6202. #ifdef OS2
  6203. /* S N D S P A C E -- send disk space message */
  6204. int
  6205. sndspace(drive) int drive; {
  6206. #ifndef NOSERVER
  6207. static char spctext[64];
  6208. unsigned long space;
  6209. if (drive) {
  6210. space = zdskspace(drive - 'A' + 1);
  6211. if (space > 0 && space < 1024)
  6212. sprintf(spctext,
  6213. " Drive %c: unknown%s",
  6214. drive,
  6215. endline
  6216. );
  6217. else
  6218. sprintf(spctext,
  6219. " Drive %c: %ldK free%s",
  6220. drive,
  6221. space / 1024L,
  6222. endline
  6223. );
  6224. } else {
  6225. space = zdskspace(0);
  6226. if (space > 0 && space < 1024)
  6227. sprintf(spctext, " Free space: unknown%s", endline);
  6228. else
  6229. sprintf(spctext, " Free space: %ldK%s", space / 1024L, endline);
  6230. }
  6231. nfils = 0; /* No files, no lists. */
  6232. xflg = 1; /* Flag we must send X packet. */
  6233. ckstrncpy(cmdstr,"free space",CMDSTRL); /* Data for X packet. */
  6234. first = 1; /* Init getchx lookahead */
  6235. memstr = 1; /* Just set the flag. */
  6236. memptr = spctext; /* And the pointer. */
  6237. binary = XYFT_T; /* Text mode for this. */
  6238. return(sinit());
  6239. #else
  6240. return(0);
  6241. #endif /* NOSERVER */
  6242. }
  6243. /* S N D W H O -- send who message */
  6244. int
  6245. sndwho(who) char * who; {
  6246. #ifndef NOSERVER
  6247. nfils = 0; /* No files, no lists. */
  6248. xflg = 1; /* Flag we must send X packet. */
  6249. ckstrncpy(cmdstr,"who",CMDSTRL); /* Data for X packet. */
  6250. first = 1; /* Init getchx lookahead */
  6251. memstr = 1; /* Just set the flag. */
  6252. #ifdef NT
  6253. memptr = "\15\12K95 SERVER\15\12"; /* And the pointer. */
  6254. #else
  6255. memptr = "\15\12K/2 SERVER\15\12";
  6256. #endif /* NT */
  6257. binary = XYFT_T; /* Use text mode */
  6258. return(sinit());
  6259. #else
  6260. return(0);
  6261. #endif /* NOSERVER */
  6262. }
  6263. #endif /* OS2 */
  6264. /* C W D -- Change server's working directory */
  6265. /*
  6266. String passed has first byte as length of directory name, rest of string
  6267. is name. Returns:
  6268. 0 on failure.
  6269. 1 on success after sending short-form response (ACK with name).
  6270. 2 on success if a CD Message file is to be sent.
  6271. */
  6272. int
  6273. cwd(vdir) char *vdir; {
  6274. char *cdd, *dirp;
  6275. vdir[xunchar(*vdir) + 1] = '\0'; /* Terminate string with a null */
  6276. dirp = vdir+1;
  6277. tlog(F110,"Directory requested: ",dirp,0L);
  6278. if (zchdir(dirp)) { /* Try to change */
  6279. cdd = zgtdir(); /* Get new working directory. */
  6280. debug(F110,"cwd",cdd,0);
  6281. if (srvcdmsg) { /* Send orientation file? */
  6282. int i;
  6283. for (i = 0; i < 8; i++) {
  6284. if (zchki(cdmsgfile[i]) > -1) {
  6285. xxscreen(SCR_CD,0,0l,cdd);
  6286. tlog(F110,"Changed directory to",cdd,0L);
  6287. return(2);
  6288. }
  6289. }
  6290. }
  6291. encstr((CHAR *)cdd); /* Send short-form reply */
  6292. ack1(data); /* containing directory name. */
  6293. xxscreen(SCR_CD,0,0l,cdd);
  6294. tlog(F110,"Changed directory to",cdd,0L);
  6295. return(1);
  6296. } else {
  6297. debug(F110,"cwd failed",dirp,0);
  6298. tlog(F110,"Failed to change directory to",dirp,0L);
  6299. return(0);
  6300. }
  6301. }
  6302. /* S Y S C M D -- Do a system command */
  6303. /* Command string is formed by concatenating the two arguments. */
  6304. int
  6305. syscmd(prefix,suffix) char *prefix, *suffix; {
  6306. extern int i_isopen;
  6307. #ifndef NOPUSH
  6308. char *cp;
  6309. i_isopen = 0;
  6310. if (!prefix)
  6311. return(0);
  6312. if (!*prefix)
  6313. return(0);
  6314. for (cp = cmdstr; *prefix != '\0'; (*cp++ = *prefix++));
  6315. while ((*cp++ = *suffix++))
  6316. #ifdef OS2
  6317. /* This takes away more than we gain in convenience
  6318. if (*(cp-1) == '/') *(cp-1) = '\\' */
  6319. #endif /* OS2 */
  6320. ; /* Copy suffix */
  6321. debug(F110,"syscmd",cmdstr,0);
  6322. if (zxcmd(ZIFILE,cmdstr) > 0) {
  6323. debug(F110,"syscmd zxcmd ok",cmdstr,0);
  6324. nfils = sndsrc = 0; /* Flag that input is from stdin */
  6325. xflg = hcflg = 1; /* And special flags for pipe */
  6326. binary = XYFT_T; /* Go to text mode */
  6327. i_isopen = 1;
  6328. return (sinit()); /* Send S packet */
  6329. } else {
  6330. debug(F100,"syscmd zxcmd failed",cmdstr,0);
  6331. i_isopen = 0;
  6332. return(0);
  6333. }
  6334. #else
  6335. debug(F100,"syscmd zxcmd NOPUSH",cmdstr,0);
  6336. i_isopen = 0;
  6337. return(0);
  6338. #endif /* NOPUSH */
  6339. }
  6340. /* R E M S E T -- Remote Set */
  6341. /* Called by server to set variables as commanded in REMOTE SET packets. */
  6342. /* Returns 1 on success, 0 on failure. */
  6343. int
  6344. remset(s) char *s; {
  6345. extern int c_save, en_del;
  6346. int len, i, x, y;
  6347. char *p;
  6348. len = xunchar(*s++); /* Length of first field */
  6349. p = s + len; /* Pointer to second length field */
  6350. *p++ = '\0'; /* Zero out second length field */
  6351. x = atoi(s); /* Value of first field */
  6352. debug(F111,"remset",s,x);
  6353. debug(F110,"remset",p,0);
  6354. switch (x) { /* Do the right thing */
  6355. case 132: /* Attributes (all, in) */
  6356. atcapr = atoi(p);
  6357. return(1);
  6358. case 133: /* File length attributes */
  6359. case 233: /* IN/OUT combined */
  6360. case 148: /* Both kinds of lengths */
  6361. case 248:
  6362. atleni = atleno = atoi(p);
  6363. return(1);
  6364. case 134: /* File Type (text/binary) */
  6365. case 234:
  6366. attypi = attypo = atoi(p);
  6367. return(1);
  6368. case 135: /* File creation date */
  6369. case 235:
  6370. atdati = atdato = atoi(p);
  6371. return(1);
  6372. case 139: /* File Blocksize */
  6373. case 239:
  6374. atblki = atblko = atoi(p);
  6375. return(1);
  6376. case 141: /* Encoding / Character Set */
  6377. case 241:
  6378. atenci = atenco = atoi(p);
  6379. return(1);
  6380. case 142: /* Disposition */
  6381. case 242:
  6382. atdisi = atdiso = atoi(p);
  6383. return(1);
  6384. case 145: /* System ID */
  6385. case 245:
  6386. atsidi = atsido = atoi(p);
  6387. return(1);
  6388. case 147: /* System-Dependent Info */
  6389. case 247:
  6390. atsysi = atsyso = atoi(p);
  6391. return(1);
  6392. case 232: /* Attributes (all, out) */
  6393. atcapr = atoi(p);
  6394. return(1);
  6395. case 300: /* File type (text, binary) */
  6396. binary = atoi(p);
  6397. b_save = binary;
  6398. #ifndef NOICP
  6399. g_binary = -1;
  6400. #endif /* NOICP */
  6401. return(1);
  6402. case 301: /* File name conversion */
  6403. fncnv = 1 - atoi(p); /* (oops) */
  6404. f_save = fncnv;
  6405. #ifndef NOICP
  6406. g_fncnv = -1;
  6407. #endif /* NOICP */
  6408. return(1);
  6409. case 302: /* File name collision */
  6410. #ifdef IKSD
  6411. #ifdef CK_LOGIN
  6412. if (inserver && isguest) /* May not be changed by guest */
  6413. return(0);
  6414. #endif /* CK_LOGIN */
  6415. #endif /* IKSD */
  6416. x = atoi(p);
  6417. if (!ENABLED(en_del) && (x == XYFX_X || x == XYFX_U))
  6418. return(0);
  6419. if (x == XYFX_R) ckwarn = 1; /* Rename */
  6420. if (x == XYFX_X) ckwarn = 0; /* Replace */
  6421. fncact = x;
  6422. return(1);
  6423. case 310: /* Incomplete File Disposition */
  6424. keep = atoi(p); /* Keep, Discard, Auto */
  6425. return(1);
  6426. case 311: /* Blocksize */
  6427. fblksiz = atoi(p);
  6428. return(1);
  6429. case 312: /* Record Length */
  6430. frecl = atoi(p);
  6431. return(1);
  6432. case 313: /* Record format */
  6433. frecfm = atoi(p);
  6434. return(1);
  6435. case 314: /* File organization */
  6436. forg = atoi(p);
  6437. return(1);
  6438. case 315: /* File carriage control */
  6439. fcctrl = atoi(p);
  6440. return(1);
  6441. case 330: /* Match dotfiles */
  6442. #ifndef NOICP
  6443. dir_dots = -1; /* This undoes DIR /DOT option */
  6444. #endif /* NOICP */
  6445. matchdot = atoi(p);
  6446. return(1);
  6447. case 331: /* Match FIFOs */
  6448. matchfifo = atoi(p);
  6449. return(1);
  6450. case 400: /* Block check */
  6451. y = atoi(p);
  6452. if (y < 5 && y > 0) {
  6453. bctr = y;
  6454. c_save = -1;
  6455. return(1);
  6456. } else if (*p == 'B') {
  6457. bctr = 4;
  6458. c_save = -1;
  6459. return(1);
  6460. } else if (*p == '5') {
  6461. bctr = 3;
  6462. c_save = -1;
  6463. return(1);
  6464. }
  6465. return(0);
  6466. case 401: /* Receive packet-length */
  6467. rpsiz = urpsiz = atoi(p);
  6468. if (urpsiz > MAXRP) urpsiz = MAXRP; /* Max long-packet length */
  6469. if (rpsiz > 94) rpsiz = 94; /* Max short-packet length */
  6470. urpsiz = adjpkl(urpsiz,wslots,bigrbsiz);
  6471. return(1);
  6472. case 402: /* Receive timeout */
  6473. y = atoi(p); /* Client is telling us */
  6474. if (y > -1 && y < 999) { /* the timeout that it wants */
  6475. pkttim = chktimo(y,timef); /* us to tell it to use. */
  6476. return(1);
  6477. } else return(0);
  6478. case 403: /* Retry limit */
  6479. y = atoi(p);
  6480. if (y > -1 && y < 95) {
  6481. maxtry = y;
  6482. return(1);
  6483. } else return(0);
  6484. case 404: /* Server timeout */
  6485. y = atoi(p);
  6486. if (y < 0) return(0);
  6487. srvtim = y;
  6488. return(1);
  6489. #ifndef NOCSETS
  6490. case 405: { /* Transfer character set */
  6491. extern int s_cset, axcset[];
  6492. int i;
  6493. for (i = 0; i < ntcsets; i++) {
  6494. if (!strcmp(tcsinfo[i].designator,p)) break;
  6495. }
  6496. debug(F101,"remset tcharset lookup","",i);
  6497. if (i == ntcsets) return(0);
  6498. tcharset = tcsinfo[i].code; /* If known, use it */
  6499. debug(F101,"remset tcharset","",tcharset);
  6500. if (s_cset == XMODE_A)
  6501. if (axcset[tcharset] > -1 && axcset[tcharset] > MAXFCSETS)
  6502. fcharset = axcset[tcharset]; /* Auto-pick file charset */
  6503. debug(F101,"remset tcharset fcharset","",fcharset);
  6504. setxlatype(tcharset,fcharset); /* Set up charset translations */
  6505. debug(F101,"remset xlatype","",xlatype);
  6506. debug(F101,"remset tcharset after setxlatype","",tcharset);
  6507. tcs_save = -1;
  6508. return(1);
  6509. }
  6510. case 320: { /* File character set */
  6511. extern struct keytab fcstab[];
  6512. extern int nfilc, s_cset, r_cset;
  6513. x = lookup(fcstab,p,nfilc,&y);
  6514. debug(F111,"RSET FILE CHAR name",p,x);
  6515. if (x < 0)
  6516. return(0);
  6517. s_cset = XMODE_M; /* No automatic charset switching */
  6518. r_cset = XMODE_M;
  6519. fcharset = x; /* Set file charset */
  6520. setxlatype(tcharset,fcharset); /* and translation type */
  6521. fcs_save = -1;
  6522. return(1);
  6523. }
  6524. #endif /* NOCSETS */
  6525. case 406: /* Window slots */
  6526. y = atoi(p);
  6527. if (y == 0) y = 1;
  6528. if (y < 1 || y > MAXWS) return(0);
  6529. wslotr = y;
  6530. swcapr = 1;
  6531. urpsiz = adjpkl(urpsiz,wslotr,bigrbsiz);
  6532. return(1);
  6533. case 410: /* Transfer mode */
  6534. y = atoi(p); /* 0 = automatic, nonzero = manual */
  6535. if (y != 0) y = 1;
  6536. xfermode = y;
  6537. debug(F101,"REMOTE SET xfermode","",xfermode);
  6538. return(1);
  6539. case 420: /* SERVER CD-MESSAGE { ON, OFF } */
  6540. y = atoi(p); /* 0 = automatic, nonzero = manual */
  6541. srvcdmsg = y;
  6542. return(1);
  6543. default: /* Anything else... */
  6544. return(0);
  6545. }
  6546. }
  6547. /* Adjust packet length based on number of window slots and buffer size */
  6548. int
  6549. adjpkl(pktlen,slots,bufsiz) int pktlen, slots, bufsiz; {
  6550. if (protocol != PROTO_K) return(pktlen);
  6551. debug(F101,"adjpkl len","",pktlen);
  6552. debug(F101,"adjpkl slots","",slots);
  6553. debug(F101,"adjpkl bufsiz","",bufsiz);
  6554. if (((pktlen + 6) * slots) > bufsiz)
  6555. pktlen = (bufsiz / slots) - 6;
  6556. debug(F101,"adjpkl new len","",pktlen);
  6557. return(pktlen);
  6558. }
  6559. /* Set transfer mode and file naming based on comparison of system types */
  6560. VOID
  6561. whoarewe() {
  6562. #ifndef NOICP
  6563. extern int g_xfermode;
  6564. #endif /* NOICP */
  6565. wearealike = 0;
  6566. debug(F101,"whoarewe xfermode","",xfermode);
  6567. #ifndef NOICP
  6568. debug(F101,"whoarewe g_xfermode","",g_xfermode);
  6569. #endif /* NOICP */
  6570. if (whoareu[0]) { /* If we know partner's system type */
  6571. char * p = (char *)whoareu;
  6572. debug(F110,"whoarewe remote sysid",whoareu,0);
  6573. if (!strcmp(p,cksysid)) /* Other system same as us */
  6574. wearealike = 1;
  6575. #ifdef UNIX
  6576. else if (!strcmp(p,"L3")) /* UNIX is sort of like AmigaDOS */
  6577. wearealike = 1; /* (same directory separator) */
  6578. else if (!strcmp(p,"N3")) /* UNIX like Aegis */
  6579. wearealike = 1;
  6580. #else
  6581. #ifdef AMIGA
  6582. /* Like UNIX, but case distinctions are ignored and can begin with device:. */
  6583. else if (!strcmp(p,"U1")) /* Amiga is sort of like UNIX */
  6584. wearealike = 1;
  6585. else if (!strcmp(p,"N3")) /* Amiga is sort of like Aegis */
  6586. wearealike = 1;
  6587. #else
  6588. #ifdef OS2 /* (Includes Windows 95/NT) */
  6589. /* DOS, GEMDOS, Windows 3.x, Windows 95, Windows NT */
  6590. /* All "the same" for FAT partitions but all bets off otherwise */
  6591. /* so this part needs some refinement ... */
  6592. else if (!strcmp(p,"U8")) /* MS-DOS */
  6593. wearealike = 1;
  6594. else if (!strcmp(p,"UO")) /* OS/2 */
  6595. wearealike = 1;
  6596. else if (!strcmp(p,"UN")) /* Windows NT or 95 */
  6597. wearealike = 1;
  6598. else if (!strcmp(p,"K2")) /* GEMDOS */
  6599. wearealike = 1;
  6600. #else
  6601. #ifdef GEMDOS
  6602. else if (!strcmp(p,"U8"))
  6603. wearealike = 1;
  6604. else if (!strcmp(p,"UO"))
  6605. wearealike = 1;
  6606. else if (!strcmp(p,"UN"))
  6607. wearealike = 1;
  6608. else if (!strcmp(p,"K2"))
  6609. wearealike = 1;
  6610. #endif /* GEMDOS */
  6611. #endif /* OS2 */
  6612. #endif /* AMIGA */
  6613. #endif /* UNIX */
  6614. /* Get here with wearealike == 1 if system types match */
  6615. debug(F101,"whoarewe wearealike","",wearealike);
  6616. if (!wearealike) /* Not alike */
  6617. return;
  6618. fncnv = XYFN_L; /* Alike, so literal filenames */
  6619. debug(F101,"whoarewe setting fncnv","",fncnv);
  6620. if (xfermode == XMODE_A) { /* Current xfer mode is auto */
  6621. #ifdef VMS
  6622. binary = XYFT_L; /* For VMS-to-VMS, use labeled */
  6623. #else
  6624. #ifdef OS2
  6625. /* OS/2 but not Windows */
  6626. if (!strcmp(cksysid,"UO") && !strcmp((char *)whoareu,"UO"))
  6627. binary = XYFT_L; /* For OS/2-to-OS/2, use labeled */
  6628. #else
  6629. binary = XYFT_B; /* For all others use binary */
  6630. #endif /* OS2 */
  6631. #endif /* VMS */
  6632. gnf_binary = binary; /* Prevailing type for gnfile() */
  6633. debug(F101,"whoarewe setting binary","",binary);
  6634. }
  6635. }
  6636. }
  6637. #endif /* NOXFER */