Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32.c 108KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. /*
  2. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2007-Mar-04 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. win32.c
  10. 32-bit Windows-specific (NT/9x) routines for use with Info-ZIP's UnZip 5.3
  11. and later.
  12. Contains: GetLoadPath()
  13. Opendir()
  14. Readdir()
  15. Closedir()
  16. SetSD() set security descriptor on file
  17. FindSDExtraField() extract SD e.f. block from extra field
  18. IsWinNT() indicate type of WIN32 platform
  19. test_NTSD() test integrity of NT security data
  20. utime2NtfsFileTime()
  21. utime2VFatFileTime()
  22. FStampIsLocTime()
  23. NtfsFileTime2utime()
  24. VFatFileTime2utime()
  25. getNTfiletime()
  26. SetFileSize()
  27. close_outfile()
  28. defer_dir_attribs()
  29. set_direc_attribs()
  30. stamp_file()
  31. isfloppy()
  32. NTQueryVolInfo()
  33. IsVolumeOldFAT()
  34. do_wild()
  35. mapattr()
  36. mapname()
  37. maskDOSdevice()
  38. map2fat()
  39. checkdir()
  40. dateformat()
  41. dateseparator()
  42. version()
  43. screensize()
  44. zstat_win32()
  45. conv_to_rule()
  46. GetPlatformLocalTimezone()
  47. getch_win32()
  48. ---------------------------------------------------------------------------*/
  49. #define UNZIP_INTERNAL
  50. #include <windows.h>
  51. #include "../unzip.h"
  52. #ifdef __RSXNT__
  53. # include "../win32/rsxntwin.h"
  54. #endif
  55. #include "../win32/nt.h"
  56. #ifndef FUNZIP /* most of this file is not used with fUnZip */
  57. /* some non-MS runtime headers (e.g. lcc) may miss this definition */
  58. #ifndef FILE_WRITE_ATTRIBUTES
  59. # define FILE_WRITE_ATTRIBUTES 0x0100
  60. #endif
  61. #if (defined(__EMX__) || defined(__CYGWIN__))
  62. # define MKDIR(path,mode) mkdir(path,mode)
  63. #else
  64. # define MKDIR(path,mode) mkdir(path)
  65. #endif
  66. #ifdef HAVE_WORKING_DIRENT_H
  67. # undef HAVE_WORKING_DIRENT_H
  68. #endif
  69. /* The emxrtl dirent support of (__GO32__ || __EMX__) converts to lowercase! */
  70. #if defined(__CYGWIN__)
  71. # define HAVE_WORKING_DIRENT_H
  72. #endif
  73. #ifndef SFX
  74. # ifdef HAVE_WORKING_DIRENT_H
  75. # include <dirent.h> /* use readdir() */
  76. # define zdirent dirent
  77. # define zDIR DIR
  78. # define Opendir opendir
  79. # define Readdir readdir
  80. # define Closedir closedir
  81. # else /* !HAVE_WORKING_DIRENT_H */
  82. typedef struct zdirent {
  83. char reserved [21];
  84. char ff_attrib;
  85. short ff_ftime;
  86. short ff_fdate;
  87. long size;
  88. char d_name[MAX_PATH];
  89. int d_first;
  90. HANDLE d_hFindFile;
  91. } zDIR;
  92. static zDIR *Opendir (const char *n);
  93. static struct zdirent *Readdir (zDIR *d);
  94. static void Closedir (zDIR *d);
  95. # endif /* ?HAVE_WORKING_DIRENT_H */
  96. #endif /* !SFX */
  97. #ifdef SET_DIR_ATTRIB
  98. typedef struct NTdirattr { /* struct for holding unix style directory */
  99. struct NTdirattr *next; /* info until can be sorted and set at end */
  100. char *fn; /* filename of directory */
  101. FILETIME Modft; /* File time type defined in NT, `last modified' time */
  102. FILETIME Accft; /* NT file time type, `last access' time */
  103. FILETIME Creft; /* NT file time type, `file creation' time */
  104. int gotTime;
  105. unsigned perms; /* same as min_info.file_attr */
  106. #ifdef NTSD_EAS
  107. unsigned SDlen; /* length of SD data in buf */
  108. #endif
  109. char buf[1]; /* buffer stub for directory SD and name */
  110. } NTdirattr;
  111. #define NtAtt(d) ((NTdirattr *)d) /* typecast shortcut */
  112. #endif /* SET_DIR_ATTRIB */
  113. /* Function prototypes */
  114. #ifdef NTSD_EAS
  115. static int SetSD(__GPRO__ char *path, unsigned fperms,
  116. uch *eb_ptr, unsigned eb_len);
  117. static int FindSDExtraField(__GPRO__
  118. uch *ef_ptr, unsigned ef_len,
  119. uch **p_ebSD_ptr, unsigned *p_ebSD_len);
  120. #endif
  121. #ifndef NO_W32TIMES_IZFIX
  122. static void utime2NtfsFileTime(time_t ut, FILETIME *pft);
  123. #endif
  124. static void utime2VFatFileTime(time_t ut, FILETIME *pft, int clipDosMin);
  125. #if (defined(W32_STAT_BANDAID) && !defined(NO_W32TIMES_IZFIX))
  126. static int NtfsFileTime2utime(const FILETIME *pft, time_t *ut);
  127. #endif
  128. #ifdef W32_STAT_BANDAID
  129. static int VFatFileTime2utime(const FILETIME *pft, time_t *ut);
  130. #endif
  131. static int FStampIsLocTime(__GPRO__ const char *path);
  132. static int getNTfiletime (__GPRO__ FILETIME *pModFT, FILETIME *pAccFT,
  133. FILETIME *pCreFT);
  134. static int isfloppy (int nDrive);
  135. static int NTQueryVolInfo (__GPRO__ const char *name);
  136. static int IsVolumeOldFAT (__GPRO__ const char *name);
  137. static void maskDOSdevice (__GPRO__ char *pathcomp);
  138. static void map2fat (char *pathcomp, char **pEndFAT);
  139. #if (defined(__MINGW32__) && !defined(USE_MINGW_GLOBBING))
  140. int _CRT_glob = 0; /* suppress command line globbing by C RTL */
  141. #endif
  142. #ifdef ACORN_FTYPE_NFS
  143. /* Acorn bits for NFS filetyping */
  144. typedef struct {
  145. uch ID[2];
  146. uch size[2];
  147. uch ID_2[4];
  148. uch loadaddr[4];
  149. uch execaddr[4];
  150. uch attr[4];
  151. } RO_extra_block;
  152. #endif /* ACORN_FTYPE_NFS */
  153. /* static int created_dir; */ /* used by mapname(), checkdir() */
  154. /* static int renamed_fullpath; */ /* ditto */
  155. /* static int fnlen; */ /* ditto */
  156. /* static unsigned nLabelDrive; */ /* ditto */
  157. extern char Far TruncNTSD[]; /* in extract.c */
  158. #ifdef SFX
  159. /**************************/
  160. /* Function GetLoadPath() */
  161. /**************************/
  162. char *GetLoadPath(__GPRO)
  163. {
  164. #ifdef MSC
  165. extern char *_pgmptr;
  166. return _pgmptr;
  167. #else /* use generic API call */
  168. GetModuleFileName(NULL, G.filename, FILNAMSIZ);
  169. _ISO_INTERN(G.filename); /* translate to codepage of C rtl's stdio */
  170. return G.filename;
  171. #endif
  172. } /* end function GetLoadPath() */
  173. #else /* !SFX */
  174. #ifndef HAVE_WORKING_DIRENT_H
  175. /**********************/ /* Borrowed from ZIP 2.0 sources */
  176. /* Function Opendir() */ /* Difference: no special handling for */
  177. /**********************/ /* hidden or system files. */
  178. static zDIR *Opendir(n)
  179. const char *n; /* directory to open */
  180. {
  181. zDIR *d; /* malloc'd return value */
  182. char *p; /* malloc'd temporary string */
  183. WIN32_FIND_DATAA fd;
  184. extent len = strlen(n);
  185. /* Start searching for files in directory n */
  186. if ((d = (zDIR *)malloc(sizeof(zDIR))) == NULL ||
  187. (p = malloc(strlen(n) + 5)) == NULL)
  188. {
  189. if (d != (zDIR *)NULL)
  190. free((void *)d);
  191. return (zDIR *)NULL;
  192. }
  193. INTERN_TO_ISO(n, p);
  194. if (len > 0) {
  195. if (p[len-1] == ':')
  196. p[len++] = '.'; /* x: => x:. */
  197. else if (p[len-1] == '/' || p[len-1] == '\\')
  198. --len; /* foo/ => foo */
  199. }
  200. strcpy(p+len, "/*");
  201. if (INVALID_HANDLE_VALUE == (d->d_hFindFile = FindFirstFileA(p, &fd))) {
  202. free((zvoid *)d);
  203. free((zvoid *)p);
  204. return NULL;
  205. }
  206. strcpy(d->d_name, fd.cFileName);
  207. free((zvoid *)p);
  208. d->d_first = 1;
  209. return d;
  210. } /* end of function Opendir() */
  211. /**********************/ /* Borrowed from ZIP 2.0 sources */
  212. /* Function Readdir() */ /* Difference: no special handling for */
  213. /**********************/ /* hidden or system files. */
  214. static struct zdirent *Readdir(d)
  215. zDIR *d; /* directory stream from which to read */
  216. {
  217. /* Return pointer to first or next directory entry, or NULL if end. */
  218. if ( d->d_first )
  219. d->d_first = 0;
  220. else
  221. {
  222. WIN32_FIND_DATAA fd;
  223. if ( !FindNextFileA(d->d_hFindFile, &fd) )
  224. return NULL;
  225. ISO_TO_INTERN(fd.cFileName, d->d_name);
  226. }
  227. return (struct zdirent *)d;
  228. } /* end of function Readdir() */
  229. /***********************/
  230. /* Function Closedir() */ /* Borrowed from ZIP 2.0 sources */
  231. /***********************/
  232. static void Closedir(d)
  233. zDIR *d; /* directory stream to close */
  234. {
  235. FindClose(d->d_hFindFile);
  236. free(d);
  237. }
  238. #endif /* !HAVE_WORKING_DIRENT_H */
  239. #endif /* ?SFX */
  240. #ifdef NTSD_EAS
  241. /**********************/
  242. /* Function SetSD() */ /* return almost-PK errors */
  243. /**********************/
  244. static int SetSD(__G__ path, fperms, eb_ptr, eb_len)
  245. __GDEF
  246. char *path;
  247. unsigned fperms;
  248. uch *eb_ptr;
  249. unsigned eb_len;
  250. {
  251. ulg ntsd_ucSize;
  252. VOLUMECAPS VolumeCaps;
  253. uch *security_data;
  254. int error;
  255. ntsd_ucSize = makelong(eb_ptr + (EB_HEADSIZE+EB_UCSIZE_P));
  256. if (ntsd_ucSize > 0L && eb_len <= (EB_NTSD_L_LEN + EB_CMPRHEADLEN))
  257. return IZ_EF_TRUNC; /* no compressed data! */
  258. /* provide useful input */
  259. VolumeCaps.dwFileAttributes = fperms;
  260. VolumeCaps.bUsePrivileges = (uO.X_flag > 1);
  261. /* check target volume capabilities - just fall through
  262. * and try if fail */
  263. if (GetVolumeCaps(G.rootpath, path, &VolumeCaps) &&
  264. !(VolumeCaps.dwFileSystemFlags & FS_PERSISTENT_ACLS))
  265. return PK_OK;
  266. /* allocate storage for uncompressed data */
  267. security_data = (uch *)malloc((extent)ntsd_ucSize);
  268. if (security_data == (uch *)NULL)
  269. return PK_MEM4;
  270. error = memextract(__G__ security_data, ntsd_ucSize,
  271. (eb_ptr + (EB_HEADSIZE+EB_NTSD_L_LEN)), (ulg)(eb_len - EB_NTSD_L_LEN));
  272. if (error == PK_OK) {
  273. if (SecuritySet(path, &VolumeCaps, security_data)) {
  274. error = PK_COOL;
  275. if (!uO.tflag && QCOND2)
  276. Info(slide, 0, ((char *)slide, " (%ld bytes security)",
  277. ntsd_ucSize));
  278. }
  279. }
  280. free(security_data);
  281. return error;
  282. }
  283. /********************************/ /* scan extra fields for something */
  284. /* Function FindSDExtraField() */ /* we happen to know */
  285. /********************************/
  286. /* Returns TRUE when a valid NTFS SD block is found.
  287. * Address and size of the NTSD e.f. block are passed up to the caller.
  288. * In case of more than one valid NTSD block in the e.f., the last block
  289. * found is passed up.
  290. * Returns FALSE and leaves the content of the ebSD_ptr and ebSD_len
  291. * parameters untouched when no valid NTFS SD block is found. */
  292. static int FindSDExtraField(__GPRO__
  293. uch *ef_ptr, unsigned ef_len,
  294. uch **p_ebSD_ptr, unsigned *p_ebSD_len)
  295. {
  296. int rc = FALSE;
  297. if (!uO.X_flag)
  298. return FALSE; /* user said don't process ACLs; for now, no other
  299. extra block types are handled here */
  300. while (ef_len >= EB_HEADSIZE)
  301. {
  302. unsigned eb_id = makeword(EB_ID + ef_ptr);
  303. unsigned eb_len = makeword(EB_LEN + ef_ptr);
  304. if (eb_len > (ef_len - EB_HEADSIZE)) {
  305. /* discovered some extra field inconsistency! */
  306. Trace((stderr,
  307. "FindSDExtraField: block length %u > rest ef_size %u\n", eb_len,
  308. ef_len - EB_HEADSIZE));
  309. break;
  310. }
  311. switch (eb_id)
  312. {
  313. /* process security descriptor extra data if:
  314. Caller is WinNT AND
  315. Target local/remote drive supports acls AND
  316. Target file is not a directory (else we defer processing
  317. until later)
  318. */
  319. case EF_NTSD:
  320. if (!IsWinNT())
  321. break; /* OS not capable of handling NTFS attributes */
  322. if (eb_len < EB_NTSD_L_LEN)
  323. break; /* not a valid NTSD extra field */
  324. /* check if we know how to handle this version */
  325. if (*(ef_ptr + (EB_HEADSIZE+EB_NTSD_VERSION))
  326. > (uch)EB_NTSD_MAX_VER)
  327. break;
  328. *p_ebSD_ptr = ef_ptr;
  329. *p_ebSD_len = eb_len;
  330. rc = TRUE;
  331. break;
  332. #ifdef DEBUG
  333. case EF_OS2:
  334. case EF_AV:
  335. case EF_PKVMS:
  336. case EF_PKW32:
  337. case EF_PKUNIX:
  338. case EF_IZVMS:
  339. case EF_IZUNIX:
  340. case EF_IZUNIX2:
  341. case EF_TIME:
  342. case EF_MAC3:
  343. case EF_JLMAC:
  344. case EF_ZIPIT:
  345. case EF_VMCMS:
  346. case EF_MVS:
  347. case EF_ACL:
  348. case EF_ATHEOS:
  349. case EF_BEOS:
  350. case EF_QDOS:
  351. case EF_AOSVS:
  352. case EF_SPARK:
  353. case EF_MD5:
  354. case EF_ASIUNIX:
  355. break; /* shut up for other known e.f. blocks */
  356. #endif /* DEBUG */
  357. default:
  358. Trace((stderr,
  359. "FindSDExtraField: unknown extra field block, ID=%u\n",
  360. eb_id));
  361. break;
  362. }
  363. ef_ptr += (eb_len + EB_HEADSIZE);
  364. ef_len -= (eb_len + EB_HEADSIZE);
  365. }
  366. return rc;
  367. }
  368. #ifndef SFX
  369. /**************************/
  370. /* Function test_NTSD() */ /* returns PK_WARN when NTSD data is invalid */
  371. /**************************/
  372. #ifdef __BORLANDC__
  373. /* Turn off warning about not using all parameters for this function only */
  374. #pragma argsused
  375. #endif
  376. int test_NTSD(__G__ eb, eb_size, eb_ucptr, eb_ucsize)
  377. __GDEF
  378. uch *eb;
  379. unsigned eb_size;
  380. uch *eb_ucptr;
  381. ulg eb_ucsize;
  382. {
  383. return (ValidateSecurity(eb_ucptr) ? PK_OK : PK_WARN);
  384. } /* end function test_NTSD() */
  385. #endif /* !SFX */
  386. #endif /* NTSD_EAS */
  387. /**********************/
  388. /* Function IsWinNT() */
  389. /**********************/
  390. int IsWinNT(void) /* returns TRUE if real NT, FALSE if Win9x or Win32s */
  391. {
  392. static DWORD g_PlatformId = 0xFFFFFFFF; /* saved platform indicator */
  393. if (g_PlatformId == 0xFFFFFFFF) {
  394. /* note: GetVersionEx() doesn't exist on WinNT 3.1 */
  395. if (GetVersion() < 0x80000000)
  396. g_PlatformId = TRUE;
  397. else
  398. g_PlatformId = FALSE;
  399. }
  400. return (int)g_PlatformId;
  401. }
  402. /* DEBUG_TIME insertion: */
  403. #ifdef DEBUG_TIME
  404. static int show_NTFileTime(FILE *hdo, char *TTmsg, int isloc, FILETIME *pft);
  405. static int show_NTFileTime(FILE *hdo, char *TTmsg, int isloc, FILETIME *pft)
  406. {
  407. SYSTEMTIME w32tm;
  408. int rval;
  409. rval = FileTimeToSystemTime(pft, &w32tm);
  410. if (!rval) {
  411. fprintf(hdo, "%s\n %08lX,%08lX (%s) -> Conversion failed !!!\n",
  412. TTmsg, (ulg)(pft->dwHighDateTime), (ulg)(pft->dwLowDateTime),
  413. (isloc ? "local" : "UTC"));
  414. } else {
  415. fprintf(hdo, "%s\n %08lx,%08lx -> %04u-%02u-%02u, %02u:%02u:%02u %s\n",
  416. TTmsg, (ulg)(pft->dwHighDateTime), (ulg)(pft->dwLowDateTime),
  417. w32tm.wYear, w32tm.wMonth, w32tm.wDay, w32tm.wHour,
  418. w32tm.wMinute, w32tm.wSecond, (isloc ? "local" : "UTC"));
  419. }
  420. return rval;
  421. }
  422. #define FTTrace(x) show_NTFileTime x
  423. #else
  424. #define FTTrace(x)
  425. #endif /* DEBUG_TIME */
  426. /* end of DEBUG_TIME insertion */
  427. #ifndef IZ_USE_INT64
  428. # if (defined(__GNUC__) || defined(ULONG_LONG_MAX))
  429. typedef long long LLONG64;
  430. typedef unsigned long long ULLNG64;
  431. # define IZ_USE_INT64
  432. # elif (defined(__WATCOMC__) && (__WATCOMC__ >= 1100))
  433. typedef __int64 LLONG64;
  434. typedef unsigned __int64 ULLNG64;
  435. # define IZ_USE_INT64
  436. # elif (defined(_MSC_VER) && (_MSC_VER >= 1100))
  437. typedef __int64 LLONG64;
  438. typedef unsigned __int64 ULLNG64;
  439. # define IZ_USE_INT64
  440. # elif (defined(__IBMC__) && (__IBMC__ >= 350))
  441. typedef __int64 LLONG64;
  442. typedef unsigned __int64 ULLNG64;
  443. # define IZ_USE_INT64
  444. # elif defined(HAVE_INT64)
  445. typedef __int64 LLONG64;
  446. typedef unsigned __int64 ULLNG64;
  447. # define IZ_USE_INT64
  448. # endif
  449. #endif
  450. /* scale factor and offset for conversion time_t -> FILETIME */
  451. #define NT_QUANTA_PER_UNIX 10000000L
  452. #define UNIX_TIME_ZERO_HI 0x019DB1DEUL
  453. #define UNIX_TIME_ZERO_LO 0xD53E8000UL
  454. /* special FILETIME values for bound-checks */
  455. #define UNIX_TIME_UMAX_HI 0x0236485EUL
  456. #define UNIX_TIME_UMAX_LO 0xD4A5E980UL
  457. #define UNIX_TIME_SMIN_HI 0x0151669EUL
  458. #define UNIX_TIME_SMIN_LO 0xD53E8000UL
  459. #define UNIX_TIME_SMAX_HI 0x01E9FD1EUL
  460. #define UNIX_TIME_SMAX_LO 0xD4A5E980UL
  461. #define DOSTIME_MIN_FT_HI 0x01A8E79FUL
  462. #define DOSTIME_MIN_FT_LO 0xE1D58000UL
  463. /* time_t equivalent of DOSTIME_MINIMUM */
  464. #define UTIME_1980_JAN_01_00_00 315532800L
  465. #ifndef NO_W32TIMES_IZFIX
  466. /*********************************/
  467. /* Function utime2NtfsFileTime() */ /* convert Unix time_t format into the */
  468. /*********************************/ /* form used by SetFileTime() in NT/9x */
  469. static void utime2NtfsFileTime(time_t ut, FILETIME *pft)
  470. {
  471. #ifdef IZ_USE_INT64
  472. ULLNG64 NTtime;
  473. /* NT_QUANTA_PER_UNIX is small enough so that "ut * NT_QUANTA_PER_UNIX"
  474. * cannot overflow in 64-bit signed calculation, regardless whether "ut"
  475. * is signed or unsigned. */
  476. NTtime = ((LLONG64)ut * NT_QUANTA_PER_UNIX) +
  477. ((ULLNG64)UNIX_TIME_ZERO_LO + ((ULLNG64)UNIX_TIME_ZERO_HI << 32));
  478. pft->dwLowDateTime = (DWORD)NTtime;
  479. pft->dwHighDateTime = (DWORD)(NTtime >> 32);
  480. #else /* !IZ_USE_INT64 (64-bit integer arithmetics may not be supported) */
  481. unsigned int b1, b2, carry = 0;
  482. unsigned long r0, r1, r2, r3;
  483. long r4; /* signed, to catch environments with signed time_t */
  484. b1 = ut & 0xFFFF;
  485. b2 = (ut >> 16) & 0xFFFF; /* if ut is over 32 bits, too bad */
  486. r1 = b1 * (NT_QUANTA_PER_UNIX & 0xFFFF);
  487. r2 = b1 * (NT_QUANTA_PER_UNIX >> 16);
  488. r3 = b2 * (NT_QUANTA_PER_UNIX & 0xFFFF);
  489. r4 = b2 * (NT_QUANTA_PER_UNIX >> 16);
  490. r0 = (r1 + (r2 << 16)) & 0xFFFFFFFFL;
  491. if (r0 < r1)
  492. carry++;
  493. r1 = r0;
  494. r0 = (r0 + (r3 << 16)) & 0xFFFFFFFFL;
  495. if (r0 < r1)
  496. carry++;
  497. pft->dwLowDateTime = r0 + UNIX_TIME_ZERO_LO;
  498. if (pft->dwLowDateTime < r0)
  499. carry++;
  500. pft->dwHighDateTime = r4 + (r2 >> 16) + (r3 >> 16)
  501. + UNIX_TIME_ZERO_HI + carry;
  502. #endif /* ?IZ_USE_INT64 */
  503. } /* end function utime2NtfsFileTime() */
  504. #endif /* !NO_W32TIMES_IZFIX */
  505. /*********************************/
  506. /* Function utime2VFatFileTime() */ /* convert Unix time_t format into the */
  507. /*********************************/ /* form used by SetFileTime() in NT/9x */
  508. static void utime2VFatFileTime(time_t ut, FILETIME *pft, int clipDosMin)
  509. {
  510. time_t utc = ut;
  511. struct tm *ltm;
  512. SYSTEMTIME w32tm;
  513. FILETIME lft;
  514. /* The milliseconds field gets always initialized to 0. */
  515. w32tm.wMilliseconds = 0;
  516. #ifdef __BORLANDC__ /* Borland C++ 5.x crashes when trying to reference tm */
  517. if (utc < UTIME_1980_JAN_01_00_00)
  518. utc = UTIME_1980_JAN_01_00_00;
  519. #endif
  520. ltm = localtime(&utc);
  521. if (ltm == (struct tm *)NULL)
  522. /* localtime() did not accept given utc time value; try to use
  523. the UTC value */
  524. ltm = gmtime(&utc);
  525. if (ltm == (struct tm *)NULL) {
  526. if (ut <= (UTIME_1980_JAN_01_00_00 + 86400)) {
  527. /* use DOSTIME_MINIMUM date instead of "early" failure dates */
  528. w32tm.wYear = 1980;
  529. w32tm.wMonth = 1;
  530. w32tm.wDay = 1;
  531. w32tm.wHour = 0;
  532. w32tm.wMinute = 0;
  533. w32tm.wSecond = 0;
  534. } else {
  535. /* as a last resort, use the current system time */
  536. GetLocalTime(&w32tm);
  537. }
  538. } else if (clipDosMin && (ltm->tm_year < 80)) {
  539. w32tm.wYear = 1980;
  540. w32tm.wMonth = 1;
  541. w32tm.wDay = 1;
  542. w32tm.wHour = 0;
  543. w32tm.wMinute = 0;
  544. w32tm.wSecond = 0;
  545. } else {
  546. w32tm.wYear = ltm->tm_year + 1900; /* year + 1900 -> year */
  547. w32tm.wMonth = ltm->tm_mon + 1; /* 0..11 -> 1..12 */
  548. w32tm.wDay = ltm->tm_mday; /* 1..31 */
  549. w32tm.wHour = ltm->tm_hour; /* 0..23 */
  550. w32tm.wMinute = ltm->tm_min; /* 0..59 */
  551. w32tm.wSecond = ltm->tm_sec; /* 0..61 in ANSI C */
  552. }
  553. SystemTimeToFileTime(&w32tm, &lft);
  554. LocalFileTimeToFileTime(&lft, pft);
  555. } /* end function utime2VFatFileTime() */
  556. /* nonzero if `y' is a leap year, else zero */
  557. #define leap(y) (((y)%4 == 0 && (y)%100 != 0) || (y)%400 == 0)
  558. /* number of leap years from 1970 to `y' (not including `y' itself) */
  559. #define nleap(y) (((y)-1969)/4 - ((y)-1901)/100 + ((y)-1601)/400)
  560. extern ZCONST ush ydays[]; /* defined in fileio.c */
  561. #if (defined(W32_STAT_BANDAID) && !defined(NO_W32TIMES_IZFIX))
  562. /*********************************/
  563. /* Function NtfsFileTime2utime() */
  564. /*********************************/
  565. static int NtfsFileTime2utime(const FILETIME *pft, time_t *ut)
  566. {
  567. #ifdef IZ_USE_INT64
  568. ULLNG64 NTtime;
  569. NTtime = ((ULLNG64)pft->dwLowDateTime +
  570. ((ULLNG64)pft->dwHighDateTime << 32));
  571. #ifndef TIME_T_TYPE_DOUBLE
  572. /* underflow and overflow handling */
  573. #ifdef CHECK_UTIME_SIGNED_UNSIGNED
  574. if ((time_t)0x80000000L < (time_t)0L)
  575. {
  576. if (NTtime < ((ULLNG64)UNIX_TIME_SMIN_LO +
  577. ((ULLNG64)UNIX_TIME_SMIN_HI << 32))) {
  578. *ut = (time_t)LONG_MIN;
  579. return FALSE;
  580. }
  581. if (NTtime > ((ULLNG64)UNIX_TIME_SMAX_LO +
  582. ((ULLNG64)UNIX_TIME_SMAX_HI << 32))) {
  583. *ut = (time_t)LONG_MAX;
  584. return FALSE;
  585. }
  586. }
  587. else
  588. #endif /* CHECK_UTIME_SIGNED_UNSIGNED */
  589. {
  590. if (NTtime < ((ULLNG64)UNIX_TIME_ZERO_LO +
  591. ((ULLNG64)UNIX_TIME_ZERO_HI << 32))) {
  592. *ut = (time_t)0;
  593. return FALSE;
  594. }
  595. if (NTtime > ((ULLNG64)UNIX_TIME_UMAX_LO +
  596. ((ULLNG64)UNIX_TIME_UMAX_HI << 32))) {
  597. *ut = (time_t)ULONG_MAX;
  598. return FALSE;
  599. }
  600. }
  601. #endif /* !TIME_T_TYPE_DOUBLE */
  602. NTtime -= ((ULLNG64)UNIX_TIME_ZERO_LO +
  603. ((ULLNG64)UNIX_TIME_ZERO_HI << 32));
  604. *ut = (time_t)(NTtime / (unsigned long)NT_QUANTA_PER_UNIX);
  605. return TRUE;
  606. #else /* !IZ_USE_INT64 (64-bit integer arithmetics may not be supported) */
  607. time_t days;
  608. SYSTEMTIME w32tm;
  609. #ifndef TIME_T_TYPE_DOUBLE
  610. /* underflow and overflow handling */
  611. #ifdef CHECK_UTIME_SIGNED_UNSIGNED
  612. if ((time_t)0x80000000L < (time_t)0L)
  613. {
  614. if ((pft->dwHighDateTime < UNIX_TIME_SMIN_HI) ||
  615. ((pft->dwHighDateTime == UNIX_TIME_SMIN_HI) &&
  616. (pft->dwLowDateTime < UNIX_TIME_SMIN_LO))) {
  617. *ut = (time_t)LONG_MIN;
  618. return FALSE;
  619. if ((pft->dwHighDateTime > UNIX_TIME_SMAX_HI) ||
  620. ((pft->dwHighDateTime == UNIX_TIME_SMAX_HI) &&
  621. (pft->dwLowDateTime > UNIX_TIME_SMAX_LO))) {
  622. *ut = (time_t)LONG_MAX;
  623. return FALSE;
  624. }
  625. }
  626. else
  627. #endif /* CHECK_UTIME_SIGNED_UNSIGNED */
  628. {
  629. if ((pft->dwHighDateTime < UNIX_TIME_ZERO_HI) ||
  630. ((pft->dwHighDateTime == UNIX_TIME_ZERO_HI) &&
  631. (pft->dwLowDateTime < UNIX_TIME_ZERO_LO))) {
  632. *ut = (time_t)0;
  633. return FALSE;
  634. }
  635. if ((pft->dwHighDateTime > UNIX_TIME_UMAX_HI) ||
  636. ((pft->dwHighDateTime == UNIX_TIME_UMAX_HI) &&
  637. (pft->dwLowDateTime > UNIX_TIME_UMAX_LO))) {
  638. *ut = (time_t)ULONG_MAX;
  639. return FALSE;
  640. }
  641. }
  642. #endif /* !TIME_T_TYPE_DOUBLE */
  643. FileTimeToSystemTime(pft, &w32tm);
  644. /* set `days' to the number of days into the year */
  645. days = w32tm.wDay - 1 + ydays[w32tm.wMonth-1] +
  646. (w32tm.wMonth > 2 && leap (w32tm.wYear));
  647. /* now set `days' to the number of days since 1 Jan 1970 */
  648. days += 365 * (time_t)(w32tm.wYear - 1970) +
  649. (time_t)(nleap(w32tm.wYear));
  650. *ut = (time_t)(86400L * days + 3600L * (time_t)w32tm.wHour +
  651. (time_t)(60 * w32tm.wMinute + w32tm.wSecond));
  652. return TRUE;
  653. #endif /* ?IZ_USE_INT64 */
  654. } /* end function NtfsFileTime2utime() */
  655. #endif /* W32_STAT_BANDAID && !NO_W32TIMES_IZFIX */
  656. #ifdef W32_STAT_BANDAID
  657. /*********************************/
  658. /* Function VFatFileTime2utime() */
  659. /*********************************/
  660. static int VFatFileTime2utime(const FILETIME *pft, time_t *ut)
  661. {
  662. FILETIME lft;
  663. #ifndef HAVE_MKTIME
  664. WORD wDOSDate, wDOSTime;
  665. #else
  666. SYSTEMTIME w32tm;
  667. struct tm ltm;
  668. #endif
  669. if (!FileTimeToLocalFileTime(pft, &lft)) {
  670. /* if pft cannot be converted to local time, set ut to current time */
  671. time(ut);
  672. return FALSE;
  673. }
  674. FTTrace((stdout, "VFatFT2utime, feed for mktime()", 1, &lft));
  675. #ifndef HAVE_MKTIME
  676. /* This version of the FILETIME-to-UNIXTIME conversion function
  677. * uses DOS-DATE-TIME format as intermediate stage. For modification
  678. * and access times, this is no problem. But, the extra fine resolution
  679. * of the VFAT-stored creation time gets lost.
  680. */
  681. if (!FileTimeToDosDateTime(&lft, &wDOSDate, &wDOSTime)) {
  682. static const FILETIME dosmin_ft =
  683. {DOSTIME_MIN_FT_LO, DOSTIME_MIN_FT_HI};
  684. if (CompareFileTime(&lft, &dosmin_ft) <= 0) {
  685. /* underflow -> set to minimum DOS time */
  686. wDOSDate = (WORD)((DWORD)DOSTIME_MINIMUM >> 16);
  687. wDOSTime = (WORD)DOSTIME_MINIMUM;
  688. } else {
  689. /* overflow -> set to maximum DOS time */
  690. wDOSDate = (WORD)0xFF9F; /* 2107-12-31 */
  691. wDOSTime = (WORD)0xBF7D; /* 23:59:58 */
  692. }
  693. }
  694. TTrace((stdout,"DosDateTime is %04u-%02u-%02u %02u:%02u:%02u\n",
  695. (unsigned)((wDOSDate>>9)&0x7f)+1980,(unsigned)((wDOSDate>>5)&0x0f),
  696. (unsigned)(wDOSDate&0x1f),(unsigned)((wDOSTime>>11)&0x1f),
  697. (unsigned)((wDOSTime>>5)&0x3f),(unsigned)((wDOSTime<<1)&0x3e)));
  698. *ut = dos_to_unix_time(((ulg)wDOSDate << 16) | (ulg)wDOSTime);
  699. /* a cheap error check: dos_to_unix_time() only returns an odd time
  700. * when clipping at maximum time_t value. DOS_DATE_TIME values have
  701. * a resolution of 2 seconds and are therefore even numbers.
  702. */
  703. return (((*ut)&1) == (time_t)0);
  704. #else /* HAVE_MKTIME */
  705. FileTimeToSystemTime(&lft, &w32tm);
  706. #ifndef TIME_T_TYPE_DOUBLE
  707. /* underflow and overflow handling */
  708. /* TODO: The range checks are not accurate, the actual limits may
  709. * be off by one daylight-saving-time shift (typically 1 hour),
  710. * depending on the current state of "is_dst".
  711. */
  712. #ifdef CHECK_UTIME_SIGNED_UNSIGNED
  713. if ((time_t)0x80000000L < (time_t)0L)
  714. {
  715. if ((pft->dwHighDateTime < UNIX_TIME_SMIN_HI) ||
  716. ((pft->dwHighDateTime == UNIX_TIME_SMIN_HI) &&
  717. (pft->dwLowDateTime < UNIX_TIME_SMIN_LO))) {
  718. *ut = (time_t)LONG_MIN;
  719. return FALSE;
  720. if ((pft->dwHighDateTime > UNIX_TIME_SMAX_HI) ||
  721. ((pft->dwHighDateTime == UNIX_TIME_SMAX_HI) &&
  722. (pft->dwLowDateTime > UNIX_TIME_SMAX_LO))) {
  723. *ut = (time_t)LONG_MAX;
  724. return FALSE;
  725. }
  726. }
  727. else
  728. #endif /* CHECK_UTIME_SIGNED_UNSIGNED */
  729. {
  730. if ((pft->dwHighDateTime < UNIX_TIME_ZERO_HI) ||
  731. ((pft->dwHighDateTime == UNIX_TIME_ZERO_HI) &&
  732. (pft->dwLowDateTime < UNIX_TIME_ZERO_LO))) {
  733. *ut = (time_t)0;
  734. return FALSE;
  735. }
  736. if ((pft->dwHighDateTime > UNIX_TIME_UMAX_HI) ||
  737. ((pft->dwHighDateTime == UNIX_TIME_UMAX_HI) &&
  738. (pft->dwLowDateTime > UNIX_TIME_UMAX_LO))) {
  739. *ut = (time_t)ULONG_MAX;
  740. return FALSE;
  741. }
  742. }
  743. #endif /* !TIME_T_TYPE_DOUBLE */
  744. ltm.tm_year = w32tm.wYear - 1900;
  745. ltm.tm_mon = w32tm.wMonth - 1;
  746. ltm.tm_mday = w32tm.wDay;
  747. ltm.tm_hour = w32tm.wHour;
  748. ltm.tm_min = w32tm.wMinute;
  749. ltm.tm_sec = w32tm.wSecond;
  750. ltm.tm_isdst = -1; /* let mktime determine if DST is in effect */
  751. *ut = mktime(&ltm);
  752. /* a cheap error check: mktime returns "(time_t)-1L" on conversion errors.
  753. * Normally, we would have to apply a consistency check because "-1"
  754. * could also be a valid time. But, it is quite unlikely to read back odd
  755. * time numbers from file systems that store time stamps in DOS format.
  756. * (The only known exception is creation time on VFAT partitions.)
  757. */
  758. return (*ut != (time_t)-1L);
  759. #endif /* ?HAVE_MKTIME */
  760. } /* end function VFatFileTime2utime() */
  761. #endif /* W32_STAT_BANDAID */
  762. /******************************/
  763. /* Function FStampIsLocTime() */
  764. /******************************/
  765. static int FStampIsLocTime(__GPRO__ const char *path)
  766. {
  767. return (NTQueryVolInfo(__G__ path) ? G.lastVolLocTim : FALSE);
  768. }
  769. #ifndef NO_W32TIMES_IZFIX
  770. # define UTIME_2_IZFILETIME(ut, pft) \
  771. if (fs_uses_loctime) {utime2VFatFileTime(ut, pft, TRUE);} \
  772. else {utime2NtfsFileTime(ut, pft);}
  773. #else
  774. # define UTIME_2_IZFILETIME(ut, pft) \
  775. utime2VFatFileTime(ut, pft, fs_uses_loctime);
  776. #endif
  777. /****************************/ /* Get the file time in a format that */
  778. /* Function getNTfiletime() */ /* can be used by SetFileTime() in NT */
  779. /****************************/
  780. static int getNTfiletime(__G__ pModFT, pAccFT, pCreFT)
  781. __GDEF
  782. FILETIME *pModFT;
  783. FILETIME *pAccFT;
  784. FILETIME *pCreFT;
  785. {
  786. #ifdef USE_EF_UT_TIME
  787. unsigned eb_izux_flg;
  788. iztimes z_utime; /* struct for Unix-style actime & modtime, + creatime */
  789. #endif
  790. int fs_uses_loctime = FStampIsLocTime(__G__ G.filename);
  791. /* Copy and/or convert time and date variables, if necessary;
  792. * return a flag indicating which time stamps are available. */
  793. #ifdef USE_EF_UT_TIME
  794. if (G.extra_field &&
  795. #ifdef IZ_CHECK_TZ
  796. G.tz_is_valid &&
  797. #endif
  798. ((eb_izux_flg = ef_scan_for_izux(G.extra_field,
  799. G.lrec.extra_field_length, 0, G.lrec.last_mod_dos_datetime,
  800. &z_utime, NULL)) & EB_UT_FL_MTIME))
  801. {
  802. TTrace((stderr, "getNTfiletime: Unix e.f. modif. time = %lu\n",
  803. z_utime.mtime));
  804. UTIME_2_IZFILETIME(z_utime.mtime, pModFT)
  805. if (eb_izux_flg & EB_UT_FL_ATIME) {
  806. UTIME_2_IZFILETIME(z_utime.atime, pAccFT)
  807. }
  808. if (eb_izux_flg & EB_UT_FL_CTIME) {
  809. UTIME_2_IZFILETIME(z_utime.ctime, pCreFT)
  810. }
  811. return (int)eb_izux_flg;
  812. }
  813. #endif /* USE_EF_UT_TIME */
  814. #ifndef NO_W32TIMES_IZFIX
  815. if (!fs_uses_loctime) {
  816. time_t ux_modtime;
  817. ux_modtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  818. utime2NtfsFileTime(ux_modtime, pModFT);
  819. } else
  820. #endif /* NO_W32TIMES_IZFIX */
  821. {
  822. FILETIME lft;
  823. DosDateTimeToFileTime((WORD)(G.lrec.last_mod_dos_datetime >> 16),
  824. (WORD)(G.lrec.last_mod_dos_datetime & 0xFFFFL),
  825. &lft);
  826. LocalFileTimeToFileTime(&lft, pModFT);
  827. }
  828. *pAccFT = *pModFT;
  829. return (EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  830. } /* end function getNTfiletime() */
  831. /**************************/
  832. /* Function SetFileSize() */
  833. /**************************/
  834. int SetFileSize(FILE *file, zusz_t filesize)
  835. {
  836. #ifdef __RSXNT__
  837. /* RSXNT environment lacks a translation function from C file pointer
  838. to Win32-API file handle. So, simply do nothing. */
  839. return 0;
  840. #else /* !__RSXNT__ */
  841. /* not yet verified, if that really creates an unfragmented file
  842. rommel@ars.de
  843. */
  844. HANDLE os_fh;
  845. #ifdef Z_UINT8_DEFINED
  846. LARGE_INTEGER fsbuf;
  847. #endif
  848. /* Win9x supports FAT file system, only; presetting file size does
  849. not help to prevent fragmentation. */
  850. if (!IsWinNT()) return 0;
  851. /* Win32-API calls require access to the Win32 file handle.
  852. The interface function used to retrieve the Win32 handle for
  853. a file opened by the C rtl is non-standard and may not be
  854. available for every Win32 compiler environment.
  855. (see also win32/win32.c of the Zip distribution)
  856. */
  857. os_fh = (HANDLE)_get_osfhandle(fileno(file));
  858. /* move file pointer behind the last byte of the expected file size */
  859. #ifdef Z_UINT8_DEFINED
  860. fsbuf.QuadPart = filesize;
  861. if ((SetFilePointer(os_fh, fsbuf.LowPart, &fsbuf.HighPart, FILE_BEGIN)
  862. == 0xFFFFFFFF) && GetLastError() != NO_ERROR)
  863. #else
  864. if (SetFilePointer(os_fh, (ulg)filesize, 0, FILE_BEGIN) == 0xFFFFFFFF)
  865. #endif
  866. return -1;
  867. /* extend/truncate file to the current position */
  868. if (SetEndOfFile(os_fh) == 0)
  869. return -1;
  870. /* move file position pointer back to the start of the file! */
  871. return (SetFilePointer(os_fh, 0, 0, FILE_BEGIN) == 0xFFFFFFFF) ? -1 : 0;
  872. #endif /* ?__RSXNT__ */
  873. } /* end function SetFileSize() */
  874. /****************************/
  875. /* Function close_outfile() */
  876. /****************************/
  877. void close_outfile(__G)
  878. __GDEF
  879. {
  880. FILETIME Modft; /* File time type defined in NT, `last modified' time */
  881. FILETIME Accft; /* NT file time type, `last access' time */
  882. FILETIME Creft; /* NT file time type, `file creation' time */
  883. HANDLE hFile = INVALID_HANDLE_VALUE; /* File handle defined in NT */
  884. int gotTime;
  885. #ifdef NTSD_EAS
  886. uch *ebSDptr;
  887. unsigned ebSDlen;
  888. #endif
  889. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  890. char *ansi_name = (char *)alloca(strlen(G.filename) + 1);
  891. INTERN_TO_ISO(G.filename, ansi_name);
  892. # define Ansi_Fname ansi_name
  893. #else
  894. # define Ansi_Fname G.filename
  895. #endif
  896. #ifndef __RSXNT__
  897. if (IsWinNT()) {
  898. /* Truncate the file to the current position.
  899. * This is needed to remove excess allocation in case the
  900. * extraction has failed or stopped prematurely. */
  901. SetEndOfFile((HANDLE)_get_osfhandle(fileno(G.outfile)));
  902. }
  903. #endif
  904. /* Close the file and then re-open it using the Win32
  905. * CreateFile call, so that the file can be created
  906. * with GENERIC_WRITE access, otherwise the SetFileTime
  907. * call will fail. */
  908. fclose(G.outfile);
  909. /* don't set the time stamp and attributes on standard output */
  910. if (uO.cflag)
  911. return;
  912. /* skip restoring time stamps on user's request */
  913. if (uO.D_flag <= 1) {
  914. gotTime = getNTfiletime(__G__ &Modft, &Accft, &Creft);
  915. /* open a handle to the file before processing extra fields;
  916. we do this in case new security on file prevents us from updating
  917. time stamps */
  918. hFile = CreateFileA(Ansi_Fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
  919. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  920. } else {
  921. gotTime = 0;
  922. }
  923. /* sfield@microsoft.com: set attributes before time in case we decide to
  924. support other filetime members later. This also allows us to apply
  925. attributes before the security is changed, which may prevent this
  926. from succeeding otherwise. Also, since most files don't have
  927. any interesting attributes, only change them if something other than
  928. FILE_ATTRIBUTE_ARCHIVE appears in the attributes. This works well
  929. as an optimization because FILE_ATTRIBUTE_ARCHIVE gets applied to the
  930. file anyway, when it's created new. */
  931. if ((G.pInfo->file_attr & 0x7F) & ~FILE_ATTRIBUTE_ARCHIVE) {
  932. if (!SetFileAttributesA(Ansi_Fname, G.pInfo->file_attr & 0x7F))
  933. Info(slide, 1, ((char *)slide,
  934. "\nwarning (%d): could not set file attributes\n",
  935. (int)GetLastError()));
  936. }
  937. #ifdef NTSD_EAS
  938. /* set NTFS SD extra fields */
  939. if (G.extra_field && /* zipfile extra field may have extended attribs */
  940. FindSDExtraField(__G__ G.extra_field, G.lrec.extra_field_length,
  941. &ebSDptr, &ebSDlen))
  942. {
  943. int err = SetSD(__G__ Ansi_Fname, G.pInfo->file_attr,
  944. ebSDptr, ebSDlen);
  945. if (err == IZ_EF_TRUNC) {
  946. if (uO.qflag)
  947. Info(slide, 1, ((char *)slide, "%-22s ",
  948. FnFilter1(G.filename)));
  949. Info(slide, 1, ((char *)slide, LoadFarString(TruncNTSD),
  950. ebSDlen-(EB_NTSD_L_LEN+EB_CMPRHEADLEN), uO.qflag? "\n":""));
  951. }
  952. }
  953. #endif /* NTSD_EAS */
  954. /* skip restoring time stamps on user's request */
  955. if (uO.D_flag <= 1) {
  956. if ( hFile == INVALID_HANDLE_VALUE )
  957. Info(slide, 1, ((char *)slide,
  958. "\nCreateFile() error %d when trying set file time\n",
  959. (int)GetLastError()));
  960. else {
  961. if (gotTime) {
  962. FILETIME *pModft = (gotTime & EB_UT_FL_MTIME) ? &Modft : NULL;
  963. FILETIME *pAccft = (gotTime & EB_UT_FL_ATIME) ? &Accft : NULL;
  964. FILETIME *pCreft = (gotTime & EB_UT_FL_CTIME) ? &Creft : NULL;
  965. if (!SetFileTime(hFile, pCreft, pAccft, pModft))
  966. Info(slide, 0, ((char *)slide,
  967. "\nSetFileTime failed: %d\n", (int)GetLastError()));
  968. }
  969. CloseHandle(hFile);
  970. }
  971. }
  972. return;
  973. #undef Ansi_Fname
  974. } /* end function close_outfile() */
  975. #ifdef SET_DIR_ATTRIB
  976. int defer_dir_attribs(__G__ pd)
  977. __GDEF
  978. direntry **pd;
  979. {
  980. NTdirattr *d_entry;
  981. #ifdef NTSD_EAS
  982. uch *ebSDptr;
  983. unsigned ebSDlen;
  984. #endif
  985. /* Win9x does not support setting directory time stamps. */
  986. if (!IsWinNT()) {
  987. *pd = (direntry *)NULL;
  988. return PK_OK;
  989. }
  990. #ifdef NTSD_EAS
  991. /* set extended attributes from extra fields */
  992. if (G.extra_field && /* zipfile e.f. may have extended attribs */
  993. FindSDExtraField(__G__ G.extra_field, G.lrec.extra_field_length,
  994. &ebSDptr, &ebSDlen)) {
  995. /* ebSDlen contains the payload size of the e.f. block, but
  996. we store it including the e.b. header. */
  997. ebSDlen += EB_HEADSIZE;
  998. } else {
  999. /* no NTSD e.f. block -> no space needed to allocate */
  1000. ebSDlen = 0;
  1001. }
  1002. #endif /* NTSD_EAS */
  1003. d_entry = (NTdirattr *)malloc(sizeof(NTdirattr)
  1004. #ifdef NTSD_EAS
  1005. + ebSDlen
  1006. #endif
  1007. + strlen(G.filename));
  1008. *pd = (direntry *)d_entry;
  1009. if (d_entry == (NTdirattr *)NULL) {
  1010. return PK_MEM;
  1011. }
  1012. #ifdef NTSD_EAS
  1013. if (ebSDlen > 0)
  1014. memcpy(d_entry->buf, ebSDptr, ebSDlen);
  1015. d_entry->SDlen = ebSDlen;
  1016. d_entry->fn = d_entry->buf + ebSDlen;
  1017. #else
  1018. d_entry->fn = d_entry->buf;
  1019. #endif
  1020. strcpy(d_entry->fn, G.filename);
  1021. d_entry->perms = G.pInfo->file_attr;
  1022. d_entry->gotTime = (uO.D_flag <= 0
  1023. ? getNTfiletime(__G__ &(d_entry->Modft),
  1024. &(d_entry->Accft), &(d_entry->Creft))
  1025. : 0);
  1026. return PK_OK;
  1027. } /* end function defer_dir_attribs() */
  1028. int set_direc_attribs(__G__ d)
  1029. __GDEF
  1030. direntry *d;
  1031. {
  1032. int errval;
  1033. HANDLE hFile = INVALID_HANDLE_VALUE; /* File handle defined in NT */
  1034. #ifdef __RSXNT__
  1035. char *ansi_name;
  1036. #endif
  1037. /* Win9x does not support setting directory time stamps. */
  1038. if (!IsWinNT())
  1039. return PK_OK;
  1040. errval = PK_OK;
  1041. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  1042. ansi_name = (char *)alloca(strlen(d->fn) + 1);
  1043. INTERN_TO_ISO(d->fn, ansi_name);
  1044. # define Ansi_Dirname ansi_name
  1045. #else
  1046. # define Ansi_Dirname d->fn
  1047. #endif
  1048. /* Skip restoring directory time stamps on user' request. */
  1049. if (uO.D_flag <= 0) {
  1050. /* Open a handle to the directory before processing extra fields;
  1051. we do this in case new security on file prevents us from updating
  1052. time stamps.
  1053. Although the WIN32 documentation recommends to use GENERIC_WRITE
  1054. access flag to create the handle for SetFileTime(), this is too
  1055. demanding for directories with the "read-only" attribute bit set.
  1056. So we use the more specific flag FILE_WRITE_ATTRIBUTES here to
  1057. request the minimum required access rights. (This problem is a
  1058. Windows bug that has been silently fixed in Windows XP SP2.) */
  1059. hFile = CreateFileA(Ansi_Dirname, FILE_WRITE_ATTRIBUTES,
  1060. FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
  1061. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  1062. }
  1063. #ifdef NTSD_EAS
  1064. if (NtAtt(d)->SDlen > 0) {
  1065. int err;
  1066. if (QCOND2) {
  1067. Info(slide, 1, ((char *)slide, " set attrib: %-22s ",
  1068. FnFilter1(d->fn)));
  1069. }
  1070. /* set NTFS SD extra fields */
  1071. err = SetSD(__G__ Ansi_Dirname, NtAtt(d)->perms,
  1072. NtAtt(d)->buf, NtAtt(d)->SDlen - EB_HEADSIZE);
  1073. if (err == IZ_EF_TRUNC) {
  1074. if (!QCOND2)
  1075. Info(slide, 1, ((char *)slide, "%-22s ",
  1076. FnFilter1(d->fn)));
  1077. Info(slide, 1, ((char *)slide, LoadFarString(TruncNTSD),
  1078. NtAtt(d)->SDlen-(EB_NTSD_L_LEN+EB_CMPRHEADLEN), "\n"));
  1079. } else if (QCOND2) {
  1080. Info(slide, 0, ((char *)slide, "\n"));
  1081. }
  1082. if (errval < err)
  1083. errval = err;
  1084. }
  1085. #endif /* NTSD_EAS */
  1086. /* Skip restoring directory time stamps on user' request. */
  1087. if (uO.D_flag <= 0) {
  1088. if (hFile == INVALID_HANDLE_VALUE) {
  1089. Info(slide, 1, ((char *)slide,
  1090. "warning: CreateFile() error %d (set file times for %s)\n",
  1091. (int)GetLastError(), FnFilter1(d->fn)));
  1092. if (!errval)
  1093. errval = PK_WARN;
  1094. } else {
  1095. if (NtAtt(d)->gotTime) {
  1096. FILETIME *pModft = (NtAtt(d)->gotTime & EB_UT_FL_MTIME)
  1097. ? &(NtAtt(d)->Modft) : NULL;
  1098. FILETIME *pAccft = (NtAtt(d)->gotTime & EB_UT_FL_ATIME)
  1099. ? &(NtAtt(d)->Accft) : NULL;
  1100. FILETIME *pCreft = (NtAtt(d)->gotTime & EB_UT_FL_CTIME)
  1101. ? &(NtAtt(d)->Creft) : NULL;
  1102. if (!SetFileTime(hFile, pCreft, pAccft, pModft)) {
  1103. Info(slide, 0, ((char *)slide,
  1104. "warning: SetFileTime() for %s error %d\n",
  1105. FnFilter1(d->fn), (int)GetLastError()));
  1106. if (!errval)
  1107. errval = PK_WARN;
  1108. }
  1109. }
  1110. CloseHandle(hFile);
  1111. }
  1112. }
  1113. return errval;
  1114. } /* end function set_direc_attribs() */
  1115. #endif /* SET_DIR_ATTRIB */
  1116. #ifdef TIMESTAMP
  1117. /*************************/
  1118. /* Function stamp_file() */
  1119. /*************************/
  1120. int stamp_file(__GPRO__ ZCONST char *fname, time_t modtime)
  1121. {
  1122. FILETIME Modft; /* File time type defined in NT, `last modified' time */
  1123. HANDLE hFile; /* File handle defined in NT */
  1124. int errstat = 0; /* return status: 0 == "OK", -1 == "Failure" */
  1125. int fs_uses_loctime = FStampIsLocTime(__G__ fname);
  1126. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  1127. char *ansi_name = (char *)alloca(strlen(fname) + 1);
  1128. INTERN_TO_ISO(fname, ansi_name);
  1129. # define Ansi_Fname ansi_name
  1130. #else
  1131. # define Ansi_Fname fname
  1132. #endif
  1133. /* open a handle to the file to prepare setting the mod-time stamp */
  1134. hFile = CreateFileA(Ansi_Fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
  1135. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1136. if ( hFile == INVALID_HANDLE_VALUE ) {
  1137. errstat = -1;
  1138. } else {
  1139. /* convert time_t modtime into WIN32 native 64bit format */
  1140. UTIME_2_IZFILETIME(modtime, &Modft)
  1141. /* set Access and Modification times of the file to modtime */
  1142. if (!SetFileTime(hFile, NULL, &Modft, &Modft)) {
  1143. errstat = -1;
  1144. }
  1145. CloseHandle(hFile);
  1146. }
  1147. return errstat;
  1148. #undef Ansi_Fname
  1149. } /* end function stamp_file() */
  1150. #endif /* TIMESTAMP */
  1151. /***********************/
  1152. /* Function isfloppy() */ /* more precisely, is it removable? */
  1153. /***********************/
  1154. static int isfloppy(int nDrive) /* 1 == A:, 2 == B:, etc. */
  1155. {
  1156. char rootPathName[4];
  1157. rootPathName[0] = (char)('A' + nDrive - 1); /* build the root path */
  1158. rootPathName[1] = ':'; /* name, e.g. "A:/" */
  1159. rootPathName[2] = '/';
  1160. rootPathName[3] = '\0';
  1161. return (GetDriveTypeA(rootPathName) == DRIVE_REMOVABLE);
  1162. } /* end function isfloppy() */
  1163. /*****************************/
  1164. /* Function NTQueryVolInfo() */
  1165. /*****************************/
  1166. /*
  1167. * Note: 8.3 limits on filenames apply only to old-style FAT filesystems.
  1168. * More recent versions of Windows (Windows NT 3.5 / Windows 4.0)
  1169. * can support long filenames (LFN) on FAT filesystems. Check the
  1170. * filesystem maximum component length field to detect LFN support.
  1171. */
  1172. static int NTQueryVolInfo(__GPRO__ const char *name)
  1173. {
  1174. /* static char lastRootPath[4] = ""; */
  1175. /* static int lastVolOldFAT; */
  1176. /* static int lastVolLocTim; */
  1177. char *tmp0;
  1178. char tmp1[MAX_PATH], tmp2[MAX_PATH];
  1179. DWORD volSerNo, maxCompLen, fileSysFlags;
  1180. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  1181. char *ansi_name = (char *)alloca(strlen(name) + 1);
  1182. INTERN_TO_ISO(name, ansi_name);
  1183. name = ansi_name;
  1184. #endif
  1185. if ((!strncmp(name, "//", 2) || !strncmp(name, "\\\\", 2)) &&
  1186. (name[2] != '\0' && name[2] != '/' && name[2] != '\\')) {
  1187. /* GetFullPathname() and GetVolumeInformation() do not work
  1188. * on UNC names. For now, we return "error".
  1189. * **FIXME**: check if UNC name is mapped to a drive letter
  1190. * and use mapped drive for volume info query.
  1191. */
  1192. return FALSE;
  1193. }
  1194. if (isalpha((uch)name[0]) && (name[1] == ':'))
  1195. tmp0 = (char *)name;
  1196. else
  1197. {
  1198. if (!GetFullPathNameA(name, MAX_PATH, tmp1, &tmp0))
  1199. return FALSE;
  1200. tmp0 = &tmp1[0];
  1201. }
  1202. if (strncmp(G.lastRootPath, tmp0, 2) != 0) {
  1203. /* For speed, we skip repeated queries for the same device */
  1204. strncpy(G.lastRootPath, tmp0, 2); /* Build the root path name, */
  1205. G.lastRootPath[2] = '/'; /* e.g. "A:/" */
  1206. G.lastRootPath[3] = '\0';
  1207. if (!GetVolumeInformationA((LPCSTR)G.lastRootPath,
  1208. (LPSTR)tmp1, (DWORD)MAX_PATH,
  1209. &volSerNo, &maxCompLen, &fileSysFlags,
  1210. (LPSTR)tmp2, (DWORD)MAX_PATH)) {
  1211. G.lastRootPath[0] = '\0';
  1212. return FALSE;
  1213. }
  1214. /* LFNs are available if the component length is > 12 */
  1215. G.lastVolOldFAT = (maxCompLen <= 12);
  1216. /* G.lastVolOldFAT = !strncmp(strupr(tmp2), "FAT", 3); old version */
  1217. /* Volumes in (V)FAT and (OS/2) HPFS format store file timestamps in
  1218. * local time!
  1219. */
  1220. G.lastVolLocTim = !strncmp(strupr(tmp2), "VFAT", 4) ||
  1221. !strncmp(tmp2, "HPFS", 4) ||
  1222. !strncmp(tmp2, "FAT", 3);
  1223. }
  1224. return TRUE;
  1225. } /* end function NTQueryVolInfo() */
  1226. /*****************************/
  1227. /* Function IsVolumeOldFAT() */
  1228. /*****************************/
  1229. static int IsVolumeOldFAT(__GPRO__ const char *name)
  1230. {
  1231. return (NTQueryVolInfo(__G__ name) ? G.lastVolOldFAT : FALSE);
  1232. }
  1233. #ifndef SFX
  1234. /************************/
  1235. /* Function do_wild() */ /* identical to OS/2 version */
  1236. /************************/
  1237. char *do_wild(__G__ wildspec)
  1238. __GDEF
  1239. ZCONST char *wildspec; /* only used first time on a given dir */
  1240. {
  1241. /* these statics are now declared in SYSTEM_SPECIFIC_GLOBALS in w32cfg.h:
  1242. static zDIR *wild_dir = NULL;
  1243. static ZCONST char *wildname;
  1244. static char *dirname, matchname[FILNAMSIZ];
  1245. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  1246. */
  1247. char *fnamestart;
  1248. struct zdirent *file;
  1249. /* Even when we're just returning wildspec, we *always* do so in
  1250. * matchname[]--calling routine is allowed to append four characters
  1251. * to the returned string, and wildspec may be a pointer to argv[].
  1252. */
  1253. if (!G.notfirstcall) { /* first call: must initialize everything */
  1254. G.notfirstcall = TRUE;
  1255. if (!iswild(wildspec)) {
  1256. strncpy(G.matchname, wildspec, FILNAMSIZ);
  1257. G.matchname[FILNAMSIZ-1] = '\0';
  1258. G.have_dirname = FALSE;
  1259. G.wild_dir = NULL;
  1260. return G.matchname;
  1261. }
  1262. /* break the wildspec into a directory part and a wildcard filename */
  1263. if ((G.wildname = MBSRCHR(wildspec, '/')) == (ZCONST char *)NULL &&
  1264. (G.wildname = MBSRCHR(wildspec, ':')) == (ZCONST char *)NULL) {
  1265. G.dirname = ".";
  1266. G.dirnamelen = 1;
  1267. G.have_dirname = FALSE;
  1268. G.wildname = wildspec;
  1269. } else {
  1270. ++G.wildname; /* point at character after '/' or ':' */
  1271. G.dirnamelen = G.wildname - wildspec;
  1272. if ((G.dirname = (char *)malloc(G.dirnamelen+1)) == NULL) {
  1273. Info(slide, 1, ((char *)slide,
  1274. "warning: cannot allocate wildcard buffers\n"));
  1275. strncpy(G.matchname, wildspec, FILNAMSIZ);
  1276. G.matchname[FILNAMSIZ-1] = '\0';
  1277. return G.matchname; /* but maybe filespec was not a wildcard */
  1278. }
  1279. strncpy(G.dirname, wildspec, G.dirnamelen);
  1280. G.dirname[G.dirnamelen] = '\0'; /* terminate for strcpy below */
  1281. G.have_dirname = TRUE;
  1282. }
  1283. Trace((stderr, "do_wild: dirname = [%s]\n", FnFilter1(G.dirname)));
  1284. if ((G.wild_dir = (zvoid *)Opendir(G.dirname)) != NULL) {
  1285. if (G.have_dirname) {
  1286. strcpy(G.matchname, G.dirname);
  1287. fnamestart = G.matchname + G.dirnamelen;
  1288. } else
  1289. fnamestart = G.matchname;
  1290. while ((file = Readdir((zDIR *)G.wild_dir)) != NULL) {
  1291. Trace((stderr, "do_wild: Readdir returns %s\n",
  1292. FnFilter1(file->d_name)));
  1293. strcpy(fnamestart, file->d_name);
  1294. if (MBSRCHR(fnamestart, '.') == (char *)NULL)
  1295. strcat(fnamestart, ".");
  1296. if (match(fnamestart, G.wildname, TRUE WISEP) &&
  1297. /* skip "." and ".." directory entries */
  1298. strcmp(fnamestart, ".") && strcmp(fnamestart, "..")) {
  1299. Trace((stderr, "do_wild: match() succeeds\n"));
  1300. /* remove trailing dot */
  1301. fnamestart = plastchar(fnamestart, strlen(fnamestart));
  1302. if (*fnamestart == '.')
  1303. *fnamestart = '\0';
  1304. return G.matchname;
  1305. }
  1306. }
  1307. /* if we get to here directory is exhausted, so close it */
  1308. Closedir((zDIR *)G.wild_dir);
  1309. G.wild_dir = NULL;
  1310. }
  1311. Trace((stderr, "do_wild: Opendir(%s) returns NULL\n",
  1312. FnFilter1(G.dirname)));
  1313. /* return the raw wildspec in case that works (e.g., directory not
  1314. * searchable, but filespec was not wild and file is readable) */
  1315. strncpy(G.matchname, wildspec, FILNAMSIZ);
  1316. G.matchname[FILNAMSIZ-1] = '\0';
  1317. return G.matchname;
  1318. }
  1319. /* last time through, might have failed opendir but returned raw wildspec */
  1320. if (G.wild_dir == NULL) {
  1321. G.notfirstcall = FALSE; /* reset for new wildspec */
  1322. if (G.have_dirname)
  1323. free(G.dirname);
  1324. return (char *)NULL;
  1325. }
  1326. /* If we've gotten this far, we've read and matched at least one entry
  1327. * successfully (in a previous call), so dirname has been copied into
  1328. * matchname already.
  1329. */
  1330. if (G.have_dirname) {
  1331. /* strcpy(G.matchname, G.dirname); */
  1332. fnamestart = G.matchname + G.dirnamelen;
  1333. } else
  1334. fnamestart = G.matchname;
  1335. while ((file = Readdir((zDIR *)G.wild_dir)) != NULL) {
  1336. Trace((stderr, "do_wild: readdir returns %s\n",
  1337. FnFilter1(file->d_name)));
  1338. strcpy(fnamestart, file->d_name);
  1339. if (MBSRCHR(fnamestart, '.') == (char *)NULL)
  1340. strcat(fnamestart, ".");
  1341. if (match(fnamestart, G.wildname, TRUE WISEP)) {
  1342. Trace((stderr, "do_wild: match() succeeds\n"));
  1343. /* remove trailing dot */
  1344. fnamestart = plastchar(fnamestart, strlen(fnamestart));
  1345. if (*fnamestart == '.')
  1346. *fnamestart = '\0';
  1347. return G.matchname;
  1348. }
  1349. }
  1350. Closedir((zDIR *)G.wild_dir); /* at least one entry read; nothing left */
  1351. G.wild_dir = NULL;
  1352. G.notfirstcall = FALSE; /* reset for new wildspec */
  1353. if (G.have_dirname)
  1354. free(G.dirname);
  1355. return (char *)NULL;
  1356. } /* end function do_wild() */
  1357. #endif /* !SFX */
  1358. /**********************/
  1359. /* Function mapattr() */
  1360. /**********************/
  1361. /* Identical to MS-DOS, OS/2 versions. However, NT has a lot of extra
  1362. * permission stuff, so this function should probably be extended in the
  1363. * future. */
  1364. int mapattr(__G)
  1365. __GDEF
  1366. {
  1367. /* set archive bit for file entries (file is not backed up): */
  1368. G.pInfo->file_attr = ((unsigned)G.crec.external_file_attributes |
  1369. (G.crec.external_file_attributes & FILE_ATTRIBUTE_DIRECTORY ?
  1370. 0 : FILE_ATTRIBUTE_ARCHIVE)) & 0xff;
  1371. return 0;
  1372. } /* end function mapattr() */
  1373. /************************/
  1374. /* Function mapname() */
  1375. /************************/
  1376. int mapname(__G__ renamed)
  1377. __GDEF
  1378. int renamed;
  1379. /*
  1380. * returns:
  1381. * MPN_OK - no problem detected
  1382. * MPN_INF_TRUNC - caution (truncated filename)
  1383. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  1384. * MPN_ERR_SKIP - error -> skip entry
  1385. * MPN_ERR_TOOLONG - error -> path is too long
  1386. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  1387. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  1388. */
  1389. {
  1390. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  1391. char *pp, *cp=NULL; /* character pointers */
  1392. char *lastsemi = NULL; /* pointer to last semi-colon in pathcomp */
  1393. #ifdef ACORN_FTYPE_NFS
  1394. char *lastcomma=(char *)NULL; /* pointer to last comma in pathcomp */
  1395. RO_extra_block *ef_spark; /* pointer Acorn FTYPE ef block */
  1396. #endif
  1397. int killed_ddot = FALSE; /* is set when skipping "../" pathcomp */
  1398. int error;
  1399. register unsigned workch; /* hold the character being tested */
  1400. /*---------------------------------------------------------------------------
  1401. Initialize various pointers and counters and stuff.
  1402. ---------------------------------------------------------------------------*/
  1403. /* can create path as long as not just freshening, or if user told us */
  1404. G.create_dirs = (!uO.fflag || renamed);
  1405. G.created_dir = FALSE; /* not yet */
  1406. G.renamed_fullpath = FALSE;
  1407. G.fnlen = strlen(G.filename);
  1408. if (renamed) {
  1409. cp = G.filename; /* point to beginning of renamed name... */
  1410. if (*cp) do {
  1411. if (*cp == '\\') /* convert backslashes to forward */
  1412. *cp = '/';
  1413. } while (*PREINCSTR(cp));
  1414. cp = G.filename;
  1415. /* use temporary rootpath if user gave full pathname */
  1416. if (G.filename[0] == '/') {
  1417. G.renamed_fullpath = TRUE;
  1418. pathcomp[0] = '/'; /* copy the '/' and terminate */
  1419. pathcomp[1] = '\0';
  1420. ++cp;
  1421. } else if (isalpha((uch)G.filename[0]) && G.filename[1] == ':') {
  1422. G.renamed_fullpath = TRUE;
  1423. pp = pathcomp;
  1424. *pp++ = *cp++; /* copy the "d:" (+ '/', possibly) */
  1425. *pp++ = *cp++;
  1426. if (*cp == '/')
  1427. *pp++ = *cp++; /* otherwise add "./"? */
  1428. *pp = '\0';
  1429. }
  1430. }
  1431. /* pathcomp is ignored unless renamed_fullpath is TRUE: */
  1432. if ((error = checkdir(__G__ pathcomp, INIT)) != 0) /* init path buffer */
  1433. return error; /* ...unless no mem or vol label on hard disk */
  1434. *pathcomp = '\0'; /* initialize translation buffer */
  1435. pp = pathcomp; /* point to translation buffer */
  1436. if (!renamed) { /* cp already set if renamed */
  1437. if (uO.jflag) /* junking directories */
  1438. cp = (char *)MBSRCHR(G.filename, '/');
  1439. if (cp == NULL) /* no '/' or not junking dirs */
  1440. cp = G.filename; /* point to internal zipfile-member pathname */
  1441. else
  1442. ++cp; /* point to start of last component of path */
  1443. }
  1444. /*---------------------------------------------------------------------------
  1445. Begin main loop through characters in filename.
  1446. ---------------------------------------------------------------------------*/
  1447. for (; (workch = (uch)*cp) != 0; INCSTR(cp)) {
  1448. switch (workch) {
  1449. case '/': /* can assume -j flag not given */
  1450. *pp = '\0';
  1451. maskDOSdevice(__G__ pathcomp);
  1452. if (strcmp(pathcomp, ".") == 0) {
  1453. /* don't bother appending "./" to the path */
  1454. *pathcomp = '\0';
  1455. } else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
  1456. /* "../" dir traversal detected, skip over it */
  1457. *pathcomp = '\0';
  1458. killed_ddot = TRUE; /* set "show message" flag */
  1459. }
  1460. /* when path component is not empty, append it now */
  1461. if (*pathcomp != '\0' &&
  1462. ((error = checkdir(__G__ pathcomp, APPEND_DIR))
  1463. & MPN_MASK) > MPN_INF_TRUNC)
  1464. return error;
  1465. pp = pathcomp; /* reset conversion buffer for next piece */
  1466. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  1467. break;
  1468. case ':': /* drive spec not stored, so no colon allowed */
  1469. case '\\': /* '\\' may come as normal filename char (not */
  1470. case '<': /* dir sep char!) from unix-like file system */
  1471. case '>': /* no redirection symbols allowed either */
  1472. case '|': /* no pipe signs allowed */
  1473. case '"': /* no double quotes allowed */
  1474. case '?': /* no wildcards allowed */
  1475. case '*':
  1476. *pp++ = '_'; /* these rules apply equally to FAT and NTFS */
  1477. break;
  1478. case ';': /* start of VMS version? */
  1479. lastsemi = pp; /* remove VMS version later... */
  1480. *pp++ = ';'; /* but keep semicolon for now */
  1481. break;
  1482. #ifdef ACORN_FTYPE_NFS
  1483. case ',': /* NFS filetype extension */
  1484. lastcomma = pp;
  1485. *pp++ = ','; /* keep for now; may need to remove */
  1486. break; /* later, if requested */
  1487. #endif
  1488. case ' ': /* keep spaces unless specifically */
  1489. /* NT cannot create filenames with spaces on FAT volumes */
  1490. if (uO.sflag || IsVolumeOldFAT(__G__ G.filename))
  1491. *pp++ = '_';
  1492. else
  1493. *pp++ = ' ';
  1494. break;
  1495. default:
  1496. /* allow European characters in filenames: */
  1497. if (isprint(workch) || workch >= 127)
  1498. #ifdef _MBCS
  1499. {
  1500. memcpy(pp, cp, CLEN(cp));
  1501. INCSTR(pp);
  1502. }
  1503. #else
  1504. *pp++ = (char)workch;
  1505. #endif
  1506. } /* end switch */
  1507. } /* end while loop */
  1508. /* Show warning when stripping insecure "parent dir" path components */
  1509. if (killed_ddot && QCOND2) {
  1510. Info(slide, 0, ((char *)slide,
  1511. "warning: skipped \"../\" path component(s) in %s\n",
  1512. FnFilter1(G.filename)));
  1513. if (!(error & ~MPN_MASK))
  1514. error = (error & MPN_MASK) | PK_WARN;
  1515. }
  1516. /*---------------------------------------------------------------------------
  1517. Report if directory was created (and no file to create: filename ended
  1518. in '/'), check name to be sure it exists, and combine path and name be-
  1519. fore exiting.
  1520. ---------------------------------------------------------------------------*/
  1521. if (lastchar(G.filename, G.fnlen) == '/') {
  1522. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  1523. char *ansi_name = (char *)alloca(strlen(G.filename) + 1);
  1524. INTERN_TO_ISO(G.filename, ansi_name);
  1525. # define Ansi_Fname ansi_name
  1526. #else
  1527. # define Ansi_Fname G.filename
  1528. #endif
  1529. checkdir(__G__ G.filename, GETPATH);
  1530. if (G.created_dir) {
  1531. if (QCOND2) {
  1532. Info(slide, 0, ((char *)slide, " creating: %-22s\n",
  1533. FnFilter1(G.filename)));
  1534. }
  1535. /* set file attributes:
  1536. The default for newly created directories is "DIR attribute
  1537. flags set", so there is no need to change attributes unless
  1538. one of the DOS style attribute flags is set. The readonly
  1539. attribute need not be masked, since it does not prevent
  1540. modifications in the new directory. */
  1541. if(G.pInfo->file_attr & (0x7F & ~FILE_ATTRIBUTE_DIRECTORY)) {
  1542. if (!SetFileAttributesA(Ansi_Fname, G.pInfo->file_attr & 0x7F))
  1543. Info(slide, 1, ((char *)slide,
  1544. "\nwarning (%d): could not set file attributes for %s\n",
  1545. (int)GetLastError(), FnFilter1(G.filename)));
  1546. }
  1547. /* set dir time (note trailing '/') */
  1548. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  1549. } else if (IS_OVERWRT_ALL) {
  1550. /* overwrite attributes of existing directory on user's request */
  1551. /* set file attributes: */
  1552. if(G.pInfo->file_attr & (0x7F & ~FILE_ATTRIBUTE_DIRECTORY)) {
  1553. if (!SetFileAttributesA(Ansi_Fname, G.pInfo->file_attr & 0x7F))
  1554. Info(slide, 1, ((char *)slide,
  1555. "\nwarning (%d): could not set file attributes for %s\n",
  1556. (int)GetLastError(), FnFilter1(G.filename)));
  1557. }
  1558. }
  1559. /* dir existed already; don't look for data to extract */
  1560. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  1561. }
  1562. *pp = '\0'; /* done with pathcomp: terminate it */
  1563. /* if not saving them, remove VMS version numbers (appended "###") */
  1564. if (!uO.V_flag && lastsemi) {
  1565. pp = lastsemi + 1; /* semi-colon was kept: expect #'s after */
  1566. while (isdigit((uch)(*pp)))
  1567. ++pp;
  1568. if (*pp == '\0') /* only digits between ';' and end: nuke */
  1569. *lastsemi = '\0';
  1570. }
  1571. #ifdef ACORN_FTYPE_NFS
  1572. /* translate Acorn filetype information if asked to do so */
  1573. if (uO.acorn_nfs_ext &&
  1574. (ef_spark = (RO_extra_block *)
  1575. getRISCOSexfield(G.extra_field, G.lrec.extra_field_length))
  1576. != (RO_extra_block *)NULL)
  1577. {
  1578. /* file *must* have a RISC OS extra field */
  1579. long ft = (long)makelong(ef_spark->loadaddr);
  1580. /*32-bit*/
  1581. if (lastcomma) {
  1582. pp = lastcomma + 1;
  1583. while (isxdigit((uch)(*pp))) ++pp;
  1584. if (pp == lastcomma+4 && *pp == '\0') *lastcomma='\0'; /* nuke */
  1585. }
  1586. if ((ft & 1<<31)==0) ft=0x000FFD00;
  1587. sprintf(pathcomp+strlen(pathcomp), ",%03x", (int)(ft>>8) & 0xFFF);
  1588. }
  1589. #endif /* ACORN_FTYPE_NFS */
  1590. maskDOSdevice(__G__ pathcomp);
  1591. if (*pathcomp == '\0') {
  1592. Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
  1593. FnFilter1(G.filename)));
  1594. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  1595. }
  1596. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  1597. checkdir(__G__ G.filename, GETPATH);
  1598. if (G.pInfo->vollabel) { /* set the volume label now */
  1599. char drive[4];
  1600. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  1601. char *ansi_name = (char *)alloca(strlen(G.filename) + 1);
  1602. INTERN_TO_ISO(G.filename, ansi_name);
  1603. # define Ansi_Fname ansi_name
  1604. #else
  1605. # define Ansi_Fname G.filename
  1606. #endif
  1607. /* Build a drive string, e.g. "b:" */
  1608. drive[0] = (char)('a' + G.nLabelDrive - 1);
  1609. strcpy(drive + 1, ":\\");
  1610. if (QCOND2)
  1611. Info(slide, 0, ((char *)slide, "labelling %s %-22s\n", drive,
  1612. FnFilter1(G.filename)));
  1613. if (!SetVolumeLabelA(drive, Ansi_Fname)) {
  1614. Info(slide, 1, ((char *)slide,
  1615. "mapname: error setting volume label\n"));
  1616. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  1617. }
  1618. /* success: skip the "extraction" quietly */
  1619. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  1620. #undef Ansi_Fname
  1621. }
  1622. Trace((stderr, "mapname returns with filename = [%s] (error = %d)\n\n",
  1623. FnFilter1(G.filename), error));
  1624. return error;
  1625. } /* end function mapname() */
  1626. /****************************/
  1627. /* Function maskDOSdevice() */
  1628. /****************************/
  1629. static void maskDOSdevice(__G__ pathcomp)
  1630. __GDEF
  1631. char *pathcomp;
  1632. {
  1633. /*---------------------------------------------------------------------------
  1634. Put an underscore in front of the file name if the file name is a
  1635. DOS/WINDOWS device name like CON.*, AUX.*, PRN.*, etc. Trying to
  1636. extract such a file would fail at best and wedge us at worst.
  1637. ---------------------------------------------------------------------------*/
  1638. #if !defined(S_IFCHR) && defined(_S_IFCHR)
  1639. # define S_IFCHR _S_IFCHR
  1640. #endif
  1641. #if !defined(S_ISCHR)
  1642. # if defined(_S_ISCHR)
  1643. # define S_ISCHR(m) _S_ISCHR(m)
  1644. # elif defined(S_IFCHR)
  1645. # define S_ISCHR(m) ((m) & S_IFCHR)
  1646. # endif
  1647. #endif
  1648. #ifdef DEBUG
  1649. if (zstat(pathcomp, &G.statbuf) == 0) {
  1650. Trace((stderr,
  1651. "maskDOSdevice() stat(\"%s\", buf) st_mode result: %X, %o\n",
  1652. FnFilter1(pathcomp), G.statbuf.st_mode, G.statbuf.st_mode));
  1653. } else {
  1654. Trace((stderr, "maskDOSdevice() stat(\"%s\", buf) failed\n",
  1655. FnFilter1(pathcomp)));
  1656. }
  1657. #endif
  1658. if (zstat(pathcomp, &G.statbuf) == 0 && S_ISCHR(G.statbuf.st_mode)) {
  1659. extent i;
  1660. /* pathcomp contains a name of a DOS character device (builtin or
  1661. * installed device driver).
  1662. * Prepend a '_' to allow creation of the item in the file system.
  1663. */
  1664. for (i = strlen(pathcomp) + 1; i > 0; --i)
  1665. pathcomp[i] = pathcomp[i - 1];
  1666. pathcomp[0] = '_';
  1667. }
  1668. } /* end function maskDOSdevice() */
  1669. /**********************/
  1670. /* Function map2fat() */ /* Not quite identical to OS/2 version */
  1671. /**********************/
  1672. static void map2fat(pathcomp, pEndFAT)
  1673. char *pathcomp, **pEndFAT;
  1674. {
  1675. char *ppc = pathcomp; /* variable pointer to pathcomp */
  1676. char *pEnd = *pEndFAT; /* variable pointer to buildpathFAT */
  1677. char *pBegin = *pEndFAT; /* constant pointer to start of this comp. */
  1678. char *last_dot = NULL; /* last dot not converted to underscore */
  1679. register unsigned workch; /* hold the character being tested */
  1680. /* Only need check those characters which are legal in NTFS but not
  1681. * in FAT: to get here, must already have passed through mapname.
  1682. * Also must truncate path component to ensure 8.3 compliance.
  1683. */
  1684. while ((workch = (uch)*ppc++) != 0) {
  1685. switch (workch) {
  1686. case '[':
  1687. case ']':
  1688. case '+':
  1689. case ',':
  1690. case ';':
  1691. case '=':
  1692. *pEnd++ = '_'; /* convert brackets to underscores */
  1693. break;
  1694. case '.':
  1695. if (pEnd == *pEndFAT) { /* nothing appended yet... */
  1696. if (*ppc == '\0') /* don't bother appending a */
  1697. break; /* "./" component to the path */
  1698. else if (*ppc == '.' && ppc[1] == '\0') { /* "../" */
  1699. *pEnd++ = '.'; /* add first dot, */
  1700. *pEnd++ = '.'; /* add second dot, and */
  1701. ++ppc; /* skip over to pathcomp's end */
  1702. } else { /* FAT doesn't allow null filename */
  1703. *pEnd++ = '_'; /* bodies, so map .exrc -> _exrc */
  1704. } /* (_.exr would keep max 3 chars) */
  1705. } else { /* found dot within path component */
  1706. last_dot = pEnd; /* point at last dot so far... */
  1707. *pEnd++ = '_'; /* convert to underscore for now */
  1708. }
  1709. break;
  1710. default:
  1711. *pEnd++ = (char)workch;
  1712. } /* end switch */
  1713. } /* end while loop */
  1714. *pEnd = '\0'; /* terminate buildpathFAT */
  1715. /* NOTE: keep in mind that pEnd points to the end of the path
  1716. * component, and *pEndFAT still points to the *beginning* of it...
  1717. * Also note that the algorithm does not try to get too fancy:
  1718. * if there are no dots already, the name either gets truncated
  1719. * at 8 characters or the last underscore is converted to a dot
  1720. * (only if more characters are saved that way). In no case is
  1721. * a dot inserted between existing characters.
  1722. */
  1723. if (last_dot == NULL) { /* no dots: check for underscores... */
  1724. char *plu = MBSRCHR(pBegin, '_'); /* pointer to last underscore */
  1725. if ((plu != NULL) && /* found underscore: convert to dot? */
  1726. (MIN(plu - pBegin, 8) + MIN(pEnd - plu - 1, 3) > 8)) {
  1727. last_dot = plu; /* be lazy: drop through to next if-blk */
  1728. } else if ((pEnd - *pEndFAT) > 8) {
  1729. /* no underscore; or converting underscore to dot would save less
  1730. chars than leaving everything in the basename */
  1731. *pEndFAT += 8; /* truncate at 8 chars */
  1732. **pEndFAT = '\0';
  1733. } else
  1734. *pEndFAT = pEnd; /* whole thing fits into 8 chars or less */
  1735. }
  1736. if (last_dot != NULL) { /* one dot is OK: */
  1737. *last_dot = '.'; /* put it back in */
  1738. if ((last_dot - pBegin) > 8) {
  1739. char *p, *q;
  1740. int i;
  1741. p = last_dot;
  1742. q = last_dot = pBegin + 8;
  1743. for (i = 0; (i < 4) && *p; ++i) /* too many chars in basename: */
  1744. *q++ = *p++; /* shift .ext left and trun- */
  1745. *q = '\0'; /* cate/terminate it */
  1746. *pEndFAT = q;
  1747. } else if ((pEnd - last_dot) > 4) { /* too many chars in extension */
  1748. *pEndFAT = last_dot + 4;
  1749. **pEndFAT = '\0';
  1750. } else
  1751. *pEndFAT = pEnd; /* filename is fine; point at terminating zero */
  1752. if ((last_dot - pBegin) > 0 && last_dot[-1] == ' ')
  1753. last_dot[-1] = '_'; /* NO blank in front of '.'! */
  1754. }
  1755. } /* end function map2fat() */
  1756. /***********************/ /* Borrowed from os2.c for UnZip 5.1. */
  1757. /* Function checkdir() */ /* Difference: no EA stuff */
  1758. /***********************/ /* HPFS stuff works on NTFS too */
  1759. int checkdir(__G__ pathcomp, flag)
  1760. __GDEF
  1761. char *pathcomp;
  1762. int flag;
  1763. /*
  1764. * returns:
  1765. * MPN_OK - no problem detected
  1766. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  1767. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  1768. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  1769. * exists and is not a directory, but is supposed to be
  1770. * MPN_ERR_TOOLONG - path is too long
  1771. * MPN_NOMEM - can't allocate memory for filename buffers
  1772. */
  1773. {
  1774. /* static int rootlen = 0; */ /* length of rootpath */
  1775. /* static char *rootpath; */ /* user's "extract-to" directory */
  1776. /* static char *buildpathHPFS; */ /* full path (so far) to extracted file, */
  1777. /* static char *buildpathFAT; */ /* both HPFS/EA (main) and FAT versions */
  1778. /* static char *endHPFS; */ /* corresponding pointers to end of */
  1779. /* static char *endFAT; */ /* buildpath ('\0') */
  1780. # define FN_MASK 7
  1781. # define FUNCTION (flag & FN_MASK)
  1782. /*---------------------------------------------------------------------------
  1783. APPEND_DIR: append the path component to the path being built and check
  1784. for its existence. If doesn't exist and we are creating directories, do
  1785. so for this one; else signal success or error as appropriate.
  1786. ---------------------------------------------------------------------------*/
  1787. if (FUNCTION == APPEND_DIR) {
  1788. char *p = pathcomp;
  1789. int too_long = FALSE;
  1790. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  1791. while ((*G.endHPFS = *p++) != '\0') /* copy to HPFS filename */
  1792. ++G.endHPFS;
  1793. if (!IsVolumeOldFAT(__G__ G.buildpathHPFS)) {
  1794. p = pathcomp;
  1795. while ((*G.endFAT = *p++) != '\0') /* copy to FAT filename, too */
  1796. ++G.endFAT;
  1797. } else
  1798. map2fat(pathcomp, &G.endFAT); /* map into FAT fn, update endFAT */
  1799. /* GRR: could do better check, see if overrunning buffer as we go:
  1800. * check endHPFS-buildpathHPFS after each append, set warning variable
  1801. * if within 20 of FILNAMSIZ; then if var set, do careful check when
  1802. * appending. Clear variable when begin new path. */
  1803. /* next check: need to append '/', at least one-char name, '\0' */
  1804. if ((G.endHPFS-G.buildpathHPFS) > FILNAMSIZ-3)
  1805. too_long = TRUE; /* check if extracting dir? */
  1806. #ifdef FIX_STAT_BUG
  1807. /* Borland C++ 5.0 does not handle a call to stat() well if the
  1808. * directory does not exist (it tends to crash in strange places.)
  1809. * This is apparently a problem only when compiling for GUI rather
  1810. * than console. The code below attempts to work around this problem.
  1811. */
  1812. if (access(G.buildpathFAT, 0) != 0) {
  1813. if (!G.create_dirs) { /* told not to create (freshening) */
  1814. free(G.buildpathHPFS);
  1815. free(G.buildpathFAT);
  1816. /* path doesn't exist: nothing to do */
  1817. return MPN_INF_SKIP;
  1818. }
  1819. if (too_long) { /* GRR: should allow FAT extraction w/o EAs */
  1820. Info(slide, 1, ((char *)slide,
  1821. "checkdir error: path too long: %s\n",
  1822. FnFilter1(G.buildpathHPFS)));
  1823. free(G.buildpathHPFS);
  1824. free(G.buildpathFAT);
  1825. /* no room for filenames: fatal */
  1826. return MPN_ERR_TOOLONG;
  1827. }
  1828. if (MKDIR(G.buildpathFAT, 0777) == -1) { /* create the directory */
  1829. Info(slide, 1, ((char *)slide,
  1830. "checkdir error: cannot create %s\n\
  1831. %s\n\
  1832. unable to process %s.\n",
  1833. FnFilter2(G.buildpathFAT),
  1834. strerror(errno),
  1835. FnFilter1(G.filename)));
  1836. free(G.buildpathHPFS);
  1837. free(G.buildpathFAT);
  1838. /* path didn't exist, tried to create, failed */
  1839. return MPN_ERR_SKIP;
  1840. }
  1841. G.created_dir = TRUE;
  1842. }
  1843. #endif /* FIX_STAT_BUG */
  1844. if (SSTAT(G.buildpathFAT, &G.statbuf)) /* path doesn't exist */
  1845. {
  1846. if (!G.create_dirs) { /* told not to create (freshening) */
  1847. free(G.buildpathHPFS);
  1848. free(G.buildpathFAT);
  1849. /* path doesn't exist: nothing to do */
  1850. return MPN_INF_SKIP;
  1851. }
  1852. if (too_long) { /* GRR: should allow FAT extraction w/o EAs */
  1853. Info(slide, 1, ((char *)slide,
  1854. "checkdir error: path too long: %s\n",
  1855. FnFilter1(G.buildpathHPFS)));
  1856. free(G.buildpathHPFS);
  1857. free(G.buildpathFAT);
  1858. /* no room for filenames: fatal */
  1859. return MPN_ERR_TOOLONG;
  1860. }
  1861. if (MKDIR(G.buildpathFAT, 0777) == -1) { /* create the directory */
  1862. Info(slide, 1, ((char *)slide,
  1863. "checkdir error: cannot create %s\n\
  1864. %s\n\
  1865. unable to process %s.\n",
  1866. FnFilter2(G.buildpathFAT),
  1867. strerror(errno),
  1868. FnFilter1(G.filename)));
  1869. free(G.buildpathHPFS);
  1870. free(G.buildpathFAT);
  1871. /* path didn't exist, tried to create, failed */
  1872. return MPN_ERR_SKIP;
  1873. }
  1874. G.created_dir = TRUE;
  1875. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  1876. Info(slide, 1, ((char *)slide,
  1877. "checkdir error: %s exists but is not directory\n\
  1878. unable to process %s.\n",
  1879. FnFilter2(G.buildpathFAT), FnFilter1(G.filename)));
  1880. free(G.buildpathHPFS);
  1881. free(G.buildpathFAT);
  1882. /* path existed but wasn't dir */
  1883. return MPN_ERR_SKIP;
  1884. }
  1885. if (too_long) {
  1886. Info(slide, 1, ((char *)slide,
  1887. "checkdir error: path too long: %s\n",
  1888. FnFilter1(G.buildpathHPFS)));
  1889. free(G.buildpathHPFS);
  1890. free(G.buildpathFAT);
  1891. /* no room for filenames: fatal */
  1892. return MPN_ERR_TOOLONG;
  1893. }
  1894. *G.endHPFS++ = '/';
  1895. *G.endFAT++ = '/';
  1896. *G.endHPFS = *G.endFAT = '\0';
  1897. Trace((stderr, "buildpathHPFS now = [%s]\nbuildpathFAT now = [%s]\n",
  1898. FnFilter1(G.buildpathHPFS), FnFilter2(G.buildpathFAT)));
  1899. return MPN_OK;
  1900. } /* end if (FUNCTION == APPEND_DIR) */
  1901. /*---------------------------------------------------------------------------
  1902. GETPATH: copy full FAT path to the string pointed at by pathcomp (want
  1903. filename to reflect name used on disk, not EAs; if full path is HPFS,
  1904. buildpathFAT and buildpathHPFS will be identical). Also free both paths.
  1905. ---------------------------------------------------------------------------*/
  1906. if (FUNCTION == GETPATH) {
  1907. Trace((stderr, "getting and freeing FAT path [%s]\n",
  1908. FnFilter1(G.buildpathFAT)));
  1909. Trace((stderr, "freeing HPFS path [%s]\n",
  1910. FnFilter1(G.buildpathHPFS)));
  1911. strcpy(pathcomp, G.buildpathFAT);
  1912. free(G.buildpathFAT);
  1913. free(G.buildpathHPFS);
  1914. G.buildpathHPFS = G.buildpathFAT = G.endHPFS = G.endFAT = NULL;
  1915. return MPN_OK;
  1916. }
  1917. /*---------------------------------------------------------------------------
  1918. APPEND_NAME: assume the path component is the filename; append it and
  1919. return without checking for existence.
  1920. ---------------------------------------------------------------------------*/
  1921. if (FUNCTION == APPEND_NAME) {
  1922. char *p = pathcomp;
  1923. int error = MPN_OK;
  1924. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  1925. /* The buildpathHPFS buffer has been allocated large enough to
  1926. * hold the complete combined name, so there is no need to check
  1927. * for OS filename size limit overflow within the copy loop.
  1928. */
  1929. while ((*G.endHPFS = *p++) != '\0') { /* copy to HPFS filename */
  1930. ++G.endHPFS;
  1931. }
  1932. /* Now, check for OS filename size overflow. When detected, the
  1933. * mapped HPFS name is truncated and a warning message is shown.
  1934. */
  1935. if ((G.endHPFS-G.buildpathHPFS) >= FILNAMSIZ) {
  1936. G.buildpathHPFS[FILNAMSIZ-1] = '\0';
  1937. Info(slide, 1, ((char *)slide,
  1938. "checkdir warning: path too long; truncating\n \
  1939. %s\n -> %s\n",
  1940. FnFilter1(G.filename), FnFilter2(G.buildpathHPFS)));
  1941. error = MPN_INF_TRUNC; /* filename truncated */
  1942. }
  1943. /* The buildpathFAT buffer has the same allocated size as the
  1944. * buildpathHPFS buffer, so there is no need for an overflow check
  1945. * within the following copy loop, either.
  1946. */
  1947. if (G.pInfo->vollabel || !IsVolumeOldFAT(__G__ G.buildpathHPFS)) {
  1948. /* copy to FAT filename, too */
  1949. p = pathcomp;
  1950. while ((*G.endFAT = *p++) != '\0')
  1951. ++G.endFAT;
  1952. } else
  1953. /* map into FAT fn, update endFAT */
  1954. map2fat(pathcomp, &G.endFAT);
  1955. /* Check that the FAT path does not exceed the FILNAMSIZ limit, and
  1956. * truncate when neccessary.
  1957. * Note that truncation can only happen when the HPFS path (which is
  1958. * never shorter than the FAT path) has been already truncated.
  1959. * So, emission of the warning message and setting the error code
  1960. * has already happened.
  1961. */
  1962. if ((G.endFAT-G.buildpathFAT) >= FILNAMSIZ)
  1963. G.buildpathFAT[FILNAMSIZ-1] = '\0';
  1964. Trace((stderr, "buildpathHPFS: %s\nbuildpathFAT: %s\n",
  1965. FnFilter1(G.buildpathHPFS), FnFilter2(G.buildpathFAT)));
  1966. return error; /* could check for existence, prompt for new name... */
  1967. } /* end if (FUNCTION == APPEND_NAME) */
  1968. /*---------------------------------------------------------------------------
  1969. INIT: allocate and initialize buffer space for the file currently being
  1970. extracted. If file was renamed with an absolute path, don't prepend the
  1971. extract-to path.
  1972. ---------------------------------------------------------------------------*/
  1973. if (FUNCTION == INIT) {
  1974. Trace((stderr, "initializing buildpathHPFS and buildpathFAT to "));
  1975. #ifdef ACORN_FTYPE_NFS
  1976. if ((G.buildpathHPFS = (char *)malloc(G.fnlen+G.rootlen+
  1977. (uO.acorn_nfs_ext ? 5 : 1)))
  1978. #else
  1979. if ((G.buildpathHPFS = (char *)malloc(G.fnlen+G.rootlen+1))
  1980. #endif
  1981. == NULL)
  1982. return MPN_NOMEM;
  1983. #ifdef ACORN_FTYPE_NFS
  1984. if ((G.buildpathFAT = (char *)malloc(G.fnlen+G.rootlen+
  1985. (uO.acorn_nfs_ext ? 5 : 1)))
  1986. #else
  1987. if ((G.buildpathFAT = (char *)malloc(G.fnlen+G.rootlen+1))
  1988. #endif
  1989. == NULL) {
  1990. free(G.buildpathHPFS);
  1991. return MPN_NOMEM;
  1992. }
  1993. if (G.pInfo->vollabel) { /* use root or renamed path, but don't store */
  1994. /* GRR: for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */
  1995. if (G.renamed_fullpath && pathcomp[1] == ':')
  1996. *G.buildpathHPFS = (char)ToLower(*pathcomp);
  1997. else if (!G.renamed_fullpath && G.rootlen > 1 &&
  1998. G.rootpath[1] == ':')
  1999. *G.buildpathHPFS = (char)ToLower(*G.rootpath);
  2000. else {
  2001. char tmpN[MAX_PATH], *tmpP;
  2002. if (GetFullPathNameA(".", MAX_PATH, tmpN, &tmpP) > MAX_PATH)
  2003. { /* by definition of MAX_PATH we should never get here */
  2004. Info(slide, 1, ((char *)slide,
  2005. "checkdir warning: current dir path too long\n"));
  2006. return MPN_INF_TRUNC; /* can't get drive letter */
  2007. }
  2008. G.nLabelDrive = *tmpN - 'a' + 1;
  2009. *G.buildpathHPFS = (char)(G.nLabelDrive - 1 + 'a');
  2010. }
  2011. G.nLabelDrive = *G.buildpathHPFS - 'a' + 1; /* save for mapname() */
  2012. if (uO.volflag == 0 || *G.buildpathHPFS < 'a' /* no labels/bogus? */
  2013. || (uO.volflag == 1 && !isfloppy(G.nLabelDrive))) { /* !fixed */
  2014. free(G.buildpathHPFS);
  2015. free(G.buildpathFAT);
  2016. return MPN_VOL_LABEL; /* skipping with message */
  2017. }
  2018. *G.buildpathHPFS = '\0';
  2019. } else if (G.renamed_fullpath) /* pathcomp = valid data */
  2020. strcpy(G.buildpathHPFS, pathcomp);
  2021. else if (G.rootlen > 0)
  2022. strcpy(G.buildpathHPFS, G.rootpath);
  2023. else
  2024. *G.buildpathHPFS = '\0';
  2025. G.endHPFS = G.buildpathHPFS;
  2026. G.endFAT = G.buildpathFAT;
  2027. while ((*G.endFAT = *G.endHPFS) != '\0') {
  2028. ++G.endFAT;
  2029. ++G.endHPFS;
  2030. }
  2031. Trace((stderr, "[%s]\n", FnFilter1(G.buildpathHPFS)));
  2032. return MPN_OK;
  2033. }
  2034. /*---------------------------------------------------------------------------
  2035. ROOT: if appropriate, store the path in rootpath and create it if neces-
  2036. sary; else assume it's a zipfile member and return. This path segment
  2037. gets used in extracting all members from every zipfile specified on the
  2038. command line. Note that under OS/2 and MS-DOS, if a candidate extract-to
  2039. directory specification includes a drive letter (leading "x:"), it is
  2040. treated just as if it had a trailing '/'--that is, one directory level
  2041. will be created if the path doesn't exist, unless this is otherwise pro-
  2042. hibited (e.g., freshening).
  2043. ---------------------------------------------------------------------------*/
  2044. #if (!defined(SFX) || defined(SFX_EXDIR))
  2045. if (FUNCTION == ROOT) {
  2046. Trace((stderr, "initializing root path to [%s]\n",
  2047. FnFilter1(pathcomp)));
  2048. if (pathcomp == NULL) {
  2049. G.rootlen = 0;
  2050. return MPN_OK;
  2051. }
  2052. if (G.rootlen > 0) /* rootpath was already set, nothing to do */
  2053. return MPN_OK;
  2054. if ((G.rootlen = strlen(pathcomp)) > 0) {
  2055. int had_trailing_pathsep=FALSE, has_drive=FALSE, add_dot=FALSE;
  2056. char *tmproot;
  2057. if ((tmproot = (char *)malloc(G.rootlen+3)) == (char *)NULL) {
  2058. G.rootlen = 0;
  2059. return MPN_NOMEM;
  2060. }
  2061. strcpy(tmproot, pathcomp);
  2062. if (isalpha((uch)tmproot[0]) && tmproot[1] == ':')
  2063. has_drive = TRUE; /* drive designator */
  2064. if (tmproot[G.rootlen-1] == '/' || tmproot[G.rootlen-1] == '\\') {
  2065. tmproot[--G.rootlen] = '\0';
  2066. had_trailing_pathsep = TRUE;
  2067. }
  2068. if (has_drive && (G.rootlen == 2)) {
  2069. if (!had_trailing_pathsep) /* i.e., original wasn't "x:/" */
  2070. add_dot = TRUE; /* relative path: add '.' before '/' */
  2071. } else if (G.rootlen > 0) { /* need not check "x:." and "x:/" */
  2072. if (SSTAT(tmproot, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  2073. {
  2074. /* path does not exist */
  2075. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  2076. free(tmproot);
  2077. G.rootlen = 0;
  2078. /* treat as stored file */
  2079. return MPN_INF_SKIP;
  2080. }
  2081. /* create directory (could add loop here scanning tmproot
  2082. * to create more than one level, but really necessary?) */
  2083. if (MKDIR(tmproot, 0777) == -1) {
  2084. Info(slide, 1, ((char *)slide,
  2085. "checkdir: cannot create extraction directory: %s\n",
  2086. FnFilter1(tmproot)));
  2087. free(tmproot);
  2088. G.rootlen = 0;
  2089. /* path didn't exist, tried to create, failed: */
  2090. /* file exists, or need 2+ subdir levels */
  2091. return MPN_ERR_SKIP;
  2092. }
  2093. }
  2094. }
  2095. if (add_dot) /* had just "x:", make "x:." */
  2096. tmproot[G.rootlen++] = '.';
  2097. tmproot[G.rootlen++] = '/';
  2098. tmproot[G.rootlen] = '\0';
  2099. if ((G.rootpath = (char *)realloc(tmproot, G.rootlen+1)) == NULL) {
  2100. free(tmproot);
  2101. G.rootlen = 0;
  2102. return MPN_NOMEM;
  2103. }
  2104. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(G.rootpath)));
  2105. }
  2106. return MPN_OK;
  2107. }
  2108. #endif /* !SFX || SFX_EXDIR */
  2109. /*---------------------------------------------------------------------------
  2110. END: free rootpath, immediately prior to program exit.
  2111. ---------------------------------------------------------------------------*/
  2112. if (FUNCTION == END) {
  2113. Trace((stderr, "freeing rootpath\n"));
  2114. if (G.rootlen > 0) {
  2115. free(G.rootpath);
  2116. G.rootlen = 0;
  2117. }
  2118. return MPN_OK;
  2119. }
  2120. return MPN_INVALID; /* should never reach */
  2121. } /* end function checkdir() */
  2122. #ifndef SFX
  2123. /*************************/
  2124. /* Function dateformat() */
  2125. /*************************/
  2126. int dateformat()
  2127. {
  2128. char df[2]; /* LOCALE_IDATE has a maximum value of 2 */
  2129. if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, df, 2) != 0) {
  2130. switch (df[0])
  2131. {
  2132. case '0':
  2133. return DF_MDY;
  2134. case '1':
  2135. return DF_DMY;
  2136. case '2':
  2137. return DF_YMD;
  2138. }
  2139. }
  2140. return DF_MDY;
  2141. }
  2142. /****************************/
  2143. /* Function dateseparator() */
  2144. /****************************/
  2145. char dateseparator()
  2146. {
  2147. char df[2]; /* use only if it is one character */
  2148. if ((GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, df, 2) != 0) &&
  2149. (df[0] != '\0'))
  2150. return df[0];
  2151. else
  2152. return '-';
  2153. }
  2154. #ifndef WINDLL
  2155. /************************/
  2156. /* Function version() */
  2157. /************************/
  2158. void version(__G)
  2159. __GDEF
  2160. {
  2161. int len;
  2162. #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DJGPP__))
  2163. char buf[80];
  2164. #if (defined(_MSC_VER) && (_MSC_VER > 900))
  2165. char buf2[80];
  2166. #endif
  2167. #endif
  2168. len = sprintf((char *)slide, CompiledWith,
  2169. #if defined(_MSC_VER) /* MSC == VC++, but what about SDK compiler? */
  2170. (sprintf(buf, "Microsoft C %d.%02d ", _MSC_VER/100, _MSC_VER%100), buf),
  2171. # if (_MSC_VER == 800)
  2172. "(Visual C++ v1.1)",
  2173. # elif (_MSC_VER == 850)
  2174. "(Windows NT v3.5 SDK)",
  2175. # elif (_MSC_VER == 900)
  2176. "(Visual C++ v2.x)",
  2177. # elif (_MSC_VER > 900)
  2178. (sprintf(buf2, "(Visual C++ %d.%d)", _MSC_VER/100 - 6, _MSC_VER%100/10),
  2179. buf2),
  2180. # else
  2181. "(bad version)",
  2182. # endif
  2183. #elif defined(__WATCOMC__)
  2184. # if (__WATCOMC__ % 10 > 0)
  2185. (sprintf(buf, "Watcom C/C++ %d.%02d", __WATCOMC__ / 100,
  2186. __WATCOMC__ % 100), buf), "",
  2187. # else
  2188. (sprintf(buf, "Watcom C/C++ %d.%d", __WATCOMC__ / 100,
  2189. (__WATCOMC__ % 100) / 10), buf), "",
  2190. # endif
  2191. #elif defined(__BORLANDC__)
  2192. "Borland C++",
  2193. # if (__BORLANDC__ < 0x0200)
  2194. " 1.0",
  2195. # elif (__BORLANDC__ == 0x0200)
  2196. " 2.0",
  2197. # elif (__BORLANDC__ == 0x0400)
  2198. " 3.0",
  2199. # elif (__BORLANDC__ == 0x0410) /* __TURBOC__ = 0x0310 */
  2200. " 3.1",
  2201. # elif (__BORLANDC__ == 0x0452) /* __TURBOC__ = 0x0320 */
  2202. " 4.0 or 4.02",
  2203. # elif (__BORLANDC__ == 0x0460) /* __TURBOC__ = 0x0340 */
  2204. " 4.5",
  2205. # elif (__BORLANDC__ == 0x0500) /* __TURBOC__ = 0x0340 */
  2206. " 5.0",
  2207. # elif (__BORLANDC__ == 0x0520) /* __TURBOC__ = 0x0520 */
  2208. " 5.2 (C++ Builder 1.0)",
  2209. # elif (__BORLANDC__ == 0x0530) /* __TURBOC__ = 0x0530 */
  2210. " 5.3 (C++ Builder 3.0)",
  2211. # elif (__BORLANDC__ == 0x0540) /* __TURBOC__ = 0x0540 */
  2212. " 5.4 (C++ Builder 4.0)",
  2213. # elif (__BORLANDC__ == 0x0550) /* __TURBOC__ = 0x0550 */
  2214. " 5.5 (C++ Builder 5.0)",
  2215. # elif (__BORLANDC__ == 0x0551) /* __TURBOC__ = 0x0551 */
  2216. " 5.5.1 (C++ Builder 5.0.1)",
  2217. # elif (__BORLANDC__ == 0x0560) /* __TURBOC__ = 0x0560 */
  2218. " 6.0 (C++ Builder 6.0)",
  2219. # else
  2220. " later than 6.0",
  2221. # endif
  2222. #elif defined(__LCC__)
  2223. "LCC-Win32", "",
  2224. #elif defined(__GNUC__)
  2225. # if defined(__RSXNT__)
  2226. # if (defined(__DJGPP__) && !defined(__EMX__))
  2227. (sprintf(buf, "rsxnt(djgpp v%d.%02d) / gcc ",
  2228. __DJGPP__, __DJGPP_MINOR__), buf),
  2229. # elif defined(__DJGPP__)
  2230. (sprintf(buf, "rsxnt(emx+djgpp v%d.%02d) / gcc ",
  2231. __DJGPP__, __DJGPP_MINOR__), buf),
  2232. # elif (defined(__GO32__) && !defined(__EMX__))
  2233. "rsxnt(djgpp v1.x) / gcc ",
  2234. # elif defined(__GO32__)
  2235. "rsxnt(emx + djgpp v1.x) / gcc ",
  2236. # elif defined(__EMX__)
  2237. "rsxnt(emx)+gcc ",
  2238. # else
  2239. "rsxnt(unknown) / gcc ",
  2240. # endif
  2241. # elif defined(__CYGWIN__)
  2242. "cygnus win32 / gcc ",
  2243. # elif defined(__MINGW32__)
  2244. "mingw32 / gcc ",
  2245. # else
  2246. "gcc ",
  2247. # endif
  2248. __VERSION__,
  2249. #else /* !_MSC_VER, !__WATCOMC__, !__BORLANDC__, !__LCC__, !__GNUC__ */
  2250. "unknown compiler (SDK?)", "",
  2251. #endif /* ?compilers */
  2252. "\nWindows 9x / Windows NT/2K/XP/2K3", " (32-bit)",
  2253. #ifdef __DATE__
  2254. " on ", __DATE__
  2255. #else
  2256. "", ""
  2257. #endif
  2258. );
  2259. (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  2260. return;
  2261. } /* end function version() */
  2262. #endif /* !WINDLL */
  2263. #endif /* !SFX */
  2264. #ifdef MORE
  2265. int screensize(int *tt_rows, int *tt_cols)
  2266. {
  2267. HANDLE hstdout;
  2268. CONSOLE_SCREEN_BUFFER_INFO scr;
  2269. hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
  2270. GetConsoleScreenBufferInfo(hstdout, &scr);
  2271. if (tt_rows != NULL) *tt_rows = scr.srWindow.Bottom - scr.srWindow.Top + 1;
  2272. if (tt_cols != NULL) *tt_cols = scr.srWindow.Right - scr.srWindow.Left + 1;
  2273. return 0; /* signal success */
  2274. }
  2275. #endif /* MORE */
  2276. #ifdef W32_STAT_BANDAID
  2277. /* All currently known variants of WIN32 operating systems (Windows 95/98,
  2278. * WinNT 3.x, 4.0, 5.x) have a nasty bug in the OS kernel concerning
  2279. * conversions between UTC and local time: In the time conversion functions
  2280. * of the Win32 API, the timezone offset (including seasonal daylight saving
  2281. * shift) between UTC and local time evaluation is erratically based on the
  2282. * current system time. The correct evaluation must determine the offset
  2283. * value as it {was/is/will be} for the actual time to be converted.
  2284. *
  2285. * Newer versions of MS C runtime lib's stat() returns utc time-stamps so
  2286. * that localtime(timestamp) corresponds to the (potentially false) local
  2287. * time shown by the OS' system programs (Explorer, command shell dir, etc.)
  2288. * The RSXNT port follows the same strategy, but fails to recognize the
  2289. * access-time attribute.
  2290. *
  2291. * For the NTFS file system (and other filesystems that store time-stamps
  2292. * as UTC values), this results in st_mtime (, st_{c|a}time) fields which
  2293. * are not stable but vary according to the seasonal change of "daylight
  2294. * saving time in effect / not in effect".
  2295. *
  2296. * Other C runtime libs (CygWin), or the crtdll.dll supplied with Win9x/NT
  2297. * return the unix-time equivalent of the UTC FILETIME values as got back
  2298. * from the Win32 API call. This time, return values from NTFS are correct
  2299. * whereas utimes from files on (V)FAT volumes vary according to the DST
  2300. * switches.
  2301. *
  2302. * To achieve timestamp consistency of UTC (UT extra field) values in
  2303. * Zip archives, the Info-ZIP programs require work-around code for
  2304. * proper time handling in stat() (and other time handling routines).
  2305. *
  2306. * However, nowadays most other programs on Windows systems use the
  2307. * time conversion strategy of Microsofts C runtime lib "msvcrt.dll".
  2308. * To improve interoperability in environments where a "consistent" (but
  2309. * false) "UTC<-->LocalTime" conversion is preferred over "stable" time
  2310. * stamps, the Info-ZIP specific time conversion handling can be
  2311. * deactivated by defining the preprocessor flag NO_W32TIMES_IZFIX.
  2312. */
  2313. /* stat() functions under Windows95 tend to fail for root directories. *
  2314. * Watcom and Borland, at least, are affected by this bug. Watcom made *
  2315. * a partial fix for 11.0 but still missed some cases. This substitute *
  2316. * detects the case and fills in reasonable values. Otherwise we get *
  2317. * effects like failure to extract to a root dir because it's not found. */
  2318. int zstat_win32(__W32STAT_GLOBALS__ const char *path, z_stat *buf)
  2319. {
  2320. if (!zstat(path, buf))
  2321. {
  2322. /* stat was successful, now redo the time-stamp fetches */
  2323. #ifndef NO_W32TIMES_IZFIX
  2324. int fs_uses_loctime = FStampIsLocTime(__G__ path);
  2325. #endif
  2326. HANDLE h;
  2327. FILETIME Modft, Accft, Creft;
  2328. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  2329. char *ansi_path = (char *)alloca(strlen(path) + 1);
  2330. INTERN_TO_ISO(path, ansi_path);
  2331. # define Ansi_Path ansi_path
  2332. #else
  2333. # define Ansi_Path path
  2334. #endif
  2335. TTrace((stdout, "stat(%s) finds modtime %08lx\n", path, buf->st_mtime));
  2336. h = CreateFileA(Ansi_Path, GENERIC_READ,
  2337. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  2338. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  2339. if (h != INVALID_HANDLE_VALUE) {
  2340. BOOL ftOK = GetFileTime(h, &Creft, &Accft, &Modft);
  2341. CloseHandle(h);
  2342. if (ftOK) {
  2343. FTTrace((stdout, "GetFileTime returned Modft", 0, &Modft));
  2344. FTTrace((stdout, "GetFileTime returned Creft", 0, &Creft));
  2345. #ifndef NO_W32TIMES_IZFIX
  2346. if (!fs_uses_loctime) {
  2347. /* On a filesystem that stores UTC timestamps, we refill
  2348. * the time fields of the struct stat buffer by directly
  2349. * using the UTC values as returned by the Win32
  2350. * GetFileTime() API call.
  2351. */
  2352. NtfsFileTime2utime(&Modft, &(buf->st_mtime));
  2353. if (Accft.dwLowDateTime != 0 || Accft.dwHighDateTime != 0)
  2354. NtfsFileTime2utime(&Accft, &(buf->st_atime));
  2355. else
  2356. buf->st_atime = buf->st_mtime;
  2357. if (Creft.dwLowDateTime != 0 || Creft.dwHighDateTime != 0)
  2358. NtfsFileTime2utime(&Creft, &(buf->st_ctime));
  2359. else
  2360. buf->st_ctime = buf->st_mtime;
  2361. TTrace((stdout,"NTFS, recalculated modtime %08lx\n",
  2362. buf->st_mtime));
  2363. } else
  2364. #endif /* NO_W32TIMES_IZFIX */
  2365. {
  2366. /* On VFAT and FAT-like filesystems, the FILETIME values
  2367. * are converted back to the stable local time before
  2368. * converting them to UTC unix time-stamps.
  2369. */
  2370. VFatFileTime2utime(&Modft, &(buf->st_mtime));
  2371. if (Accft.dwLowDateTime != 0 || Accft.dwHighDateTime != 0)
  2372. VFatFileTime2utime(&Accft, &(buf->st_atime));
  2373. else
  2374. buf->st_atime = buf->st_mtime;
  2375. if (Creft.dwLowDateTime != 0 || Creft.dwHighDateTime != 0)
  2376. VFatFileTime2utime(&Creft, &(buf->st_ctime));
  2377. else
  2378. buf->st_ctime = buf->st_mtime;
  2379. TTrace((stdout, "VFAT, recalculated modtime %08lx\n",
  2380. buf->st_mtime));
  2381. }
  2382. }
  2383. }
  2384. # undef Ansi_Path
  2385. return 0;
  2386. }
  2387. #ifdef W32_STATROOT_FIX
  2388. else
  2389. {
  2390. DWORD flags;
  2391. #ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */
  2392. char *ansi_path = (char *)alloca(strlen(path) + 1);
  2393. INTERN_TO_ISO(path, ansi_path);
  2394. # define Ansi_Path ansi_path
  2395. #else
  2396. # define Ansi_Path path
  2397. #endif
  2398. flags = GetFileAttributesA(Ansi_Path);
  2399. if (flags != 0xFFFFFFFF && flags & FILE_ATTRIBUTE_DIRECTORY) {
  2400. Trace((stderr, "\nstat(\"%s\",...) failed on existing directory\n",
  2401. FnFilter1(path)));
  2402. memset(buf, 0, sizeof(z_stat));
  2403. buf->st_atime = buf->st_ctime = buf->st_mtime =
  2404. dos_to_unix_time(DOSTIME_MINIMUM); /* 1-1-80 */
  2405. buf->st_mode = S_IFDIR | S_IREAD |
  2406. ((flags & FILE_ATTRIBUTE_READONLY) ? 0 : S_IWRITE);
  2407. return 0;
  2408. } /* assumes: stat() won't fail on non-dirs without good reason */
  2409. # undef Ansi_Path
  2410. }
  2411. #endif /* W32_STATROOT_FIX */
  2412. return -1;
  2413. }
  2414. #endif /* W32_STAT_BANDAID */
  2415. #ifdef W32_USE_IZ_TIMEZONE
  2416. #include "timezone.h"
  2417. #define SECSPERMIN 60
  2418. #define MINSPERHOUR 60
  2419. #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
  2420. static void conv_to_rule(LPSYSTEMTIME lpw32tm, struct rule * ZCONST ptrule);
  2421. static void conv_to_rule(LPSYSTEMTIME lpw32tm, struct rule * ZCONST ptrule)
  2422. {
  2423. if (lpw32tm->wYear != 0) {
  2424. ptrule->r_type = JULIAN_DAY;
  2425. ptrule->r_day = ydays[lpw32tm->wMonth - 1] + lpw32tm->wDay;
  2426. } else {
  2427. ptrule->r_type = MONTH_NTH_DAY_OF_WEEK;
  2428. ptrule->r_mon = lpw32tm->wMonth;
  2429. ptrule->r_day = lpw32tm->wDayOfWeek;
  2430. ptrule->r_week = lpw32tm->wDay;
  2431. }
  2432. ptrule->r_time = (long)lpw32tm->wHour * SECSPERHOUR +
  2433. (long)(lpw32tm->wMinute * SECSPERMIN) +
  2434. (long)lpw32tm->wSecond;
  2435. }
  2436. int GetPlatformLocalTimezone(register struct state * ZCONST sp,
  2437. void (*fill_tzstate_from_rules)(struct state * ZCONST sp_res,
  2438. ZCONST struct rule * ZCONST start,
  2439. ZCONST struct rule * ZCONST end))
  2440. {
  2441. TIME_ZONE_INFORMATION tzinfo;
  2442. DWORD res;
  2443. /* read current timezone settings from registry if TZ envvar missing */
  2444. res = GetTimeZoneInformation(&tzinfo);
  2445. if (res != TIME_ZONE_ID_INVALID)
  2446. {
  2447. struct rule startrule, stoprule;
  2448. conv_to_rule(&(tzinfo.StandardDate), &stoprule);
  2449. conv_to_rule(&(tzinfo.DaylightDate), &startrule);
  2450. sp->timecnt = 0;
  2451. sp->ttis[0].tt_abbrind = 0;
  2452. if ((sp->charcnt =
  2453. WideCharToMultiByte(CP_ACP, 0, tzinfo.StandardName, -1,
  2454. sp->chars, sizeof(sp->chars), NULL, NULL))
  2455. == 0)
  2456. sp->chars[sp->charcnt++] = '\0';
  2457. sp->ttis[1].tt_abbrind = sp->charcnt;
  2458. sp->charcnt +=
  2459. WideCharToMultiByte(CP_ACP, 0, tzinfo.DaylightName, -1,
  2460. sp->chars + sp->charcnt,
  2461. sizeof(sp->chars) - sp->charcnt, NULL, NULL);
  2462. if ((sp->charcnt - sp->ttis[1].tt_abbrind) == 0)
  2463. sp->chars[sp->charcnt++] = '\0';
  2464. sp->ttis[0].tt_gmtoff = - (tzinfo.Bias + tzinfo.StandardBias)
  2465. * MINSPERHOUR;
  2466. sp->ttis[1].tt_gmtoff = - (tzinfo.Bias + tzinfo.DaylightBias)
  2467. * MINSPERHOUR;
  2468. sp->ttis[0].tt_isdst = 0;
  2469. sp->ttis[1].tt_isdst = 1;
  2470. sp->typecnt = (startrule.r_mon == 0 && stoprule.r_mon == 0) ? 1 : 2;
  2471. if (sp->typecnt > 1)
  2472. (*fill_tzstate_from_rules)(sp, &startrule, &stoprule);
  2473. return TRUE;
  2474. }
  2475. return FALSE;
  2476. }
  2477. #endif /* W32_USE_IZ_TIMEZONE */
  2478. #endif /* !FUNZIP */
  2479. #ifndef WINDLL
  2480. /* This replacement getch() function was originally created for Watcom C
  2481. * and then additionally used with CYGWIN. Since UnZip 5.4, all other Win32
  2482. * ports apply this replacement rather that their supplied getch() (or
  2483. * alike) function. There are problems with unabsorbed LF characters left
  2484. * over in the keyboard buffer under Win95 (and 98) when ENTER was pressed.
  2485. * (Under Win95, ENTER returns two(!!) characters: CR-LF.) This problem
  2486. * does not appear when run on a WinNT console prompt!
  2487. */
  2488. /* Watcom 10.6's getch() does not handle Alt+<digit><digit><digit>. */
  2489. /* Note that if PASSWD_FROM_STDIN is defined, the file containing */
  2490. /* the password must have a carriage return after the word, not a */
  2491. /* Unix-style newline (linefeed only). This discards linefeeds. */
  2492. int getch_win32(void)
  2493. {
  2494. HANDLE stin;
  2495. DWORD rc;
  2496. unsigned char buf[2];
  2497. int ret = -1;
  2498. DWORD odemode = ~(DWORD)0;
  2499. # ifdef PASSWD_FROM_STDIN
  2500. stin = GetStdHandle(STD_INPUT_HANDLE);
  2501. # else
  2502. stin = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE,
  2503. FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  2504. if (stin == INVALID_HANDLE_VALUE)
  2505. return -1;
  2506. # endif
  2507. if (GetConsoleMode(stin, &odemode))
  2508. SetConsoleMode(stin, ENABLE_PROCESSED_INPUT); /* raw except ^C noticed */
  2509. if (ReadFile(stin, &buf, 1, &rc, NULL) && rc == 1)
  2510. ret = buf[0];
  2511. /* when the user hits return we get CR LF. We discard the LF, not the CR,
  2512. * because when we call this for the first time after a previous input
  2513. * such as the one for "replace foo? [y]es, ..." the LF may still be in
  2514. * the input stream before whatever the user types at our prompt. */
  2515. if (ret == '\n')
  2516. if (ReadFile(stin, &buf, 1, &rc, NULL) && rc == 1)
  2517. ret = buf[0];
  2518. if (odemode != ~(DWORD)0)
  2519. SetConsoleMode(stin, odemode);
  2520. # ifndef PASSWD_FROM_STDIN
  2521. CloseHandle(stin);
  2522. # endif
  2523. return ret;
  2524. }
  2525. #endif /* !WINDLL */
  2526. #if (defined(UNICODE_SUPPORT) && !defined(FUNZIP))
  2527. /* convert wide character string to multi-byte character string */
  2528. char *wide_to_local_string(wide_string, escape_all)
  2529. ZCONST zwchar *wide_string;
  2530. int escape_all;
  2531. {
  2532. int i;
  2533. wchar_t wc;
  2534. int bytes_char;
  2535. int default_used;
  2536. int wsize = 0;
  2537. int max_bytes = 9;
  2538. char buf[9];
  2539. char *buffer = NULL;
  2540. char *local_string = NULL;
  2541. for (wsize = 0; wide_string[wsize]; wsize++) ;
  2542. if (max_bytes < MB_CUR_MAX)
  2543. max_bytes = MB_CUR_MAX;
  2544. if ((buffer = (char *)malloc(wsize * max_bytes + 1)) == NULL) {
  2545. return NULL;
  2546. }
  2547. /* convert it */
  2548. buffer[0] = '\0';
  2549. for (i = 0; i < wsize; i++) {
  2550. if (sizeof(wchar_t) < 4 && wide_string[i] > 0xFFFF) {
  2551. /* wchar_t probably 2 bytes */
  2552. /* could do surrogates if state_dependent and wctomb can do */
  2553. wc = zwchar_to_wchar_t_default_char;
  2554. } else {
  2555. wc = (wchar_t)wide_string[i];
  2556. }
  2557. /* Unter some vendor's C-RTL, the Wide-to-MultiByte conversion functions
  2558. * (like wctomb() et. al.) do not use the same codepage as the other
  2559. * string arguments I/O functions (fopen, mkdir, rmdir etc.).
  2560. * Therefore, we have to fall back to the underlying Win32-API call to
  2561. * achieve a consistent behaviour for all supported compiler environments.
  2562. * Failing RTLs are for example:
  2563. * Borland (locale uses OEM-CP as default, but I/O functions expect ANSI
  2564. * names)
  2565. * Watcom (only "C" locale, wctomb() always uses OEM CP)
  2566. * (in other words: all supported environments except the Microsoft RTLs)
  2567. */
  2568. bytes_char = WideCharToMultiByte(
  2569. CP_ACP, WC_COMPOSITECHECK,
  2570. &wc, 1,
  2571. (LPSTR)buf, sizeof(buf),
  2572. NULL, &default_used);
  2573. if (default_used)
  2574. bytes_char = -1;
  2575. if (escape_all) {
  2576. if (bytes_char == 1 && (uch)buf[0] <= 0x7f) {
  2577. /* ASCII */
  2578. strncat(buffer, buf, 1);
  2579. } else {
  2580. /* use escape for wide character */
  2581. char *escape_string = wide_to_escape_string(wide_string[i]);
  2582. strcat(buffer, escape_string);
  2583. free(escape_string);
  2584. }
  2585. } else if (bytes_char > 0) {
  2586. /* multi-byte char */
  2587. strncat(buffer, buf, bytes_char);
  2588. } else {
  2589. /* no MB for this wide */
  2590. /* use escape for wide character */
  2591. char *escape_string = wide_to_escape_string(wide_string[i]);
  2592. strcat(buffer, escape_string);
  2593. free(escape_string);
  2594. }
  2595. }
  2596. if ((local_string = (char *)realloc(buffer, strlen(buffer) + 1)) == NULL) {
  2597. free(buffer);
  2598. return NULL;
  2599. }
  2600. return local_string;
  2601. }
  2602. #if 0
  2603. wchar_t *utf8_to_wchar_string(utf8_string)
  2604. char *utf8_string; /* path to get utf-8 name for */
  2605. {
  2606. wchar_t *qw;
  2607. int ulen;
  2608. int ulenw;
  2609. if (utf8_string == NULL)
  2610. return NULL;
  2611. /* get length */
  2612. ulenw = MultiByteToWideChar(
  2613. CP_UTF8, /* UTF-8 code page */
  2614. 0, /* flags for character-type options */
  2615. utf8_string, /* string to convert */
  2616. -1, /* string length (-1 = NULL terminated) */
  2617. NULL, /* buffer */
  2618. 0 ); /* buffer length (0 = return length) */
  2619. if (ulenw == 0) {
  2620. /* failed */
  2621. return NULL;
  2622. }
  2623. ulenw++;
  2624. /* get length in bytes */
  2625. ulen = sizeof(wchar_t) * (ulenw + 1);
  2626. if ((qw = (wchar_t *)malloc(ulen + 1)) == NULL) {
  2627. return NULL;
  2628. }
  2629. /* convert multibyte to wide */
  2630. ulen = MultiByteToWideChar(
  2631. CP_UTF8, /* UTF-8 code page */
  2632. 0, /* flags for character-type options */
  2633. utf8_string, /* string to convert */
  2634. -1, /* string length (-1 = NULL terminated) */
  2635. qw, /* buffer */
  2636. ulenw); /* buffer length (0 = return length) */
  2637. if (ulen == 0) {
  2638. /* failed */
  2639. free(qw);
  2640. return NULL;
  2641. }
  2642. return qw;
  2643. }
  2644. wchar_t *local_to_wchar_string(local_string)
  2645. char *local_string; /* path of local name */
  2646. {
  2647. wchar_t *qw;
  2648. int ulen;
  2649. int ulenw;
  2650. if (local_string == NULL)
  2651. return NULL;
  2652. /* get length */
  2653. ulenw = MultiByteToWideChar(
  2654. CP_ACP, /* ANSI code page */
  2655. 0, /* flags for character-type options */
  2656. local_string, /* string to convert */
  2657. -1, /* string length (-1 = NULL terminated) */
  2658. NULL, /* buffer */
  2659. 0 ); /* buffer length (0 = return length) */
  2660. if (ulenw == 0) {
  2661. /* failed */
  2662. return NULL;
  2663. }
  2664. ulenw++;
  2665. /* get length in bytes */
  2666. ulen = sizeof(wchar_t) * (ulenw + 1);
  2667. if ((qw = (wchar_t *)malloc(ulen + 1)) == NULL) {
  2668. return NULL;
  2669. }
  2670. /* convert multibyte to wide */
  2671. ulen = MultiByteToWideChar(
  2672. CP_ACP, /* ANSI code page */
  2673. 0, /* flags for character-type options */
  2674. local_string, /* string to convert */
  2675. -1, /* string length (-1 = NULL terminated) */
  2676. qw, /* buffer */
  2677. ulenw); /* buffer length (0 = return length) */
  2678. if (ulen == 0) {
  2679. /* failed */
  2680. free(qw);
  2681. return NULL;
  2682. }
  2683. return qw;
  2684. }
  2685. #endif /* 0 */
  2686. #endif /* UNICODE_SUPPORT && !FUNZIP */
  2687. /* --------------------------------------------------- */
  2688. /* Large File Support
  2689. *
  2690. * Initial functions by E. Gordon and R. Nausedat
  2691. * 9/10/2003
  2692. * Lifted from Zip 3b, win32.c and place here by Myles Bennett
  2693. * 7/6/2004
  2694. *
  2695. * These implement 64-bit file support for Windows. The
  2696. * defines and headers are in win32/w32cfg.h.
  2697. *
  2698. * Moved to win32i64.c by Mike White to avoid conflicts in
  2699. * same name functions in WiZ using UnZip and Zip libraries.
  2700. * 9/25/2003
  2701. */