ckcfn3.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /* C K C F N 3 -- Packet buffer management for C-Kermit */
  2. /* (plus assorted functions tacked on at the end) */
  3. /*
  4. Author: Frank da Cruz <fdc@columbia.edu>,
  5. Columbia University Academic Information Systems, New York City.
  6. Copyright (C) 1985, 2010,
  7. Trustees of Columbia University in the City of New York.
  8. All rights reserved. See the C-Kermit COPYING.TXT file or the
  9. copyright text in the ckcmai.c module for disclaimer and permissions.
  10. */
  11. /*
  12. Note -- if you change this file, please amend the version number and date at
  13. the top of ckcfns.c accordingly.
  14. */
  15. #include "ckcsym.h"
  16. #include "ckcdeb.h"
  17. #include "ckcasc.h"
  18. #include "ckcker.h"
  19. #include "ckcxla.h"
  20. /* C K M K D I R -- Create a directory */
  21. /*
  22. Call with:
  23. int fc = 0 to create, nonzero to remove, a directory.
  24. char * s = pointer to name of directory to create or remove.
  25. char ** r = address of pointer to return name or message.
  26. int m = 1 to print error messages, 0 to be silent.
  27. int cvt = 1 means convert s from standard format to local format;
  28. 0 means use s as is.
  29. Returns:
  30. 0 on success (directory was created or removed).
  31. -1 when attempt to create the directory failed.
  32. -2 on internal error (e.g. no code for creating directories).
  33. On success, the name is pointed to by p.
  34. On failure, the reason is pointed to by p.
  35. */
  36. #ifdef CK_MKDIR
  37. static char ckmkdbuf[CKMAXPATH+1];
  38. #else
  39. #ifdef datageneral
  40. static char ckmkdbuf[CKMAXPATH+1];
  41. #endif /* datageneral */
  42. #endif /* CK_MKDIR */
  43. #ifdef CK_MKDIR
  44. int
  45. ckmkdir(fc,s,r,m,cvt) int fc; char * s; char ** r; int m; int cvt; {
  46. int x, rc = -2;
  47. char tmpbuf[CKMAXPATH+1];
  48. char buf2[CKMAXPATH+1];
  49. if (!s) s = "";
  50. debug(F110,"ckmkdir 1 fc",s,fc);
  51. if (!*s) {
  52. ckmakmsg(ckmkdbuf,
  53. CKMAXPATH+1,
  54. (fc == 0) ? "mkdir" : "rmdir",
  55. ": no name given",
  56. NULL,
  57. NULL
  58. );
  59. *r = ckmkdbuf;
  60. return(-2);
  61. }
  62. #ifdef datageneral
  63. /* Come back and make this nicer later if anybody notices */
  64. if (fc == 0) { /* mkdir */
  65. rc = createdir(s,0);
  66. } else { /* rmdir */
  67. /* AOS/VS rmdir() is a no-op. */
  68. ckmakmsg(tmpbuf,CKMAXPATH+1,"delete ",s,NULL,NULL);
  69. debug(F110,"ckmkdir 2",tmpbuf,0);
  70. rc = system(tmpbuf);
  71. }
  72. *r = NULL;
  73. #else /* not datageneral */
  74. /* First make sure the name has an acceptable directory-name format */
  75. #ifdef VMS
  76. {
  77. char *p = s;
  78. int lb = 0, rb = 0, sl = 0;
  79. while (*p) {
  80. if (*p == '[' || *p == '<') lb++; /* Count brackets */
  81. else if (*p == ']' || *p == '>') rb++;
  82. else if (*p == '/') sl++; /* and slashes */
  83. p++;
  84. }
  85. if (lb != 1 && rb != 1 && sl == 0 && p > s && *(p-1) != ':') {
  86. /* Probably just a word - convert to VMS format */
  87. ckmakmsg(buf2,
  88. CKMAXPATH+1,
  89. "[",
  90. (*s == '.') ? "" : ".",
  91. s,
  92. "]"
  93. );
  94. s = buf2;
  95. } else if (lb == 0 && rb == 0 && sl != 0 && p > s && *(p-1) != ':') {
  96. int flag = 0;
  97. /* Seems to be in UNIX format */
  98. x = strlen(s);
  99. if (x > 0 && s[x-1] != '/')
  100. flag = 1;
  101. ckmakmsg(buf2,CKMAXPATH+1,s,flag ? "/" : "",NULL,NULL);
  102. s = buf2;
  103. }
  104. if (s == buf2) {
  105. ckstrncpy(tmpbuf,s,CKMAXPATH+1);
  106. s = tmpbuf;
  107. }
  108. debug(F110,"ckmkdir 2+VMS",s,0);
  109. }
  110. #else
  111. #ifdef UNIXOROSK
  112. #ifdef DTILDE
  113. s = tilde_expand(s);
  114. #endif /* DTILDE */
  115. ckstrncpy(tmpbuf,s,CKMAXPATH+1);
  116. s = tmpbuf;
  117. x = strlen(s);
  118. if (x > 0 && s[x-1] != '/') { /* Must end in "/" for zmkdir() */
  119. s[x] = '/';
  120. s[x+1] = NUL;
  121. debug(F110,"ckmkdir 2+UNIXOROSK",s,0);
  122. }
  123. #else /* UNIXOROSK */
  124. #ifdef OS2
  125. ckstrncpy(tmpbuf,s,CKMAXPATH+1);
  126. s = tmpbuf;
  127. x = strlen(s);
  128. if (fc == 0 && x > 0 && s[x-1] != '/') { /* Must end in "/" for zmkdir() */
  129. s[x] = '/';
  130. s[x+1] = NUL;
  131. debug(F110,"ckmkdir 2+OS2",s,0);
  132. }
  133. #endif /* OS2 */
  134. #endif /* UNIXOROSK */
  135. #endif /* VMS */
  136. #ifdef NZLTOR
  137. /* Server is calling us, so convert to local format if necessary */
  138. if (cvt) {
  139. nzrtol(s,(char *)buf2,1,PATH_ABS,CKMAXPATH);
  140. s = buf2;
  141. debug(F110,"ckmkdir 3",s,0);
  142. }
  143. #endif /* NZLTOR */
  144. debug(F110,"ckmkdir 4",s,0);
  145. if (fc == 0) { /* Making */
  146. #ifdef CK_MKDIR
  147. rc = zmkdir(s);
  148. #else
  149. #ifdef NT
  150. rc = _mkdir(s);
  151. #else
  152. rc = mkdir(s,0777);
  153. #endif /* NT */
  154. #endif /* CK_MKDIR */
  155. } else { /* Removing */
  156. #ifdef ZRMDIR
  157. rc = zrmdir(s);
  158. #else
  159. #ifdef NT
  160. rc = _rmdir(s);
  161. #else
  162. #ifdef OSK
  163. rc = -2;
  164. #else
  165. rc = rmdir(s);
  166. #endif /* OSK */
  167. #endif /* NT */
  168. #endif /* ZRMDIR */
  169. }
  170. #endif /* datageneral */
  171. debug(F101,"ckmkdir rc","",rc);
  172. if (rc == -2) {
  173. ckmakmsg(ckmkdbuf,
  174. CKMAXPATH,
  175. "Directory ",
  176. (fc == 0) ? "creation" : "removal",
  177. "not implemented in this version of C-Kermit",
  178. NULL
  179. );
  180. *r = ckmkdbuf;
  181. if (m) printf("%s\n",*r);
  182. } else if (rc < 0) {
  183. if (m) perror(s);
  184. ckmakmsg(ckmkdbuf,CKMAXPATH,s,": ",ck_errstr(),NULL);
  185. *r = ckmkdbuf;
  186. } else if (fc == 0 && zfnqfp(s,CKMAXPATH,ckmkdbuf)) {
  187. *r = ckmkdbuf;
  188. } else if (fc != 0) {
  189. ckmakmsg(ckmkdbuf,CKMAXPATH,s,": removed",NULL,NULL);
  190. *r = ckmkdbuf;
  191. }
  192. return(rc);
  193. }
  194. #endif /* CK_MKDIR */
  195. #ifndef NOXFER /* Rest of this file... */
  196. #ifndef NODISPO
  197. #ifdef pdp11
  198. #define NODISPO
  199. #endif /* pdpd11 */
  200. #endif /* NODISPO */
  201. extern int pipesend;
  202. #ifdef PIPESEND
  203. extern char ** sndfilter;
  204. #endif /* PIPESEND */
  205. extern int unkcs, wmax, wcur, discard, bctu, bctl, local, fdispla, what,
  206. sendmode, opnerr, dest, epktrcvd, epktsent, filestatus, eofmethod, dispos,
  207. fncnv, fnrpath;
  208. extern char * ofn2;
  209. extern char * rfspec, * sfspec, * prfspec, * psfspec, * rrfspec, * prrfspec;
  210. extern char ofn1[];
  211. extern int ofn1x;
  212. extern char * ofperms;
  213. #ifdef VMS
  214. extern int batch;
  215. #else
  216. extern int backgrd;
  217. #endif /* VMS */
  218. extern int xflg, remfile, remappd;
  219. extern CHAR *data;
  220. extern char filnam[];
  221. #ifndef NOFRILLS
  222. extern int rprintf, rmailf; /* REMOTE MAIL, PRINT */
  223. char optbuf[OPTBUFLEN]; /* Options for MAIL or REMOTE PRINT */
  224. #endif /* NOFRILLS */
  225. extern int wslots;
  226. extern int fblksiz, frecl, forg, frecfm, fncact, fncsav, fcctrl, lf_opts;
  227. extern CHAR * srvcmd;
  228. extern int srvcmdlen;
  229. extern int binary, spsiz;
  230. extern int pktnum, cxseen, czseen, nfils, stdinf;
  231. extern int memstr, stdouf, keep, sndsrc, hcflg;
  232. extern int server, en_cwd, en_mai, en_pri;
  233. /* Attributes in/out enabled flags */
  234. extern int
  235. atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
  236. attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso;
  237. #ifdef CK_PERMS
  238. extern int atlpri, atlpro, atgpri, atgpro;
  239. #endif /* CK_PERMS */
  240. #ifdef STRATUS
  241. extern int atfrmi, atfrmo, atcrei, atcreo, atacti, atacto;
  242. #endif /* STRATUS */
  243. #ifdef datageneral
  244. extern int quiet;
  245. #endif /* datageneral */
  246. extern long filcnt;
  247. extern CK_OFF_T fsize, ffc, tfc, sendstart, calibrate;
  248. CK_OFF_T rs_len;
  249. #ifndef NOCSETS
  250. _PROTOTYP (VOID setxlate, (void));
  251. extern int tcharset, fcharset;
  252. extern int ntcsets, xlatype, xfrxla;
  253. extern struct csinfo tcsinfo[], fcsinfo[];
  254. #endif /* NOCSETS */
  255. /* Variables global to Kermit that are defined in this module */
  256. #ifdef CKXXCHAR /* DOUBLE / IGNORE char table */
  257. int dblflag = 0;
  258. int ignflag = 0;
  259. short dblt[256] = {
  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,0,
  262. 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,
  263. 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,
  264. 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,
  265. 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,
  266. 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,
  267. 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
  268. };
  269. #endif /* CKXXCHAR */
  270. int winlo; /* packet number at low window edge */
  271. int sbufnum; /* number of free buffers */
  272. int dum001 = 1234; /* protection... */
  273. int sbufuse[MAXWS]; /* buffer in-use flag */
  274. int dum003 = 1111;
  275. int rbufnum; /* number of free buffers */
  276. int dum002 = 4321; /* more protection */
  277. int rbufuse[MAXWS]; /* buffer in-use flag */
  278. int sseqtbl[64]; /* sequence # to buffer # table */
  279. int rseqtbl[64]; /* sequence # to buffer # table */
  280. int sacktbl[64]; /* sequence # ack table */
  281. int o_isopen = 0, i_isopen = 0; /* Input & output files are open */
  282. #ifdef DYNAMIC
  283. struct pktinfo *s_pkt = NULL; /* array of pktinfo structures */
  284. struct pktinfo *r_pkt = NULL; /* array of pktinfo structures */
  285. #else
  286. struct pktinfo s_pkt[MAXWS]; /* array of pktinfo structures */
  287. struct pktinfo r_pkt[MAXWS]; /* array of pktinfo structures */
  288. #endif /* DYNAMIC */
  289. #ifdef DEBUG
  290. char xbuf[200]; /* For debug logging */
  291. #endif /* DEBUG */
  292. #ifdef DYNAMIC
  293. CHAR *bigsbuf = NULL, *bigrbuf = NULL;
  294. #else
  295. char bigsbt[8]; /* Protection (shouldn't need this). */
  296. /* BUT DON'T REMOVE IT! */
  297. CHAR bigsbuf[SBSIZ + 5]; /* Send-packet buffer area */
  298. char bigrbt[8]; /* Safety padding */
  299. CHAR bigrbuf[RBSIZ + 5]; /* Receive-packet area */
  300. #endif
  301. int bigsbsiz = SBSIZ; /* Sizes of big send & rcv buffers. */
  302. int bigrbsiz = RBSIZ;
  303. #ifdef VMS
  304. int zchkpath(char *s);
  305. #endif /* VMS */
  306. /* FUNCTIONS */
  307. VOID
  308. dofast() {
  309. long maxbufsiz = RBSIZ; /* Configuration parameters */
  310. int maxpktsiz = MAXSP;
  311. extern int spsizf, /* For bug in IRIX Telnet server */
  312. rpsiz, urpsiz, spsizr, spmax, wslotr;
  313. extern struct ck_p ptab[];
  314. if (maxpktsiz < 40) /* Long packet length */
  315. maxpktsiz = 40;
  316. else if (maxpktsiz > 4000)
  317. maxpktsiz = 4000;
  318. wslotr = maxbufsiz / maxpktsiz;
  319. if (wslotr > MAXWS) /* Window slots */
  320. wslotr = MAXWS;
  321. if (wslotr > 30)
  322. wslotr = 30;
  323. else if (wslotr < 1)
  324. wslotr = 1;
  325. urpsiz = adjpkl(maxpktsiz,wslotr,maxbufsiz);
  326. ptab[PROTO_K].rpktlen = urpsiz;
  327. rpsiz = (urpsiz > 94) ? 94 : urpsiz; /* Max non-long packet length */
  328. debug(F111,"dofast","uprsiz",urpsiz);
  329. #ifdef IRIX
  330. #ifndef IRIX65
  331. /* IRIX Telnet server chops off writes longer than 4K */
  332. spsiz = spmax = spsizr = urpsiz;
  333. debug(F101,"doarg Q IRIX spsiz","",spsiz);
  334. spsizf = 1;
  335. #endif /* IRIX65 */
  336. #endif /* IRIX */
  337. #ifdef CK_SPEED
  338. setprefix(PX_CAU); /* Cautious unprefixing */
  339. #endif /* CK_SPEED */
  340. }
  341. /* For sanity, use "i" for buffer slots, "n" for packet numbers. */
  342. /* I N I B U F S */
  343. /*
  344. Allocates the big send and receive buffers.
  345. Call with size for big send buffer (s) and receive buffer (r).
  346. These sizes can be different.
  347. Attempts to allocate buffers of the requested size, but if it can't,
  348. it will allocate smaller ones.
  349. Sets global variables bigsbsiz and bigrbsiz to the actual sizes,
  350. and bigsbuf and bigrbuf pointing to the actual buffers.
  351. Designed to be called more than once.
  352. Returns 0 on success, -1 on failure.
  353. */
  354. CHAR *bigbufp = NULL;
  355. int
  356. inibufs(s,r) int s, r; {
  357. #ifdef DYNAMIC
  358. unsigned
  359. int size;
  360. #ifdef OS2
  361. unsigned /* Don't you wish everybody had unsigned long... */
  362. #endif /* OS2 */
  363. long z;
  364. int x;
  365. debug(F101,"inibufs s","",s);
  366. debug(F101,"inibufs r","",r);
  367. if (s < 80 || r < 80) return(-1); /* Validate arguments. */
  368. if (!s_pkt) { /* Allocate packet info structures */
  369. if (!(s_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
  370. fatal("ini_pkts: no memory for s_pkt");
  371. }
  372. for (x = 0; x < MAXWS; x++)
  373. s_pkt[x].pk_adr = NULL; /* Initialize addresses */
  374. if (!r_pkt) {
  375. if (!(r_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
  376. fatal("ini_pkts: no memory for s_pkt");
  377. }
  378. for (x = 0; x < MAXWS; x++)
  379. r_pkt[x].pk_adr = NULL; /* Initialize addresses */
  380. if (!srvcmd) { /* Allocate srvcmd buffer */
  381. srvcmd = (CHAR *) malloc(r + 100);
  382. if (!srvcmd) return(-1);
  383. srvcmdlen = r + 99;
  384. *srvcmd = NUL;
  385. }
  386. if (bigbufp) { /* Free previous buffers, if any. */
  387. free(bigbufp);
  388. bigbufp = NULL;
  389. }
  390. size = s + r + 40; /* Combined requested size + padding */
  391. z = (unsigned) s + (unsigned) r + 40;
  392. debug(F101,"inibufs size 1","",size);
  393. debug(F101,"inibufs size z","",z);
  394. if ((long) size != z) {
  395. debug(F100,"inibufs overflow","",0);
  396. size = 65535;
  397. }
  398. /* Try to get the space. If malloc fails, try to get a little less. */
  399. /* (Obviously, this algorithm can be refined.) */
  400. while (!(bigbufp = (CHAR *) malloc(size))) {
  401. debug(F101,"inibufs bigbuf malloc failed","",size);
  402. size = (size * 2) / 3; /* Failed, cut size by 1/3. */
  403. if (size < 200) /* Try again until too small. */
  404. return(-1);
  405. }
  406. debug(F101,"inibufs size 2","",size); /* OK, we got some space. */
  407. /*
  408. Now divide the allocated space between the send and receive buffers in the
  409. requested proportion. The natural formula would be (s / (s + r)) * size
  410. (for the send buffer), but that doesn't work with integer arithmetic and we
  411. can't use floating point because some machines don't have it. This can be
  412. rearranged as (s * size) / (s + r). But (s * size) can be VERY large, too
  413. large for 32 bits. So let's do it this way. This arithmetic works for
  414. buffer sizes up to about 5,000,000.
  415. */
  416. #define FACTOR 20L
  417. z = ( (long) s * FACTOR ) / ( (long) s + (long) r );
  418. x = ( z * ( (long) size / FACTOR ) );
  419. if (x < 0) return(-1); /* Catch overflow */
  420. bigsbsiz = x - 5; /* Size of send buffer */
  421. bigsbuf = bigbufp; /* Address of send buffer */
  422. debug(F101,"inibufs bigsbsiz","",bigsbsiz);
  423. bigrbsiz = size - x - 5; /* Size of receive buffer */
  424. bigrbuf = bigbufp + x; /* Addresss of receive buffer */
  425. debug(F101,"inibufs bigrbsiz","",bigrbsiz);
  426. return(0); /* Success */
  427. #else /* No dynamic allocation */
  428. bigsbsiz = SBSIZ; /* Just use the symbols */
  429. bigrbsiz = RBSIZ; /* ... */
  430. return(0); /* Success. */
  431. #endif /* DYNAMIC */
  432. }
  433. /* M A K E B U F -- Makes and clears a new buffers. */
  434. /* Call with: */
  435. /* slots: number of buffer slots to make, 1 to 32 */
  436. /* bufsiz: size of the big buffer */
  437. /* buf: address of the big buffer */
  438. /* xx: pointer to array of pktinfo structures for these buffers */
  439. /* Subdivides the big buffer into "slots" buffers. */
  440. /* Returns: */
  441. /* -1 if too many or too few slots requested, */
  442. /* -2 if slots would be too small. */
  443. /* n (positive) on success = size of one buffer. */
  444. /* with pktinfo structure initialized for this set of buffers. */
  445. int
  446. makebuf(slots,bufsiz,buf,xx)
  447. /* makebuf */ int slots, bufsiz; CHAR buf[]; struct pktinfo *xx; {
  448. CHAR *a;
  449. int i, size;
  450. debug(F101,"makebuf","",slots);
  451. debug(F101,"makebuf bufsiz","",bufsiz);
  452. debug(F101,"makebuf MAXWS","",MAXWS);
  453. if (slots > MAXWS || slots < 1) return(-1);
  454. if (bufsiz < slots * 10 ) return(-2);
  455. size = bufsiz / slots; /* Divide up the big buffer. */
  456. a = buf; /* Address of first piece. */
  457. for (i = 0; i < slots; i++) {
  458. struct pktinfo *x = &xx[i];
  459. x->bf_adr = a; /* Address of this buffer */
  460. x->bf_len = size; /* Length of this buffer */
  461. x->pk_len = 0; /* Length of data field */
  462. x->pk_typ = ' '; /* packet type */
  463. x->pk_seq = -1; /* packet sequence number */
  464. x->pk_rtr = 0; /* retransmissions */
  465. *a = '\0'; /* Clear the buffer */
  466. a += size; /* Position to next buffer slot */
  467. }
  468. return(size);
  469. }
  470. /* M A K S B U F -- Makes the send-packet buffer */
  471. int
  472. mksbuf(slots) int slots; {
  473. int i, x;
  474. sbufnum = 0;
  475. if ((x = makebuf(slots,bigsbsiz,bigsbuf,s_pkt)) < 0) {
  476. debug(F101,"mksbuf makebuf return","",x);
  477. return(x);
  478. }
  479. debug(F101,"mksbuf makebuf return","",x);
  480. for (i = 0; i < 64; i++) { /* Initialize sequence-number- */
  481. sseqtbl[i] = -1; /* to-buffer-number table. */
  482. sacktbl[i] = 0;
  483. }
  484. for (i = 0; i < MAXWS; i++)
  485. sbufuse[i] = 0; /* Mark each buffer as free */
  486. sbufnum = slots;
  487. wcur = 0;
  488. return(x);
  489. }
  490. /* M A K R B U F -- Makes the receive-packet buffer */
  491. int
  492. mkrbuf(slots) int slots; {
  493. int i, x;
  494. rbufnum = 0;
  495. if ((x = makebuf(slots,bigrbsiz,bigrbuf,r_pkt)) < 0) {
  496. debug(F101,"mkrbuf makebuf return","",x);
  497. return(x);
  498. }
  499. debug(F101,"mkrbuf makebuf return","",x);
  500. for (i = 0; i < 64; i++) { /* Initialize sequence-number- */
  501. rseqtbl[i] = -1; /* to-buffer-number table. */
  502. }
  503. for (i = 0; i < MAXWS; i++)
  504. rbufuse[i] = 0; /* Mark each buffer as free */
  505. rbufnum = slots;
  506. wcur = 0;
  507. return(x);
  508. }
  509. /* W I N D O W -- Resize the window to n */
  510. int
  511. window(n) int n; {
  512. debug(F101,"window","",n);
  513. if (n < 1 || n > MAXWS) return(-1);
  514. if (mksbuf(n) < 0) return(-1);
  515. if (mkrbuf(n) < 0) return(-1);
  516. wslots = n;
  517. #ifdef DEBUG
  518. if (deblog) dumpsbuf();
  519. if (deblog) dumprbuf();
  520. #endif /* DEBUG */
  521. return(0);
  522. }
  523. /* G E T S B U F -- Allocate a send-buffer. */
  524. /* Call with packet sequence number to allocate buffer for. */
  525. /* Returns: */
  526. /* -4 if argument is invalid (negative, or greater than 63) */
  527. /* -3 if buffers were thought to be available but really weren't (bug!) */
  528. /* -2 if the number of free buffers is negative (bug!) */
  529. /* -1 if no free buffers. */
  530. /* 0 or positive, packet sequence number, with buffer allocated for it. */
  531. int
  532. getsbuf(n) int n; { /* Allocate a send-buffer */
  533. int i;
  534. CHAR * p = NULL;
  535. if (n < 0 || n > 63) {
  536. debug(F101,"getsbuf bad arg","",n);
  537. return(-4); /* Bad argument */
  538. }
  539. debug(F101,"getsbuf packet","",n);
  540. /* debug(F101,"getsbuf, sbufnum","",sbufnum); */
  541. if (sbufnum == 0) return(-1); /* No free buffers. */
  542. if (sbufnum < 0) return(-2); /* Shouldn't happen. */
  543. for (i = 0; i < wslots; i++) /* Find the first one not in use. */
  544. if (sbufuse[i] == 0) { /* Got one? */
  545. sbufuse[i] = 1; /* Mark it as in use. */
  546. sbufnum--; /* One less free buffer. */
  547. *s_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
  548. s_pkt[i].pk_seq = n; /* Put in the sequence number */
  549. sseqtbl[n] = i; /* Back pointer from sequence number */
  550. sacktbl[n] = 0; /* ACK flag */
  551. s_pkt[i].pk_len = 0; /* Data field length now zero. */
  552. s_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
  553. s_pkt[i].pk_rtr = 0; /* Zero the retransmission count */
  554. p = s_pkt[i].bf_adr + 7; /* Set global "data" address. */
  555. debug(F101,"getsbuf p","",0);
  556. data = p;
  557. if (!data) {
  558. debug(F100,"getsbuf data == NULL","",0);
  559. return(-3);
  560. }
  561. if ((what & (W_SEND|W_REMO)) && (++wcur > wmax))
  562. wmax = wcur; /* For statistics. */
  563. /* debug(F101,"getsbuf wcur","",wcur); */
  564. return(n); /* Return its index. */
  565. }
  566. sbufnum = 0; /* Didn't find one. */
  567. return(-3); /* Shouldn't happen! */
  568. }
  569. int
  570. getrbuf() { /* Allocate a receive buffer */
  571. int i;
  572. #ifdef COMMENT
  573. /* This code is pretty stable by now... */
  574. /* Looks like we might need this after all */
  575. debug(F101,"getrbuf rbufnum","",rbufnum);
  576. debug(F101,"getrbuf wslots","",wslots);
  577. debug(F101,"getrbuf dum002","",dum002);
  578. debug(F101,"getrbuf dum003","",dum003);
  579. #endif /* COMMENT */
  580. if (rbufnum == 0) return(-1); /* No free buffers. */
  581. if (rbufnum < 0) return(-2); /* Shouldn't happen. */
  582. for (i = 0; i < wslots; i++) /* Find the first one not in use. */
  583. if (rbufuse[i] == 0) { /* Got one? */
  584. rbufuse[i] = 1; /* Mark it as in use. */
  585. *r_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
  586. rbufnum--; /* One less free buffer. */
  587. debug(F101,"getrbuf new rbufnum","",rbufnum);
  588. if ((what & W_RECV) && (++wcur > wmax))
  589. wmax = wcur; /* For statistics. */
  590. /* debug(F101,"getrbuf wcur","",wcur); */
  591. return(i); /* Return its index. */
  592. }
  593. /* debug(F101,"getrbuf foulup","",i); */
  594. rbufnum = 0; /* Didn't find one. */
  595. return(-3); /* Shouldn't happen! */
  596. }
  597. /* F R E E S B U F -- Free send-buffer for given packet sequence number */
  598. /* Returns: */
  599. /* 1 upon success */
  600. /* -1 if specified buffer does not exist */
  601. int
  602. freesbuf(n) int n; { /* Release send-buffer for packet n. */
  603. int i;
  604. debug(F101,"freesbuf","",n);
  605. if (n < 0 || n > 63) /* No such packet. */
  606. return(-1);
  607. i = sseqtbl[n]; /* Get the window slot number. */
  608. if (i > -1 && i <= wslots) {
  609. sseqtbl[n] = -1; /* If valid, remove from seqtbl */
  610. sbufnum++; /* and count one more free buffer */
  611. sbufuse[i] = 0; /* and mark it as free, */
  612. if (what & (W_SEND|W_REMO)) /* decrement active slots */
  613. wcur--; /* for statistics and display. */
  614. } else {
  615. debug(F101," sseqtbl[n]","",sseqtbl[n]);
  616. return(-1);
  617. }
  618. /* The following is done only so dumped buffers will look right. */
  619. if (1) {
  620. *s_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
  621. s_pkt[i].pk_seq = -1; /* Invalidate the sequence number */
  622. s_pkt[i].pk_len = 0; /* Data field length now zero. */
  623. s_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
  624. s_pkt[i].pk_rtr = 0; /* And the retries field. */
  625. }
  626. return(1);
  627. }
  628. int
  629. freerbuf(i) int i; { /* Release receive-buffer slot "i". */
  630. int n;
  631. /* NOTE !! Currently, this function frees the indicated buffer, but */
  632. /* does NOT erase the data. The program counts on this. Will find a */
  633. /* better way later.... */
  634. /* debug(F101,"freerbuf, slot","",i); */
  635. if (i < 0 || i >= wslots) { /* No such slot. */
  636. debug(F101,"freerbuf no such slot","",i);
  637. return(-1);
  638. }
  639. n = r_pkt[i].pk_seq; /* Get the packet sequence number */
  640. debug(F101,"freerbuf packet","",n);
  641. if (n > -1 && n < 64) /* If valid, remove from seqtbl */
  642. rseqtbl[n] = -1;
  643. if (rbufuse[i] != 0) { /* If really allocated, */
  644. rbufuse[i] = 0; /* mark it as free, */
  645. rbufnum++; /* and count one more free buffer. */
  646. if (what & W_RECV) /* Keep track of current slots */
  647. wcur--; /* for statistics and display */
  648. debug(F101,"freerbuf rbufnum","",rbufnum);
  649. }
  650. /* The following is done only so dumped buffers will look right. */
  651. if (1) {
  652. /* *r_pkt[i].bf_adr = '\0'; */ /* Zero the buffer data field */
  653. r_pkt[i].pk_seq = -1; /* And from packet list */
  654. r_pkt[i].pk_len = 0; /* Data field length now zero. */
  655. r_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
  656. r_pkt[i].pk_rtr = 0; /* And the retries field. */
  657. }
  658. return(1);
  659. }
  660. /* This is like freerbuf, except it's called with a packet sequence number */
  661. /* rather than a packet buffer index. */
  662. VOID
  663. freerpkt(seq) int seq; {
  664. int k;
  665. debug(F101,"freerpkt seq","",seq);
  666. k = rseqtbl[seq];
  667. /* debug(F101,"freerpkt k","",k); */
  668. if (k > -1) {
  669. k = freerbuf(k);
  670. /* debug(F101,"freerpkt freerbuf","",k); */
  671. }
  672. }
  673. /* C H K W I N -- Check if packet n is in window. */
  674. /* Returns: */
  675. /* 0 if it is in the current window, */
  676. /* +1 if it would have been in previous window (e.g. if ack was lost), */
  677. /* -1 if it is outside any window (protocol error), */
  678. /* -2 if either of the argument packet numbers is out of range. */
  679. /* Call with packet number to check (n), lowest packet number in window */
  680. /* (bottom), and number of slots in window (slots). */
  681. int
  682. chkwin(n,bottom,slots) int n, bottom, slots; {
  683. int top, prev;
  684. debug(F101,"chkwin packet","",n);
  685. debug(F101,"chkwin winlo","",bottom);
  686. debug(F101,"chkwin slots","",slots);
  687. /* First do the easy and common cases, where the windows are not split. */
  688. if (n < 0 || n > 63 || bottom < 0 || bottom > 63)
  689. return(-2);
  690. if (n == bottom) return(0); /* In a perfect world... */
  691. top = bottom + slots; /* Calculate window top. */
  692. if (top < 64 && n < top && n >= bottom)
  693. return(0); /* In current window. */
  694. prev = bottom - slots; /* Bottom of previous window. */
  695. if (prev > -1 && n < bottom && n > prev)
  696. return(1); /* In previous. */
  697. /* Now consider the case where the current window is split. */
  698. if (top > 63) { /* Wraparound... */
  699. top -= 64; /* Get modulo-64 sequence number */
  700. if (n < top || n >= bottom) {
  701. return(0); /* In current window. */
  702. } else { /* Not in current window. */
  703. if (n < bottom && n >= prev) /* Previous window can't be split. */
  704. return(1); /* In previous window. */
  705. else
  706. return(-1); /* Not in previous window. */
  707. }
  708. }
  709. /* Now the case where current window not split, but previous window is. */
  710. if (prev < 0) { /* Is previous window split? */
  711. prev += 64; /* Yes. */
  712. if (n < bottom || n >= prev)
  713. return(1); /* In previous window. */
  714. } else { /* Previous window not split. */
  715. if (n < bottom && n >= prev)
  716. return(1); /* In previous window. */
  717. }
  718. /* It's not in the current window, and not in the previous window... */
  719. return(-1); /* So it's not in any window. */
  720. }
  721. int
  722. dumpsbuf() { /* Dump send-buffers */
  723. #ifdef DEBUG
  724. int j, x, z; /* to debug log. */
  725. if (! deblog) return(0);
  726. x = zsoutl(ZDFILE,"SEND BUFFERS:");
  727. if (x < 0) {
  728. deblog = 0;
  729. return(0);
  730. }
  731. x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
  732. if (x < 0) {
  733. deblog = 0;
  734. return(0);
  735. }
  736. for (j = 0; j < wslots; j++) {
  737. if (!sbufuse[j])
  738. continue;
  739. z = ((unsigned long)(s_pkt[j].bf_adr)) & 0xffff;
  740. sprintf(xbuf, /* safe (200) */
  741. "%4d%6d%10d%5d%6d%4c%5d%6d\n",
  742. j,
  743. sbufuse[j],
  744. /* Avoid warnings when addresses are bigger than ints */
  745. z,
  746. s_pkt[j].bf_len,
  747. s_pkt[j].pk_len,
  748. s_pkt[j].pk_typ,
  749. s_pkt[j].pk_seq,
  750. s_pkt[j].pk_rtr
  751. );
  752. if (zsout(ZDFILE,xbuf) < 0) {
  753. deblog = 0;
  754. return(0);
  755. }
  756. if (s_pkt[j].pk_adr) {
  757. x = (int)strlen((char *) s_pkt[j].pk_adr);
  758. if (x)
  759. sprintf(xbuf, /* safe (checked) */
  760. "[%.72s%s]\n",s_pkt[j].pk_adr, x > 72 ? "..." : "");
  761. else
  762. sprintf(xbuf,"[(empty string)]\n"); /* safe (200) */
  763. } else {
  764. sprintf(xbuf,"[(null pointer)]\n"); /* safe (200) */
  765. }
  766. if (zsout(ZDFILE,xbuf) < 0) {
  767. deblog = 0;
  768. return(0);
  769. }
  770. }
  771. sprintf(xbuf,"free: %d, winlo: %d\n", sbufnum, winlo); /* safe (200) */
  772. if (zsout(ZDFILE,xbuf) < 0) {
  773. deblog = 0;
  774. return(0);
  775. }
  776. #endif /* DEBUG */
  777. return(0);
  778. }
  779. int
  780. dumprbuf() { /* Dump receive-buffers */
  781. #ifdef DEBUG
  782. int j, x, z;
  783. if (! deblog) return(0);
  784. if (zsoutl(ZDFILE,"RECEIVE BUFFERS:") < 0) {
  785. deblog = 0;
  786. return(0);
  787. }
  788. x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
  789. if (x < 0) {
  790. deblog = 0;
  791. return(0);
  792. }
  793. for ( j = 0; j < wslots; j++ ) {
  794. if (!rbufuse[j])
  795. continue;
  796. z = ((unsigned long)(r_pkt[j].bf_adr)) & 0xffff;
  797. sprintf(xbuf, /* 200, safe */
  798. "%4d%6d%10d%5d%6d%4c%5d%6d\n",
  799. j,
  800. rbufuse[j],
  801. /* Avoid warnings when addresses are bigger than ints */
  802. z,
  803. r_pkt[j].bf_len,
  804. r_pkt[j].pk_len,
  805. r_pkt[j].pk_typ,
  806. r_pkt[j].pk_seq,
  807. r_pkt[j].pk_rtr
  808. );
  809. if (zsout(ZDFILE,xbuf) < 0) {
  810. deblog = 0;
  811. return(0);
  812. }
  813. x = (int)strlen((char *)r_pkt[j].bf_adr);
  814. sprintf(xbuf, /* safe (checked) */
  815. "[%.72s%s]\n",r_pkt[j].bf_adr, x > 72 ? "..." : "");
  816. if (zsout(ZDFILE,xbuf) < 0) {
  817. deblog = 0;
  818. return(0);
  819. }
  820. }
  821. sprintf(xbuf,"free: %d, winlo: %d\n", rbufnum, winlo); /* safe (200) */
  822. if (zsout(ZDFILE,xbuf) < 0) {
  823. deblog = 0;
  824. return(0);
  825. }
  826. #endif /* DEBUG */
  827. return(0);
  828. }
  829. /* S A T T R -- Send an Attribute Packet */
  830. /*
  831. Sends attribute packet(s) for the current file. If the info will not
  832. fit into one packet, it can be called repeatedly until all the fields
  833. that will fit are sent.
  834. Call with:
  835. xp == 0 if we're sending a real file (F packet), or:
  836. xp != 0 for screen data (X packet).
  837. And:
  838. flag == 1 for first A packet
  839. flag == 0 for subsequent A packets.
  840. Returns:
  841. 1 or greater if an A packet was sent, or:
  842. 0 if an S-packet was not sent because there was no data to send or
  843. there was no data left that was short enough to send, or:
  844. -1 on any kind of error.
  845. */
  846. /* (don't) #define TSOFORMAT */
  847. /* which was only for making C-Kermit send TSO-Kermit-like A packets */
  848. /* to try to track down a problem somebody reported... */
  849. int
  850. sattr(xp, flag) int xp, flag; { /* Send Attributes */
  851. static int max; /* Maximum length for Attributes */
  852. static short done[95]; /* Field-complete array */
  853. static struct zattr x; /* File attribute struct */
  854. static char xdate[24];
  855. extern char * cksysid;
  856. /* Some extra flags are used because the "done" array is sparse */
  857. int i, j, rc, aln, left = 0, numset = 0, xbin = 0; /* Workers */
  858. int notafile = 0;
  859. char *tp, c;
  860. notafile = sndarray || pipesend ||
  861. #ifdef PIPESEND
  862. sndfilter ||
  863. #endif /* PIPESEND */
  864. calibrate;
  865. debug(F101,"sattr flag","",flag);
  866. if (!flag) /* No more attributes to send */
  867. if (done[xunchar('@')])
  868. return(0);
  869. /* Initialize Attribute mechanism */
  870. if (flag) { /* First time here for this file? */
  871. initattr(&x); /* Blank out all the fields. */
  872. for (j = 0; j < 95; j++) /* Init array of completed fields */
  873. done[j] = 0;
  874. max = maxdata(); /* Get maximum data field length */
  875. if (notafile || xp == 1) { /* Is it not a real file? */
  876. extern char * zzndate();
  877. char * p;
  878. int i;
  879. #ifdef CALIBRATE
  880. if (calibrate) { /* Calibration run... */
  881. x.lengthk = calibrate / 1024L; /* We know the length */
  882. x.length = calibrate;
  883. }
  884. #endif /* CALIBRATE */
  885. x.systemid.val = cksysid; /* System ID */
  886. x.systemid.len = (int)strlen(cksysid);
  887. ckstrncpy(xdate,zzndate(),24);
  888. xdate[8] = SP;
  889. ztime(&p);
  890. for (i = 11; i < 19; i++) /* copy hh:mm:ss */
  891. xdate[i - 2] = p[i]; /* to xdate */
  892. xdate[17] = NUL; /* terminate */
  893. x.date.val = xdate;
  894. x.date.len = 17;
  895. debug(F111,"sattr notafile date",x.date.val,x.date.len);
  896. } else { /* Real file */
  897. rc = zsattr(&x); /* Get attributes for this file */
  898. debug(F101,"sattr zsattr","",rc);
  899. if (rc < 0) /* Can't get 'em so don't send 'em */
  900. return(0);
  901. debug(F101,"sattr init max","",max);
  902. }
  903. }
  904. if (nxtpkt() < 0) /* Got 'em, get next packet number */
  905. return(-1); /* Bad news if we can't */
  906. i = 0; /* Init data field character number */
  907. /* Do each attribute using first-fit method, marking as we go */
  908. /* This is rather long and repititious - could be done more cleverly */
  909. if (atsido && !done[xunchar(c = '.')]) { /* System type */
  910. if (max - i >= x.systemid.len + 2) { /* Enough space ? */
  911. data[i++] = c; /* Yes, add parameter */
  912. data[i++] = tochar(x.systemid.len); /* Add length */
  913. for (j = 0; j < x.systemid.len; j++) /* Add data */
  914. data[i++] = x.systemid.val[j];
  915. numset++; /* Count that we did at least one */
  916. done[xunchar(c)] = 1; /* Mark this attribute as done */
  917. } else /* No */
  918. left++; /* so mark this one left to do */
  919. }
  920. #ifdef STRATUS
  921. if (atcreo && !done[xunchar(c = '$')]) { /* Creator */
  922. if (max - i >= x.creator.len + 2) { /* Enough space ? */
  923. data[i++] = c;
  924. data[i++] = tochar(x.creator.len);
  925. for (j = 0; j < x.creator.len; j++)
  926. data[i++] = x.creator.val[j];
  927. numset++;
  928. done[xunchar(c)] = 1;
  929. } else
  930. left++;
  931. }
  932. if (atacto && !done[xunchar(c = '%')]) { /* File account */
  933. if (max - i >= x.account.len + 2) {
  934. data[i++] = c;
  935. data[i++] = tochar(x.account.len);
  936. for (j = 0; j < x.account.len; j++)
  937. data[i++] = x.account.val[j];
  938. numset++;
  939. done[xunchar(c)] = 1;
  940. } else
  941. left++;
  942. }
  943. if (atfrmo && !done[xunchar(c = '/')]) { /* Packet data format */
  944. if (max - i >= x.recfm.len + 2) {
  945. data[i++] = c;
  946. data[i++] = tochar(x.recfm.len); /* Copy from attr structure */
  947. for (j = 0; j < x.recfm.len; j++)
  948. data[i++] = x.recfm.val[j];
  949. numset++;
  950. done[xunchar(c)] = 1;
  951. } else
  952. left++;
  953. }
  954. #endif /* STRATUS */
  955. xbin = /* Is the transfer in binary mode? */
  956. #ifdef VMS
  957. binary == XYFT_I || binary == XYFT_L || /* IMAGE or LABELED */
  958. !strncmp(x.recfm.val,"F",1) /* or RECFM=Fxxxxxx */
  959. #else
  960. binary /* User said SET FILE TYPE BINARY */
  961. #endif /* VMS */
  962. ;
  963. if (attypo && !done[xunchar(c = '"')]) { /* File type */
  964. if (max - i >= 5) { /* Max length for this field */
  965. data[i++] = c;
  966. if (xbin) { /* Binary */
  967. data[i++] = tochar(2); /* Two characters */
  968. data[i++] = 'B'; /* B for Binary */
  969. data[i++] = '8'; /* 8-bit bytes (note assumption...) */
  970. #ifdef CK_LABELED
  971. if (binary != XYFT_L
  972. #ifdef VMS
  973. && binary != XYFT_I
  974. #endif /* VMS */
  975. )
  976. binary = XYFT_B;
  977. #endif /* CK_LABELED */
  978. } else { /* Text */
  979. #ifdef TSOFORMAT
  980. data[i++] = tochar(1); /* One character */
  981. data[i++] = 'A'; /* A = (extended) ASCII with CRLFs */
  982. #else
  983. data[i++] = tochar(3); /* Three characters */
  984. data[i++] = 'A'; /* A = (extended) ASCII with CRLFs */
  985. data[i++] = 'M'; /* M for carriage return */
  986. data[i++] = 'J'; /* J for linefeed */
  987. #endif /* TSOFORMAT */
  988. #ifdef VMS
  989. binary = XYFT_T; /* We automatically detected text */
  990. #endif /* VMS */
  991. }
  992. numset++;
  993. done[xunchar(c)] = 1;
  994. } else
  995. left++;
  996. }
  997. #ifdef TSOFORMAT
  998. if (attypo && !xbin && !done[xunchar(c = '/')]) { /* Record format */
  999. if (max - i >= 5) {
  1000. data[i++] = c;
  1001. data[i++] = tochar(3); /* Three characters */
  1002. data[i++] = 'A'; /* A = variable with CRLFs */
  1003. data[i++] = 'M'; /* M for carriage return */
  1004. data[i++] = 'J'; /* J for linefeed */
  1005. }
  1006. }
  1007. #endif /* TSOFORMAT */
  1008. if (attypo && !xbin && !done[xunchar(c = '*')]) { /* Text encoding */
  1009. #ifdef NOCSETS
  1010. if (max - i >= 3) {
  1011. data[i++] = c;
  1012. data[i++] = tochar(1); /* Length of value is 1 */
  1013. data[i++] = 'A'; /* A for ASCII */
  1014. numset++;
  1015. done[xunchar(c)] = 1;
  1016. } else
  1017. left++;
  1018. #else
  1019. if (tcharset == TC_TRANSP || !xfrxla) { /* Transfer character set */
  1020. if (max - i >= 3) {
  1021. data[i++] = c; /* Encoding */
  1022. data[i++] = tochar(1); /* Length of value is 1 */
  1023. data[i++] = 'A'; /* A for ASCII (i.e. text) */
  1024. numset++;
  1025. done[xunchar(c)] = 1;
  1026. } else
  1027. left++;
  1028. } else {
  1029. tp = tcsinfo[tcharset].designator;
  1030. if (!tp) tp = "";
  1031. aln = strlen(tp);
  1032. if (aln > 0) {
  1033. if (max - i >= aln + 2) {
  1034. data[i++] = c; /* Encoding */
  1035. data[i++] = tochar(aln+1); /* Length of designator. */
  1036. data[i++] = 'C'; /* Text in specified charset. */
  1037. for (j = 0; j < aln; j++) /* Copy designator */
  1038. data[i++] = *tp++; /* Example: *&I6/100 */
  1039. numset++;
  1040. done[xunchar(c)] = 1;
  1041. } else
  1042. left++;
  1043. } else
  1044. done[xunchar(c)] = 1;
  1045. }
  1046. #endif /* NOCSETS */
  1047. }
  1048. if (atdato && !done[xunchar(c = '#')] && /* Creation date, if any */
  1049. (aln = x.date.len) > 0) {
  1050. if (max - i >= aln + 2) {
  1051. data[i++] = c;
  1052. data[i++] = tochar(aln);
  1053. for (j = 0; j < aln; j++)
  1054. data[i++] = x.date.val[j];
  1055. numset++;
  1056. done[xunchar(c)] = 1;
  1057. } else
  1058. left++;
  1059. }
  1060. /* File length in K */
  1061. if (atleno && !done[xunchar(c = '!')] && x.lengthk > (CK_OFF_T)-1) {
  1062. #ifdef COMMENT
  1063. sprintf((char *) &data[i+2],"%ld",x.lengthk); /* safe */
  1064. #else
  1065. ckstrncpy((char *)&data[i+2],ckfstoa(x.lengthk),32);
  1066. #endif /* COMMENT */
  1067. aln = (int)strlen((char *)(data+i+2));
  1068. if (max - i >= aln + 2) {
  1069. data[i] = c;
  1070. data[i+1] = tochar(aln);
  1071. i += aln + 2;
  1072. numset++;
  1073. done[xunchar(c)] = 1;
  1074. } else {
  1075. data[i] = NUL;
  1076. left++;
  1077. }
  1078. }
  1079. /* File length in bytes */
  1080. if (atleno && !done[xunchar(c = '1')] && x.length > (CK_OFF_T)-1) {
  1081. #ifdef COMMENT
  1082. sprintf((char *) &data[i+2],"%ld",x.length); /* safe */
  1083. #else
  1084. ckstrncpy((char *)&data[i+2],ckfstoa(x.length),32);
  1085. #endif /* COMMENT */
  1086. aln = (int)strlen((char *)(data+i+2));
  1087. if (max - i >= aln + 2) {
  1088. data[i] = c;
  1089. data[i+1] = tochar(aln);
  1090. i += aln + 2;
  1091. numset++;
  1092. done[xunchar(c)] = 1;
  1093. } else {
  1094. data[i] = NUL;
  1095. left++;
  1096. }
  1097. }
  1098. #ifdef CK_PERMS
  1099. if (atlpro && !done[xunchar(c = ',')] && /* Local protection */
  1100. (aln = x.lprotect.len) > 0 && !notafile && xp == 0) {
  1101. if (max - i >= aln + 2) {
  1102. data[i++] = c;
  1103. data[i++] = tochar(aln);
  1104. for (j = 0; j < aln; j++)
  1105. data[i++] = x.lprotect.val[j];
  1106. numset++;
  1107. done[xunchar(c)] = 1;
  1108. } else
  1109. left++;
  1110. }
  1111. if (atgpro && !done[xunchar(c = '-')] && /* Generic protection */
  1112. (aln = x.gprotect.len) > 0 && !notafile && xp == 0) {
  1113. if (max - i >= aln + 2) {
  1114. data[i++] = c;
  1115. data[i++] = tochar(aln);
  1116. for (j = 0; j < aln; j++)
  1117. data[i++] = x.gprotect.val[j];
  1118. numset++;
  1119. done[xunchar(c)] = 1;
  1120. } else
  1121. left++;
  1122. }
  1123. #endif /* CK_PERMS */
  1124. if (atblko && fblksiz && !done[xunchar(c = '(')] &&
  1125. !notafile && xp == 0) { /* Blocksize */
  1126. sprintf((char *) &data[i+2],"%d",fblksiz); /* safe */
  1127. aln = (int)strlen((char *)(data+i+2));
  1128. if (max - i >= aln + 2) {
  1129. data[i] = c;
  1130. data[i+1] = tochar(aln);
  1131. i += aln + 2;
  1132. numset++;
  1133. done[xunchar(c)] = 1;
  1134. } else {
  1135. data[i] = NUL;
  1136. left++;
  1137. }
  1138. }
  1139. #ifndef NOFRILLS
  1140. if ((rprintf || rmailf) && atdiso && /* MAIL, or REMOTE PRINT? */
  1141. !done[xunchar(c = '+')]) {
  1142. aln = (int) strlen(optbuf) + 1; /* Options, if any */
  1143. if (max - i >= aln + 2) {
  1144. data[i++] = c; /* Disposition */
  1145. data[i++] = tochar(aln); /* Options, if any */
  1146. if (rprintf)
  1147. data[i++] = 'P'; /* P for Print */
  1148. else
  1149. data[i++] = 'M'; /* M for Mail */
  1150. for (j = 0; optbuf[j]; j++) /* Copy any options */
  1151. data[i++] = optbuf[j];
  1152. numset++;
  1153. done[xunchar(c)] = 1;
  1154. } else {
  1155. data[i] = NUL;
  1156. left++;
  1157. }
  1158. }
  1159. #endif /* NOFRILLS */
  1160. #ifdef CK_RESEND
  1161. if (sendmode == SM_RESEND && !done[xunchar(c = '+')]) {
  1162. if (max - i >= 3) {
  1163. data[i++] = c; /* Disposition */
  1164. data[i++] = tochar(1);
  1165. data[i++] = 'R'; /* is RESEND */
  1166. numset++;
  1167. done[xunchar(c)] = 1;
  1168. } else
  1169. left++;
  1170. }
  1171. #endif /* CK_RESEND */
  1172. /* End of Attributes -- to be sent only after sending all others */
  1173. debug(F111,"sattr","@",i);
  1174. debug(F101,"sattr numset","",numset);
  1175. debug(F101,"sattr left","",left);
  1176. if ((left == 0 || numset == 0) && !done[xunchar(c = '@')]) {
  1177. if (max - i >= 3) {
  1178. data[i++] = c; /* End of Attributes */
  1179. data[i++] = SP; /* Length 0 */
  1180. data[i] = NUL; /* Make sure it's null-terminated */
  1181. numset++;
  1182. done[xunchar(c)] = 1;
  1183. }
  1184. }
  1185. /* Finished - send the packet off if we have anything in it */
  1186. if (numset) {
  1187. data[i] = NUL; /* Terminate last good field */
  1188. debug(F111,"sattr sending",data,left);
  1189. aln = (int)strlen((char *)data); /* Get overall length of attributes */
  1190. return(spack('A',pktnum,aln,data)); /* Send it */
  1191. } else
  1192. return(0);
  1193. }
  1194. static char *refused = "";
  1195. static char *reason[] = {
  1196. "size", "type", "date", "creator", "account", "area", "password",
  1197. "blocksize", "access", "encoding", "disposition", "protection",
  1198. "protection", "origin", "format",
  1199. "sys-dependent", /* 0 */
  1200. "size", /* 1 */
  1201. "2", /* 2 */
  1202. "3", /* 3 */
  1203. "4", /* 4 */
  1204. "5", /* 5 */
  1205. "6", /* 6 */
  1206. "7", /* 7 */
  1207. "8", /* 8 */
  1208. "9", /* 9 */
  1209. ":", /* : */
  1210. ";", /* ; */
  1211. "<", /* < */
  1212. "=", /* = */
  1213. ">", /* > */
  1214. "name", /* ? */
  1215. "@"
  1216. };
  1217. static int nreason = sizeof(reason) / sizeof(char *);
  1218. int rejection = -1;
  1219. char *
  1220. getreason(s) char *s; { /* Decode attribute refusal reason */
  1221. char c, *p;
  1222. if (rejection == 1) /* Kludge for SET FIL COLL DISCARD */
  1223. return("name"); /* when other Kermit doesn't... */
  1224. p = s;
  1225. if (*p++ != 'N') return(""); /* Should start with N */
  1226. else if ((c = *p) > SP) { /* get reason, */
  1227. rejection = c; /* remember it, */
  1228. c -= '!'; /* get offset */
  1229. p = ((unsigned int) ((CHAR) c) <= (unsigned int) nreason) ?
  1230. reason[c] :
  1231. "unknown";
  1232. }
  1233. return(p);
  1234. }
  1235. int
  1236. rsattr(s) CHAR *s; { /* Read response to attribute packet */
  1237. debug(F111,"rsattr",s,*s);
  1238. if (*s == 'N') { /* If it's 'N' followed by anything, */
  1239. refused = getreason((char *)s); /* they are refusing, get reason. */
  1240. debug(F110,"rsattr refused",refused,0);
  1241. tlog(F110," refused:",refused,0L);
  1242. return(-1);
  1243. }
  1244. #ifdef CK_RESEND
  1245. if (sendmode == SM_RESEND && *s == '1') { /* RESEND length */
  1246. int n; CK_OFF_T z; CHAR *p;
  1247. p = s + 1;
  1248. n = xunchar(*p++);
  1249. debug(F101,"rsattr RESEND n","",n);
  1250. z = (CK_OFF_T)0;
  1251. while (n-- > 0) /* We assume the format is good. */
  1252. z = (CK_OFF_T)10 * z + (CK_OFF_T)(*p++ - '0');
  1253. debug(F101,"rsattr RESEND z","",z);
  1254. if (z > (CK_OFF_T)0) sendstart = z;
  1255. debug(F101,"rsattr RESEND sendstart","",sendstart);
  1256. if (sendstart > (CK_OFF_T)0)
  1257. if (zfseek(sendstart) < 0) /* Input file is already open. */
  1258. return(0);
  1259. #ifdef CK_CURSES
  1260. if (fdispla == XYFD_C)
  1261. xxscreen(SCR_FS,0,fsize,""); /* Refresh file transfer display */
  1262. #endif /* CK_CURSES */
  1263. }
  1264. #endif /* CK_RESEND */
  1265. refused = "";
  1266. return(0);
  1267. }
  1268. /*
  1269. Get attributes from incoming A packet. Returns:
  1270. 0 on success, file is to be accepted
  1271. -1 on failure, file is to be refused
  1272. */
  1273. int
  1274. gattr(s, yy) CHAR *s; struct zattr *yy; { /* Read incoming attribute packet */
  1275. char c, d;
  1276. char *ff;
  1277. int aln, i, overflow = 0;
  1278. #ifndef NOCSETS
  1279. extern int r_cset, axcset[];
  1280. #endif /* NOCSETS */
  1281. #define ABUFL 40 /* Temporary buffer for conversions */
  1282. char abuf[ABUFL+1];
  1283. #define RFBUFL 10 /* Record-format buffer */
  1284. static char rfbuf[RFBUFL+1];
  1285. #define FTBUFL 10 /* File type buffer */
  1286. static char ftbuf[FTBUFL+1];
  1287. #define DTBUFL 40 /* File creation date */
  1288. static char dtbuf[DTBUFL+1];
  1289. #define TSBUFL 10 /* Transfer syntax */
  1290. static char tsbuf[TSBUFL+1];
  1291. #define IDBUFL 10 /* System ID */
  1292. static char idbuf[IDBUFL+1];
  1293. #ifndef DYNAMIC
  1294. #define DSBUFL 100 /* Disposition */
  1295. static char dsbuf[DSBUFL+1];
  1296. #define SPBUFL 512 /* System-dependent parameters */
  1297. static char spbuf[SPBUFL+1];
  1298. #else
  1299. #define DSBUFL 100 /* Disposition */
  1300. static char *dsbuf = NULL;
  1301. #define SPBUFL 512 /* System-dependent parameters */
  1302. static char *spbuf = NULL;
  1303. #endif /* DYNAMIC */
  1304. #define RPBUFL 20 /* Attribute reply */
  1305. static char rpbuf[RPBUFL+1];
  1306. #ifdef CK_PERMS
  1307. static char lprmbuf[CK_PERMLEN+1];
  1308. static char gprmbuf[2];
  1309. #endif /* CK_PERMS */
  1310. char *rp; /* Pointer to reply buffer */
  1311. int retcode; /* Return code */
  1312. d = SP; /* Initialize disposition */
  1313. ff = filnam; /* Filename returned by rcvfil */
  1314. if (fncact == XYFX_R && ofn1x && ofn1[0]) /* But watch out for FC=RENAME */
  1315. ff = ofn1; /* because we haven't renamed it yet */
  1316. /* Fill in the attributes we have received */
  1317. rp = rpbuf; /* Initialize reply buffer */
  1318. *rp++ = 'N'; /* for negative reply. */
  1319. *rp = NUL;
  1320. retcode = 0; /* Initialize return code. */
  1321. if (dest == DEST_P) { /* SET DESTINATION PRINTER */
  1322. #ifdef DYNAMIC
  1323. if (!dsbuf)
  1324. if ((dsbuf = malloc(DSBUFL+1)) == NULL)
  1325. fatal("gtattr: no memory for dsbuf");
  1326. #endif /* DYNAMIC */
  1327. dsbuf[0] = 'P';
  1328. dsbuf[1] = '\0';
  1329. yy->disp.val = dsbuf;
  1330. yy->disp.len = 1;
  1331. }
  1332. while (c = *s++) { /* Get attribute tag */
  1333. aln = xunchar(*s++); /* Length of attribute string */
  1334. switch (c) {
  1335. #ifdef COMMENT /* This case combined with '1' below */
  1336. case '!': /* File length in K */
  1337. for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1338. abuf[i] = *s++;
  1339. abuf[i] = '\0'; /* Terminate with null */
  1340. if (i < aln) s += (aln - i); /* If field was too long for buffer */
  1341. yy->lengthk = ckatofs(abuf); /* Convert to number */
  1342. break;
  1343. #endif /* COMMENT */
  1344. case '/': /* Record format */
  1345. rfbuf[1] = NUL;
  1346. rfbuf[2] = NUL;
  1347. for (i = 0; (i < aln) && (i < RFBUFL); i++) /* Copy it */
  1348. rfbuf[i] = *s++;
  1349. rfbuf[i] = NUL; /* Terminate with null */
  1350. yy->recfm.val = rfbuf; /* Pointer to string */
  1351. yy->recfm.len = i; /* Length of string */
  1352. if ((rfbuf[0] != 'A') ||
  1353. (rfbuf[1] && rfbuf[1] != 'M') ||
  1354. (rfbuf[2] && rfbuf[2] != 'J')) {
  1355. debug(F110,"gattr bad recfm",rfbuf,0);
  1356. *rp++ = c;
  1357. retcode = -1;
  1358. }
  1359. break;
  1360. case '"': /* File type (text, binary, ...) */
  1361. for (i = 0; (i < aln) && (i < FTBUFL); i++)
  1362. ftbuf[i] = *s++; /* Copy it into a static string */
  1363. ftbuf[i] = '\0';
  1364. if (i < aln) s += (aln - i);
  1365. /* TYPE attribute is enabled? */
  1366. if (attypi) {
  1367. yy->type.val = ftbuf; /* Pointer to string */
  1368. yy->type.len = i; /* Length of string */
  1369. debug(F111,"gattr file type", ftbuf, i);
  1370. debug(F101,"gattr binary 1","",binary);
  1371. /* Unknown type? */
  1372. if ((*ftbuf != 'A' && *ftbuf != 'B' && *ftbuf != 'I')
  1373. #ifdef CK_LABELED
  1374. /* ... Or our FILE TYPE is LABELED and the incoming file is text... */
  1375. || (binary == XYFT_L && *ftbuf == 'A' && !xflg)
  1376. #endif /* CK_LABELED */
  1377. ) {
  1378. retcode = -1; /* Reject the file */
  1379. *rp++ = c;
  1380. if (!opnerr) tlog(F100," refused: type","",0);
  1381. break;
  1382. }
  1383. /*
  1384. The following code moved here from opena() so we set binary mode
  1385. as soon as requested by the attribute packet. That way when the first
  1386. data packet comes, the mode of transfer can be displayed correctly
  1387. before opena() is called.
  1388. */
  1389. if (yy->type.val[0] == 'A') { /* Check received attributes. */
  1390. #ifdef VMS
  1391. if (binary != XYFT_I) /* VMS IMAGE overrides this */
  1392. #endif /* VMS */
  1393. binary = XYFT_T; /* Set current type to Text. */
  1394. debug(F101,"gattr binary 2","",binary);
  1395. } else if (yy->type.val[0] == 'B') {
  1396. #ifdef CK_LABELED
  1397. if (binary != XYFT_L
  1398. #ifdef VMS
  1399. && binary != XYFT_U /* VMS special case */
  1400. #endif /* VMS */
  1401. )
  1402. #endif /* CK_LABELED */
  1403. #ifdef MAC
  1404. if (binary != XYFT_M) /* If not MacBinary... */
  1405. #endif /* MAC */
  1406. binary = XYFT_B;
  1407. debug(F101,"gattr binary 3","",binary);
  1408. }
  1409. }
  1410. break;
  1411. case '#': /* File creation date */
  1412. for (i = 0; (i < aln) && (i < DTBUFL); i++)
  1413. dtbuf[i] = *s++; /* Copy it into a static string */
  1414. if (i < aln) s += (aln - i);
  1415. dtbuf[i] = '\0';
  1416. if (atdati && !xflg) { /* Real file and dates enabled */
  1417. yy->date.val = dtbuf; /* Pointer to string */
  1418. yy->date.len = i; /* Length of string */
  1419. if (fncact == XYFX_U) { /* Receiving in update mode? */
  1420. if (zstime(ff,yy,1) > 0) { /* Compare dates */
  1421. *rp++ = c; /* Discard if older, reason = date. */
  1422. if (!opnerr) tlog(F100," refused: date","",0);
  1423. retcode = -1; /* Rejection notice. */
  1424. }
  1425. }
  1426. }
  1427. break;
  1428. case '(': /* File Block Size */
  1429. for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1430. abuf[i] = *s++;
  1431. abuf[i] = '\0'; /* Terminate with null */
  1432. if (i < aln) s += (aln - i);
  1433. if (atblki)
  1434. yy->blksize = atol(abuf); /* Convert to number */
  1435. break;
  1436. case '*': /* Encoding (transfer syntax) */
  1437. for (i = 0; (i < aln) && (i < TSBUFL); i++)
  1438. tsbuf[i] = *s++; /* Copy it into a static string */
  1439. if (i < aln) s += (aln - i);
  1440. tsbuf[i] = '\0';
  1441. #ifndef NOCSETS
  1442. xlatype = XLA_NONE; /* Assume no translation */
  1443. #endif /* NOCSETS */
  1444. if (atenci) {
  1445. char * ss;
  1446. yy->encoding.val = tsbuf; /* Pointer to string */
  1447. yy->encoding.len = i; /* Length of string */
  1448. debug(F101,"gattr encoding",tsbuf,i);
  1449. ss = tsbuf+1;
  1450. switch (*tsbuf) {
  1451. #ifndef NOCSETS
  1452. case 'A': /* Normal, nothing special */
  1453. tcharset = TC_TRANSP; /* Transparent chars untranslated */
  1454. debug(F110,"gattr sets tcharset TC_TRANSP","A",0);
  1455. break;
  1456. case 'C': /* Specified character set */
  1457. if (!xfrxla) { /* But translation disabled */
  1458. tcharset = TC_TRANSP;
  1459. debug(F110,"gattr sets tcharset TC_TRANSP","C",0);
  1460. break;
  1461. }
  1462. #ifdef UNICODE
  1463. if (!strcmp("I196",ss)) /* Treat I196 (UTF-8 no level) */
  1464. ss = "I190"; /* as I190 (UTF-8 Level 1) */
  1465. #endif /* UNICODE */
  1466. if (!strcmp("I6/204",ss)) /* Treat "Latin-1 + Euro" */
  1467. ss = "I6/100"; /* as I6/100 (regular Latin-1) */
  1468. for (i = 0; i < ntcsets; i++) {
  1469. if (!strcmp(tcsinfo[i].designator,ss))
  1470. break;
  1471. }
  1472. debug(F101,"gattr xfer charset lookup","",i);
  1473. if (i == ntcsets) { /* If unknown character set, */
  1474. debug(F110,"gattr: xfer charset unknown",ss,0);
  1475. if (!unkcs) { /* and SET UNKNOWN DISCARD, */
  1476. retcode = -1; /* reject the file. */
  1477. *rp++ = c;
  1478. if (!opnerr)
  1479. tlog(F100," refused: character set","",0);
  1480. }
  1481. } else {
  1482. tcharset = tcsinfo[i].code; /* it's known, use it */
  1483. debug(F101,"gattr switch tcharset","",tcharset);
  1484. debug(F101,"gattr fcharset","",fcharset);
  1485. if (r_cset == XMODE_A) { /* Automatic switching? */
  1486. if (tcharset > -1 && tcharset <= MAXTCSETS) {
  1487. int x;
  1488. x = axcset[tcharset];
  1489. if (x > 0 && x <= MAXFCSETS) {
  1490. fcharset = x;
  1491. debug(F101,"gattr switch fcharset","",x);
  1492. }
  1493. }
  1494. }
  1495. /* Set up translation type and function */
  1496. setxlatype(tcharset,fcharset);
  1497. }
  1498. break;
  1499. #endif /* NOCSETS */
  1500. default: /* Something else. */
  1501. debug(F110,"gattr unk encoding attribute",tsbuf,0);
  1502. if (!unkcs) { /* If SET UNK DISC */
  1503. retcode = -1;
  1504. *rp++ = c;
  1505. if (!opnerr) tlog(F100," refused: encoding","",0);
  1506. }
  1507. break;
  1508. }
  1509. }
  1510. break;
  1511. case '+': /* Disposition */
  1512. #ifdef DYNAMIC
  1513. if (!dsbuf)
  1514. if ((dsbuf = malloc(DSBUFL+1)) == NULL)
  1515. fatal("gtattr: no memory for dsbuf");
  1516. #endif /* DYNAMIC */
  1517. for (i = 0; (i < aln) && (i < DSBUFL); i++)
  1518. dsbuf[i] = *s++; /* Copy it into a separate string */
  1519. dsbuf[i] = '\0';
  1520. if (i < aln) s += (aln - i);
  1521. rs_len = (CK_OFF_T)0;
  1522. if (atdisi) { /* We are doing this attribute */
  1523. /* Copy it into the attribute structure */
  1524. yy->disp.val = dsbuf; /* Pointer to string */
  1525. yy->disp.len = i; /* Length of string */
  1526. d = *dsbuf;
  1527. #ifndef NODISPO
  1528. /*
  1529. Define NODISPO to disable receipt of mail or print files and of RESEND.
  1530. */
  1531. if (
  1532. #ifndef datageneral /* MAIL supported only for */
  1533. #ifndef OS2 /* UNIX, VMS, and OS-9 */
  1534. #ifndef MAC
  1535. #ifndef GEMDOS
  1536. #ifndef AMIGA
  1537. d != 'M' && /* MAIL */
  1538. #endif /* AMIGA */
  1539. #endif /* GEMDOS */
  1540. #endif /* MAC */
  1541. #endif /* OS/2 */
  1542. #endif /* datageneral */
  1543. #ifdef CK_RESEND
  1544. d != 'R' && /* RESEND */
  1545. #endif /* CK_RESEND */
  1546. d != 'P') { /* PRINT */
  1547. retcode = -1; /* Unknown/unsupported disposition */
  1548. *rp++ = c;
  1549. if (!opnerr) tlog(F101," refused: bad disposition","",d);
  1550. }
  1551. dispos = d;
  1552. debug(F000,"gattr dispos","",dispos);
  1553. switch (d) {
  1554. #ifndef NOFRILLS
  1555. case 'M':
  1556. if (!en_mai) {
  1557. retcode = -1;
  1558. *rp++ = c;
  1559. if (!opnerr) tlog(F100," refused: mail disabled","",0);
  1560. dispos = 0;
  1561. }
  1562. break;
  1563. #endif /* NOFRILLS */
  1564. case 'P':
  1565. if (!en_pri) {
  1566. retcode = -1;
  1567. *rp++ = c;
  1568. if (!opnerr)
  1569. tlog(F100," refused: print disabled","",0);
  1570. dispos = 0;
  1571. }
  1572. break;
  1573. case 'R':
  1574. dispos = 0;
  1575. #ifdef CK_RESEND
  1576. rs_len = zgetfs(ff); /* Get length of file */
  1577. debug(F111,"gattr RESEND",ff,rs_len);
  1578. #ifdef VMS
  1579. rs_len &= (long) -512; /* Ensure block boundary if VMS */
  1580. rs_len -= 512; /* In case last block not complete */
  1581. debug(F111,"gattr rs_len",ff,rs_len);
  1582. #endif /* VMS */
  1583. #ifdef COMMENT
  1584. if (rs_len < 0L) /* Local file doesn't exist */
  1585. rs_len = 0L;
  1586. #endif /* COMMENT */
  1587. /*
  1588. Another possibility here (or later, really) would be to check if the two
  1589. file lengths are the same, and if so, keep the prevailing collision action
  1590. as is (note: rs_len == length of existing file; yy->length == fsize ==
  1591. length of incoming file). This could be complicated, though, since
  1592. (a) we might not have received the length attribute yet, and in fact it
  1593. might even be in a subsequent A-packet, yet (b) we have to accept or reject
  1594. the Recover attribute now. So better to leave as-is. Anyway, it's probably
  1595. more useful this way.
  1596. */
  1597. if (rs_len > (CK_OFF_T)0) {
  1598. fncsav = fncact; /* Save collision action */
  1599. fncact = XYFX_A; /* Switch to APPEND */
  1600. }
  1601. #else
  1602. retcode = -1; /* This shouldn't happen */
  1603. *rp++ = c; /* 'cause it wasn't negotiated. */
  1604. if (!opnerr) tlog(F100," refused: resend","",0);
  1605. #endif /* CK_RESEND */
  1606. }
  1607. #else /* NODISPO */
  1608. retcode = -1;
  1609. *rp++ = c;
  1610. if (!opnerr) tlog(F100," refused: NODISPO","",0);
  1611. #endif /* NODISPO */
  1612. }
  1613. break;
  1614. case '.': /* Sender's system ID */
  1615. for (i = 0; (i < aln) && (i < IDBUFL); i++)
  1616. idbuf[i] = *s++; /* Copy it into a static string */
  1617. idbuf[i] = '\0';
  1618. if (i < aln) s += (aln - i);
  1619. if (atsidi) {
  1620. yy->systemid.val = idbuf; /* Pointer to string */
  1621. yy->systemid.len = i; /* Length of string */
  1622. }
  1623. break;
  1624. case '0': /* System-dependent parameters */
  1625. #ifdef DYNAMIC
  1626. if (!spbuf && !(spbuf = malloc(SPBUFL)))
  1627. fatal("gattr: no memory for spbuf");
  1628. #endif /* DYNAMIC */
  1629. for (i = 0; (i < aln) && (i < SPBUFL); i++)
  1630. spbuf[i] = *s++; /* Copy it into a static string */
  1631. spbuf[i] = '\0';
  1632. if (i < aln) s += (aln - i);
  1633. if (atsysi) {
  1634. yy->sysparam.val = spbuf; /* Pointer to string */
  1635. yy->sysparam.len = i; /* Length of string */
  1636. }
  1637. break;
  1638. case '!': /* File length in K */
  1639. case '1': { /* File length in bytes */
  1640. char * l2;
  1641. CK_OFF_T xlen;
  1642. for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1643. abuf[i] = *s++;
  1644. abuf[i] = '\0'; /* Terminate with null */
  1645. if (i < aln) s += (aln - i);
  1646. if (rdigits(abuf)) { /* Make sure string is all digits */
  1647. xlen = ckatofs(abuf); /* Convert to number */
  1648. l2 = ckfstoa(xlen); /* Convert number back to string */
  1649. if (c == '1')
  1650. debug(F111,"gattr length",abuf,xlen);
  1651. else
  1652. debug(F111,"gattr lengthk",abuf,xlen);
  1653. if (ckstrcmp(abuf,l2,-1,1)) { /* This is how we check... */
  1654. xlen = (CK_OFF_T)-2; /* -2 = unk, possibly too long */
  1655. overflow++;
  1656. debug(F111,"gattr overflow",
  1657. (c == '1') ? "length" : "lengthk",
  1658. xlen);
  1659. }
  1660. if (c == '1') {
  1661. yy->length = xlen;
  1662. debug(F101,"gattr length","",xlen);
  1663. } else {
  1664. yy->lengthk = xlen;
  1665. debug(F101,"gattr lengthk","",xlen);
  1666. }
  1667. }
  1668. /* If the length field is not numeric accept the file */
  1669. /* anyway but with an unknown length */
  1670. break;
  1671. }
  1672. #ifdef CK_PERMS
  1673. case ',': /* System-dependent protection code */
  1674. for (i = 0; (i < aln) && (i < CK_PERMLEN); i++)
  1675. lprmbuf[i] = *s++; /* Just copy it - decode later */
  1676. lprmbuf[i] = '\0'; /* Terminate with null */
  1677. if (i < aln) s += (aln - i);
  1678. if (atlpri) {
  1679. yy->lprotect.val = (char *)lprmbuf;
  1680. yy->lprotect.len = i;
  1681. } else
  1682. lprmbuf[0] = NUL;
  1683. break;
  1684. case '-': /* Generic "world" protection code */
  1685. gprmbuf[0] = NUL; /* Just 1 byte by definition */
  1686. for (i = 0; i < aln; i++) /* But allow for more... */
  1687. if (i == 0) gprmbuf[0] = *s++;
  1688. gprmbuf[1] = NUL;
  1689. if (atgpri) {
  1690. yy->gprotect.val = (char *)gprmbuf;
  1691. yy->gprotect.len = gprmbuf[0] ? 1 : 0;
  1692. } else
  1693. gprmbuf[0] = NUL;
  1694. break;
  1695. #endif /* CK_PERMS */
  1696. default: /* Unknown attribute */
  1697. s += aln; /* Just skip past it */
  1698. break;
  1699. }
  1700. }
  1701. /* Check space now, because we also need to know the file type */
  1702. /* in case zchkspa() differentiates text and binary (VMS version does) */
  1703. if (atleni && !calibrate) { /* Length attribute enabled? */
  1704. if (yy->length > (CK_OFF_T)-1) { /* Length-in-bytes attribute rec'd? */
  1705. if (!zchkspa(ff,(yy->length))) { /* Check space */
  1706. retcode = -1; /* Not enuf */
  1707. *rp++ = '1';
  1708. if (!opnerr) tlog(F100," refused: length bytes","",0);
  1709. }
  1710. } else if (yy->lengthk > (CK_OFF_T)-1) { /* Length in K received? */
  1711. long xlen;
  1712. xlen = yy->lengthk * 1024;
  1713. if (!zchkspa(ff,xlen)) {
  1714. retcode = -1; /* Check space */
  1715. *rp++ = '!';
  1716. if (!opnerr) tlog(F100," refused: length K","",0);
  1717. }
  1718. }
  1719. }
  1720. if (retcode > -1L) { /* Remember the file size */
  1721. if (yy->length > (CK_OFF_T)-1) {
  1722. fsize = yy->length;
  1723. } else if (yy->lengthk > (CK_OFF_T)-1 && !overflow) {
  1724. fsize = yy->lengthk * 1024L;
  1725. } else fsize = yy->length; /* (e.g. -2L) */
  1726. }
  1727. #ifdef DEBUG
  1728. if (deblog) {
  1729. #ifdef COMMENT
  1730. sprintf(abuf,"%ld",fsize); /* safe */
  1731. #else
  1732. ckstrncpy(abuf,ckfstoa(fsize),ABUFL);
  1733. #endif /* COMMENT */
  1734. debug(F110,"gattr fsize",abuf,0);
  1735. }
  1736. #endif /* DEBUG */
  1737. if (retcode == 0) rp = rpbuf; /* Null reply string if accepted */
  1738. *rp = '\0'; /* End of reply string */
  1739. #ifdef CK_RESEND
  1740. if (d == 'R') { /* Receiving a RESEND? */
  1741. debug(F101,"gattr RESEND","",retcode);
  1742. /* We ignore retcodes because this overrides */
  1743. if (binary != XYFT_B) { /* Reject if not binary */
  1744. retcode = -1; /* in case type field came */
  1745. ckstrncpy(rpbuf,"N+",RPBUFL); /* after the disposition field */
  1746. debug(F111,"gattr RESEND not binary",rpbuf,binary);
  1747. } else { /* Binary mode */
  1748. retcode = 0; /* Accept the file */
  1749. discard = 0; /* If SET FILE COLLISION DISCARD */
  1750. #ifdef COMMENT
  1751. sprintf(rpbuf+2,"%ld",rs_len); /* Reply with length of file */
  1752. #else
  1753. ckstrncpy(rpbuf+2,ckfstoa(rs_len),RPBUFL-2);
  1754. #endif /* COMMENT */
  1755. rpbuf[0] = '1'; /* '1' means Length in Bytes */
  1756. rpbuf[1] = tochar((int)strlen(rpbuf+2)); /* Length of length */
  1757. debug(F111,"gattr RESEND OK",rpbuf,retcode);
  1758. }
  1759. }
  1760. #endif /* CK_RESEND */
  1761. if (retcode == 0 && discard != 0) { /* Do we still have a discard flag? */
  1762. ckstrncpy(rpbuf,"N?",RPBUFL); /* Yes, must be filename collision */
  1763. retcode = -1; /* "?" = name (reply-only code) */
  1764. }
  1765. yy->reply.val = rpbuf; /* Add it to attribute structure */
  1766. yy->reply.len = (int)strlen(rpbuf);
  1767. if (retcode < 0) { /* If we are rejecting */
  1768. discard = 1; /* remember to discard the file */
  1769. rejection = rpbuf[1]; /* and use the first reason given. */
  1770. if (fncsav != -1) {
  1771. fncact = fncsav;
  1772. fncsav = -1;
  1773. }
  1774. }
  1775. debug(F111,"gattr return",rpbuf,retcode);
  1776. return(retcode);
  1777. }
  1778. /* I N I T A T T R -- Initialize file attribute structure */
  1779. int
  1780. initattr(yy) struct zattr *yy; {
  1781. yy->lengthk = yy->length = (CK_OFF_T)-1;
  1782. yy->type.val = "";
  1783. yy->type.len = 0;
  1784. yy->date.val = "";
  1785. yy->date.len = 0;
  1786. yy->encoding.val = "";
  1787. yy->encoding.len = 0;
  1788. yy->disp.val = "";
  1789. yy->disp.len = 0;
  1790. yy->systemid.val = "";
  1791. yy->systemid.len = 0;
  1792. yy->sysparam.val = "";
  1793. yy->sysparam.len = 0;
  1794. yy->creator.val = "";
  1795. yy->creator.len = 0;
  1796. yy->account.val = "";
  1797. yy->account.len = 0;
  1798. yy->area.val = "";
  1799. yy->area.len = 0;
  1800. yy->password.val = "";
  1801. yy->password.len = 0;
  1802. yy->blksize = -1L;
  1803. yy->xaccess.val = "";
  1804. yy->xaccess.len = 0;
  1805. #ifdef CK_PERMS
  1806. if (!ofperms) ofperms = "";
  1807. debug(F110,"initattr ofperms",ofperms,0);
  1808. yy->lprotect.val = ofperms;
  1809. yy->lprotect.len = 0 - strlen(ofperms); /* <-- NOTE! */
  1810. /*
  1811. A negative length indicates that we have a permissions string but it has
  1812. been inherited from a previously existing file rather than picked up
  1813. from an incoming A-packet.
  1814. */
  1815. #else
  1816. yy->lprotect.val = "";
  1817. yy->lprotect.len = 0;
  1818. #endif /* CK_PERMS */
  1819. yy->gprotect.val = "";
  1820. yy->gprotect.len = 0;
  1821. yy->recfm.val = "";
  1822. yy->recfm.len = 0;
  1823. yy->reply.val = "";
  1824. yy->reply.len = 0;
  1825. #ifdef OS2
  1826. yy->longname.len = 0 ;
  1827. yy->longname.val = "" ;
  1828. #endif /* OS2 */
  1829. return(0);
  1830. }
  1831. /* A D E B U -- Write attribute packet info to debug log */
  1832. int
  1833. adebu(f,zz) char *f; struct zattr *zz; {
  1834. #ifdef DEBUG
  1835. if (deblog == 0) return(0);
  1836. debug(F110,"Attributes for incoming file ",f,0);
  1837. debug(F101," length in K","",(int) zz->lengthk);
  1838. debug(F111," file type",zz->type.val,zz->type.len);
  1839. debug(F111," creation date",zz->date.val,zz->date.len);
  1840. debug(F111," creator",zz->creator.val,zz->creator.len);
  1841. debug(F111," account",zz->account.val,zz->account.len);
  1842. debug(F111," area",zz->area.val,zz->area.len);
  1843. debug(F111," password",zz->password.val,zz->password.len);
  1844. debug(F101," blksize","",(int) zz->blksize);
  1845. debug(F111," access",zz->xaccess.val,zz->xaccess.len);
  1846. debug(F111," encoding",zz->encoding.val,zz->encoding.len);
  1847. debug(F111," disposition",zz->disp.val,zz->disp.len);
  1848. debug(F111," lprotection",zz->lprotect.val,zz->lprotect.len);
  1849. debug(F111," gprotection",zz->gprotect.val,zz->gprotect.len);
  1850. debug(F111," systemid",zz->systemid.val,zz->systemid.len);
  1851. debug(F111," recfm",zz->recfm.val,zz->recfm.len);
  1852. debug(F111," sysparam",zz->sysparam.val,zz->sysparam.len);
  1853. debug(F101," length","",(int) zz->length);
  1854. debug(F110," reply",zz->reply.val,0);
  1855. #endif /* DEBUG */
  1856. return(0);
  1857. }
  1858. /* O P E N A -- Open a file, with attributes. */
  1859. /*
  1860. This function tries to open a new file to put the arriving data in. The
  1861. filename is the one in the srvcmd buffer. File collision actions are:
  1862. OVERWRITE (the existing file is overwritten), RENAME (the new file is
  1863. renamed), BACKUP (the existing file is renamed), DISCARD (the new file is
  1864. refused), UPDATE (the incoming file replaces the existing file only if the
  1865. incoming file has a newer creation date).
  1866. Returns 0 on failure, nonzero on success.
  1867. */
  1868. extern char *rf_err;
  1869. int
  1870. opena(f,zz) char *f; struct zattr *zz; {
  1871. int x, dispos = 0;
  1872. static struct filinfo fcb; /* Must be static! */
  1873. debug(F110,"opena f",f,0);
  1874. debug(F101,"opena discard","",discard);
  1875. adebu(f,zz); /* Write attributes to debug log */
  1876. ffc = (CK_OFF_T)0; /* Init file-character counter */
  1877. #ifdef PIPESEND
  1878. if (pipesend) /* Receiving to a pipe - easy. */
  1879. return(openo(f,zz,&fcb)); /* Just open the pipe. */
  1880. #endif /* PIPESEND */
  1881. /* Receiving to a file - set up file control structure */
  1882. fcb.bs = fblksiz; /* Blocksize */
  1883. #ifndef NOCSETS
  1884. fcb.cs = fcharset; /* Character set */
  1885. #else
  1886. fcb.cs = 0; /* Character set */
  1887. #endif /* NOCSETS */
  1888. fcb.rl = frecl; /* Record Length */
  1889. fcb.fmt = frecfm; /* Record Format */
  1890. fcb.org = forg; /* Organization */
  1891. fcb.cc = fcctrl; /* Carriage control */
  1892. fcb.typ = binary; /* Type */
  1893. debug(F101,"opena xflg","",xflg);
  1894. debug(F101,"opena remfile","",remfile);
  1895. debug(F101,"opena remappd","",remappd);
  1896. if (xflg && remfile && remappd) /* REMOTE output redirected with >> */
  1897. fcb.dsp = XYFZ_A;
  1898. else
  1899. fcb.dsp = (fncact == XYFX_A) ? XYFZ_A : XYFZ_N; /* Disposition */
  1900. debug(F101,"opena disp","",fcb.dsp);
  1901. fcb.os_specific = ""; /* OS-specific info */
  1902. #ifdef CK_LABELED
  1903. fcb.lblopts = lf_opts; /* Labeled file options */
  1904. #else
  1905. fcb.lblopts = 0;
  1906. #endif /* CK_LABELED */
  1907. if (zz->disp.len > 0) { /* Incoming file has a disposition? */
  1908. debug(F111,"open disposition",zz->disp.val,zz->disp.len);
  1909. dispos = (int) (*(zz->disp.val));
  1910. }
  1911. if (!dispos && xflg && remfile && remappd) /* REMOTE redirect append ? */
  1912. dispos = fcb.dsp;
  1913. debug(F101,"opena dispos","",dispos);
  1914. if (!dispos) { /* No special disposition? */
  1915. if (fncact == XYFX_B && ofn1x && ofn2) { /* File collision = BACKUP? */
  1916. if (zrename(ofn1,ofn2) < 0) { /* Rename existing file. */
  1917. debug(F110,"opena rename fails",ofn1,0);
  1918. rf_err = "Can't create backup file";
  1919. return(0);
  1920. } else debug(F110,"opena rename ok",ofn2,0);
  1921. }
  1922. } else if (dispos == 'R') { /* Receiving a RESEND */
  1923. debug(F101,"opena remote len","",zz->length);
  1924. debug(F101,"opena local len","",rs_len);
  1925. #ifdef COMMENT
  1926. if (fncact == XYFX_R) /* and file collision = RENAME */
  1927. if (ofn1x)
  1928. #endif /* COMMENT */
  1929. if (ofn1[0])
  1930. f = ofn1; /* use original name. */
  1931. if (fncact == XYFX_R) /* if file collision is RENAME */
  1932. ckstrncpy(filnam,ofn1,CKMAXPATH+1); /* restore the real name */
  1933. xxscreen(SCR_AN,0,0L,f); /* update name on screen */
  1934. if (zz->length == rs_len) /* Local and remote lengths equal? */
  1935. return(-17); /* Secret code */
  1936. }
  1937. debug(F111,"opena [file]=mode: ",f,fcb.dsp);
  1938. if (x = openo(f,zz,&fcb)) { /* Try to open the file. */
  1939. #ifdef pdp11
  1940. tlog(F110," local name:",f,0L); /* OK, open, record local name. */
  1941. makestr(&prfspec,f); /* New preliminary name */
  1942. #else
  1943. #ifndef ZFNQFP
  1944. tlog(F110," local name:",f,0L);
  1945. makestr(&prfspec,f);
  1946. #else
  1947. { /* Log full local pathname */
  1948. char *p = NULL, *q = f;
  1949. if ((p = malloc(CKMAXPATH+1)))
  1950. if (zfnqfp(filnam, CKMAXPATH, p))
  1951. q = p;
  1952. tlog(F110," local name:",q,0L);
  1953. makestr(&prfspec,q);
  1954. if (p) free(p);
  1955. }
  1956. #endif /* ZFNQFP */
  1957. #endif /* pdp11 */
  1958. if (binary) { /* Log file mode in transaction log */
  1959. tlog(F101," mode: binary","",(long) binary);
  1960. } else { /* If text mode, check character set */
  1961. tlog(F100," mode: text","",0L);
  1962. #ifndef NOCSETS
  1963. if (xfrxla) {
  1964. if (fcharset > -1 && fcharset <= MAXFCSETS)
  1965. tlog(F110," file character-set:",fcsinfo[fcharset].name,0L);
  1966. if (tcharset > -1 && tcharset <= MAXTCSETS)
  1967. tlog(F110," xfer character-set:",tcsinfo[tcharset].name,0L);
  1968. } else {
  1969. tlog(F110," character-set:","transparent",0L);
  1970. }
  1971. #endif /* NOCSETS */
  1972. debug(F111,"opena charset",zz->encoding.val,zz->encoding.len);
  1973. }
  1974. debug(F101,"opena binary","",binary);
  1975. #ifdef COMMENT
  1976. if (fsize >= 0)
  1977. #endif /* COMMENT */
  1978. xxscreen(SCR_FS,0,fsize,"");
  1979. #ifdef datageneral
  1980. /*
  1981. Need to turn on multi-tasking console interrupt task here, since multiple
  1982. files may be received (huh?) ...
  1983. */
  1984. if ((local) && (!quiet)) /* Only do this if local & not quiet */
  1985. consta_mt(); /* Start the async read task */
  1986. #endif /* datageneral */
  1987. } else { /* Did not open file OK. */
  1988. rf_err = ck_errstr(); /* Get system error message */
  1989. if (*rf_err)
  1990. xxscreen(SCR_EM,0,0l,rf_err);
  1991. else
  1992. xxscreen(SCR_EM,0,0l,"Can't open output file");
  1993. tlog(F110,"Failure to open",f,0L);
  1994. tlog(F110,"Error:",rf_err,0L);
  1995. debug(F110,"opena error",rf_err,0);
  1996. }
  1997. return(x); /* Pass on return code from openo */
  1998. }
  1999. /* O P E N C -- Open a command (in place of a file) for output */
  2000. int
  2001. openc(n,s) int n; char * s; {
  2002. int x;
  2003. #ifndef NOPUSH
  2004. x = zxcmd(n,s);
  2005. #else
  2006. x = 0;
  2007. #endif /* NOPUSH */
  2008. debug(F111,"openc zxcmd",s,x);
  2009. o_isopen = (x > 0) ? 1 : 0;
  2010. return(x);
  2011. }
  2012. /* C A N N E D -- Check if current file transfer cancelled */
  2013. int
  2014. canned(buf) CHAR *buf; {
  2015. extern int interrupted;
  2016. if (*buf == 'X') cxseen = 1;
  2017. if (*buf == 'Z') czseen = 1;
  2018. if (czseen || cxseen)
  2019. interrupted = 1;
  2020. debug(F101,"canned: cxseen","",cxseen);
  2021. debug(F101," czseen","",czseen);
  2022. return((czseen || cxseen) ? 1 : 0);
  2023. }
  2024. /* O P E N I -- Open an existing file for input */
  2025. int
  2026. openi(name) char *name; {
  2027. #ifndef NOSERVER
  2028. extern int fromgetpath;
  2029. #endif /* NOSERVER */
  2030. int x, filno;
  2031. char *name2;
  2032. extern CHAR *epktmsg;
  2033. epktmsg[0] = NUL; /* Initialize error message */
  2034. if (memstr || sndarray) { /* Just return if "file" is memory. */
  2035. i_isopen = 1;
  2036. return(1);
  2037. }
  2038. debug(F110,"openi name",name,0);
  2039. debug(F101,"openi sndsrc","",sndsrc);
  2040. filno = (sndsrc == 0) ? ZSTDIO : ZIFILE; /* ... */
  2041. debug(F101,"openi file number","",filno);
  2042. #ifndef NOSERVER
  2043. /* If I'm a server and CWD is disabled and name is not from GET-PATH... */
  2044. if (server && !en_cwd && !fromgetpath) {
  2045. zstrip(name,&name2);
  2046. if ( /* ... check if pathname included. */
  2047. #ifdef VMS
  2048. zchkpath(name)
  2049. #else
  2050. strcmp(name,name2)
  2051. #endif /* VMS */
  2052. ) {
  2053. tlog(F110,name,"access denied",0L);
  2054. debug(F110,"openi CD disabled",name,0);
  2055. ckstrncpy((char *)epktmsg,"Access denied",PKTMSGLEN);
  2056. return(0);
  2057. } else name = name2;
  2058. }
  2059. #endif /* NOSERVER */
  2060. #ifdef PIPESEND
  2061. debug(F101,"openi pipesend","",pipesend);
  2062. if (pipesend) {
  2063. int x;
  2064. #ifndef NOPUSH
  2065. x = zxcmd(ZIFILE,name);
  2066. #else
  2067. x = 0;
  2068. #endif /* NOPUSH */
  2069. i_isopen = (x > 0) ? 1 : 0;
  2070. if (!i_isopen)
  2071. ckstrncpy((char *)epktmsg,"Command or pipe failure",PKTMSGLEN);
  2072. debug(F111,"openi pipesend zxcmd",name,x);
  2073. return(i_isopen);
  2074. }
  2075. #endif /* PIPESEND */
  2076. #ifdef CALIBRATE
  2077. if (calibrate) {
  2078. i_isopen = 1;
  2079. return(1);
  2080. }
  2081. #endif /* CALIBRATE */
  2082. x = zopeni(filno,name); /* Otherwise, try to open it. */
  2083. debug(F111,"openi zopeni 1",name,x);
  2084. if (x) {
  2085. i_isopen = 1;
  2086. return(1);
  2087. } else { /* If not found, */
  2088. char xname[CKMAXPATH]; /* convert the name */
  2089. #ifdef NZLTOR
  2090. nzrtol(name,xname,fncnv,fnrpath,CKMAXPATH);
  2091. #else
  2092. zrtol(name,xname); /* to local form and then */
  2093. #endif /* NZLTOR */
  2094. x = zopeni(filno,xname); /* try opening it again. */
  2095. debug(F111,"openi zopeni 2",xname,x);
  2096. if (x) {
  2097. i_isopen = 1;
  2098. return(1); /* It worked. */
  2099. } else {
  2100. char * s;
  2101. s = ck_errstr();
  2102. if (s) if (!s) s = NULL;
  2103. if (!s) s = "Can't open file";
  2104. ckstrncpy((char *)epktmsg,s,PKTMSGLEN);
  2105. tlog(F110,xname,s,0L);
  2106. debug(F110,"openi failed",xname,0);
  2107. debug(F110,"openi message",s,0);
  2108. i_isopen = 0;
  2109. return(0);
  2110. }
  2111. }
  2112. }
  2113. /* O P E N O -- Open a new file for output. */
  2114. int
  2115. openo(name,zz,fcb) char *name; struct zattr *zz; struct filinfo *fcb; {
  2116. char *name2;
  2117. #ifdef DTILDE
  2118. char *dirp;
  2119. #endif /* DTILDE */
  2120. int channel, x;
  2121. if (stdouf) { /* Receiving to stdout? */
  2122. x = zopeno(ZSTDIO,"",zz,NULL);
  2123. o_isopen = (x > 0);
  2124. debug(F101,"openo stdouf zopeno","",x);
  2125. return(x);
  2126. }
  2127. debug(F110,"openo: name",name,0);
  2128. if (cxseen || czseen || discard) { /* If interrupted, get out before */
  2129. debug(F100," open cancelled","",0); /* destroying existing file. */
  2130. return(1); /* Pretend to succeed. */
  2131. }
  2132. channel = ZOFILE; /* SET DESTINATION DISK or PRINTER */
  2133. #ifdef PIPESEND
  2134. debug(F101,"openo pipesend","",pipesend);
  2135. if (pipesend) {
  2136. int x;
  2137. #ifndef NOPUSH
  2138. x = zxcmd(ZOFILE,(char *)srvcmd);
  2139. #else
  2140. x = 0;
  2141. #endif /* NOPUSH */
  2142. o_isopen = x > 0;
  2143. debug(F101,"openo zxcmd","",x);
  2144. return(x);
  2145. }
  2146. #endif /* PIPESEND */
  2147. if (dest == DEST_S) { /* SET DEST SCREEN... */
  2148. channel = ZCTERM;
  2149. fcb = NULL;
  2150. }
  2151. #ifdef DTILDE
  2152. if (*name == '~') {
  2153. dirp = tilde_expand(name);
  2154. if (*dirp) ckstrncpy(name,dirp,CKMAXPATH+1);
  2155. }
  2156. #endif /* DTILDE */
  2157. if (server && !en_cwd) { /* If running as server */
  2158. zstrip(name,&name2); /* and CWD is disabled, */
  2159. if (strcmp(name,name2)) { /* check if pathname was included. */
  2160. tlog(F110,name,"authorization failure",0L);
  2161. debug(F110,"openo CD disabled",name,0);
  2162. return(0);
  2163. } else name = name2;
  2164. }
  2165. if (zopeno(channel,name,zz,fcb) <= 0) { /* Try to open the file */
  2166. o_isopen = 0;
  2167. debug(F110,"openo failed",name,0);
  2168. /* tlog(F110,"Failure to open",name,0L); */
  2169. return(0);
  2170. } else {
  2171. o_isopen = 1;
  2172. debug(F110,"openo ok, name",name,0);
  2173. return(1);
  2174. }
  2175. }
  2176. /* O P E N T -- Open the terminal for output, in place of a file */
  2177. int
  2178. opent(zz) struct zattr *zz; {
  2179. int x;
  2180. ffc = tfc = (CK_OFF_T)0;
  2181. x = zopeno(ZCTERM,"",zz,NULL);
  2182. debug(F101,"opent zopeno","",x);
  2183. if (x >= 0) {
  2184. o_isopen = 1;
  2185. binary = XYFT_T;
  2186. } else
  2187. return(0);
  2188. return(x);
  2189. }
  2190. /* O P E N X -- Open nothing (incoming file to be accepted but ignored) */
  2191. int
  2192. ckopenx(zz) struct zattr *zz; {
  2193. ffc = tfc = (CK_OFF_T)0; /* Reset counters */
  2194. o_isopen = 1;
  2195. debug(F101,"ckopenx fsize","",fsize);
  2196. xxscreen(SCR_FS,0,fsize,""); /* Let screen display know the size */
  2197. return(1);
  2198. }
  2199. /* C L S I F -- Close the current input file. */
  2200. int
  2201. clsif() {
  2202. extern int xferstat, success;
  2203. int x = 0;
  2204. fcps(); /* Calculate CPS quickly */
  2205. #ifdef datageneral
  2206. if ((local) && (!quiet)) /* Only do this if local & not quiet */
  2207. if (nfils < 1) /* More files to send ... leave it on! */
  2208. connoi_mt();
  2209. #endif /* datageneral */
  2210. debug(F101,"clsif i_isopen","",i_isopen);
  2211. if (i_isopen) { /* If input file is open... */
  2212. if (memstr) { /* If input was memory string, */
  2213. memstr = 0; /* indicate no more. */
  2214. } else {
  2215. x = zclose(ZIFILE); /* else close input file. */
  2216. }
  2217. #ifdef DEBUG
  2218. if (deblog) {
  2219. debug(F101,"clsif zclose","",x);
  2220. debug(F101,"clsif success","",success);
  2221. debug(F101,"clsif xferstat","",xferstat);
  2222. debug(F101,"clsif fsize","",fsize);
  2223. debug(F101,"clsif ffc","",ffc);
  2224. debug(F101,"clsif cxseen","",cxseen);
  2225. debug(F101,"clsif czseen","",czseen);
  2226. debug(F101,"clsif discard","",czseen);
  2227. }
  2228. #endif /* DEBUG */
  2229. if ((cxseen || czseen) && !epktsent) { /* If interrupted */
  2230. xxscreen(SCR_ST,ST_INT,0l,""); /* say so */
  2231. #ifdef TLOG
  2232. if (tralog && !tlogfmt)
  2233. doxlog(what,psfspec,fsize,binary,1,"Interrupted");
  2234. #endif /* TLOG */
  2235. } else if (discard && !epktsent) { /* If I'm refusing */
  2236. xxscreen(SCR_ST,ST_REFU,0l,refused); /* say why */
  2237. #ifdef TLOG
  2238. if (tralog && !tlogfmt) {
  2239. char buf[128];
  2240. ckmakmsg(buf,128,"Refused: ",refused,NULL,NULL);
  2241. doxlog(what,psfspec,fsize,binary,1,buf);
  2242. }
  2243. #endif /* TLOG */
  2244. } else if (!epktrcvd && !epktsent && !cxseen && !czseen) {
  2245. CK_OFF_T zz;
  2246. zz = ffc;
  2247. #ifdef CK_RESEND
  2248. if (sendmode == SM_RESEND || sendmode == SM_PSEND)
  2249. zz += sendstart;
  2250. #endif /* CK_RESEND */
  2251. debug(F101,"clsif fstats","",zz);
  2252. fstats(); /* Update statistics */
  2253. if ( /* Was the whole file sent? */
  2254. #ifdef VMS
  2255. 0 /* Not a reliable check in VMS */
  2256. #else
  2257. #ifdef STRATUS
  2258. 0 /* Probably not for VOS either */
  2259. #else
  2260. zz < fsize
  2261. #ifdef CK_CTRLZ
  2262. && ((eofmethod != XYEOF_Z && !binary) || binary)
  2263. #endif /* CK_CTRLZ */
  2264. #endif /* STRATUS */
  2265. #endif /* VMS */
  2266. ) {
  2267. xxscreen(SCR_ST,ST_INT,0l,"");
  2268. #ifdef TLOG
  2269. if (tralog && !tlogfmt)
  2270. doxlog(what,psfspec,fsize,binary,1,"Incomplete");
  2271. #endif /* TLOG */
  2272. } else {
  2273. #ifdef COMMENT
  2274. /* Not yet -- we don't have confirmation from the receiver */
  2275. xxscreen(SCR_ST,ST_OK,0l,"");
  2276. #endif /* COMMENT */
  2277. #ifdef TLOG
  2278. if (tralog && !tlogfmt)
  2279. doxlog(what,psfspec,fsize,binary,0,"");
  2280. #endif /* TLOG */
  2281. }
  2282. }
  2283. }
  2284. i_isopen = 0;
  2285. hcflg = 0; /* Reset flags */
  2286. sendstart = (CK_OFF_T)0; /* Don't do this again! */
  2287. #ifdef COMMENT
  2288. /*
  2289. This prevents a subsequent call to clsof() from deleting the file
  2290. when given the discard flag.
  2291. */
  2292. *filnam = '\0'; /* and current file name */
  2293. #endif /* COMMENT */
  2294. return(x);
  2295. }
  2296. /* C L S O F -- Close an output file. */
  2297. /* Call with disp != 0 if file is to be discarded. */
  2298. /* Returns -1 upon failure to close, 0 or greater on success. */
  2299. int
  2300. clsof(disp) int disp; {
  2301. int x = 0;
  2302. extern int success;
  2303. fcps(); /* Calculate CPS quickly */
  2304. debug(F101,"clsof disp","",disp);
  2305. debug(F101,"clsof cxseen","",cxseen);
  2306. debug(F101,"clsof success","",success);
  2307. debug(F101,"clsof o_isopen","",o_isopen);
  2308. if (fncsav != -1) { /* Saved file collision action... */
  2309. fncact = fncsav; /* Restore it. */
  2310. fncsav = -1; /* Unsave it. */
  2311. }
  2312. #ifdef datageneral
  2313. if ((local) && (!quiet)) /* Only do this if local & not quiet */
  2314. connoi_mt();
  2315. #endif /* datageneral */
  2316. if (o_isopen && !calibrate) {
  2317. if ((x = zclose(ZOFILE)) < 0) { /* Try to close the file */
  2318. tlog(F100,"Failure to close",filnam,0L);
  2319. xxscreen(SCR_ST,ST_ERR,0l,"Can't close file");
  2320. #ifdef TLOG
  2321. if (tralog && !tlogfmt)
  2322. doxlog(what,prfspec,fsize,binary,1,"Can't close file");
  2323. #endif /* TLOG */
  2324. } else if (disp) { /* Interrupted or refused */
  2325. if (keep == 0 || /* If not keeping incomplete files */
  2326. (keep == SET_AUTO && binary == XYFT_T)
  2327. ) {
  2328. if (*filnam && (what & W_RECV)) /* AND we're receiving */
  2329. zdelet(filnam); /* ONLY THEN, delete it */
  2330. if (what & W_KERMIT) {
  2331. debug(F100,"clsof incomplete discarded","",0);
  2332. tlog(F100," incomplete: discarded","",0L);
  2333. if (!epktrcvd && !epktsent) {
  2334. xxscreen(SCR_ST,ST_DISC,0l,"");
  2335. #ifdef TLOG
  2336. if (tralog && !tlogfmt)
  2337. doxlog(what,prfspec,fsize,binary,1,"Discarded");
  2338. #endif /* TLOG */
  2339. }
  2340. }
  2341. } else { /* Keep incomplete copy */
  2342. debug(F100,"clsof fstats 1","",0);
  2343. fstats();
  2344. if (!discard) { /* Unless discarding for other reason... */
  2345. if (what & W_KERMIT) {
  2346. debug(F100,"closf incomplete kept","",0);
  2347. tlog(F100," incomplete: kept","",0L);
  2348. }
  2349. }
  2350. if (what & W_KERMIT) {
  2351. if (!epktrcvd && !epktsent) {
  2352. xxscreen(SCR_ST,ST_INC,0l,"");
  2353. #ifdef TLOG
  2354. if (tralog && !tlogfmt)
  2355. doxlog(what,prfspec,fsize,binary,1,"Incomplete");
  2356. #endif /* TLOG */
  2357. }
  2358. }
  2359. }
  2360. }
  2361. }
  2362. if (o_isopen && x > -1 && !disp) {
  2363. debug(F110,"clsof OK",rfspec,0);
  2364. makestr(&rfspec,prfspec);
  2365. makestr(&rrfspec,prrfspec);
  2366. fstats();
  2367. if (!epktrcvd && !epktsent && !cxseen && !czseen) {
  2368. xxscreen(SCR_ST,ST_OK,0L,"");
  2369. #ifdef TLOG
  2370. if (tralog && !tlogfmt)
  2371. doxlog(what,rfspec,fsize,binary,0,"");
  2372. #endif /* TLOG */
  2373. }
  2374. }
  2375. rs_len = (CK_OFF_T)0;
  2376. o_isopen = 0; /* The file is not open any more. */
  2377. cxseen = 0; /* Reset per-file interruption flag */
  2378. return(x); /* Send back zclose() return code. */
  2379. }
  2380. #ifdef SUNOS4S5
  2381. tolower(c) char c; { return((c)-'A'+'a'); }
  2382. toupper(c) char c; { return((c)-'a'+'A'); }
  2383. #endif /* SUNOS4S5 */
  2384. #endif /* NOXFER */