msdos.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  1. /*
  2. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*---------------------------------------------------------------------------
  9. msdos.c
  10. MSDOS-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  11. Contains: Opendir() (from zip)
  12. Readdir() (from zip)
  13. do_wild()
  14. mapattr()
  15. mapname()
  16. maskDOSdevice()
  17. map2fat()
  18. checkdir()
  19. isfloppy()
  20. z_dos_chmod()
  21. volumelabel() (non-djgpp, non-emx)
  22. close_outfile()
  23. stamp_file() (TIMESTAMP only)
  24. prepare_ISO_OEM_translat()
  25. dateformat()
  26. version()
  27. zcalloc() (16-bit, only)
  28. zcfree() (16-bit, only)
  29. _dos_getcountryinfo() (djgpp 1.x, emx)
  30. [_dos_getftime() (djgpp 1.x, emx) to be added]
  31. _dos_setftime() (djgpp 1.x, emx)
  32. _dos_setfileattr() (djgpp 1.x, emx)
  33. _dos_getdrive() (djgpp 1.x, emx)
  34. _dos_creat() (djgpp 1.x, emx)
  35. _dos_close() (djgpp 1.x, emx)
  36. volumelabel() (djgpp, emx)
  37. _dos_getcountryinfo() (djgpp 2.x)
  38. _is_executable() (djgpp 2.x)
  39. __crt0_glob_function() (djgpp 2.x)
  40. __crt0_load_environment_file() (djgpp 2.x)
  41. dos_getcodepage() (all, ASM system call)
  42. screensize() (emx, Watcom 32-bit)
  43. int86x_realmode() (Watcom 32-bit)
  44. stat_bandaid() (Watcom)
  45. ---------------------------------------------------------------------------*/
  46. #define UNZIP_INTERNAL
  47. #include "unzip.h"
  48. /* fUnZip does not need anything from here except the zcalloc() & zcfree()
  49. * function pair (when Deflate64 support is enabled in 16-bit environment).
  50. */
  51. #ifndef FUNZIP
  52. static void maskDOSdevice(__GPRO__ char *pathcomp, char *last_dot);
  53. #ifdef MAYBE_PLAIN_FAT
  54. static void map2fat OF((char *pathcomp, char *last_dot));
  55. #endif
  56. static int isfloppy OF((int nDrive));
  57. static int z_dos_chmod OF((__GPRO__ ZCONST char *fname, int attributes));
  58. static int volumelabel OF((ZCONST char *newlabel));
  59. #if (!defined(SFX) && !defined(WINDLL))
  60. static int is_running_on_windows OF((void));
  61. #endif
  62. static int getdoscodepage OF((void));
  63. static int created_dir; /* used by mapname(), checkdir() */
  64. static int renamed_fullpath; /* ditto */
  65. static unsigned nLabelDrive; /* ditto, plus volumelabel() */
  66. /*****************************/
  67. /* Strings used in msdos.c */
  68. /*****************************/
  69. #ifndef SFX
  70. static ZCONST char Far CantAllocateWildcard[] =
  71. "warning: cannot allocate wildcard buffers\n";
  72. #endif
  73. static ZCONST char Far WarnDirTraversSkip[] =
  74. "warning: skipped \"../\" path component(s) in %s\n";
  75. static ZCONST char Far Creating[] = " creating: %s\n";
  76. static ZCONST char Far ConversionFailed[] =
  77. "mapname: conversion of %s failed\n";
  78. static ZCONST char Far Labelling[] = "labelling %c: %-22s\n";
  79. static ZCONST char Far ErrSetVolLabel[] =
  80. "mapname: error setting volume label\n";
  81. static ZCONST char Far PathTooLong[] = "checkdir error: path too long: %s\n";
  82. static ZCONST char Far CantCreateDir[] = "checkdir error: cannot create %s\n\
  83. unable to process %s.\n";
  84. static ZCONST char Far DirIsntDirectory[] =
  85. "checkdir error: %s exists but is not directory\n\
  86. unable to process %s.\n";
  87. static ZCONST char Far PathTooLongTrunc[] =
  88. "checkdir warning: path too long; truncating\n %s\n\
  89. -> %s\n";
  90. #if (!defined(SFX) || defined(SFX_EXDIR))
  91. static ZCONST char Far CantCreateExtractDir[] =
  92. "checkdir: cannot create extraction directory: %s\n";
  93. #endif
  94. static ZCONST char Far AttribsMayBeWrong[] =
  95. "\nwarning: file attributes may not be correct\n";
  96. #if (!defined(SFX) && !defined(WINDLL))
  97. static ZCONST char Far WarnUsedOnWindows[] =
  98. "\n%s warning: You are using the MSDOS version on Windows.\n"
  99. "Please try the native Windows version before reporting any problems.\n";
  100. #endif
  101. /****************************/
  102. /* Macros used in msdos.c */
  103. /****************************/
  104. #ifdef WATCOMC_386
  105. # define WREGS(v,r) (v.w.r)
  106. # define int86x int386x
  107. static int int86x_realmode(int inter_no, union REGS *in,
  108. union REGS *out, struct SREGS *seg);
  109. # define F_intdosx(ir,or,sr) int86x_realmode(0x21, ir, or, sr)
  110. # define XXX__MK_FP_IS_BROKEN
  111. #else
  112. # if (defined(__DJGPP__) && (__DJGPP__ >= 2))
  113. # define WREGS(v,r) (v.w.r)
  114. # else
  115. # define WREGS(v,r) (v.x.r)
  116. # endif
  117. # define F_intdosx(ir,or,sr) intdosx(ir, or, sr)
  118. #endif
  119. #if (defined(__GO32__) || defined(__EMX__))
  120. # include <dirent.h> /* use readdir() */
  121. # define MKDIR(path,mode) mkdir(path,mode)
  122. # define Opendir opendir
  123. # define Readdir readdir
  124. # define Closedir closedir
  125. # define zdirent dirent
  126. # define zDIR DIR
  127. # ifdef __EMX__
  128. # include <dos.h>
  129. # define GETDRIVE(d) d = _getdrive()
  130. # define FA_LABEL A_LABEL
  131. # else
  132. # define GETDRIVE(d) _dos_getdrive(&d)
  133. # endif
  134. # if defined(_A_SUBDIR) /* MSC dos.h and compatibles */
  135. # define FSUBDIR _A_SUBDIR
  136. # elif defined(FA_DIREC) /* Borland dos.h and compatible variants */
  137. # define FSUBDIR FA_DIREC
  138. # elif defined(A_DIR) /* EMX dir.h (and dirent.h) */
  139. # define FSUBDIR A_DIR
  140. # else /* fallback definition */
  141. # define FSUBDIR 0x10
  142. # endif
  143. # if defined(_A_VOLID) /* MSC dos.h and compatibles */
  144. # define FVOLID _A_VOLID
  145. # elif defined(FA_LABEL) /* Borland dos.h and compatible variants */
  146. # define FVOLID FA_LABEL
  147. # elif defined(A_LABEL) /* EMX dir.h (and dirent.h) */
  148. # define FVOLID A_LABEL
  149. # else
  150. # define FVOLID 0x08
  151. # endif
  152. #else /* !(__GO32__ || __EMX__) */
  153. # define MKDIR(path,mode) mkdir(path)
  154. # ifdef __TURBOC__
  155. # define FATTR FA_HIDDEN+FA_SYSTEM+FA_DIREC
  156. # define FVOLID FA_LABEL
  157. # define FSUBDIR FA_DIREC
  158. # define FFIRST(n,d,a) findfirst(n,(struct ffblk *)d,a)
  159. # define FNEXT(d) findnext((struct ffblk *)d)
  160. # define GETDRIVE(d) d=getdisk()+1
  161. # include <dir.h>
  162. # else /* !__TURBOC__ */
  163. # define FATTR _A_HIDDEN+_A_SYSTEM+_A_SUBDIR
  164. # define FVOLID _A_VOLID
  165. # define FSUBDIR _A_SUBDIR
  166. # define FFIRST(n,d,a) _dos_findfirst(n,a,(struct find_t *)d)
  167. # define FNEXT(d) _dos_findnext((struct find_t *)d)
  168. # define GETDRIVE(d) _dos_getdrive(&d)
  169. # include <direct.h>
  170. # endif /* ?__TURBOC__ */
  171. typedef struct zdirent {
  172. char d_reserved[30];
  173. char d_name[13];
  174. int d_first;
  175. } zDIR;
  176. zDIR *Opendir OF((const char *));
  177. struct zdirent *Readdir OF((zDIR *));
  178. # define Closedir free
  179. #ifndef SFX
  180. /**********************/ /* Borland C++ 3.x has its own opendir/readdir */
  181. /* Function Opendir() */ /* library routines, but earlier versions don't, */
  182. /**********************/ /* so use ours regardless */
  183. zDIR *Opendir(name)
  184. const char *name; /* name of directory to open */
  185. {
  186. zDIR *dirp; /* malloc'd return value */
  187. char *nbuf; /* malloc'd temporary string */
  188. extent len = strlen(name); /* path length to avoid strlens and strcats */
  189. if ((dirp = (zDIR *)malloc(sizeof(zDIR))) == (zDIR *)NULL)
  190. return (zDIR *)NULL;
  191. if ((nbuf = malloc(len + 6)) == (char *)NULL) {
  192. free(dirp);
  193. return (zDIR *)NULL;
  194. }
  195. strcpy(nbuf, name);
  196. if (len > 0) {
  197. if (nbuf[len-1] == ':') {
  198. nbuf[len++] = '.';
  199. } else if (nbuf[len-1] == '/' || nbuf[len-1] == '\\')
  200. --len;
  201. }
  202. strcpy(nbuf+len, "/*.*");
  203. Trace((stderr, "Opendir: nbuf = [%s]\n", FnFilter1(nbuf)));
  204. if (FFIRST(nbuf, dirp, FATTR)) {
  205. free((zvoid *)nbuf);
  206. return (zDIR *)NULL;
  207. }
  208. free((zvoid *)nbuf);
  209. dirp->d_first = 1;
  210. return dirp;
  211. }
  212. /**********************/
  213. /* Function Readdir() */
  214. /**********************/
  215. struct zdirent *Readdir(d)
  216. zDIR *d; /* directory stream from which to read */
  217. {
  218. /* Return pointer to first or next directory entry, or NULL if end. */
  219. if (d->d_first)
  220. d->d_first = 0;
  221. else
  222. if (FNEXT(d))
  223. return (struct zdirent *)NULL;
  224. return (struct zdirent *)d;
  225. }
  226. #endif /* !SFX */
  227. #endif /* ?(__GO32__ || __EMX__) */
  228. #ifndef SFX
  229. /************************/
  230. /* Function do_wild() */ /* identical to OS/2 version */
  231. /************************/
  232. char *do_wild(__G__ wildspec)
  233. __GDEF
  234. ZCONST char *wildspec; /* only used first time on a given dir */
  235. {
  236. static zDIR *wild_dir = (zDIR *)NULL;
  237. static ZCONST char *wildname;
  238. static char *dirname, matchname[FILNAMSIZ];
  239. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  240. char *fnamestart;
  241. struct zdirent *file;
  242. /* Even when we're just returning wildspec, we *always* do so in
  243. * matchname[]--calling routine is allowed to append four characters
  244. * to the returned string, and wildspec may be a pointer to argv[].
  245. */
  246. if (!notfirstcall) { /* first call: must initialize everything */
  247. notfirstcall = TRUE;
  248. if (!iswild(wildspec)) {
  249. strncpy(matchname, wildspec, FILNAMSIZ);
  250. matchname[FILNAMSIZ-1] = '\0';
  251. have_dirname = FALSE;
  252. wild_dir = NULL;
  253. return matchname;
  254. }
  255. /* break the wildspec into a directory part and a wildcard filename */
  256. if ((wildname = strrchr(wildspec, '/')) == (ZCONST char *)NULL &&
  257. (wildname = strrchr(wildspec, ':')) == (ZCONST char *)NULL) {
  258. dirname = ".";
  259. dirnamelen = 1;
  260. have_dirname = FALSE;
  261. wildname = wildspec;
  262. } else {
  263. ++wildname; /* point at character after '/' or ':' */
  264. dirnamelen = (int)(wildname - wildspec);
  265. if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  266. Info(slide, 1, ((char *)slide,
  267. LoadFarString(CantAllocateWildcard)));
  268. strncpy(matchname, wildspec, FILNAMSIZ);
  269. matchname[FILNAMSIZ-1] = '\0';
  270. return matchname; /* but maybe filespec was not a wildcard */
  271. }
  272. /* GRR: can't strip trailing char for opendir since might be "d:/" or "d:"
  273. * (would have to check for "./" at end--let opendir handle it instead) */
  274. strncpy(dirname, wildspec, dirnamelen);
  275. dirname[dirnamelen] = '\0'; /* terminate for strcpy below */
  276. have_dirname = TRUE;
  277. }
  278. Trace((stderr, "do_wild: dirname = [%s]\n", FnFilter1(dirname)));
  279. if ((wild_dir = Opendir(dirname)) != (zDIR *)NULL) {
  280. if (have_dirname) {
  281. strcpy(matchname, dirname);
  282. fnamestart = matchname + dirnamelen;
  283. } else
  284. fnamestart = matchname;
  285. while ((file = Readdir(wild_dir)) != (struct zdirent *)NULL) {
  286. Trace((stderr, "do_wild: readdir returns %s\n",
  287. FnFilter1(file->d_name)));
  288. strcpy(fnamestart, file->d_name);
  289. if (strrchr(fnamestart, '.') == (char *)NULL)
  290. strcat(fnamestart, ".");
  291. /* 1 == ignore case (for case-insensitive DOS-FS) */
  292. if (match(fnamestart, wildname, 1 WISEP) &&
  293. /* skip "." and ".." directory entries */
  294. strcmp(fnamestart, ".") && strcmp(fnamestart, "..")) {
  295. Trace((stderr, "do_wild: match() succeeds\n"));
  296. /* remove trailing dot */
  297. fnamestart += strlen(fnamestart) - 1;
  298. if (*fnamestart == '.')
  299. *fnamestart = '\0';
  300. return matchname;
  301. }
  302. }
  303. /* if we get to here directory is exhausted, so close it */
  304. Closedir(wild_dir);
  305. wild_dir = (zDIR *)NULL;
  306. }
  307. #ifdef DEBUG
  308. else {
  309. Trace((stderr, "do_wild: Opendir(%s) returns NULL\n",
  310. FnFilter1(dirname)));
  311. }
  312. #endif /* DEBUG */
  313. /* return the raw wildspec in case that works (e.g., directory not
  314. * searchable, but filespec was not wild and file is readable) */
  315. strncpy(matchname, wildspec, FILNAMSIZ);
  316. matchname[FILNAMSIZ-1] = '\0';
  317. return matchname;
  318. }
  319. /* last time through, might have failed opendir but returned raw wildspec */
  320. if (wild_dir == (zDIR *)NULL) {
  321. notfirstcall = FALSE; /* nothing left to try--reset for new wildspec */
  322. if (have_dirname)
  323. free(dirname);
  324. return (char *)NULL;
  325. }
  326. /* If we've gotten this far, we've read and matched at least one entry
  327. * successfully (in a previous call), so dirname has been copied into
  328. * matchname already.
  329. */
  330. if (have_dirname) {
  331. /* strcpy(matchname, dirname); */
  332. fnamestart = matchname + dirnamelen;
  333. } else
  334. fnamestart = matchname;
  335. while ((file = Readdir(wild_dir)) != (struct zdirent *)NULL) {
  336. Trace((stderr, "do_wild: readdir returns %s\n",
  337. FnFilter1(file->d_name)));
  338. strcpy(fnamestart, file->d_name);
  339. if (strrchr(fnamestart, '.') == (char *)NULL)
  340. strcat(fnamestart, ".");
  341. if (match(fnamestart, wildname, 1 WISEP)) { /* 1 == ignore case */
  342. Trace((stderr, "do_wild: match() succeeds\n"));
  343. /* remove trailing dot */
  344. fnamestart += strlen(fnamestart) - 1;
  345. if (*fnamestart == '.')
  346. *fnamestart = '\0';
  347. return matchname;
  348. }
  349. }
  350. Closedir(wild_dir); /* have read at least one entry; nothing left */
  351. wild_dir = (zDIR *)NULL;
  352. notfirstcall = FALSE; /* reset for new wildspec */
  353. if (have_dirname)
  354. free(dirname);
  355. return (char *)NULL;
  356. } /* end function do_wild() */
  357. #endif /* !SFX */
  358. /**********************/
  359. /* Function mapattr() */
  360. /**********************/
  361. int mapattr(__G)
  362. __GDEF
  363. {
  364. /* set archive bit for file entries (file is not backed up): */
  365. G.pInfo->file_attr = ((unsigned)G.crec.external_file_attributes |
  366. (G.crec.external_file_attributes & FSUBDIR ? 0 : 32)) & 0xff;
  367. return 0;
  368. } /* end function mapattr() */
  369. /************************/
  370. /* Function mapname() */
  371. /************************/
  372. int mapname(__G__ renamed)
  373. __GDEF
  374. int renamed;
  375. /*
  376. * returns:
  377. * MPN_OK - no problem detected
  378. * MPN_INF_TRUNC - caution (truncated filename)
  379. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  380. * MPN_ERR_SKIP - error -> skip entry
  381. * MPN_ERR_TOOLONG - error -> path is too long
  382. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  383. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  384. */
  385. {
  386. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  387. char *pp, *cp=(char *)NULL; /* character pointers */
  388. char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  389. #ifdef MAYBE_PLAIN_FAT
  390. char *last_dot=(char *)NULL; /* last dot not converted to underscore */
  391. # ifdef USE_LFN
  392. int use_lfn = USE_LFN; /* file system supports long filenames? */
  393. # endif
  394. #endif
  395. int killed_ddot = FALSE; /* is set when skipping "../" pathcomp */
  396. int error = MPN_OK;
  397. register unsigned workch; /* hold the character being tested */
  398. /*---------------------------------------------------------------------------
  399. Initialize various pointers and counters and stuff.
  400. ---------------------------------------------------------------------------*/
  401. /* can create path as long as not just freshening, or if user told us */
  402. G.create_dirs = (!uO.fflag || renamed);
  403. created_dir = FALSE; /* not yet */
  404. renamed_fullpath = FALSE;
  405. if (renamed) {
  406. cp = G.filename - 1; /* point to beginning of renamed name... */
  407. while (*++cp)
  408. if (*cp == '\\') /* convert backslashes to forward */
  409. *cp = '/';
  410. cp = G.filename;
  411. /* use temporary rootpath if user gave full pathname */
  412. if (G.filename[0] == '/') {
  413. renamed_fullpath = TRUE;
  414. pathcomp[0] = '/'; /* copy the '/' and terminate */
  415. pathcomp[1] = '\0';
  416. ++cp;
  417. } else if (isalpha((uch)G.filename[0]) && G.filename[1] == ':') {
  418. renamed_fullpath = TRUE;
  419. pp = pathcomp;
  420. *pp++ = *cp++; /* copy the "d:" (+ '/', possibly) */
  421. *pp++ = *cp++;
  422. if (*cp == '/')
  423. *pp++ = *cp++; /* otherwise add "./"? */
  424. *pp = '\0';
  425. }
  426. }
  427. /* pathcomp is ignored unless renamed_fullpath is TRUE: */
  428. if ((error = checkdir(__G__ pathcomp, INIT)) != 0) /* initialize path buf */
  429. return error; /* ...unless no mem or vol label on hard disk */
  430. *pathcomp = '\0'; /* initialize translation buffer */
  431. pp = pathcomp; /* point to translation buffer */
  432. if (!renamed) { /* cp already set if renamed */
  433. if (uO.jflag) /* junking directories */
  434. cp = (char *)strrchr(G.filename, '/');
  435. if (cp == (char *)NULL) /* no '/' or not junking dirs */
  436. cp = G.filename; /* point to internal zipfile-member pathname */
  437. else
  438. ++cp; /* point to start of last component of path */
  439. }
  440. /*---------------------------------------------------------------------------
  441. Begin main loop through characters in filename.
  442. ---------------------------------------------------------------------------*/
  443. while ((workch = (uch)*cp++) != 0) {
  444. switch (workch) {
  445. case '/': /* can assume -j flag not given */
  446. *pp = '\0';
  447. #ifdef MAYBE_PLAIN_FAT
  448. maskDOSdevice(__G__ pathcomp, last_dot);
  449. #else
  450. maskDOSdevice(__G__ pathcomp, NULL);
  451. #endif
  452. #ifdef MAYBE_PLAIN_FAT
  453. # ifdef USE_LFN
  454. if (!use_lfn)
  455. # endif
  456. {
  457. map2fat(pathcomp, last_dot); /* 8.3 trunc. (in place) */
  458. last_dot = (char *)NULL;
  459. }
  460. #endif
  461. if (strcmp(pathcomp, ".") == 0) {
  462. /* don't bother appending "./" to the path */
  463. *pathcomp = '\0';
  464. } else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
  465. /* "../" dir traversal detected, skip over it */
  466. *pathcomp = '\0';
  467. killed_ddot = TRUE; /* set "show message" flag */
  468. }
  469. /* when path component is not empty, append it now */
  470. if (*pathcomp != '\0' &&
  471. ((error = checkdir(__G__ pathcomp, APPEND_DIR))
  472. & MPN_MASK) > MPN_INF_TRUNC)
  473. return error;
  474. pp = pathcomp; /* reset conversion buffer for next piece */
  475. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  476. break;
  477. #ifdef MAYBE_PLAIN_FAT
  478. case '.':
  479. # ifdef USE_LFN
  480. if (use_lfn) { /* LFN filenames may contain many */
  481. *pp++ = '.'; /* dots, so simply copy it ... */
  482. } else
  483. # endif
  484. if (pp == pathcomp && *cp == '.' && cp[1] == '/') {
  485. /* nothing appended yet.., and found "../" */
  486. *pp++ = '.'; /* add first dot, */
  487. *pp++ = '.'; /* second dot, and */
  488. ++cp; /* skip over to the '/' */
  489. } else { /* found dot within path component */
  490. last_dot = pp; /* point at last dot so far... */
  491. *pp++ = '_'; /* convert to underscore for now */
  492. }
  493. break;
  494. #endif /* MAYBE_PLAIN_FAT */
  495. /* drive names are not stored in zipfile, so no colons allowed;
  496. * no brackets or most other punctuation either (all of which
  497. * can appear in Unix-created archives; backslash is particularly
  498. * bad unless all necessary directories exist) */
  499. #ifdef MAYBE_PLAIN_FAT
  500. case '[': /* these punctuation characters forbidden */
  501. case ']': /* only on plain FAT file systems */
  502. case '+':
  503. case ',':
  504. case '=':
  505. # ifdef USE_LFN
  506. if (use_lfn)
  507. *pp++ = (char)workch;
  508. else
  509. *pp++ = '_';
  510. break;
  511. # endif
  512. #endif
  513. case ':': /* special shell characters of command.com */
  514. case '\\': /* (device and directory limiters, wildcard */
  515. case '"': /* characters, stdin/stdout redirection and */
  516. case '<': /* pipe indicators and the quote sign) are */
  517. case '>': /* never allowed in filenames on (V)FAT */
  518. case '|':
  519. case '*':
  520. case '?':
  521. *pp++ = '_';
  522. break;
  523. case ';': /* start of VMS version? */
  524. lastsemi = pp;
  525. #ifdef MAYBE_PLAIN_FAT
  526. # ifdef USE_LFN
  527. if (use_lfn)
  528. *pp++ = ';'; /* keep for now; remove VMS ";##" later */
  529. # endif
  530. #else
  531. *pp++ = ';'; /* keep for now; remove VMS ";##" later */
  532. #endif
  533. break;
  534. #ifdef MAYBE_PLAIN_FAT
  535. case ' ': /* change spaces to underscores */
  536. # ifdef USE_LFN
  537. if (!use_lfn && uO.sflag) /* only if requested and NO lfn! */
  538. # else
  539. if (uO.sflag) /* only if requested */
  540. # endif
  541. *pp++ = '_';
  542. else
  543. *pp++ = (char)workch;
  544. break;
  545. #endif /* MAYBE_PLAIN_FAT */
  546. default:
  547. /* allow ASCII 255 and European characters in filenames: */
  548. if (isprint(workch) || workch >= 127)
  549. *pp++ = (char)workch;
  550. } /* end switch */
  551. } /* end while loop */
  552. /* Show warning when stripping insecure "parent dir" path components */
  553. if (killed_ddot && QCOND2) {
  554. Info(slide, 0, ((char *)slide, LoadFarString(WarnDirTraversSkip),
  555. FnFilter1(G.filename)));
  556. if (!(error & ~MPN_MASK))
  557. error = (error & MPN_MASK) | PK_WARN;
  558. }
  559. /*---------------------------------------------------------------------------
  560. Report if directory was created (and no file to create: filename ended
  561. in '/'), check name to be sure it exists, and combine path and name be-
  562. fore exiting.
  563. ---------------------------------------------------------------------------*/
  564. if (G.filename[strlen(G.filename) - 1] == '/') {
  565. checkdir(__G__ G.filename, GETPATH);
  566. if (created_dir) {
  567. if (QCOND2) {
  568. Info(slide, 0, ((char *)slide, LoadFarString(Creating),
  569. FnFilter1(G.filename)));
  570. }
  571. /* set file attributes: */
  572. z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  573. /* set dir time (note trailing '/') */
  574. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  575. } else if (IS_OVERWRT_ALL) {
  576. /* overwrite attributes of existing directory on user's request */
  577. /* set file attributes: */
  578. z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  579. }
  580. /* dir existed already; don't look for data to extract */
  581. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  582. }
  583. *pp = '\0'; /* done with pathcomp: terminate it */
  584. /* if not saving them, remove VMS version numbers (appended ";###") */
  585. if (!uO.V_flag && lastsemi) {
  586. #ifndef MAYBE_PLAIN_FAT
  587. pp = lastsemi + 1;
  588. #else
  589. # ifdef USE_LFN
  590. if (use_lfn)
  591. pp = lastsemi + 1;
  592. else
  593. pp = lastsemi; /* semi-colon was omitted: expect all #'s */
  594. # else
  595. pp = lastsemi; /* semi-colon was omitted: expect all #'s */
  596. # endif
  597. #endif
  598. while (isdigit((uch)(*pp)))
  599. ++pp;
  600. if (*pp == '\0') /* only digits between ';' and end: nuke */
  601. *lastsemi = '\0';
  602. }
  603. #ifdef MAYBE_PLAIN_FAT
  604. maskDOSdevice(__G__ pathcomp, last_dot);
  605. #else
  606. maskDOSdevice(__G__ pathcomp, NULL);
  607. #endif
  608. if (G.pInfo->vollabel) {
  609. if (strlen(pathcomp) > 11)
  610. pathcomp[11] = '\0';
  611. } else {
  612. #ifdef MAYBE_PLAIN_FAT
  613. # ifdef USE_LFN
  614. if (!use_lfn)
  615. map2fat(pathcomp, last_dot); /* 8.3 truncation (in place) */
  616. # else
  617. map2fat(pathcomp, last_dot); /* 8.3 truncation (in place) */
  618. # endif
  619. #endif
  620. }
  621. if (*pathcomp == '\0') {
  622. Info(slide, 1, ((char *)slide, LoadFarString(ConversionFailed),
  623. FnFilter1(G.filename)));
  624. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  625. }
  626. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  627. checkdir(__G__ G.filename, GETPATH);
  628. if (G.pInfo->vollabel) { /* set the volume label now */
  629. if (QCOND2)
  630. Info(slide, 0, ((char *)slide, LoadFarString(Labelling),
  631. (nLabelDrive + 'a' - 1),
  632. FnFilter1(G.filename)));
  633. if (volumelabel(G.filename)) {
  634. Info(slide, 1, ((char *)slide, LoadFarString(ErrSetVolLabel)));
  635. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  636. }
  637. /* success: skip the "extraction" quietly */
  638. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  639. }
  640. return error;
  641. } /* end function mapname() */
  642. /****************************/
  643. /* Function maskDOSdevice() */
  644. /****************************/
  645. static void maskDOSdevice(__G__ pathcomp, last_dot)
  646. __GDEF
  647. char *pathcomp, *last_dot;
  648. {
  649. /*---------------------------------------------------------------------------
  650. Put an underscore in front of the file name if the file name is a
  651. DOS/WINDOWS device name like CON.*, AUX.*, PRN.*, etc. Trying to
  652. extract such a file would fail at best and wedge us at worst.
  653. ---------------------------------------------------------------------------*/
  654. #if !defined(S_IFCHR) && defined(_S_IFCHR)
  655. # define S_IFCHR _S_IFCHR
  656. #endif
  657. #if !defined(S_ISCHR)
  658. # if defined(_S_ISCHR)
  659. # define S_ISCHR(m) _S_ISCHR(m)
  660. # elif defined(S_IFCHR)
  661. # define S_ISCHR(m) ((m) & S_IFCHR)
  662. # endif
  663. #endif
  664. #ifdef DEBUG
  665. if (stat(pathcomp, &G.statbuf) == 0) {
  666. Trace((stderr,
  667. "maskDOSdevice() stat(\"%s\", buf) st_mode result: %X, %o\n",
  668. FnFilter1(pathcomp), G.statbuf.st_mode, G.statbuf.st_mode));
  669. } else {
  670. Trace((stderr, "maskDOSdevice() stat(\"%s\", buf) failed\n",
  671. FnFilter1(pathcomp)));
  672. }
  673. #endif
  674. if (stat(pathcomp, &G.statbuf) == 0 && S_ISCHR(G.statbuf.st_mode)) {
  675. extent i;
  676. /* pathcomp contains a name of a DOS character device (builtin or
  677. * installed device driver).
  678. * Prepend a '_' to allow creation of the item in the file system.
  679. */
  680. for (i = strlen(pathcomp) + 1; i > 0; --i)
  681. pathcomp[i] = pathcomp[i - 1];
  682. pathcomp[0] = '_';
  683. if (last_dot != (char *)NULL)
  684. last_dot++;
  685. }
  686. } /* end function maskDOSdevice() */
  687. #ifdef MAYBE_PLAIN_FAT
  688. /**********************/
  689. /* Function map2fat() */
  690. /**********************/
  691. static void map2fat(pathcomp, last_dot)
  692. char *pathcomp, *last_dot;
  693. {
  694. char *pEnd = pathcomp + strlen(pathcomp);
  695. /*---------------------------------------------------------------------------
  696. Case 1: filename has no dot, so figure out if we should add one. Note
  697. that the algorithm does not try to get too fancy: if there are no dots
  698. already, the name either gets truncated at 8 characters or the last un-
  699. derscore is converted to a dot (only if more characters are saved that
  700. way). In no case is a dot inserted between existing characters.
  701. GRR: have problem if filename is volume label??
  702. ---------------------------------------------------------------------------*/
  703. if (last_dot == (char *)NULL) { /* no dots: check for underscores... */
  704. char *plu = strrchr(pathcomp, '_'); /* pointer to last underscore */
  705. if ((plu != (char *)NULL) && /* found underscore: convert to dot? */
  706. (MIN(plu - pathcomp, 8) + MIN(pEnd - plu - 1, 3) > 8)) {
  707. last_dot = plu; /* be lazy: drop through to next if-block */
  708. } else if ((pEnd - pathcomp) > 8)
  709. /* no underscore; or converting underscore to dot would save less
  710. chars than leaving everything in the basename */
  711. pathcomp[8] = '\0'; /* truncate at 8 chars */
  712. /* else whole thing fits into 8 chars or less: no change */
  713. }
  714. /*---------------------------------------------------------------------------
  715. Case 2: filename has dot in it, so truncate first half at 8 chars (shift
  716. extension if necessary) and second half at three.
  717. ---------------------------------------------------------------------------*/
  718. if (last_dot != (char *)NULL) { /* one dot is OK: */
  719. *last_dot = '.'; /* put the last back in */
  720. if ((last_dot - pathcomp) > 8) {
  721. char *p, *q;
  722. int i;
  723. p = last_dot;
  724. q = last_dot = pathcomp + 8;
  725. for (i = 0; (i < 4) && *p; ++i) /* too many chars in basename: */
  726. *q++ = *p++; /* shift extension left and */
  727. *q = '\0'; /* truncate/terminate it */
  728. } else if ((pEnd - last_dot) > 4)
  729. last_dot[4] = '\0'; /* too many chars in extension */
  730. /* else filename is fine as is: no change */
  731. if ((last_dot - pathcomp) > 0 && last_dot[-1] == ' ')
  732. last_dot[-1] = '_'; /* NO blank in front of '.'! */
  733. }
  734. } /* end function map2fat() */
  735. #endif /* MAYBE_PLAIN_FAT */
  736. /***********************/
  737. /* Function checkdir() */
  738. /***********************/
  739. int checkdir(__G__ pathcomp, flag)
  740. __GDEF
  741. char *pathcomp;
  742. int flag;
  743. /*
  744. * returns:
  745. * MPN_OK - no problem detected
  746. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  747. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  748. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  749. * exists and is not a directory, but is supposed to be
  750. * MPN_ERR_TOOLONG - path is too long
  751. * MPN_NOMEM - can't allocate memory for filename buffers
  752. */
  753. {
  754. static int rootlen = 0; /* length of rootpath */
  755. static char *rootpath; /* user's "extract-to" directory */
  756. static char *buildpath; /* full path (so far) to extracted file */
  757. static char *end; /* pointer to end of buildpath ('\0') */
  758. #ifdef MSC
  759. int attrs; /* work around MSC stat() bug */
  760. #endif
  761. # define FN_MASK 7
  762. # define FUNCTION (flag & FN_MASK)
  763. /*---------------------------------------------------------------------------
  764. APPEND_DIR: append the path component to the path being built and check
  765. for its existence. If doesn't exist and we are creating directories, do
  766. so for this one; else signal success or error as appropriate.
  767. ---------------------------------------------------------------------------*/
  768. if (FUNCTION == APPEND_DIR) {
  769. int too_long = FALSE;
  770. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  771. while ((*end = *pathcomp++) != '\0')
  772. ++end;
  773. /* GRR: could do better check, see if overrunning buffer as we go:
  774. * check end-buildpath after each append, set warning variable if
  775. * within 20 of FILNAMSIZ; then if var set, do careful check when
  776. * appending. Clear variable when begin new path. */
  777. if ((end-buildpath) > FILNAMSIZ-3) /* need '/', one-char name, '\0' */
  778. too_long = TRUE; /* check if extracting directory? */
  779. #ifdef MSC /* MSC 6.00 bug: stat(non-existent-dir) == 0 [exists!] */
  780. if (_dos_getfileattr(buildpath, &attrs) || stat(buildpath, &G.statbuf))
  781. #else
  782. if (SSTAT(buildpath, &G.statbuf)) /* path doesn't exist */
  783. #endif
  784. {
  785. if (!G.create_dirs) { /* told not to create (freshening) */
  786. free(buildpath);
  787. /* path doesn't exist: nothing to do */
  788. return MPN_INF_SKIP;
  789. }
  790. if (too_long) {
  791. Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  792. FnFilter1(buildpath)));
  793. free(buildpath);
  794. /* no room for filenames: fatal */
  795. return MPN_ERR_TOOLONG;
  796. }
  797. if (MKDIR(buildpath, 0777) == -1) { /* create the directory */
  798. Info(slide, 1, ((char *)slide, LoadFarString(CantCreateDir),
  799. FnFilter2(buildpath), FnFilter1(G.filename)));
  800. free(buildpath);
  801. /* path didn't exist, tried to create, failed */
  802. return MPN_ERR_SKIP;
  803. }
  804. created_dir = TRUE;
  805. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  806. Info(slide, 1, ((char *)slide, LoadFarString(DirIsntDirectory),
  807. FnFilter2(buildpath), FnFilter1(G.filename)));
  808. free(buildpath);
  809. /* path existed but wasn't dir */
  810. return MPN_ERR_SKIP;
  811. }
  812. if (too_long) {
  813. Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  814. FnFilter1(buildpath)));
  815. free(buildpath);
  816. /* no room for filenames: fatal */
  817. return MPN_ERR_TOOLONG;
  818. }
  819. *end++ = '/';
  820. *end = '\0';
  821. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  822. return MPN_OK;
  823. } /* end if (FUNCTION == APPEND_DIR) */
  824. /*---------------------------------------------------------------------------
  825. GETPATH: copy full path to the string pointed at by pathcomp, and free
  826. buildpath.
  827. ---------------------------------------------------------------------------*/
  828. if (FUNCTION == GETPATH) {
  829. strcpy(pathcomp, buildpath);
  830. Trace((stderr, "getting and freeing path [%s]\n",
  831. FnFilter1(pathcomp)));
  832. free(buildpath);
  833. buildpath = end = (char *)NULL;
  834. return MPN_OK;
  835. }
  836. /*---------------------------------------------------------------------------
  837. APPEND_NAME: assume the path component is the filename; append it and
  838. return without checking for existence.
  839. ---------------------------------------------------------------------------*/
  840. if (FUNCTION == APPEND_NAME) {
  841. #ifdef NOVELL_BUG_WORKAROUND
  842. if (end == buildpath && !G.pInfo->vollabel) {
  843. /* work-around for Novell's "overwriting executables" bug:
  844. prepend "./" to name when no path component is specified */
  845. *end++ = '.';
  846. *end++ = '/';
  847. }
  848. #endif /* NOVELL_BUG_WORKAROUND */
  849. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  850. while ((*end = *pathcomp++) != '\0') {
  851. ++end;
  852. if ((end-buildpath) >= FILNAMSIZ) {
  853. *--end = '\0';
  854. Info(slide, 1, ((char *)slide, LoadFarString(PathTooLongTrunc),
  855. FnFilter1(G.filename), FnFilter2(buildpath)));
  856. return MPN_INF_TRUNC; /* filename truncated */
  857. }
  858. }
  859. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  860. /* could check for existence here, prompt for new name... */
  861. return MPN_OK;
  862. }
  863. /*---------------------------------------------------------------------------
  864. INIT: allocate and initialize buffer space for the file currently being
  865. extracted. If file was renamed with an absolute path, don't prepend the
  866. extract-to path.
  867. ---------------------------------------------------------------------------*/
  868. if (FUNCTION == INIT) {
  869. Trace((stderr, "initializing buildpath to "));
  870. /* allocate space for full filename, root path, and maybe "./" */
  871. if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+3)) ==
  872. (char *)NULL)
  873. return MPN_NOMEM;
  874. if (G.pInfo->vollabel) {
  875. /* GRR: for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */
  876. if (renamed_fullpath && pathcomp[1] == ':')
  877. *buildpath = (char)ToLower(*pathcomp);
  878. else if (!renamed_fullpath && rootlen > 1 && rootpath[1] == ':')
  879. *buildpath = (char)ToLower(*rootpath);
  880. else {
  881. GETDRIVE(nLabelDrive); /* assumed that a == 1, b == 2, etc. */
  882. *buildpath = (char)(nLabelDrive - 1 + 'a');
  883. }
  884. nLabelDrive = *buildpath - 'a' + 1; /* save for mapname() */
  885. if (uO.volflag == 0 || *buildpath < 'a' || /* no label/bogus disk */
  886. (uO.volflag == 1 && !isfloppy(nLabelDrive))) /* -$: no fixed */
  887. {
  888. free(buildpath);
  889. return MPN_VOL_LABEL; /* skipping with message */
  890. }
  891. *buildpath = '\0';
  892. end = buildpath;
  893. } else if (renamed_fullpath) { /* pathcomp = valid data */
  894. end = buildpath;
  895. while ((*end = *pathcomp++) != '\0')
  896. ++end;
  897. } else if (rootlen > 0) {
  898. strcpy(buildpath, rootpath);
  899. end = buildpath + rootlen;
  900. } else {
  901. *buildpath = '\0';
  902. end = buildpath;
  903. }
  904. Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  905. return MPN_OK;
  906. }
  907. /*---------------------------------------------------------------------------
  908. ROOT: if appropriate, store the path in rootpath and create it if neces-
  909. sary; else assume it's a zipfile member and return. This path segment
  910. gets used in extracting all members from every zipfile specified on the
  911. command line. Note that under OS/2 and MS-DOS, if a candidate extract-to
  912. directory specification includes a drive letter (leading "x:"), it is
  913. treated just as if it had a trailing '/'--that is, one directory level
  914. will be created if the path doesn't exist, unless this is otherwise pro-
  915. hibited (e.g., freshening).
  916. ---------------------------------------------------------------------------*/
  917. #if (!defined(SFX) || defined(SFX_EXDIR))
  918. if (FUNCTION == ROOT) {
  919. Trace((stderr, "initializing root path to [%s]\n",
  920. FnFilter1(pathcomp)));
  921. if (pathcomp == (char *)NULL) {
  922. rootlen = 0;
  923. return MPN_OK;
  924. }
  925. if (rootlen > 0) /* rootpath was already set, nothing to do */
  926. return MPN_OK;
  927. if ((rootlen = strlen(pathcomp)) > 0) {
  928. int had_trailing_pathsep=FALSE, has_drive=FALSE, add_dot=FALSE;
  929. char *tmproot;
  930. if ((tmproot = (char *)malloc(rootlen+3)) == (char *)NULL) {
  931. rootlen = 0;
  932. return MPN_NOMEM;
  933. }
  934. strcpy(tmproot, pathcomp);
  935. if (isalpha((uch)tmproot[0]) && tmproot[1] == ':')
  936. has_drive = TRUE; /* drive designator */
  937. if (tmproot[rootlen-1] == '/' || tmproot[rootlen-1] == '\\') {
  938. tmproot[--rootlen] = '\0';
  939. had_trailing_pathsep = TRUE;
  940. }
  941. if (has_drive && (rootlen == 2)) {
  942. if (!had_trailing_pathsep) /* i.e., original wasn't "x:/" */
  943. add_dot = TRUE; /* relative path: add '.' before '/' */
  944. } else if (rootlen > 0) { /* need not check "x:." and "x:/" */
  945. #ifdef MSC
  946. /* MSC 6.00 bug: stat(non-existent-dir) == 0 [exists!] */
  947. if (_dos_getfileattr(tmproot, &attrs) ||
  948. SSTAT(tmproot, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  949. #else
  950. if (SSTAT(tmproot, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  951. #endif
  952. {
  953. /* path does not exist */
  954. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  955. free(tmproot);
  956. rootlen = 0;
  957. /* treat as stored file */
  958. return MPN_INF_SKIP;
  959. }
  960. /* GRR: scan for wildcard characters? OS-dependent... if find any, return 2:
  961. * treat as stored file(s) */
  962. /* create directory (could add loop here scanning tmproot
  963. * to create more than one level, but really necessary?) */
  964. if (MKDIR(tmproot, 0777) == -1) {
  965. Info(slide, 1, ((char *)slide,
  966. LoadFarString(CantCreateExtractDir),
  967. FnFilter1(tmproot)));
  968. free(tmproot);
  969. rootlen = 0;
  970. /* path didn't exist, tried to create, failed: */
  971. /* file exists, or need 2+ subdir levels */
  972. return MPN_ERR_SKIP;
  973. }
  974. }
  975. }
  976. if (add_dot) /* had just "x:", make "x:." */
  977. tmproot[rootlen++] = '.';
  978. tmproot[rootlen++] = '/';
  979. tmproot[rootlen] = '\0';
  980. if ((rootpath = (char *)realloc(tmproot, rootlen+1)) == NULL) {
  981. free(tmproot);
  982. rootlen = 0;
  983. return MPN_NOMEM;
  984. }
  985. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  986. }
  987. return MPN_OK;
  988. }
  989. #endif /* !SFX || SFX_EXDIR */
  990. /*---------------------------------------------------------------------------
  991. END: free rootpath, immediately prior to program exit.
  992. ---------------------------------------------------------------------------*/
  993. if (FUNCTION == END) {
  994. Trace((stderr, "freeing rootpath\n"));
  995. if (rootlen > 0) {
  996. free(rootpath);
  997. rootlen = 0;
  998. }
  999. return MPN_OK;
  1000. }
  1001. return MPN_INVALID; /* should never reach */
  1002. } /* end function checkdir() */
  1003. /***********************/
  1004. /* Function isfloppy() */
  1005. /***********************/
  1006. static int isfloppy(nDrive) /* more precisely, is it removable? */
  1007. int nDrive;
  1008. {
  1009. union REGS regs;
  1010. regs.h.ah = 0x44;
  1011. regs.h.al = 0x08;
  1012. regs.h.bl = (uch)nDrive;
  1013. #ifdef __EMX__
  1014. _int86(0x21, &regs, &regs);
  1015. if (WREGS(regs,flags) & 1)
  1016. #else
  1017. intdos(&regs, &regs);
  1018. if (WREGS(regs,cflag)) /* error: do default a/b check instead */
  1019. #endif
  1020. {
  1021. Trace((stderr,
  1022. "error in DOS function 0x44 (AX = 0x%04x): guessing instead...\n",
  1023. (unsigned int)(WREGS(regs,ax))));
  1024. return (nDrive == 1 || nDrive == 2)? TRUE : FALSE;
  1025. } else
  1026. return WREGS(regs,ax)? FALSE : TRUE;
  1027. }
  1028. /**************************/
  1029. /* Function z_dos_chmod() */
  1030. /**************************/
  1031. static int z_dos_chmod(__G__ fname, attributes)
  1032. __GDEF
  1033. ZCONST char *fname;
  1034. int attributes;
  1035. {
  1036. char *name;
  1037. unsigned fnamelength;
  1038. int errv;
  1039. /* set file attributes:
  1040. The DOS `chmod' system call requires to mask out the
  1041. directory and volume_label attribute bits.
  1042. And, a trailing '/' has to be removed from the directory name,
  1043. the DOS `chmod' system call does not accept it. */
  1044. fnamelength = strlen(fname);
  1045. if (fnamelength > 1 && fname[fnamelength-1] == '/' &&
  1046. fname[fnamelength-2] != ':' &&
  1047. (name = (char *)malloc(fnamelength)) != (char *)NULL) {
  1048. strncpy(name, fname, fnamelength-1);
  1049. name[fnamelength-1] = '\0';
  1050. } else {
  1051. name = (char *)fname;
  1052. fnamelength = 0;
  1053. }
  1054. #if defined(__TURBOC__) || (defined(__DJGPP__) && (__DJGPP__ >= 2))
  1055. # if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x0452))
  1056. # define Chmod _rtl_chmod
  1057. # else
  1058. # define Chmod _chmod
  1059. # endif
  1060. errv = (Chmod(name, 1, attributes & (~FSUBDIR & ~FVOLID)) !=
  1061. (attributes & (~FSUBDIR & ~FVOLID)));
  1062. # undef Chmod
  1063. #else /* !(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */
  1064. errv = (_dos_setfileattr(name, attributes & (~FSUBDIR & ~FVOLID)) != 0);
  1065. #endif /* ?(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */
  1066. if (errv)
  1067. Info(slide, 1, ((char *)slide, LoadFarString(AttribsMayBeWrong)));
  1068. if (fnamelength > 0)
  1069. free(name);
  1070. return errv;
  1071. } /* end function z_dos_chmod() */
  1072. #if (!defined(__GO32__) && !defined(__EMX__))
  1073. typedef struct dosfcb {
  1074. uch flag; /* ff to indicate extended FCB */
  1075. char res[5]; /* reserved */
  1076. uch vattr; /* attribute */
  1077. uch drive; /* drive (1=A, 2=B, ...) */
  1078. uch vn[11]; /* file or volume name */
  1079. char dmmy[5];
  1080. uch nn[11]; /* holds new name if renaming (else reserved) */
  1081. char dmmy2[9];
  1082. } dos_fcb;
  1083. /**************************/
  1084. /* Function volumelabel() */
  1085. /**************************/
  1086. static int volumelabel(newlabel)
  1087. ZCONST char *newlabel;
  1088. {
  1089. #ifdef DEBUG
  1090. char *p;
  1091. #endif
  1092. int len = strlen(newlabel);
  1093. int fcbseg, dtaseg, fcboff, dtaoff, retv;
  1094. dos_fcb fcb, dta, far *pfcb=&fcb, far *pdta=&dta;
  1095. struct SREGS sregs;
  1096. union REGS regs;
  1097. /*---------------------------------------------------------------------------
  1098. Label the diskette specified by nLabelDrive using FCB calls. (Old ver-
  1099. sions of MS-DOS and OS/2 DOS boxes can't use DOS function 3Ch to create
  1100. labels.) Must use far pointers for MSC FP_* macros to work; must pad
  1101. FCB filenames with spaces; and cannot include dot in 8th position. May
  1102. or may not need to zero out FCBs before using; do so just in case.
  1103. ---------------------------------------------------------------------------*/
  1104. #ifdef WATCOMC_386
  1105. int truseg;
  1106. memset(&sregs, 0, sizeof(sregs));
  1107. memset(&regs, 0, sizeof(regs));
  1108. /* PMODE/W does not support extended versions of any dos FCB functions, */
  1109. /* so we have to use brute force, allocating real mode memory for them. */
  1110. regs.w.ax = 0x0100;
  1111. regs.w.bx = (2 * sizeof(dos_fcb) + 15) >> 4; /* size in paragraphs */
  1112. int386(0x31, &regs, &regs); /* DPMI allocate DOS memory */
  1113. if (regs.w.cflag)
  1114. return DF_MDY; /* no memory, return default */
  1115. truseg = regs.w.dx; /* protected mode selector */
  1116. dtaseg = regs.w.ax; /* real mode paragraph */
  1117. fcboff = 0;
  1118. dtaoff = sizeof(dos_fcb);
  1119. #ifdef XXX__MK_FP_IS_BROKEN
  1120. /* XXX This code may not be trustworthy in general, though it is */
  1121. /* valid with DOS/4GW and PMODE/w, which is all we support for now. */
  1122. regs.w.ax = 6;
  1123. regs.w.bx = truseg;
  1124. int386(0x31, &regs, &regs); /* convert seg to linear address */
  1125. pfcb = (dos_fcb far *) (((ulg) regs.w.cx << 16) | regs.w.dx);
  1126. /* pfcb = (dos_fcb far *) ((ulg) dtaseg << 4); */
  1127. pdta = pfcb + 1;
  1128. #else
  1129. pfcb = MK_FP(truseg, fcboff);
  1130. pdta = MK_FP(truseg, dtaoff);
  1131. #endif
  1132. _fmemset((char far *)pfcb, 0, 2 * sizeof(dos_fcb));
  1133. /* we pass the REAL MODE paragraph to the dos interrupts: */
  1134. fcbseg = dtaseg;
  1135. #else /* !WATCOMC_386 */
  1136. memset((char *)&dta, 0, sizeof(dos_fcb));
  1137. memset((char *)&fcb, 0, sizeof(dos_fcb));
  1138. fcbseg = FP_SEG(pfcb);
  1139. fcboff = FP_OFF(pfcb);
  1140. dtaseg = FP_SEG(pdta);
  1141. dtaoff = FP_OFF(pdta);
  1142. #endif /* ?WATCOMC_386 */
  1143. #ifdef DEBUG
  1144. for (p = (char *)&dta; (p - (char *)&dta) < sizeof(dos_fcb); ++p)
  1145. if (*p)
  1146. fprintf(stderr, "error: dta[%d] = %x\n", (p - (char *)&dta), *p);
  1147. for (p = (char *)&fcb; (p - (char *)&fcb) < sizeof(dos_fcb); ++p)
  1148. if (*p)
  1149. fprintf(stderr, "error: fcb[%d] = %x\n", (p - (char *)&fcb), *p);
  1150. printf("testing pointer macros:\n");
  1151. segread(&sregs);
  1152. printf("cs = %x, ds = %x, es = %x, ss = %x\n", sregs.cs, sregs.ds, sregs.es,
  1153. sregs.ss);
  1154. #endif /* DEBUG */
  1155. #if 0
  1156. #ifdef __TURBOC__
  1157. bdosptr(0x1a, dta, DO_NOT_CARE);
  1158. #else
  1159. (intdosx method below)
  1160. #endif
  1161. #endif /* 0 */
  1162. /* set the disk transfer address for subsequent FCB calls */
  1163. sregs.ds = dtaseg;
  1164. WREGS(regs,dx) = dtaoff;
  1165. Trace((stderr, "segment:offset of pdta = %x:%x\n", dtaseg, dtaoff));
  1166. Trace((stderr, "&dta = %lx, pdta = %lx\n", (ulg)&dta, (ulg)pdta));
  1167. regs.h.ah = 0x1a;
  1168. F_intdosx(&regs, &regs, &sregs);
  1169. /* fill in the FCB */
  1170. sregs.ds = fcbseg;
  1171. WREGS(regs,dx) = fcboff;
  1172. pfcb->flag = 0xff; /* extended FCB */
  1173. pfcb->vattr = 0x08; /* attribute: disk volume label */
  1174. pfcb->drive = (uch)nLabelDrive;
  1175. #ifdef DEBUG
  1176. Trace((stderr, "segment:offset of pfcb = %x:%x\n",
  1177. (unsigned int)(sregs.ds),
  1178. (unsigned int)(WREGS(regs,dx))));
  1179. Trace((stderr, "&fcb = %lx, pfcb = %lx\n", (ulg)&fcb, (ulg)pfcb));
  1180. Trace((stderr, "(2nd check: labelling drive %c:)\n", pfcb->drive-1+'A'));
  1181. if (pfcb->flag != fcb.flag)
  1182. fprintf(stderr, "error: pfcb->flag = %d, fcb.flag = %d\n",
  1183. pfcb->flag, fcb.flag);
  1184. if (pfcb->drive != fcb.drive)
  1185. fprintf(stderr, "error: pfcb->drive = %d, fcb.drive = %d\n",
  1186. pfcb->drive, fcb.drive);
  1187. if (pfcb->vattr != fcb.vattr)
  1188. fprintf(stderr, "error: pfcb->vattr = %d, fcb.vattr = %d\n",
  1189. pfcb->vattr, fcb.vattr);
  1190. #endif /* DEBUG */
  1191. /* check for existing label */
  1192. Trace((stderr, "searching for existing label via FCBs\n"));
  1193. regs.h.ah = 0x11; /* FCB find first */
  1194. #ifdef WATCOMC_386
  1195. _fstrncpy((char far *)&pfcb->vn, "???????????", 11);
  1196. #else
  1197. strncpy((char *)fcb.vn, "???????????", 11); /* i.e., "*.*" */
  1198. #endif /* ?WATCOMC_386 */
  1199. Trace((stderr, "fcb.vn = %lx\n", (ulg)fcb.vn));
  1200. Trace((stderr, "regs.h.ah = %x, regs.x.dx = %04x, sregs.ds = %04x\n",
  1201. (unsigned int)(regs.h.ah), (unsigned int)(WREGS(regs,dx)),
  1202. (unsigned int)(sregs.ds)));
  1203. Trace((stderr, "flag = %x, drive = %d, vattr = %x, vn = %s = %s.\n",
  1204. fcb.flag, fcb.drive, fcb.vattr, fcb.vn, pfcb->vn));
  1205. F_intdosx(&regs, &regs, &sregs);
  1206. /*---------------------------------------------------------------------------
  1207. If not previously labelled, write a new label. Otherwise just rename,
  1208. since MS-DOS 2.x has a bug that damages the FAT when the old label is
  1209. deleted.
  1210. ---------------------------------------------------------------------------*/
  1211. if (regs.h.al) {
  1212. Trace((stderr, "no label found\n\n"));
  1213. regs.h.ah = 0x16; /* FCB create file */
  1214. #ifdef WATCOMC_386
  1215. _fstrncpy((char far *)pfcb->vn, newlabel, len);
  1216. if (len < 11)
  1217. _fstrncpy((char far *)(pfcb->vn+len), " ", 11-len);
  1218. #else
  1219. strncpy((char *)fcb.vn, newlabel, len);
  1220. if (len < 11) /* fill with spaces */
  1221. strncpy((char *)(fcb.vn+len), " ", 11-len);
  1222. #endif
  1223. Trace((stderr, "fcb.vn = %lx pfcb->vn = %lx\n", (ulg)fcb.vn,
  1224. (ulg)pfcb->vn));
  1225. Trace((stderr, "flag = %x, drive = %d, vattr = %x\n", fcb.flag,
  1226. fcb.drive, fcb.vattr));
  1227. Trace((stderr, "vn = %s = %s.\n", fcb.vn, pfcb->vn));
  1228. F_intdosx(&regs, &regs, &sregs);
  1229. regs.h.ah = 0x10; /* FCB close file */
  1230. if (regs.h.al) {
  1231. Trace((stderr, "unable to write volume name (AL = %x)\n",
  1232. (unsigned int)(regs.h.al)));
  1233. F_intdosx(&regs, &regs, &sregs);
  1234. retv = 1;
  1235. } else {
  1236. F_intdosx(&regs, &regs, &sregs);
  1237. Trace((stderr, "new volume label [%s] written\n", newlabel));
  1238. retv = 0;
  1239. }
  1240. } else {
  1241. Trace((stderr, "found old label [%s]\n\n", dta.vn)); /* not term. */
  1242. regs.h.ah = 0x17; /* FCB rename */
  1243. #ifdef WATCOMC_386
  1244. _fstrncpy((char far *)pfcb->vn, (char far *)pdta->vn, 11);
  1245. _fstrncpy((char far *)pfcb->nn, newlabel, len);
  1246. if (len < 11)
  1247. _fstrncpy((char far *)(pfcb->nn+len), " ", 11-len);
  1248. #else
  1249. strncpy((char *)fcb.vn, (char *)dta.vn, 11);
  1250. strncpy((char *)fcb.nn, newlabel, len);
  1251. if (len < 11) /* fill with spaces */
  1252. strncpy((char *)(fcb.nn+len), " ", 11-len);
  1253. #endif
  1254. Trace((stderr, "fcb.vn = %lx pfcb->vn = %lx\n", (ulg)fcb.vn,
  1255. (ulg)pfcb->vn));
  1256. Trace((stderr, "fcb.nn = %lx pfcb->nn = %lx\n", (ulg)fcb.nn,
  1257. (ulg)pfcb->nn));
  1258. Trace((stderr, "flag = %x, drive = %d, vattr = %x\n", fcb.flag,
  1259. fcb.drive, fcb.vattr));
  1260. Trace((stderr, "vn = %s = %s.\n", fcb.vn, pfcb->vn));
  1261. Trace((stderr, "nn = %s = %s.\n", fcb.nn, pfcb->nn));
  1262. F_intdosx(&regs, &regs, &sregs);
  1263. if (regs.h.al) {
  1264. Trace((stderr, "Unable to change volume name (AL = %x)\n",
  1265. (unsigned int)(regs.h.al)));
  1266. retv = 1;
  1267. } else {
  1268. Trace((stderr, "volume label changed to [%s]\n", newlabel));
  1269. retv = 0;
  1270. }
  1271. }
  1272. #ifdef WATCOMC_386
  1273. regs.w.ax = 0x0101; /* free dos memory */
  1274. regs.w.dx = truseg;
  1275. int386(0x31, &regs, &regs);
  1276. #endif
  1277. return retv;
  1278. } /* end function volumelabel() */
  1279. #endif /* !__GO32__ && !__EMX__ */
  1280. #if (defined(USE_EF_UT_TIME) || defined(TIMESTAMP))
  1281. /* The following DOS date/time structure is machine-dependent as it
  1282. * assumes "little-endian" byte order. For MSDOS-specific code, which
  1283. * is run on ix86 CPUs (or emulators), this assumption is valid; but
  1284. * care should be taken when using this code as template for other ports.
  1285. */
  1286. typedef union {
  1287. ulg z_dostime;
  1288. # ifdef __TURBOC__
  1289. struct ftime ft; /* system file time record */
  1290. # endif
  1291. struct { /* date and time words */
  1292. ush ztime; /* DOS file modification time word */
  1293. ush zdate; /* DOS file modification date word */
  1294. } zft;
  1295. struct { /* DOS date/time components bitfield */
  1296. unsigned zt_se : 5;
  1297. unsigned zt_mi : 6;
  1298. unsigned zt_hr : 5;
  1299. unsigned zd_dy : 5;
  1300. unsigned zd_mo : 4;
  1301. unsigned zd_yr : 7;
  1302. } z_dtf;
  1303. } dos_fdatetime;
  1304. #endif /* USE_EF_UT_TIME || TIMESTAMP */
  1305. /****************************/
  1306. /* Function close_outfile() */
  1307. /****************************/
  1308. void close_outfile(__G)
  1309. __GDEF
  1310. /*
  1311. * MS-DOS VERSION
  1312. *
  1313. * Set the output file date/time stamp according to information from the
  1314. * zipfile directory record for this member, then close the file and set
  1315. * its permissions (archive, hidden, read-only, system). Aside from closing
  1316. * the file, this routine is optional (but most compilers support it).
  1317. */
  1318. {
  1319. /* skip restoring time stamps on user's request */
  1320. if (uO.D_flag <= 1) {
  1321. #ifdef USE_EF_UT_TIME
  1322. dos_fdatetime dos_dt;
  1323. iztimes z_utime;
  1324. struct tm *t;
  1325. #endif /* USE_EF_UT_TIME */
  1326. /*---------------------------------------------------------------------------
  1327. Copy and/or convert time and date variables, if necessary; then set
  1328. the file time/date. WEIRD BORLAND "BUG": if output is buffered,
  1329. and if run under at least some versions of DOS (e.g., 6.0), and if
  1330. files are smaller than DOS physical block size (i.e., 512 bytes) (?),
  1331. then files MAY NOT get timestamped correctly--apparently setftime()
  1332. occurs before any data are written to the file, and when file is
  1333. closed and buffers are flushed, timestamp is overwritten with
  1334. current time. Even with a 32K buffer, this does not seem to occur
  1335. with larger files. UnZip output is now unbuffered, but if it were
  1336. not, could still avoid problem by adding "fflush(outfile)" just
  1337. before setftime() call. Weird, huh?
  1338. ---------------------------------------------------------------------------*/
  1339. #ifdef USE_EF_UT_TIME
  1340. if (G.extra_field &&
  1341. #ifdef IZ_CHECK_TZ
  1342. G.tz_is_valid &&
  1343. #endif
  1344. (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  1345. G.lrec.last_mod_dos_datetime, &z_utime, NULL)
  1346. & EB_UT_FL_MTIME))
  1347. {
  1348. TTrace((stderr, "close_outfile: Unix e.f. modif. time = %ld\n",
  1349. z_utime.mtime));
  1350. /* round up (down if "up" overflows) to even seconds */
  1351. if (z_utime.mtime & 1)
  1352. z_utime.mtime = (z_utime.mtime + 1 > z_utime.mtime) ?
  1353. z_utime.mtime + 1 : z_utime.mtime - 1;
  1354. TIMET_TO_NATIVE(z_utime.mtime) /* NOP unless MSC 7 or Macintosh */
  1355. t = localtime(&(z_utime.mtime));
  1356. } else
  1357. t = (struct tm *)NULL;
  1358. if (t != (struct tm *)NULL) {
  1359. if (t->tm_year < 80) {
  1360. dos_dt.z_dtf.zt_se = 0;
  1361. dos_dt.z_dtf.zt_mi = 0;
  1362. dos_dt.z_dtf.zt_hr = 0;
  1363. dos_dt.z_dtf.zd_dy = 1;
  1364. dos_dt.z_dtf.zd_mo = 1;
  1365. dos_dt.z_dtf.zd_yr = 0;
  1366. } else {
  1367. dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  1368. dos_dt.z_dtf.zt_mi = t->tm_min;
  1369. dos_dt.z_dtf.zt_hr = t->tm_hour;
  1370. dos_dt.z_dtf.zd_dy = t->tm_mday;
  1371. dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  1372. dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  1373. }
  1374. } else {
  1375. dos_dt.z_dostime = G.lrec.last_mod_dos_datetime;
  1376. }
  1377. # ifdef __TURBOC__
  1378. setftime(fileno(G.outfile), &dos_dt.ft);
  1379. # else
  1380. _dos_setftime(fileno(G.outfile), dos_dt.zft.zdate, dos_dt.zft.ztime);
  1381. # endif
  1382. #else /* !USE_EF_UT_TIME */
  1383. # ifdef __TURBOC__
  1384. setftime(fileno(G.outfile),
  1385. (struct ftime *)(&(G.lrec.last_mod_dos_datetime)));
  1386. # else
  1387. _dos_setftime(fileno(G.outfile),
  1388. (ush)(G.lrec.last_mod_dos_datetime >> 16),
  1389. (ush)(G.lrec.last_mod_dos_datetime));
  1390. # endif
  1391. #endif /* ?USE_EF_UT_TIME */
  1392. }
  1393. /*---------------------------------------------------------------------------
  1394. And finally we can close the file...at least everybody agrees on how to
  1395. do *this*. I think... Also change the mode according to the stored file
  1396. attributes, since we didn't do that when we opened the dude.
  1397. ---------------------------------------------------------------------------*/
  1398. fclose(G.outfile);
  1399. z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  1400. } /* end function close_outfile() */
  1401. #ifdef TIMESTAMP
  1402. /*************************/
  1403. /* Function stamp_file() */
  1404. /*************************/
  1405. int stamp_file(fname, modtime)
  1406. ZCONST char *fname;
  1407. time_t modtime;
  1408. {
  1409. dos_fdatetime dos_dt;
  1410. time_t t_even;
  1411. struct tm *t;
  1412. int fd; /* file handle */
  1413. /* round up (down if "up" overflows) to even seconds */
  1414. t_even = ((modtime + 1 > modtime) ? modtime + 1 : modtime) & (~1);
  1415. TIMET_TO_NATIVE(t_even) /* NOP unless MSC 7.0 or Macintosh */
  1416. t = localtime(&t_even);
  1417. if (t == (struct tm *)NULL)
  1418. return -1; /* time conversion error */
  1419. if (t->tm_year < 80) {
  1420. dos_dt.z_dtf.zt_se = 0;
  1421. dos_dt.z_dtf.zt_mi = 0;
  1422. dos_dt.z_dtf.zt_hr = 0;
  1423. dos_dt.z_dtf.zd_dy = 1;
  1424. dos_dt.z_dtf.zd_mo = 1;
  1425. dos_dt.z_dtf.zd_yr = 0;
  1426. } else {
  1427. dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  1428. dos_dt.z_dtf.zt_mi = t->tm_min;
  1429. dos_dt.z_dtf.zt_hr = t->tm_hour;
  1430. dos_dt.z_dtf.zd_dy = t->tm_mday;
  1431. dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  1432. dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  1433. }
  1434. if (((fd = open((char *)fname, 0)) == -1) ||
  1435. # ifdef __TURBOC__
  1436. (setftime(fd, &dos_dt.ft)))
  1437. # else
  1438. (_dos_setftime(fd, dos_dt.zft.zdate, dos_dt.zft.ztime)))
  1439. # endif
  1440. {
  1441. if (fd != -1)
  1442. close(fd);
  1443. return -1;
  1444. }
  1445. close(fd);
  1446. return 0;
  1447. } /* end function stamp_file() */
  1448. #endif /* TIMESTAMP */
  1449. void prepare_ISO_OEM_translat(__G)
  1450. __GDEF
  1451. {
  1452. switch (getdoscodepage()) {
  1453. case 437:
  1454. case 850:
  1455. case 858:
  1456. #ifdef IZ_ISO2OEM_ARRAY
  1457. iso2oem = iso2oem_850;
  1458. #endif
  1459. #ifdef IZ_OEM2ISO_ARRAY
  1460. oem2iso = oem2iso_850;
  1461. #endif
  1462. case 932: /* Japanese */
  1463. case 949: /* Korean */
  1464. case 936: /* Chinese, simple */
  1465. case 950: /* Chinese, traditional */
  1466. case 874: /* Thai */
  1467. case 1258: /* Vietnamese */
  1468. #ifdef IZ_ISO2OEM_ARRAY
  1469. iso2oem = NULL;
  1470. #endif
  1471. #ifdef IZ_OEM2ISO_ARRAY
  1472. oem2iso = NULL;
  1473. #endif
  1474. default:
  1475. #ifdef IZ_ISO2OEM_ARRAY
  1476. iso2oem = NULL;
  1477. #endif
  1478. #ifdef IZ_OEM2ISO_ARRAY
  1479. oem2iso = NULL;
  1480. #endif
  1481. }
  1482. } /* end function prepare_ISO_OEM_translat() */
  1483. #ifndef SFX
  1484. /*************************/
  1485. /* Function dateformat() */
  1486. /*************************/
  1487. int dateformat()
  1488. {
  1489. /*---------------------------------------------------------------------------
  1490. For those operating systems that support it, this function returns a
  1491. value that tells how national convention says that numeric dates are
  1492. displayed. Return values are DF_YMD, DF_DMY and DF_MDY (the meanings
  1493. should be fairly obvious).
  1494. ---------------------------------------------------------------------------*/
  1495. #ifndef WINDLL
  1496. ush CountryInfo[18];
  1497. #if (!defined(__GO32__) && !defined(__EMX__))
  1498. ush far *_CountryInfo = CountryInfo;
  1499. struct SREGS sregs;
  1500. union REGS regs;
  1501. #ifdef WATCOMC_386
  1502. ush seg, para;
  1503. memset(&sregs, 0, sizeof(sregs));
  1504. memset(&regs, 0, sizeof(regs));
  1505. /* PMODE/W does not support an extended version of dos function 38, */
  1506. /* so we have to use brute force, allocating real mode memory for it. */
  1507. regs.w.ax = 0x0100;
  1508. regs.w.bx = 3; /* 36 bytes rounds up to 48 */
  1509. int386(0x31, &regs, &regs); /* DPMI allocate DOS memory */
  1510. if (regs.w.cflag)
  1511. return DF_MDY; /* no memory, return default */
  1512. seg = regs.w.dx;
  1513. para = regs.w.ax;
  1514. #ifdef XXX__MK_FP_IS_BROKEN
  1515. /* XXX This code may not be trustworthy in general, though it is
  1516. * valid with DOS/4GW and PMODE/w, which is all we support for now. */
  1517. /* _CountryInfo = (ush far *) (para << 4); */ /* works for some extenders */
  1518. regs.w.ax = 6;
  1519. regs.w.bx = seg;
  1520. int386(0x31, &regs, &regs); /* convert seg to linear address */
  1521. _CountryInfo = (ush far *) (((ulg) regs.w.cx << 16) | regs.w.dx);
  1522. #else
  1523. _CountryInfo = (ush far *) MK_FP(seg, 0);
  1524. #endif
  1525. sregs.ds = para; /* real mode paragraph */
  1526. regs.w.dx = 0; /* no offset from segment */
  1527. regs.w.ax = 0x3800;
  1528. int86x_realmode(0x21, &regs, &regs, &sregs);
  1529. CountryInfo[0] = regs.w.cflag ? 0 : _CountryInfo[0];
  1530. regs.w.ax = 0x0101;
  1531. regs.w.dx = seg;
  1532. int386(0x31, &regs, &regs); /* DPMI free DOS memory */
  1533. #else /* !WATCOMC_386 */
  1534. sregs.ds = FP_SEG(_CountryInfo);
  1535. regs.x.dx = FP_OFF(_CountryInfo);
  1536. regs.x.ax = 0x3800;
  1537. intdosx(&regs, &regs, &sregs);
  1538. #endif /* ?WATCOMC_386 */
  1539. #else /* __GO32__ || __EMX__ */
  1540. _dos_getcountryinfo(CountryInfo);
  1541. #endif /* ?(__GO32__ || __EMX__) */
  1542. switch(CountryInfo[0]) {
  1543. case 0:
  1544. return DF_MDY;
  1545. case 1:
  1546. return DF_DMY;
  1547. case 2:
  1548. return DF_YMD;
  1549. }
  1550. #endif /* !WINDLL */
  1551. return DF_MDY; /* default for systems without locale info */
  1552. } /* end function dateformat() */
  1553. #ifndef WINDLL
  1554. /**************************************/
  1555. /* Function is_running_on_windows() */
  1556. /**************************************/
  1557. static int is_running_on_windows(void)
  1558. {
  1559. char *var = getenv("OS");
  1560. /* if the OS env.var says 'Windows_NT' then */
  1561. /* we're likely running on a variant of WinNT */
  1562. if ((var != NULL) && (strcmp("Windows_NT", var) == 0))
  1563. return TRUE;
  1564. /* if the windir env.var is non-null then */
  1565. /* we're likely running on a variant of Win9x */
  1566. /* DOS mode of Win9x doesn't define windir, only winbootdir */
  1567. /* NT's command.com can't see lowercase env. vars */
  1568. var = getenv("windir");
  1569. if ((var != NULL) && (var[0] != '\0'))
  1570. return TRUE;
  1571. return FALSE;
  1572. }
  1573. /**********************************/
  1574. /* Function check_for_windows() */
  1575. /**********************************/
  1576. void check_for_windows(ZCONST char *app)
  1577. {
  1578. #ifdef SMALL_MEM
  1579. char msg_str[160]; /* enough space for two 79-char-lines */
  1580. (void)zfstrcpy(msg_buf, WarnUsedOnWindows)
  1581. #else
  1582. # define msg_str WarnUsedOnWindows
  1583. #endif
  1584. /* Print a warning for users running under Windows */
  1585. /* to reduce bug reports due to running DOS version */
  1586. /* under Windows, when Windows version usually works correctly */
  1587. if (is_running_on_windows())
  1588. printf(msg_str, app);
  1589. } /* end function check_for_windows() */
  1590. /************************/
  1591. /* Function version() */
  1592. /************************/
  1593. void version(__G)
  1594. __GDEF
  1595. {
  1596. int len;
  1597. #if defined(__DJGPP__) || defined(__WATCOMC__) || \
  1598. (defined(_MSC_VER) && (_MSC_VER != 800))
  1599. char buf[80];
  1600. #endif
  1601. len = sprintf((char *)slide, LoadFarString(CompiledWith),
  1602. #if defined(__GNUC__)
  1603. # if defined(__DJGPP__)
  1604. (sprintf(buf, "djgpp v%d.%02d / gcc ", __DJGPP__, __DJGPP_MINOR__), buf),
  1605. # elif defined(__GO32__) /* __GO32__ is defined as "1" only (sigh) */
  1606. "djgpp v1.x / gcc ",
  1607. # elif defined(__EMX__) /* ...so is __EMX__ (double sigh) */
  1608. "emx+gcc ",
  1609. # else
  1610. "gcc ",
  1611. # endif
  1612. __VERSION__,
  1613. #elif defined(__WATCOMC__)
  1614. # if (__WATCOMC__ % 10 != 0)
  1615. "Watcom C/C++", (sprintf(buf, " %d.%02d", __WATCOMC__ / 100,
  1616. __WATCOMC__ % 100), buf),
  1617. # else
  1618. "Watcom C/C++", (sprintf(buf, " %d.%d", __WATCOMC__ / 100,
  1619. (__WATCOMC__ % 100) / 10), buf),
  1620. # endif
  1621. #elif defined(__TURBOC__)
  1622. # ifdef __BORLANDC__
  1623. "Borland C++",
  1624. # if (__BORLANDC__ < 0x0200)
  1625. " 1.0",
  1626. # elif (__BORLANDC__ == 0x0200) /* James: __TURBOC__ = 0x0297 */
  1627. " 2.0",
  1628. # elif (__BORLANDC__ == 0x0400)
  1629. " 3.0",
  1630. # elif (__BORLANDC__ == 0x0410) /* __BCPLUSPLUS__ = 0x0310 */
  1631. " 3.1",
  1632. # elif (__BORLANDC__ == 0x0452) /* __BCPLUSPLUS__ = 0x0320 */
  1633. " 4.0 or 4.02",
  1634. # elif (__BORLANDC__ == 0x0460) /* __BCPLUSPLUS__ = 0x0340 */
  1635. " 4.5",
  1636. # elif (__BORLANDC__ == 0x0500)
  1637. " 5.0",
  1638. # else
  1639. " later than 5.0",
  1640. # endif
  1641. # else
  1642. "Turbo C",
  1643. # if (__TURBOC__ > 0x0401) /* Kevin: 3.0 -> 0x0401 */
  1644. "++ later than 3.0",
  1645. # elif (__TURBOC__ >= 0x0400)
  1646. "++ 3.0",
  1647. # elif (__TURBOC__ >= 0x0297) /* see remark for Borland C++ 2.0 */
  1648. "++ 2.0",
  1649. # elif (__TURBOC__ == 0x0296) /* [662] checked by SPC */
  1650. "++ 1.01",
  1651. # elif (__TURBOC__ == 0x0295) /* [661] vfy'd by Kevin */
  1652. "++ 1.0",
  1653. # elif (__TURBOC__ == 0x0201) /* Brian: 2.01 -> 0x0201 */
  1654. " 2.01",
  1655. # elif ((__TURBOC__ >= 0x018d) && (__TURBOC__ <= 0x0200)) /* James: 0x0200 */
  1656. " 2.0",
  1657. # elif (__TURBOC__ > 0x0100)
  1658. " 1.5", /* James: 0x0105? */
  1659. # else
  1660. " 1.0", /* James: 0x0100 */
  1661. # endif
  1662. # endif
  1663. #elif defined(MSC)
  1664. # if defined(_QC) && !defined(_MSC_VER)
  1665. "MS Quick C ", "2.0 or earlier", /* _QC is defined as 1 */
  1666. # elif defined(_QC) && (_MSC_VER == 600)
  1667. "MS Quick C ", "2.5 (MSC 6.00)",
  1668. # else
  1669. "Microsoft C ",
  1670. # ifdef _MSC_VER
  1671. # if (_MSC_VER == 800)
  1672. "8.0/8.0c (Visual C++ 1.0/1.5)",
  1673. # else
  1674. (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),
  1675. # endif
  1676. # else
  1677. "5.1 or earlier",
  1678. # endif
  1679. # endif
  1680. #else
  1681. "unknown compiler", "",
  1682. #endif /* ?compilers */
  1683. "\nMS-DOS",
  1684. #if (defined(__GNUC__) || defined(WATCOMC_386))
  1685. " (32-bit)",
  1686. #else
  1687. # if defined(M_I86HM) || defined(__HUGE__)
  1688. " (16-bit, huge)",
  1689. # elif defined(M_I86LM) || defined(__LARGE__)
  1690. " (16-bit, large)",
  1691. # elif defined(M_I86MM) || defined(__MEDIUM__)
  1692. " (16-bit, medium)",
  1693. # elif defined(M_I86CM) || defined(__COMPACT__)
  1694. " (16-bit, compact)",
  1695. # elif defined(M_I86SM) || defined(__SMALL__)
  1696. " (16-bit, small)",
  1697. # elif defined(M_I86TM) || defined(__TINY__)
  1698. " (16-bit, tiny)",
  1699. # else
  1700. " (16-bit)",
  1701. # endif
  1702. #endif
  1703. #ifdef __DATE__
  1704. " on ", __DATE__
  1705. #else
  1706. "", ""
  1707. #endif
  1708. );
  1709. (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  1710. /* MSC can't handle huge macro expansion */
  1711. /* temporary debugging code for Borland compilers only */
  1712. #if (defined(__TURBOC__) && defined(DEBUG))
  1713. Info(slide, 0, ((char *)slide, "\tdebug(__TURBOC__ = 0x%04x = %d)\n",
  1714. __TURBOC__, __TURBOC__));
  1715. #ifdef __BORLANDC__
  1716. Info(slide, 0, ((char *)slide, "\tdebug(__BORLANDC__ = 0x%04x)\n",
  1717. __BORLANDC__));
  1718. #else
  1719. Info(slide, 0, ((char *)slide, "\tdebug(__BORLANDC__ not defined)\n"));
  1720. #endif
  1721. #ifdef __TCPLUSPLUS__
  1722. Info(slide, 0, ((char *)slide, "\tdebug(__TCPLUSPLUS__ = 0x%04x)\n",
  1723. __TCPLUSPLUS__));
  1724. #else
  1725. Info(slide, 0, ((char *)slide, "\tdebug(__TCPLUSPLUS__ not defined)\n"));
  1726. #endif
  1727. #ifdef __BCPLUSPLUS__
  1728. Info(slide, 0, ((char *)slide, "\tdebug(__BCPLUSPLUS__ = 0x%04x)\n\n",
  1729. __BCPLUSPLUS__));
  1730. #else
  1731. Info(slide, 0, ((char *)slide, "\tdebug(__BCPLUSPLUS__ not defined)\n\n"));
  1732. #endif
  1733. #endif /* __TURBOC__ && DEBUG */
  1734. } /* end function version() */
  1735. #endif /* !WINDLL */
  1736. #endif /* !SFX */
  1737. #endif /* !FUNZIP */
  1738. #ifdef MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  1739. #if defined(__TURBOC__) && !defined(OS2)
  1740. #include <alloc.h>
  1741. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  1742. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  1743. * must fix the pointer. Warning: the pointer must be put back to its
  1744. * original form in order to free it, use zcfree().
  1745. */
  1746. #define MAX_PTR 2 /* reduced from 10 to save space */
  1747. /* 10*64K = 640K */
  1748. static int next_ptr = 0;
  1749. typedef struct ptr_table_s {
  1750. zvoid far *org_ptr;
  1751. zvoid far *new_ptr;
  1752. } ptr_table;
  1753. static ptr_table table[MAX_PTR];
  1754. /* This table is used to remember the original form of pointers
  1755. * to large buffers (64K). Such pointers are normalized with a zero offset.
  1756. * Since MSDOS is not a preemptive multitasking OS, this table is not
  1757. * protected from concurrent access. This hack doesn't work anyway on
  1758. * a protected system like OS/2. Use Microsoft C instead.
  1759. */
  1760. zvoid far *zcalloc(unsigned items, unsigned size)
  1761. {
  1762. zvoid far *buf;
  1763. ulg bsize = (ulg)items*size;
  1764. if (bsize < (65536L-16L)) {
  1765. buf = farmalloc(bsize);
  1766. if (*(ush*)&buf != 0) return buf;
  1767. } else {
  1768. buf = farmalloc(bsize + 16L);
  1769. }
  1770. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  1771. table[next_ptr].org_ptr = buf;
  1772. /* Normalize the pointer to seg:0 */
  1773. *((ush*)&buf+1) += ((ush)((uch*)buf-NULL) + 15) >> 4;
  1774. *(ush*)&buf = 0;
  1775. table[next_ptr++].new_ptr = buf;
  1776. return buf;
  1777. }
  1778. zvoid zcfree(zvoid far *ptr)
  1779. {
  1780. int n;
  1781. if (*(ush*)&ptr != 0) { /* object < 64K */
  1782. farfree(ptr);
  1783. return;
  1784. }
  1785. /* Find the original pointer */
  1786. for (n = next_ptr - 1; n >= 0; n--) {
  1787. if (ptr != table[n].new_ptr) continue;
  1788. farfree(table[n].org_ptr);
  1789. while (++n < next_ptr) {
  1790. table[n-1] = table[n];
  1791. }
  1792. next_ptr--;
  1793. return;
  1794. }
  1795. Trace((stderr, "zcfree: ptr not found!\n"));
  1796. }
  1797. #endif /* __TURBOC__ */
  1798. #if defined(MSC) || defined(__WATCOMC__)
  1799. #if (!defined(_MSC_VER) || (_MSC_VER < 700))
  1800. # define _halloc halloc
  1801. # define _hfree hfree
  1802. #endif
  1803. zvoid far *zcalloc(unsigned items, unsigned size)
  1804. {
  1805. return (zvoid far *)_halloc((long)items, size);
  1806. }
  1807. zvoid zcfree(zvoid far *ptr)
  1808. {
  1809. _hfree((void huge *)ptr);
  1810. }
  1811. #endif /* MSC || __WATCOMC__ */
  1812. #endif /* MY_ZCALLOC */
  1813. #ifndef FUNZIP
  1814. #if (defined(__GO32__) || defined(__EMX__))
  1815. #if (!defined(__DJGPP__) || (__DJGPP__ < 2) || \
  1816. ((__DJGPP__ == 2) && (__DJGPP_MINOR__ < 2)))
  1817. int volatile _doserrno;
  1818. #endif /* not "djgpp v2.02 or newer" */
  1819. #if (!defined(__DJGPP__) || (__DJGPP__ < 2))
  1820. unsigned _dos_getcountryinfo(void *countrybuffer)
  1821. {
  1822. asm("movl %0, %%edx": : "g" (countrybuffer));
  1823. asm("movl $0x3800, %eax");
  1824. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1825. _doserrno = 0;
  1826. asm("jnc 1f");
  1827. asm("movl %%eax, %0": "=m" (_doserrno));
  1828. asm("1:");
  1829. return (unsigned)_doserrno;
  1830. }
  1831. unsigned _dos_setftime(int fd, unsigned dosdate, unsigned dostime)
  1832. {
  1833. asm("movl %0, %%ebx": : "g" (fd));
  1834. asm("movl %0, %%ecx": : "g" (dostime));
  1835. asm("movl %0, %%edx": : "g" (dosdate));
  1836. asm("movl $0x5701, %eax");
  1837. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1838. _doserrno = 0;
  1839. asm("jnc 1f");
  1840. asm("movl %%eax, %0": "=m" (_doserrno));
  1841. errno = EBADF;
  1842. asm("1:");
  1843. return (unsigned)_doserrno;
  1844. }
  1845. unsigned _dos_setfileattr(const char *name, unsigned attr)
  1846. {
  1847. #if 0 /* stripping of trailing '/' is not needed for unzip-internal use */
  1848. unsigned namlen = strlen(name);
  1849. char *i_name = alloca(namlen + 1);
  1850. strcpy(i_name, name);
  1851. if (namlen > 1 && i_name[namlen-1] == '/' && i_name[namlen-2] != ':')
  1852. i_name[namlen-1] = '\0';
  1853. asm("movl %0, %%edx": : "g" (i_name));
  1854. #else
  1855. asm("movl %0, %%edx": : "g" (name));
  1856. #endif
  1857. asm("movl %0, %%ecx": : "g" (attr));
  1858. asm("movl $0x4301, %eax");
  1859. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1860. _doserrno = 0;
  1861. asm("jnc 1f");
  1862. asm("movl %%eax, %0": "=m" (_doserrno));
  1863. switch (_doserrno) {
  1864. case 2:
  1865. case 3:
  1866. errno = ENOENT;
  1867. break;
  1868. case 5:
  1869. errno = EACCES;
  1870. break;
  1871. }
  1872. asm("1:");
  1873. return (unsigned)_doserrno;
  1874. }
  1875. void _dos_getdrive(unsigned *d)
  1876. {
  1877. asm("movl $0x1900, %eax");
  1878. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1879. asm("xorb %ah, %ah");
  1880. asm("incb %al");
  1881. asm("movl %%eax, %0": "=a" (*d));
  1882. }
  1883. unsigned _dos_creat(const char *path, unsigned attr, int *fd)
  1884. {
  1885. asm("movl $0x3c00, %eax");
  1886. asm("movl %0, %%edx": :"g" (path));
  1887. asm("movl %0, %%ecx": :"g" (attr));
  1888. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1889. asm("movl %%eax, %0": "=a" (*fd));
  1890. _doserrno = 0;
  1891. asm("jnc 1f");
  1892. _doserrno = *fd;
  1893. switch (_doserrno) {
  1894. case 3:
  1895. errno = ENOENT;
  1896. break;
  1897. case 4:
  1898. errno = EMFILE;
  1899. break;
  1900. case 5:
  1901. errno = EACCES;
  1902. break;
  1903. }
  1904. asm("1:");
  1905. return (unsigned)_doserrno;
  1906. }
  1907. unsigned _dos_close(int fd)
  1908. {
  1909. asm("movl %0, %%ebx": : "g" (fd));
  1910. asm("movl $0x3e00, %eax");
  1911. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1912. _doserrno = 0;
  1913. asm("jnc 1f");
  1914. asm ("movl %%eax, %0": "=m" (_doserrno));
  1915. if (_doserrno == 6) {
  1916. errno = EBADF;
  1917. }
  1918. asm("1:");
  1919. return (unsigned)_doserrno;
  1920. }
  1921. #endif /* !__DJGPP__ || (__DJGPP__ < 2) */
  1922. static int volumelabel(ZCONST char *name)
  1923. {
  1924. int fd;
  1925. return _dos_creat(name, FA_LABEL, &fd) ? fd : _dos_close(fd);
  1926. }
  1927. #if (defined(__DJGPP__) && (__DJGPP__ >= 2))
  1928. #include <dpmi.h> /* These includes for the country info */
  1929. #include <go32.h>
  1930. #include <sys/farptr.h>
  1931. /* The above _dos_getcountryinfo function doesn't work with djgpp v2, presumably
  1932. * because ds is not set correctly (does it really work at all?). Note that
  1933. * this version only sets the date (ie. CountryInfo[0]).
  1934. */
  1935. unsigned _dos_getcountryinfo(void *countrybuffer)
  1936. {
  1937. __dpmi_regs regs;
  1938. regs.x.ax = 0x3800;
  1939. regs.x.dx = __tb & 0x0f;
  1940. regs.x.ds = (__tb >> 4) & 0xffff;
  1941. _doserrno = __dpmi_int(0x21, &regs);
  1942. *(ush*)countrybuffer = _farpeekw(_dos_ds, __tb & 0xfffff);
  1943. return (unsigned)_doserrno;
  1944. }
  1945. /* Disable determination of "x" bit in st_mode field for [f]stat() calls. */
  1946. int _is_executable (const char *path, int fhandle, const char *ext)
  1947. {
  1948. return 0;
  1949. }
  1950. #ifndef USE_DJGPP_GLOB
  1951. /* Prevent globbing of filenames. This gives the same functionality as
  1952. * "stubedit <program> globbing=no" did with DJGPP v1.
  1953. */
  1954. char **__crt0_glob_function(char *_arg)
  1955. {
  1956. return NULL;
  1957. }
  1958. #endif /* !USE_DJGPP_GLOB */
  1959. #ifndef USE_DJGPP_ENV
  1960. /* Reduce the size of the executable and remove the functionality to read
  1961. * the program's environment from whatever $DJGPP points to.
  1962. */
  1963. void __crt0_load_environment_file(char *_app_name)
  1964. {
  1965. }
  1966. #endif /* !USE_DJGPP_ENV */
  1967. #endif /* __DJGPP__ >= 2 */
  1968. #endif /* __GO32__ || __EMX__ */
  1969. static int getdoscodepage(void)
  1970. {
  1971. union REGS regs;
  1972. WREGS(regs,ax) = 0x6601;
  1973. #ifdef __EMX__
  1974. _int86(0x21, &regs, &regs);
  1975. if (WREGS(regs,flags) & 1)
  1976. #else
  1977. intdos(&regs, &regs);
  1978. if (WREGS(regs,cflag))
  1979. #endif
  1980. {
  1981. Trace((stderr,
  1982. "error in DOS function 0x66 (AX = 0x%04x): default to 850...\n",
  1983. (unsigned int)(WREGS(regs,ax))));
  1984. return 858;
  1985. } else
  1986. return WREGS(regs,bx);
  1987. }
  1988. #ifdef __EMX__
  1989. #ifdef MORE
  1990. /*************************/
  1991. /* Function screensize() */
  1992. /*************************/
  1993. int screensize(int *tt_rows, int *tt_cols)
  1994. {
  1995. int scr_dimen[2]; /* scr_dimen[0]: columns, src_dimen[1]: rows */
  1996. _scrsize(scr_dimen);
  1997. if (tt_rows != NULL) *tt_rows = scr_dimen[1];
  1998. if (tt_cols != NULL) *tt_cols = scr_dimen[0];
  1999. return 0;
  2000. }
  2001. #endif /* MORE */
  2002. #endif /* __EMX__ */
  2003. #ifdef WATCOMC_386
  2004. #ifdef MORE
  2005. #include <graph.h>
  2006. /*************************/
  2007. /* Function screensize() */
  2008. /*************************/
  2009. int screensize(int *tt_rows, int *tt_cols)
  2010. {
  2011. struct videoconfig vc;
  2012. _getvideoconfig(&vc);
  2013. if (tt_rows != NULL) *tt_rows = (int)(vc.numtextrows);
  2014. if (tt_cols != NULL) *tt_cols = (int)(vc.numtextcols);
  2015. return 0;
  2016. }
  2017. #endif /* MORE */
  2018. static struct RMINFO {
  2019. ulg edi, esi, ebp;
  2020. ulg reserved;
  2021. ulg ebx, edx, ecx, eax;
  2022. ush flags;
  2023. ush es,ds,fs,gs;
  2024. ush ip_ignored,cs_ignored;
  2025. ush sp,ss;
  2026. };
  2027. /* This function is used to call dos interrupts that may not be supported
  2028. * by some particular 32-bit DOS extender. It uses DPMI function 300h to
  2029. * simulate a real mode call of the interrupt. The caller is responsible
  2030. * for providing real mode addresses of any buffer areas used. The docs
  2031. * for PMODE/W imply that this should not be necessary for calling the DOS
  2032. * interrupts that it doesn't extend, but it crashes when this isn't used. */
  2033. static int int86x_realmode(int inter_no, union REGS *in,
  2034. union REGS *out, struct SREGS *seg)
  2035. {
  2036. union REGS local;
  2037. struct SREGS localseg;
  2038. struct RMINFO rmi;
  2039. int r;
  2040. rmi.eax = in->x.eax;
  2041. rmi.ebx = in->x.ebx;
  2042. rmi.ecx = in->x.ecx;
  2043. rmi.edx = in->x.edx;
  2044. rmi.edi = in->x.edi;
  2045. rmi.esi = in->x.esi;
  2046. rmi.ebp = rmi.reserved = 0L;
  2047. rmi.es = seg->es;
  2048. rmi.ds = seg->ds;
  2049. rmi.fs = seg->fs;
  2050. rmi.gs = seg->gs;
  2051. rmi.sp = rmi.ss = rmi.ip_ignored = rmi.cs_ignored = rmi.flags = 0;
  2052. memset(&local, 0, sizeof(local));
  2053. memset(&localseg, 0, sizeof(localseg));
  2054. local.w.ax = 0x0300;
  2055. local.h.bl = inter_no;
  2056. local.h.bh = 0;
  2057. local.w.cx = 0;
  2058. localseg.es = FP_SEG(&rmi);
  2059. local.x.edi = FP_OFF(&rmi);
  2060. r = int386x(0x31, &local, &local, &localseg);
  2061. out->x.eax = rmi.eax;
  2062. out->x.ebx = rmi.ebx;
  2063. out->x.ecx = rmi.ecx;
  2064. out->x.edx = rmi.edx;
  2065. out->x.edi = rmi.edi;
  2066. out->x.esi = rmi.esi;
  2067. out->x.cflag = rmi.flags & INTR_CF;
  2068. return r;
  2069. }
  2070. #endif /* WATCOMC_386 */
  2071. #ifdef DOS_STAT_BANDAID
  2072. /* This papers over a bug in Watcom 10.6's standard library...sigh.
  2073. * Apparently it applies to both the DOS and Win32 stat()s. */
  2074. int stat_bandaid(const char *path, struct stat *buf)
  2075. {
  2076. char newname[4];
  2077. if (!stat(path, buf))
  2078. return 0;
  2079. else if (!strcmp(path, ".") || (path[0] && !strcmp(path + 1, ":."))) {
  2080. strcpy(newname, path);
  2081. newname[strlen(path) - 1] = '\\'; /* stat(".") fails for root! */
  2082. return stat(newname, buf);
  2083. } else
  2084. return -1;
  2085. }
  2086. #endif /* DOS_STAT_BANDAID */
  2087. #endif /* !FUNZIP */