home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / os2 / os2.c < prev    next >
C/C++ Source or Header  |  1997-10-31  |  69KB  |  2,147 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   os2.c
  4.  
  5.   OS/2-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   This file contains the OS/2 versions of the file name/attribute/time/etc
  8.   code.  Most or all of the routines which make direct use of OS/2 system
  9.   calls (i.e., the non-lowercase routines) are Kai Uwe Rommel's.  The read-
  10.   dir() suite was written by Michael Rendell and ported to OS/2 by Kai Uwe;
  11.   it is in the public domain.
  12.  
  13.   Contains:  GetCountryInfo()
  14.              GetFileTime()
  15.              SetFileTime()              (TIMESTAMP only)
  16.              stamp_file()               (TIMESTAMP only)
  17.              Utime2DosDateTime()
  18.              SetPathAttrTimes()
  19.              SetEAs()
  20.              GetLoadPath()
  21.              opendir()
  22.              closedir()
  23.              readdir()
  24.              [ seekdir() ]             not used
  25.              [ telldir() ]             not used
  26.              free_dircontents()
  27.              getdirent()
  28.              IsFileSystemFAT()
  29.              do_wild()
  30.              mapattr()
  31.              mapname()
  32.              checkdir()
  33.              isfloppy()
  34.              IsFileNameValid()
  35.              map2fat()
  36.              SetLongNameEA()
  37.              close_outfile()
  38.              check_for_newer()
  39.              dateformat()
  40.              version()
  41.              InitNLS()
  42.              IsUpperNLS()
  43.              ToLowerNLS()
  44.              StringLower()
  45.              DebugMalloc()
  46.  
  47.   ---------------------------------------------------------------------------*/
  48.  
  49.  
  50. #define UNZIP_INTERNAL
  51. #include "unzip.h"
  52. #include "os2acl.h"
  53.  
  54. extern char Far TruncEAs[];
  55.  
  56. /* local prototypes */
  57.  
  58. #ifdef TIMESTAMP
  59.   static int SetFileTime(ZCONST char *name, ulg stamp);
  60. #endif
  61. #if defined(USE_EF_UT_TIME) || defined(TIMESTAMP)
  62.   static ulg Utime2DosDateTime  OF((time_t uxtime));
  63. #endif
  64. static int   getOS2filetimes    OF((__GPRO__
  65.                                     ulg *pM_dt, ulg *pA_dt, ulg *pC_dt));
  66. static void  SetPathAttrTimes   OF((__GPRO__ int flags, int dir));
  67. static int   SetEAs             OF((__GPRO__ const char *path,
  68.                                     void *ef_block));
  69. static int   SetACL             OF((__GPRO__ const char *path,
  70.                                     void *ef_block));
  71. static int   EvalExtraFields    OF((__GPRO__ const char *path,
  72.                                     void *extra_field, unsigned ef_len));
  73. static int   isfloppy           OF((int nDrive));
  74. static int   IsFileNameValid    OF((const char *name));
  75. static void  map2fat            OF((char *pathcomp, char **pEndFAT));
  76. static int   SetLongNameEA      OF((char *name, char *longname));
  77. static void  InitNLS            OF((void));
  78.  
  79.  
  80. /*****************************/
  81. /*  Strings used in os2.c  */
  82. /*****************************/
  83.  
  84. #ifndef SFX
  85.   static char Far CantAllocateWildcard[] =
  86.     "warning:  cannot allocate wildcard buffers\n";
  87. #endif
  88. static char Far Creating[] = "   creating: %-22s ";
  89. static char Far ConversionFailed[] = "mapname:  conversion of %s failed\n";
  90. static char Far Labelling[] = "labelling %c: %-22s\n";
  91. static char Far ErrSetVolLabel[] = "mapname:  error setting volume label\n";
  92. static char Far PathTooLong[] = "checkdir error:  path too long: %s\n";
  93. static char Far CantCreateDir[] = "checkdir error:  cannot create %s\n\
  94.                  unable to process %s.\n";
  95. static char Far DirIsntDirectory[] =
  96.   "checkdir error:  %s exists but is not directory\n\
  97.                  unable to process %s.\n";
  98. static char Far PathTooLongTrunc[] =
  99.   "checkdir warning:  path too long; truncating\n                   %s\n\
  100.                 -> %s\n";
  101. #if (!defined(SFX) || defined(SFX_EXDIR))
  102.    static char Far CantCreateExtractDir[] =
  103.      "checkdir:  cannot create extraction directory: %s\n";
  104. #endif
  105.  
  106. #ifndef __EMX__
  107. #  if (_MSC_VER >= 600) || defined(__IBMC__)
  108. #    include <direct.h>          /* have special MSC/IBM C mkdir prototype */
  109. #  else                          /* own prototype because dir.h conflicts? */
  110.      int mkdir(const char *path);
  111. #  endif
  112. #  define MKDIR(path,mode)   mkdir(path)
  113. #else
  114. #  define MKDIR(path,mode)   mkdir(path,mode)
  115. #endif
  116.  
  117.  
  118. #ifdef __32BIT__
  119.  
  120. USHORT DosDevIOCtl32(PVOID pData, USHORT cbData, PVOID pParms, USHORT cbParms,
  121.                      USHORT usFunction, USHORT usCategory, HFILE hDevice)
  122. {
  123.   ULONG ulParmLengthInOut = cbParms, ulDataLengthInOut = cbData;
  124.   return (USHORT) DosDevIOCtl(hDevice, usCategory, usFunction,
  125.                               pParms, cbParms, &ulParmLengthInOut,
  126.                               pData, cbData, &ulDataLengthInOut);
  127. }
  128.  
  129. #  define DosDevIOCtl DosDevIOCtl32
  130. #else
  131. #  define DosDevIOCtl DosDevIOCtl2
  132. #endif
  133.  
  134.  
  135. typedef struct
  136. {
  137.   ush nID;
  138.   ush nSize;
  139.   ulg lSize;
  140. }
  141. EFHEADER, *PEFHEADER;
  142.  
  143.  
  144. #ifdef __32BIT__
  145.  
  146. #define DosFindFirst(p1, p2, p3, p4, p5, p6) \
  147.         DosFindFirst(p1, p2, p3, p4, p5, p6, 1)
  148.  
  149. #else
  150.  
  151. typedef struct
  152. {
  153.   ULONG oNextEntryOffset;
  154.   BYTE fEA;
  155.   BYTE cbName;
  156.   USHORT cbValue;
  157.   CHAR szName[1];
  158. }
  159. FEA2, *PFEA2;
  160.  
  161. typedef struct
  162. {
  163.   ULONG cbList;
  164.   FEA2 list[1];
  165. }
  166. FEA2LIST, *PFEA2LIST;
  167.  
  168. #define DosQueryCurrentDisk DosQCurDisk
  169. #define DosQueryFSAttach(p1, p2, p3, p4, p5) \
  170.         DosQFSAttach(p1, p2, p3, p4, p5, 0)
  171. #define DosEnumAttribute(p1, p2, p3, p4, p5, p6, p7) \
  172.         DosEnumAttribute(p1, p2, p3, p4, p5, p6, p7, 0)
  173. #define DosFindFirst(p1, p2, p3, p4, p5, p6) \
  174.         DosFindFirst(p1, p2, p3, p4, p5, p6, 0)
  175. #define DosMapCase DosCaseMap
  176. #define DosSetPathInfo(p1, p2, p3, p4, p5) \
  177.         DosSetPathInfo(p1, p2, p3, p4, p5, 0)
  178. #define DosQueryPathInfo(p1, p2, p3, p4) \
  179.         DosQPathInfo(p1, p2, p3, p4, 0)
  180. #define DosQueryFileInfo DosQFileInfo
  181. #define DosMapCase DosCaseMap
  182. #define DosQueryCtryInfo DosGetCtryInfo
  183.  
  184. #endif /* !__32BIT__ */
  185.  
  186.  
  187.  
  188.  
  189.  
  190. /*
  191.  * @(#) dir.h 1.4 87/11/06   Public Domain.
  192.  */
  193.  
  194. #define A_RONLY    0x01
  195. #define A_HIDDEN   0x02
  196. #define A_SYSTEM   0x04
  197. #define A_LABEL    0x08
  198. #define A_DIR      0x10
  199. #define A_ARCHIVE  0x20
  200.  
  201.  
  202. const int attributes = A_DIR | A_HIDDEN | A_SYSTEM;
  203.  
  204.  
  205. extern DIR *opendir(__GPRO__ ZCONST char *);
  206. extern struct direct *readdir(__GPRO__ DIR *);
  207. extern void seekdir(DIR *, long);
  208. extern long telldir(DIR *);
  209. extern void closedir(DIR *);
  210. #define rewinddir(dirp) seekdir(dirp, 0L)
  211.  
  212. int IsFileSystemFAT(__GPRO__ ZCONST char *dir);
  213. char *StringLower(char *szArg);
  214.  
  215.  
  216.  
  217.  
  218. /*
  219.  * @(#)dir.c 1.4 87/11/06 Public Domain.
  220.  */
  221.  
  222.  
  223. #ifndef S_IFMT
  224. #  define S_IFMT 0xF000
  225. #endif
  226.  
  227.  
  228. #ifndef SFX
  229.    static char *getdirent(__GPRO__ ZCONST char *);
  230.    static void free_dircontents(struct _dircontents *);
  231. #endif /* !SFX */
  232.  
  233.  
  234.  
  235.  
  236. int GetCountryInfo(void)
  237. {
  238.     COUNTRYINFO ctryi;
  239.     COUNTRYCODE ctryc;
  240. #ifdef __32BIT__
  241.     ULONG cbInfo;
  242. #else
  243.     USHORT cbInfo;
  244. #endif
  245.  
  246.   ctryc.country = ctryc.codepage = 0;
  247.  
  248.   if ( DosQueryCtryInfo(sizeof(ctryi), &ctryc, &ctryi, &cbInfo) != NO_ERROR )
  249.     return 0;
  250.  
  251.   return ctryi.fsDateFmt;
  252. }
  253.  
  254.  
  255. long GetFileTime(ZCONST char *name)
  256. {
  257. #ifdef __32BIT__
  258.   FILESTATUS3 fs;
  259. #else
  260.   FILESTATUS fs;
  261. #endif
  262.   USHORT nDate, nTime;
  263.  
  264.   if ( DosQueryPathInfo(name, 1, (PBYTE) &fs, sizeof(fs)) )
  265.     return -1;
  266.  
  267.   nDate = * (USHORT *) &fs.fdateLastWrite;
  268.   nTime = * (USHORT *) &fs.ftimeLastWrite;
  269.  
  270.   return ((ULONG) nDate) << 16 | nTime;
  271. }
  272.  
  273.  
  274. #ifdef TIMESTAMP
  275.  
  276. static int SetFileTime(ZCONST char *name, ulg stamp)   /* swiped from Zip */
  277. {
  278.   FILESTATUS fs;
  279.   USHORT fd, ft;
  280.  
  281.   if (DosQueryPathInfo(name, FIL_STANDARD, (PBYTE) &fs, sizeof(fs)))
  282.     return -1;
  283.  
  284.   fd = (USHORT) (stamp >> 16);
  285.   ft = (USHORT) stamp;
  286.   fs.fdateLastWrite = fs.fdateCreation = * (FDATE *) &fd;
  287.   fs.ftimeLastWrite = fs.ftimeCreation = * (FTIME *) &ft;
  288.  
  289.   if (DosSetPathInfo(name, FIL_STANDARD, (PBYTE) &fs, sizeof(fs), 0))
  290.     return -1;
  291.  
  292.   return 0;
  293. }
  294.  
  295.  
  296. int stamp_file(ZCONST char *fname, time_t modtime)
  297. {
  298.     return SetFileTime(fname, Utime2DosDateTime(modtime));
  299. }
  300.  
  301. #endif /* TIMESTAMP */
  302.  
  303.  
  304. /* The following DOS date/time structures are machine-dependent as they
  305.  * assume "little-endian" byte order.  For OS/2-specific code, which
  306.  * is run on x86 CPUs (or emulators?), this assumption is valid; but
  307.  * care should be taken when using this code as template for other ports.
  308.  */
  309. typedef union {
  310.   ULONG timevalue;          /* combined value, useful for comparisons */
  311.   struct {
  312.     FTIME ft;               /* system file time record:
  313.                              *    USHORT twosecs : 5
  314.                              *    USHORT minutes : 6;
  315.                              *    USHORT hours   : 5;   */
  316.     FDATE fd;               /* system file date record:
  317.                              *    USHORT day     : 5
  318.                              *    USHORT month   : 4;
  319.                              *    USHORT year    : 7;   */
  320.   } _fdt;
  321. } F_DATE_TIME, *PF_DATE_TIME;
  322.  
  323.  
  324. #if defined(USE_EF_UT_TIME) || defined(TIMESTAMP)
  325.  
  326. static ulg Utime2DosDateTime(uxtime)
  327.     time_t uxtime;
  328. {
  329.     F_DATE_TIME dosfiletime;
  330.     struct tm *t;
  331.  
  332.     /* round up to even seconds */
  333.     /* round up (down if "up" overflows) to even seconds */
  334.     if (uxtime & 1)
  335.         uxtime = (uxtime + 1 > uxtime) ? uxtime + 1 : uxtime - 1;
  336.  
  337.     t = localtime(&(uxtime));
  338.     if (t->tm_year < 80) {
  339.         dosfiletime._fdt.ft.twosecs = 0;
  340.         dosfiletime._fdt.ft.minutes = 0;
  341.         dosfiletime._fdt.ft.hours   = 0;
  342.         dosfiletime._fdt.fd.day     = 1;
  343.         dosfiletime._fdt.fd.month   = 1;
  344.         dosfiletime._fdt.fd.year    = 0;
  345.     } else {
  346.         dosfiletime._fdt.ft.twosecs = t->tm_sec >> 1;
  347.         dosfiletime._fdt.ft.minutes = t->tm_min;
  348.         dosfiletime._fdt.ft.hours   = t->tm_hour;
  349.         dosfiletime._fdt.fd.day     = t->tm_mday;
  350.         dosfiletime._fdt.fd.month   = t->tm_mon + 1;
  351.         dosfiletime._fdt.fd.year    = t->tm_year - 80;
  352.     }
  353.     return dosfiletime.timevalue;
  354.  
  355. } /* end function Utime2DosDateTime() */
  356.  
  357. #endif /* USE_EF_UT_TIME || TIMESTAMP */
  358.  
  359.  
  360. static int getOS2filetimes(__GPRO__ ulg *pM_dt, ulg *pA_dt, ulg *pC_dt)
  361. {
  362. #ifdef USE_EF_UT_TIME
  363.     unsigned eb_izux_flg;
  364.     iztimes z_utime;
  365. #endif
  366.  
  367.     /* Copy and/or convert time and date variables, if necessary;   */
  368.     /* return a flag indicating which time stamps are available.    */
  369. #ifdef USE_EF_UT_TIME
  370.     if (G.extra_field &&
  371.         ((eb_izux_flg = ef_scan_for_izux(G.extra_field,
  372.           G.lrec.extra_field_length, 0, G.lrec.last_mod_file_date,
  373.           &z_utime, NULL)) & EB_UT_FL_MTIME))
  374.     {
  375.         TTrace((stderr, "getOS2filetimes: UT e.f. modif. time = %lu\n",
  376.                 z_utime.mtime));
  377.         *pM_dt = Utime2DosDateTime(z_utime.mtime);
  378.         if (eb_izux_flg & EB_UT_FL_ATIME) {
  379.             TTrace((stderr, "getOS2filetimes: UT e.f. access time = %lu\n",
  380.                     z_utime.atime));
  381.             *pA_dt = Utime2DosDateTime(z_utime.atime);
  382.         }
  383.         if (eb_izux_flg & EB_UT_FL_CTIME) {
  384.             TTrace((stderr, "getOS2filetimes: UT e.f. creation time = %lu\n",
  385.                     z_utime.ctime));
  386.             *pC_dt = Utime2DosDateTime(z_utime.ctime);
  387.         } else {
  388.             /* no creation time value supplied, set it to modification time */
  389.             *pC_dt = *pM_dt;
  390.             eb_izux_flg |= EB_UT_FL_CTIME;
  391.         }
  392.         return (int)eb_izux_flg;
  393.     }
  394. #endif /* USE_EF_UT_TIME */
  395.     *pC_dt = *pM_dt = ((ulg)G.lrec.last_mod_file_date) << 16 |
  396.                       G.lrec.last_mod_file_time;
  397.     TTrace((stderr, "\ngetOS2filetimes: DOS dir modific./creation time = %lu\n",
  398.             *pM_dt));
  399.     return (EB_UT_FL_MTIME | EB_UT_FL_CTIME);
  400. }
  401.  
  402.  
  403. static void SetPathAttrTimes(__GPRO__ int flags, int dir)
  404. {
  405.   HFILE hFile;
  406. #ifdef __32BIT__
  407.   ULONG nAction;
  408. #else
  409.   USHORT nAction;
  410. #endif
  411.   FILESTATUS fs;
  412.   USHORT nLength;
  413.   char szName[CCHMAXPATH];
  414.   ulg Mod_dt, Acc_dt, Cre_dt;
  415.   int gotTimes;
  416.  
  417.   strcpy(szName, G.filename);
  418.   nLength = strlen(szName);
  419.   if (szName[nLength - 1] == '/')
  420.     szName[nLength - 1] = 0;
  421.  
  422.   if (dir)
  423.   {
  424.     if ( DosQueryPathInfo(szName, FIL_STANDARD, (PBYTE) &fs, sizeof(fs)) )
  425.       return;
  426.   }
  427.   else
  428.   {
  429.     /* for regular files, open them and operate on the file handle, to
  430.        work around certain network operating system bugs ... */
  431.  
  432.     if ( DosOpen(szName, &hFile, &nAction, 0, 0,
  433.                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  434.                  OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE, 0) )
  435.       return;
  436.  
  437.     if ( DosQueryFileInfo(hFile, FIL_STANDARD, (PBYTE) &fs, sizeof(fs)) )
  438.       return;
  439.   }
  440.  
  441.   /* set date/time stamps */
  442.   gotTimes = getOS2filetimes(__G__ &Mod_dt, &Acc_dt, &Cre_dt);
  443.   if (gotTimes & EB_UT_FL_MTIME) {
  444.     fs.fdateLastWrite = ((F_DATE_TIME *)&Mod_dt)->_fdt.fd;
  445.     fs.ftimeLastWrite = ((F_DATE_TIME *)&Mod_dt)->_fdt.ft;
  446.   }
  447.   if (gotTimes & EB_UT_FL_ATIME) {
  448.     fs.fdateLastAccess = ((F_DATE_TIME *)&Acc_dt)->_fdt.fd;
  449.     fs.ftimeLastAccess = ((F_DATE_TIME *)&Acc_dt)->_fdt.ft;
  450.   }
  451.   if (gotTimes & EB_UT_FL_CTIME) {
  452.     fs.fdateCreation = ((F_DATE_TIME *)&Cre_dt)->_fdt.fd;
  453.     fs.ftimeCreation = ((F_DATE_TIME *)&Cre_dt)->_fdt.ft;
  454.   }
  455.  
  456.   if ( flags != -1 )
  457.     fs.attrFile = flags; /* hidden, system, archive, read-only */
  458.  
  459.   if (dir)
  460.   {
  461.     DosSetPathInfo(szName, FIL_STANDARD, (PBYTE) &fs, sizeof(fs), 0);
  462.   }
  463.   else
  464.   {
  465.     DosSetFileInfo(hFile, FIL_STANDARD, (PBYTE) &fs, sizeof(fs));
  466.     DosClose(hFile);
  467.   }
  468. }
  469.  
  470.  
  471. typedef struct
  472. {
  473.   ULONG cbList;               /* length of value + 22 */
  474. #ifdef __32BIT__
  475.   ULONG oNext;
  476. #endif
  477.   BYTE fEA;                   /* 0 */
  478.   BYTE cbName;                /* length of ".LONGNAME" = 9 */
  479.   USHORT cbValue;             /* length of value + 4 */
  480.   BYTE szName[10];            /* ".LONGNAME" */
  481.   USHORT eaType;              /* 0xFFFD for length-preceded ASCII */
  482.   USHORT eaSize;              /* length of value */
  483.   BYTE szValue[CCHMAXPATH];
  484. }
  485. FEALST;
  486.  
  487.  
  488. static int SetEAs(__GPRO__ const char *path, void *ef_block)
  489. {                                               /* returns almost-PK errors */
  490.   EFHEADER *pEAblock = (PEFHEADER) ef_block;
  491. #ifdef __32BIT__
  492.   EAOP2 eaop;
  493.   PFEA2LIST pFEA2list;
  494. #else
  495.   EAOP eaop;
  496.   PFEALIST pFEAlist;
  497.   PFEA pFEA;
  498.   PFEA2LIST pFEA2list;
  499.   PFEA2 pFEA2;
  500.   ULONG nLength2;
  501. #endif
  502.   USHORT nLength;
  503.   char szName[CCHMAXPATH];
  504.   int error;
  505.  
  506.   if ( ef_block == NULL || pEAblock -> nID != EF_OS2 )
  507.     return PK_OK;  /* not an OS/2 extra field:  assume OK */
  508.  
  509.   if ( pEAblock->nSize < 4 || (pEAblock->lSize > 0L && pEAblock->nSize <= 10) )
  510.     return IZ_EF_TRUNC;  /* no compressed data! */
  511.  
  512.   strcpy(szName, path);
  513.   nLength = strlen(szName);
  514.   if (szName[nLength - 1] == '/')
  515.     szName[nLength - 1] = 0;
  516.  
  517.   if ( (pFEA2list = (PFEA2LIST) malloc((size_t) pEAblock -> lSize)) == NULL )
  518.     return PK_MEM4;
  519.  
  520.   if ( (error = memextract(__G__ (uch *)pFEA2list, pEAblock->lSize,
  521.        (uch *)(pEAblock+1), (ulg)(pEAblock->nSize - 4))) != PK_OK )
  522.   {
  523.     free(pFEA2list);
  524.     return error;
  525.   }
  526.  
  527. #ifdef __32BIT__
  528.   eaop.fpGEA2List = NULL;
  529.   eaop.fpFEA2List = pFEA2list;
  530. #else
  531.   pFEAlist  = (PVOID) pFEA2list;
  532.   pFEA2 = pFEA2list -> list;
  533.   pFEA  = pFEAlist  -> list;
  534.  
  535.   do
  536.   {
  537.     nLength2 = pFEA2 -> oNextEntryOffset;
  538.     nLength = sizeof(FEA) + pFEA2 -> cbName + 1 + pFEA2 -> cbValue;
  539.  
  540.     memcpy(pFEA, (PCH) pFEA2 + sizeof(pFEA2 -> oNextEntryOffset), nLength);
  541.  
  542.     pFEA2 = (PFEA2) ((PCH) pFEA2 + nLength2);
  543.     pFEA = (PFEA) ((PCH) pFEA + nLength);
  544.   }
  545.   while ( nLength2 != 0 );
  546.  
  547.   pFEAlist -> cbList = (PCH) pFEA - (PCH) pFEAlist;
  548.  
  549.   eaop.fpGEAList = NULL;
  550.   eaop.fpFEAList = pFEAlist;
  551. #endif
  552.  
  553.   eaop.oError = 0;
  554.   DosSetPathInfo(szName, FIL_QUERYEASIZE, (PBYTE) &eaop, sizeof(eaop), 0);
  555.  
  556.   if (!G.tflag && (G.qflag < 2))
  557.     Info(slide, 0, ((char *)slide, " (%ld bytes EAs)", pFEA2list -> cbList));
  558.  
  559.   free(pFEA2list);
  560.   return PK_COOL;
  561. }
  562.  
  563.  
  564. static int SetACL(__GPRO__ const char *path, void *ef_block)
  565. {                                               /* returns almost-PK errors */
  566.   EFHEADER *pACLblock = (PEFHEADER) ef_block;
  567.   char *szACL;
  568.   int error;
  569.  
  570.   if ( ef_block == NULL || pACLblock -> nID != EF_ACL )
  571.     return PK_OK;  /* not an OS/2 extra field:  assume OK */
  572.  
  573.   if (pACLblock->nSize < 4 || (pACLblock->lSize > 0L && pACLblock->nSize <= 10))
  574.     return IZ_EF_TRUNC;  /* no compressed data! */
  575.  
  576.   if ( (szACL = malloc((size_t) pACLblock -> lSize)) == NULL )
  577.     return PK_MEM4;
  578.  
  579.   if ( (error = memextract(__G__ (uch *)szACL, pACLblock->lSize,
  580.        (uch *)(pACLblock+1), (ulg)(pACLblock->nSize - 4))) != PK_OK )
  581.   {
  582.     free(szACL);
  583.     return error;
  584.   }
  585.  
  586.   if (acl_set(NULL, path, szACL) == 0)
  587.     if (!G.tflag && (G.qflag < 2))
  588.       Info(slide, 0, ((char *)slide, " (%ld bytes ACL)", strlen(szACL)));
  589.  
  590.   free(szACL);
  591.   return PK_COOL;
  592. }
  593.  
  594.  
  595. #ifdef SFX
  596.  
  597. char *GetLoadPath(__GPRO)
  598. {
  599. #ifdef __32BIT__ /* generic for 32-bit API */
  600.   PTIB pptib;
  601.   PPIB pppib;
  602.   char *szPath;
  603.  
  604.   DosGetInfoBlocks(&pptib, &pppib);
  605.   szPath = pppib -> pib_pchenv;
  606. #else /* 16-bit, note: requires large data model */
  607.   SEL selEnv;
  608.   USHORT offCmd;
  609.   char *szPath;
  610.  
  611.   DosGetEnv(&selEnv, &offCmd);
  612.   szPath = MAKEP(selEnv, 0);
  613. #endif
  614.  
  615.   while (*szPath) /* find end of process environment */
  616.     szPath = strchr(szPath, 0) + 1;
  617.  
  618.   return szPath + 1; /* .exe file name follows environment */
  619.  
  620. } /* end function GetLoadPath() */
  621.  
  622.  
  623.  
  624.  
  625.  
  626. #else /* !SFX */
  627.  
  628. DIR *opendir(__GPRO__ const char *name)
  629. {
  630.   struct stat statb;
  631.   DIR *dirp;
  632.   char c;
  633.   char *s;
  634.   struct _dircontents *dp;
  635.   char nbuf[MAXPATHLEN + 1];
  636.   int len;
  637.  
  638.   strcpy(nbuf, name);
  639.   if ((len = strlen(nbuf)) == 0)
  640.     return NULL;
  641.  
  642.   if ( ((c = nbuf[len - 1]) == '\\' || c == '/') && (len > 1) )
  643.   {
  644.     nbuf[len - 1] = 0;
  645.     --len;
  646.  
  647.     if ( nbuf[len - 1] == ':' )
  648.     {
  649.       strcpy(nbuf+len, "\\.");
  650.       len += 2;
  651.     }
  652.   }
  653.   else
  654.     if ( nbuf[len - 1] == ':' )
  655.     {
  656.       strcpy(nbuf+len, ".");
  657.       ++len;
  658.     }
  659.  
  660.   /* GRR:  Borland and Watcom C return non-zero on wildcards... < 0 ? */
  661.   if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  662.   {
  663.     Trace((stderr, "opendir:  stat(%s) returns negative or not directory\n",
  664.       nbuf));
  665.     return NULL;
  666.   }
  667.  
  668.   if ( (dirp = malloc(sizeof(DIR))) == NULL )
  669.     return NULL;
  670.  
  671.   if ( nbuf[len - 1] == '.' && (len == 1 || nbuf[len - 2] != '.') )
  672.     strcpy(nbuf+len-1, "*");
  673.   else
  674.     if ( ((c = nbuf[len - 1]) == '\\' || c == '/') && (len == 1) )
  675.       strcpy(nbuf+len, "*");
  676.     else
  677.       strcpy(nbuf+len, "\\*");
  678.  
  679.   /* len is no longer correct (but no longer needed) */
  680.   Trace((stderr, "opendir:  nbuf = [%s]\n", nbuf));
  681.  
  682.   dirp -> dd_loc = 0;
  683.   dirp -> dd_contents = dirp -> dd_cp = NULL;
  684.  
  685.   if ((s = getdirent(__G__ nbuf)) == NULL)
  686.     return dirp;
  687.  
  688.   do
  689.   {
  690.     if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
  691.         ((dp -> _d_entry = malloc(strlen(s) + 1)) == NULL)      )
  692.     {
  693.       if (dp)
  694.         free(dp);
  695.       free_dircontents(dirp -> dd_contents);
  696.  
  697.       return NULL;
  698.     }
  699.  
  700.     if (dirp -> dd_contents)
  701.     {
  702.       dirp -> dd_cp -> _d_next = dp;
  703.       dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  704.     }
  705.     else
  706.       dirp -> dd_contents = dirp -> dd_cp = dp;
  707.  
  708.     strcpy(dp -> _d_entry, s);
  709.     dp -> _d_next = NULL;
  710.  
  711.     dp -> _d_size = G.os2.find.cbFile;
  712.     dp -> _d_mode = G.os2.find.attrFile;
  713.     dp -> _d_time = *(unsigned *) &(G.os2.find.ftimeLastWrite);
  714.     dp -> _d_date = *(unsigned *) &(G.os2.find.fdateLastWrite);
  715.   }
  716.   while ((s = getdirent(__G__ NULL)) != NULL);
  717.  
  718.   dirp -> dd_cp = dirp -> dd_contents;
  719.  
  720.   return dirp;
  721. }
  722.  
  723.  
  724. void closedir(DIR * dirp)
  725. {
  726.   free_dircontents(dirp -> dd_contents);
  727.   free(dirp);
  728. }
  729.  
  730.  
  731. struct direct *readdir(__GPRO__ DIR * dirp)
  732. {
  733.   /* moved to os2data.h so it can be global */
  734.   /* static struct direct dp; */
  735.  
  736.   if (dirp -> dd_cp == NULL)
  737.     return NULL;
  738.  
  739.   G.os2.dp.d_namlen = G.os2.dp.d_reclen =
  740.     strlen(strcpy(G.os2.dp.d_name, dirp -> dd_cp -> _d_entry));
  741.  
  742.   G.os2.dp.d_ino = 0;
  743.  
  744.   G.os2.dp.d_size = dirp -> dd_cp -> _d_size;
  745.   G.os2.dp.d_mode = dirp -> dd_cp -> _d_mode;
  746.   G.os2.dp.d_time = dirp -> dd_cp -> _d_time;
  747.   G.os2.dp.d_date = dirp -> dd_cp -> _d_date;
  748.  
  749.   dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  750.   dirp -> dd_loc++;
  751.  
  752.   return &G.os2.dp;
  753. }
  754.  
  755.  
  756.  
  757. #if 0  /* not used in unzip; retained for possibly future use */
  758.  
  759. void seekdir(DIR * dirp, long off)
  760. {
  761.   long i = off;
  762.   struct _dircontents *dp;
  763.  
  764.   if (off >= 0)
  765.   {
  766.     for (dp = dirp -> dd_contents; --i >= 0 && dp; dp = dp -> _d_next);
  767.  
  768.     dirp -> dd_loc = off - (i + 1);
  769.     dirp -> dd_cp = dp;
  770.   }
  771. }
  772.  
  773.  
  774. long telldir(DIR * dirp)
  775. {
  776.   return dirp -> dd_loc;
  777. }
  778.  
  779. #endif /* 0 */
  780.  
  781.  
  782.  
  783. static void free_dircontents(struct _dircontents * dp)
  784. {
  785.   struct _dircontents *odp;
  786.  
  787.   while (dp)
  788.   {
  789.     if (dp -> _d_entry)
  790.       free(dp -> _d_entry);
  791.  
  792.     dp = (odp = dp) -> _d_next;
  793.     free(odp);
  794.   }
  795. }
  796.  
  797.  
  798. static char *getdirent(__GPRO__ ZCONST char *dir)
  799. {
  800.   int done;
  801.   /* moved to os2data.h so it can be global */
  802.   /* static int lower; */
  803.  
  804.   if (dir != NULL)
  805.   {                                    /* get first entry */
  806.     G.os2.hdir = HDIR_SYSTEM;
  807.     G.os2.count = 1;
  808.     done = DosFindFirst(dir, &G.os2.hdir, attributes,
  809.                         &G.os2.find, sizeof(G.os2.find), &G.os2.count);
  810.     G.os2.lower = IsFileSystemFAT(__G__ dir);
  811.   }
  812.   else                                 /* get next entry */
  813.     done = DosFindNext(G.os2.hdir,
  814.                        &G.os2.find, sizeof(G.os2.find), &G.os2.count);
  815.  
  816.   if (done == 0)
  817.   {
  818.     if ( G.os2.lower )
  819.       StringLower(G.os2.find.achName);
  820.     return G.os2.find.achName;
  821.   }
  822.   else
  823.   {
  824.     DosFindClose(G.os2.hdir);
  825.     return NULL;
  826.   }
  827. }
  828.  
  829.  
  830.  
  831. int IsFileSystemFAT(__GPRO__ ZCONST char *dir)  /* FAT / HPFS detection */
  832. {
  833.   /* moved to os2data.h so they can be global */
  834.   /* static USHORT nLastDrive=(USHORT)(-1), nResult; */
  835.   ULONG lMap;
  836.   BYTE bData[64];
  837.   char bName[3];
  838. #ifdef __32BIT__
  839.   ULONG nDrive, cbData;
  840.   PFSQBUFFER2 pData = (PFSQBUFFER2) bData;
  841. #else
  842.   USHORT nDrive, cbData;
  843.   PFSQBUFFER pData = (PFSQBUFFER) bData;
  844. #endif
  845.  
  846.     /* We separate FAT and HPFS+other file systems here.
  847.        at the moment I consider other systems to be similar to HPFS,
  848.        i.e. support long file names and case sensitive */
  849.  
  850.     if ( isalpha(dir[0]) && (dir[1] == ':') )
  851.       nDrive = toupper(dir[0]) - '@';
  852.     else
  853.       DosQueryCurrentDisk(&nDrive, &lMap);
  854.  
  855.     if ( nDrive == G.os2.nLastDrive )
  856.       return G.os2.nResult;
  857.  
  858.     bName[0] = (char) (nDrive + '@');
  859.     bName[1] = ':';
  860.     bName[2] = 0;
  861.  
  862.     G.os2.nLastDrive = nDrive;
  863.     cbData = sizeof(bData);
  864.  
  865.     if ( !DosQueryFSAttach(bName, 0, FSAIL_QUERYNAME, (PVOID) pData, &cbData) )
  866.       G.os2.nResult = !strcmp((char *) (pData -> szFSDName) + pData -> cbName,
  867.                               "FAT");
  868.     else
  869.       G.os2.nResult = FALSE;
  870.  
  871.     /* End of this ugly code */
  872.     return G.os2.nResult;
  873. } /* end function IsFileSystemFAT() */
  874.  
  875.  
  876.  
  877.  
  878.  
  879. /************************/
  880. /*  Function do_wild()  */
  881. /************************/
  882.  
  883. char *do_wild(__G__ wildspec)
  884.     __GDEF
  885.     char *wildspec;         /* only used first time on a given dir */
  886. {
  887.   /* moved to os2data.h so they can be global */
  888. #if 0
  889.   static DIR *dir = NULL;
  890.   static char *dirname, *wildname, matchname[FILNAMSIZ];
  891.   static int firstcall=TRUE, have_dirname, dirnamelen;
  892. #endif
  893.     struct direct *file;
  894.  
  895.  
  896.     /* Even when we're just returning wildspec, we *always* do so in
  897.      * matchname[]--calling routine is allowed to append four characters
  898.      * to the returned string, and wildspec may be a pointer to argv[].
  899.      */
  900.     if (G.os2.firstcall) {        /* first call:  must initialize everything */
  901.         G.os2.firstcall = FALSE;
  902.  
  903.         /* break the wildspec into a directory part and a wildcard filename */
  904.         if ((G.os2.wildname = strrchr(wildspec, '/')) == NULL &&
  905.             (G.os2.wildname = strrchr(wildspec, ':')) == NULL) {
  906.             G.os2.dirname = ".";
  907.             G.os2.dirnamelen = 1;
  908.             G.os2.have_dirname = FALSE;
  909.             G.os2.wildname = wildspec;
  910.         } else {
  911.             ++G.os2.wildname;     /* point at character after '/' or ':' */
  912.             G.os2.dirnamelen = G.os2.wildname - wildspec;
  913.             if ((G.os2.dirname = (char *)malloc(G.os2.dirnamelen+1)) == NULL) {
  914.                 Info(slide, 1, ((char *)slide,
  915.                   LoadFarString(CantAllocateWildcard)));
  916.                 strcpy(G.os2.matchname, wildspec);
  917.                 return G.os2.matchname;   /* but maybe filespec was not a wildcard */
  918.             }
  919.             strncpy(G.os2.dirname, wildspec, G.os2.dirnamelen);
  920.             G.os2.dirname[G.os2.dirnamelen] = '\0';   /* terminate for strcpy below */
  921.             G.os2.have_dirname = TRUE;
  922.         }
  923.         Trace((stderr, "do_wild:  dirname = [%s]\n", G.os2.dirname));
  924.  
  925.         if ((G.os2.dir = opendir(__G__ G.os2.dirname)) != NULL) {
  926.             while ((file = readdir(__G__ G.os2.dir)) != NULL) {
  927.                 Trace((stderr, "do_wild:  readdir returns %s\n", file->d_name));
  928.                 if (match(file->d_name, G.os2.wildname, 1)) {  /* 1 == ignore case */
  929.                     Trace((stderr, "do_wild:  match() succeeds\n"));
  930.                     if (G.os2.have_dirname) {
  931.                         strcpy(G.os2.matchname, G.os2.dirname);
  932.                         strcpy(G.os2.matchname+G.os2.dirnamelen, file->d_name);
  933.                     } else
  934.                         strcpy(G.os2.matchname, file->d_name);
  935.                     return G.os2.matchname;
  936.                 }
  937.             }
  938.             /* if we get to here directory is exhausted, so close it */
  939.             closedir(G.os2.dir);
  940.             G.os2.dir = NULL;
  941.         }
  942.         Trace((stderr, "do_wild:  opendir(%s) returns NULL\n", G.os2.dirname));
  943.  
  944.         /* return the raw wildspec in case that works (e.g., directory not
  945.          * searchable, but filespec was not wild and file is readable) */
  946.         strcpy(G.os2.matchname, wildspec);
  947.         return G.os2.matchname;
  948.     }
  949.  
  950.     /* last time through, might have failed opendir but returned raw wildspec */
  951.     if (G.os2.dir == NULL) {
  952.         G.os2.firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  953.         if (G.os2.have_dirname)
  954.             free(G.os2.dirname);
  955.         return (char *)NULL;
  956.     }
  957.  
  958.     /* If we've gotten this far, we've read and matched at least one entry
  959.      * successfully (in a previous call), so dirname has been copied into
  960.      * matchname already.
  961.      */
  962.     while ((file = readdir(__G__ G.os2.dir)) != NULL)
  963.         if (match(file->d_name, G.os2.wildname, 1)) {   /* 1 == ignore case */
  964.             if (G.os2.have_dirname) {
  965.                 /* strcpy(G.os2.matchname, G.os2.dirname); */
  966.                 strcpy(G.os2.matchname+G.os2.dirnamelen, file->d_name);
  967.             } else
  968.                 strcpy(G.os2.matchname, file->d_name);
  969.             return G.os2.matchname;
  970.         }
  971.  
  972.     closedir(G.os2.dir);     /* have read at least one dir entry; nothing left */
  973.     G.os2.dir = NULL;
  974.     G.os2.firstcall = TRUE;  /* reset for new wildspec */
  975.     if (G.os2.have_dirname)
  976.         free(G.os2.dirname);
  977.     return (char *)NULL;
  978.  
  979. } /* end function do_wild() */
  980.  
  981. #endif /* !SFX */
  982.  
  983.  
  984. /* scan extra fields for something we happen to know */
  985.  
  986. static int EvalExtraFields(__GPRO__ const char *path,
  987.                            void *extra_field, unsigned ef_len)
  988. {
  989.   char *ef_ptr = extra_field;
  990.   PEFHEADER pEFblock;
  991.   int rc = PK_OK;
  992.  
  993.   while (ef_len >= sizeof(EFHEADER))
  994.   {
  995.     pEFblock = (PEFHEADER) ef_ptr;
  996.  
  997.     if (pEFblock -> nSize > (ef_len - EB_HEADSIZE))
  998.       return PK_ERR;            /* claimed EFblock length exceeds EF size! */
  999.  
  1000.     switch (pEFblock -> nID)
  1001.     {
  1002.     case EF_OS2:
  1003.       rc = SetEAs(__G__ path, ef_ptr);
  1004.       break;
  1005.     case EF_ACL:
  1006.       rc = (G.X_flag) ? SetACL(__G__ path, ef_ptr) : PK_OK;
  1007.       break;
  1008. #if 0
  1009.     case EF_IZUNIX:
  1010.       /* handled elsewhere */
  1011.       break;
  1012. #endif
  1013.     default:
  1014.       TTrace((stderr,"EvalExtraFields: unknown extra field block, ID=%d\n",
  1015.               pEFblock -> nID));
  1016.       break;
  1017.     }
  1018.  
  1019.     ef_ptr += (pEFblock -> nSize + EB_HEADSIZE);
  1020.     ef_len -= (pEFblock -> nSize + EB_HEADSIZE);
  1021.  
  1022.     if (rc != PK_OK)
  1023.       break;
  1024.   }
  1025.  
  1026.   return rc;
  1027. }
  1028.  
  1029.  
  1030.  
  1031. /************************/
  1032. /*  Function mapattr()  */
  1033. /************************/
  1034.  
  1035. int mapattr(__G)
  1036.     __GDEF
  1037. {
  1038.     /* set archive bit (file is not backed up): */
  1039.     G.pInfo->file_attr = (unsigned)(G.crec.external_file_attributes | 32) & 0xff;
  1040.     return 0;
  1041. }
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047. /************************/
  1048. /*  Function mapname()  */
  1049. /************************/
  1050.  
  1051. /*
  1052.  * There are presently two possibilities in OS/2:  the output filesystem is
  1053.  * FAT, or it is HPFS.  If the former, we need to map to FAT, obviously, but
  1054.  * we *also* must map to HPFS and store that version of the name in extended
  1055.  * attributes.  Either way, we need to map to HPFS, so the main mapname
  1056.  * routine does that.  In the case that the output file system is FAT, an
  1057.  * extra filename-mapping routine is called in checkdir().  While it should
  1058.  * be possible to determine the filesystem immediately upon entry to mapname(),
  1059.  * it is conceivable that the DOS APPEND utility could be added to OS/2 some-
  1060.  * day, allowing a FAT directory to be APPENDed to an HPFS drive/path.  There-
  1061.  * fore we simply check the filesystem at each path component.
  1062.  *
  1063.  * Note that when alternative IFSes become available/popular, everything will
  1064.  * become immensely more complicated.  For example, a Minix filesystem would
  1065.  * have limited filename lengths like FAT but no extended attributes in which
  1066.  * to store the longer versions of the names.  A BSD Unix filesystem would
  1067.  * support paths of length 1024 bytes or more, but it is not clear that FAT
  1068.  * EAs would allow such long .LONGNAME fields or that OS/2 would properly
  1069.  * restore such fields when moving files from FAT to the new filesystem.
  1070.  *
  1071.  * GRR:  some or all of the following chars should be checked in either
  1072.  *       mapname (HPFS) or map2fat (FAT), depending:  ,=^+'"[]<>|\t&
  1073.  */
  1074.                              /* return 0 if no error, 1 if caution (filename */
  1075. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  1076.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  1077.     int renamed;             /*  or 10 if out of memory (skip file) */
  1078. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  1079.     char pathcomp[FILNAMSIZ];      /* path-component buffer */
  1080.     char *pp, *cp=(char *)NULL;    /* character pointers */
  1081.     char *lastsemi=(char *)NULL;   /* pointer to last semi-colon in pathcomp */
  1082.     int quote = FALSE;             /* flag:  next char is literal */
  1083.     int error = 0;
  1084.     register unsigned workch;      /* hold the character being tested */
  1085.  
  1086.  
  1087. /*---------------------------------------------------------------------------
  1088.     Initialize various pointers and counters and stuff.
  1089.   ---------------------------------------------------------------------------*/
  1090.  
  1091.     /* can create path as long as not just freshening, or if user told us */
  1092.     G.create_dirs = (!G.fflag || renamed);
  1093.  
  1094.     G.os2.created_dir = FALSE;  /* not yet */
  1095.     G.os2.renamed_fullpath = FALSE;
  1096.     G.os2.fnlen = strlen(G.filename);
  1097.  
  1098. /* GRR:  for VMS, convert to internal format now or later? or never? */
  1099.     if (renamed) {
  1100.         cp = G.filename - 1;    /* point to beginning of renamed name... */
  1101.         while (*++cp)
  1102.             if (*cp == '\\')    /* convert backslashes to forward */
  1103.                 *cp = '/';
  1104.         cp = G.filename;
  1105.         /* use temporary rootpath if user gave full pathname */
  1106.         if (G.filename[0] == '/') {
  1107.             G.os2.renamed_fullpath = TRUE;
  1108.             pathcomp[0] = '/';  /* copy the '/' and terminate */
  1109.             pathcomp[1] = '\0';
  1110.             ++cp;
  1111.         } else if (isalpha(G.filename[0]) && G.filename[1] == ':') {
  1112.             G.os2.renamed_fullpath = TRUE;
  1113.             pp = pathcomp;
  1114.             *pp++ = *cp++;      /* copy the "d:" (+ '/', possibly) */
  1115.             *pp++ = *cp++;
  1116.             if (*cp == '/')
  1117.                 *pp++ = *cp++;  /* otherwise add "./"? */
  1118.             *pp = '\0';
  1119.         }
  1120.     }
  1121.  
  1122.     /* pathcomp is ignored unless renamed_fullpath is TRUE: */
  1123.     if ((error = checkdir(__G__ pathcomp, INIT)) != 0)    /* init path buffer */
  1124.         return error;           /* ...unless no mem or vol label on hard disk */
  1125.  
  1126.     *pathcomp = '\0';           /* initialize translation buffer */
  1127.     pp = pathcomp;              /* point to translation buffer */
  1128.     if (!renamed) {             /* cp already set if renamed */
  1129.         if (G.jflag)            /* junking directories */
  1130. /* GRR:  watch out for VMS version... */
  1131.             cp = (char *)strrchr(G.filename, '/');
  1132.         if (cp == (char *)NULL) /* no '/' or not junking dirs */
  1133.             cp = G.filename;    /* point to internal zipfile-member pathname */
  1134.         else
  1135.             ++cp;               /* point to start of last component of path */
  1136.     }
  1137.  
  1138. /*---------------------------------------------------------------------------
  1139.     Begin main loop through characters in filename.
  1140.   ---------------------------------------------------------------------------*/
  1141.  
  1142.     while ((workch = (uch)*cp++) != 0) {
  1143.  
  1144.         if (quote) {              /* if character quoted, */
  1145.             *pp++ = (char)workch; /*  include it literally */
  1146.             quote = FALSE;
  1147.         } else
  1148.             switch (workch) {
  1149.             case '/':             /* can assume -j flag not given */
  1150.                 *pp = '\0';
  1151.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  1152.                     return error;
  1153.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  1154.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  1155.                 break;
  1156.  
  1157.             case ':':
  1158.                 *pp++ = '_';      /* drive names not stored in zipfile, */
  1159.                 break;            /*  so no colons allowed */
  1160.  
  1161.             case ';':             /* start of VMS version? */
  1162.                 lastsemi = pp;    /* remove VMS version later... */
  1163.                 *pp++ = ';';      /*  but keep semicolon for now */
  1164.                 break;
  1165.  
  1166.             case '\026':          /* control-V quote for special chars */
  1167.                 quote = TRUE;     /* set flag for next character */
  1168.                 break;
  1169.  
  1170.             case ' ':             /* keep spaces unless specifically */
  1171.                 if (G.sflag)      /*  requested to change to underscore */
  1172.                     *pp++ = '_';
  1173.                 else
  1174.                     *pp++ = ' ';
  1175.                 break;
  1176.  
  1177.             default:
  1178.                 /* allow ASCII 255 and European characters in filenames: */
  1179.                 if (isprint(workch) || workch >= 127)
  1180.                     *pp++ = (char)workch;
  1181.             } /* end switch */
  1182.  
  1183.     } /* end while loop */
  1184.  
  1185.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  1186.  
  1187.     /* if not saving them, remove VMS version numbers (appended "###") */
  1188.     if (!G.V_flag && lastsemi) {
  1189.         pp = lastsemi + 1;        /* semi-colon was kept:  expect #s after */
  1190.         while (isdigit((uch)(*pp)))
  1191.             ++pp;
  1192.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  1193.             *lastsemi = '\0';
  1194.     }
  1195.  
  1196. /*---------------------------------------------------------------------------
  1197.     Report if directory was created (and no file to create:  filename ended
  1198.     in '/'), check name to be sure it exists, and combine path and name be-
  1199.     fore exiting.
  1200.   ---------------------------------------------------------------------------*/
  1201.  
  1202.     if (G.filename[G.os2.fnlen-1] == '/') {
  1203.         checkdir(__G__ G.filename, GETPATH);
  1204.         if (G.os2.created_dir) {
  1205.             if (!G.qflag)
  1206.                 Info(slide, 0, ((char *)slide, LoadFarString(Creating),
  1207.                   G.filename));
  1208.             if (G.extra_field) { /* zipfile extra field has extended attribs */
  1209.                 int err = EvalExtraFields(__G__ G.filename, G.extra_field,
  1210.                                           G.lrec.extra_field_length);
  1211.  
  1212.                 if (err == IZ_EF_TRUNC) {
  1213.                     if (G.qflag)
  1214.                         Info(slide, 1, ((char *)slide, "%-22s ", G.filename));
  1215.                     Info(slide, 1, ((char *)slide, LoadFarString(TruncEAs),
  1216.                       makeword(G.extra_field+2)-10, "\n"));
  1217.                 } else if (!G.qflag)
  1218.                     (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  1219.             } else if (!G.qflag)
  1220.                 (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  1221.  
  1222.             /* set date/time stamps */
  1223.             SetPathAttrTimes(__G__ -1, 1);
  1224.  
  1225.             return IZ_CREATED_DIR;   /* dir time already set */
  1226.  
  1227.         } else if (G.extra_field && G.overwrite_all) {
  1228.             /* overwrite EAs of existing directory since user requested it */
  1229.             int err = EvalExtraFields(__G__ G.filename, G.extra_field,
  1230.                                       G.lrec.extra_field_length);
  1231.  
  1232.             if (err == IZ_EF_TRUNC) {
  1233.                 Info(slide, 0x421, ((char *)slide, "%-22s ", G.filename));
  1234.                 Info(slide, 0x401, ((char *)slide, LoadFarString(TruncEAs),
  1235.                   makeword(G.extra_field+2)-10, "\n"));
  1236.             }
  1237.  
  1238.             /* set date/time stamps (dirs only have creation times) */
  1239.             SetPathAttrTimes(__G__ -1, 1);
  1240.         }
  1241.         return 2;   /* dir existed already; don't look for data to extract */
  1242.     }
  1243.  
  1244.     if (*pathcomp == '\0') {
  1245.         Info(slide, 1, ((char *)slide, LoadFarString(ConversionFailed),
  1246.           G.filename));
  1247.         return 3;
  1248.     }
  1249.  
  1250.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  1251.     checkdir(__G__ G.filename, GETPATH);
  1252.     Trace((stderr, "mapname returns with filename = [%s] (error = %d)\n\n",
  1253.       G.filename, error));
  1254.  
  1255.     if (G.pInfo->vollabel) {    /* set the volume label now */
  1256.         VOLUMELABEL FSInfoBuf;
  1257. /* GRR:  "VOLUMELABEL" defined for IBM C and emx, but haven't checked MSC... */
  1258.  
  1259.         strcpy(FSInfoBuf.szVolLabel, G.filename);
  1260.         FSInfoBuf.cch = (BYTE)strlen(FSInfoBuf.szVolLabel);
  1261.  
  1262.         if (!G.qflag)
  1263.             Info(slide, 0, ((char *)slide, LoadFarString(Labelling),
  1264.               (char)(G.os2.nLabelDrive + 'a' - 1), G.filename));
  1265.         if (DosSetFSInfo(G.os2.nLabelDrive, FSIL_VOLSER, (PBYTE)&FSInfoBuf,
  1266.                          sizeof(VOLUMELABEL)))
  1267.         {
  1268.             Info(slide, 1, ((char *)slide, LoadFarString(ErrSetVolLabel)));
  1269.             return 3;
  1270.         }
  1271.         return 2;   /* success:  skip the "extraction" quietly */
  1272.     }
  1273.  
  1274.     return error;
  1275.  
  1276. } /* end function mapname() */
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282. /***********************/
  1283. /* Function checkdir() */
  1284. /***********************/
  1285.  
  1286. int checkdir(__G__ pathcomp, flag)
  1287.     __GDEF
  1288.     char *pathcomp;
  1289.     int flag;
  1290. /*
  1291.  * returns:  1 - (on APPEND_NAME) truncated filename
  1292.  *           2 - path doesn't exist, not allowed to create
  1293.  *           3 - path doesn't exist, tried to create and failed; or
  1294.  *               path exists and is not a directory, but is supposed to be
  1295.  *           4 - path is too long
  1296.  *          10 - can't allocate memory for filename buffers
  1297.  */
  1298. {
  1299.   /* moved to os2data.h so they can be global */
  1300. #if 0
  1301.     static int rootlen = 0;      /* length of rootpath */
  1302.     static char *rootpath;       /* user's "extract-to" directory */
  1303.     static char *buildpathHPFS;  /* full path (so far) to extracted file, */
  1304.     static char *buildpathFAT;   /*  both HPFS/EA (main) and FAT versions */
  1305.     static char *endHPFS;        /* corresponding pointers to end of */
  1306.     static char *endFAT;         /*  buildpath ('\0') */
  1307. #endif
  1308.  
  1309. #   define FN_MASK   7
  1310. #   define FUNCTION  (flag & FN_MASK)
  1311.  
  1312.  
  1313.  
  1314. /*---------------------------------------------------------------------------
  1315.     APPEND_DIR:  append the path component to the path being built and check
  1316.     for its existence.  If doesn't exist and we are creating directories, do
  1317.     so for this one; else signal success or error as appropriate.
  1318.   ---------------------------------------------------------------------------*/
  1319.  
  1320.     if (FUNCTION == APPEND_DIR) {
  1321.         char *p = pathcomp;
  1322.         int longdirEA, too_long=FALSE;
  1323.  
  1324.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  1325.         while ((*G.os2.endHPFS = *p++) != '\0')     /* copy to HPFS filename */
  1326.             ++G.os2.endHPFS;
  1327.         if (IsFileNameValid(G.os2.buildpathHPFS)) {
  1328.             longdirEA = FALSE;
  1329.             p = pathcomp;
  1330.             while ((*G.os2.endFAT = *p++) != '\0')  /* copy to FAT filename, too */
  1331.                 ++G.os2.endFAT;
  1332.         } else {
  1333.             longdirEA = TRUE;
  1334. /* GRR:  check error return? */
  1335.             map2fat(pathcomp, &G.os2.endFAT);  /* map, put in FAT fn, update endFAT */
  1336.         }
  1337.  
  1338.         /* GRR:  could do better check, see if overrunning buffer as we go:
  1339.          * check endHPFS-G.os2.buildpathHPFS after each append, set warning variable
  1340.          * if within 20 of FILNAMSIZ; then if var set, do careful check when
  1341.          * appending.  Clear variable when begin new path. */
  1342.  
  1343.         /* next check:  need to append '/', at least one-char name, '\0' */
  1344.         if ((G.os2.endHPFS-G.os2.buildpathHPFS) > FILNAMSIZ-3)
  1345.             too_long = TRUE;                 /* check if extracting dir? */
  1346. #ifdef MSC /* MSC 6.00 bug:  stat(non-existent-dir) == 0 [exists!] */
  1347.         if (GetFileTime(G.os2.buildpathFAT) == -1 || stat(G.os2.buildpathFAT, &G.statbuf))
  1348. #else
  1349.         if (stat(G.os2.buildpathFAT, &G.statbuf))    /* path doesn't exist */
  1350. #endif
  1351.         {
  1352.             if (!G.create_dirs) { /* told not to create (freshening) */
  1353.                 free(G.os2.buildpathHPFS);
  1354.                 free(G.os2.buildpathFAT);
  1355.                 return 2;         /* path doesn't exist:  nothing to do */
  1356.             }
  1357.             if (too_long) {   /* GRR:  should allow FAT extraction w/o EAs */
  1358.                 Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  1359.                   G.os2.buildpathHPFS));
  1360.                 free(G.os2.buildpathHPFS);
  1361.                 free(G.os2.buildpathFAT);
  1362.                 return 4;         /* no room for filenames:  fatal */
  1363.             }
  1364.             if (MKDIR(G.os2.buildpathFAT, 0777) == -1) {   /* create the directory */
  1365.                 Info(slide, 1, ((char *)slide, LoadFarString(CantCreateDir),
  1366.                   G.os2.buildpathFAT, G.filename));
  1367.                 free(G.os2.buildpathHPFS);
  1368.                 free(G.os2.buildpathFAT);
  1369.                 return 3;      /* path didn't exist, tried to create, failed */
  1370.             }
  1371.             G.os2.created_dir = TRUE;
  1372.             /* only set EA if creating directory */
  1373. /* GRR:  need trailing '/' before function call? */
  1374.             if (longdirEA) {
  1375. #ifdef DEBUG
  1376.                 int e =
  1377. #endif
  1378.                   SetLongNameEA(G.os2.buildpathFAT, pathcomp);
  1379.                 Trace((stderr, "APPEND_DIR:  SetLongNameEA() returns %d\n", e));
  1380.             }
  1381.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  1382.             Info(slide, 1, ((char *)slide, LoadFarString(DirIsntDirectory),
  1383.               G.os2.buildpathFAT, G.filename));
  1384.             free(G.os2.buildpathHPFS);
  1385.             free(G.os2.buildpathFAT);
  1386.             return 3;          /* path existed but wasn't dir */
  1387.         }
  1388.         if (too_long) {
  1389.             Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  1390.               G.os2.buildpathHPFS));
  1391.             free(G.os2.buildpathHPFS);
  1392.             free(G.os2.buildpathFAT);
  1393.             return 4;         /* no room for filenames:  fatal */
  1394.         }
  1395.         *G.os2.endHPFS++ = '/';
  1396.         *G.os2.endFAT++ = '/';
  1397.         *G.os2.endHPFS = *G.os2.endFAT = '\0';
  1398.         Trace((stderr, "buildpathHPFS now = [%s]\n", G.os2.buildpathHPFS));
  1399.         Trace((stderr, "buildpathFAT now =  [%s]\n", G.os2.buildpathFAT));
  1400.         return 0;
  1401.  
  1402.     } /* end if (FUNCTION == APPEND_DIR) */
  1403.  
  1404. /*---------------------------------------------------------------------------
  1405.     GETPATH:  copy full FAT path to the string pointed at by pathcomp (want
  1406.     filename to reflect name used on disk, not EAs; if full path is HPFS,
  1407.     buildpathFAT and buildpathHPFS will be identical).  Also free both paths.
  1408.   ---------------------------------------------------------------------------*/
  1409.  
  1410.     if (FUNCTION == GETPATH) {
  1411.         Trace((stderr, "getting and freeing FAT path [%s]\n", G.os2.buildpathFAT));
  1412.         Trace((stderr, "freeing HPFS path [%s]\n", G.os2.buildpathHPFS));
  1413.         strcpy(pathcomp, G.os2.buildpathFAT);
  1414.         free(G.os2.buildpathFAT);
  1415.         free(G.os2.buildpathHPFS);
  1416.         G.os2.buildpathHPFS = G.os2.buildpathFAT = G.os2.endHPFS = G.os2.endFAT = (char *)NULL;
  1417.         return 0;
  1418.     }
  1419.  
  1420. /*---------------------------------------------------------------------------
  1421.     APPEND_NAME:  assume the path component is the filename; append it and
  1422.     return without checking for existence.
  1423.   ---------------------------------------------------------------------------*/
  1424.  
  1425.     if (FUNCTION == APPEND_NAME) {
  1426.         char *p = pathcomp;
  1427.         int error = 0;
  1428.  
  1429.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  1430.         while ((*G.os2.endHPFS = *p++) != '\0') {    /* copy to HPFS filename */
  1431.             ++G.os2.endHPFS;
  1432.             if ((G.os2.endHPFS-G.os2.buildpathHPFS) >= FILNAMSIZ) {
  1433.                 *--G.os2.endHPFS = '\0';
  1434.                 Info(slide, 1, ((char *)slide, LoadFarString(PathTooLongTrunc),
  1435.                   G.filename, G.os2.buildpathHPFS));
  1436.                 error = 1;   /* filename truncated */
  1437.             }
  1438.         }
  1439.  
  1440. /* GRR:  how can longnameEA ever be set before this point???  we don't want
  1441.  * to save the original name to EAs if user renamed it, do we?
  1442.  *
  1443.  * if (!G.os2.longnameEA && ((G.os2.longnameEA = !IsFileNameValid(name)) != 0))
  1444.  */
  1445.         if (G.pInfo->vollabel || IsFileNameValid(G.os2.buildpathHPFS)) {
  1446.             G.os2.longnameEA = FALSE;
  1447.             p = pathcomp;
  1448.             while ((*G.os2.endFAT = *p++) != '\0')   /* copy to FAT filename, too */
  1449.                 ++G.os2.endFAT;
  1450.         } else {
  1451.             G.os2.longnameEA = TRUE;
  1452.             if ((G.os2.lastpathcomp = (char *)malloc(strlen(pathcomp)+1)) ==
  1453.                 (char *)NULL)
  1454.             {
  1455.                 Info(slide, 1, ((char *)slide,
  1456.                  "checkdir warning:  cannot save longname EA: out of memory\n"));
  1457.                 G.os2.longnameEA = FALSE;
  1458.                 error = 1;   /* can't set .LONGNAME extended attribute */
  1459.             } else           /* used and freed in close_outfile() */
  1460.                 strcpy(G.os2.lastpathcomp, pathcomp);
  1461.             map2fat(pathcomp, &G.os2.endFAT);  /* map, put in FAT fn, update endFAT */
  1462.         }
  1463.         Trace((stderr, "buildpathHPFS: %s\nbuildpathFAT:  %s\n",
  1464.           G.os2.buildpathHPFS, G.os2.buildpathFAT));
  1465.  
  1466.         return error;  /* could check for existence, prompt for new name... */
  1467.  
  1468.     } /* end if (FUNCTION == APPEND_NAME) */
  1469.  
  1470. /*---------------------------------------------------------------------------
  1471.     INIT:  allocate and initialize buffer space for the file currently being
  1472.     extracted.  If file was renamed with an absolute path, don't prepend the
  1473.     extract-to path.
  1474.   ---------------------------------------------------------------------------*/
  1475.  
  1476.     if (FUNCTION == INIT) {
  1477.         Trace((stderr, "initializing buildpathHPFS and buildpathFAT to "));
  1478.         if ((G.os2.buildpathHPFS = (char *)malloc(G.os2.fnlen+G.os2.rootlen+1)) == (char *)NULL)
  1479.             return 10;
  1480.         if ((G.os2.buildpathFAT = (char *)malloc(G.os2.fnlen+G.os2.rootlen+1)) == (char *)NULL) {
  1481.             free(G.os2.buildpathHPFS);
  1482.             return 10;
  1483.         }
  1484.         if (G.pInfo->vollabel) {  /* use root or renamed path, but don't store */
  1485. /* GRR:  for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */
  1486.             if (G.os2.renamed_fullpath && pathcomp[1] == ':')
  1487.                 *G.os2.buildpathHPFS = (char)ToLower(*pathcomp);
  1488.             else if (!G.os2.renamed_fullpath && G.os2.rootlen > 1 && G.os2.rootpath[1] == ':')
  1489.                 *G.os2.buildpathHPFS = (char)ToLower(*G.os2.rootpath);
  1490.             else {
  1491.                 ULONG lMap;
  1492.                 DosQueryCurrentDisk(&G.os2.nLabelDrive, &lMap);
  1493.                 *G.os2.buildpathHPFS = (char)(G.os2.nLabelDrive - 1 + 'a');
  1494.             }
  1495.             G.os2.nLabelDrive = *G.os2.buildpathHPFS - 'a' + 1;     /* save for mapname() */
  1496.             if (G.volflag == 0 || *G.os2.buildpathHPFS < 'a' ||   /* no labels/bogus? */
  1497.                 (G.volflag == 1 && !isfloppy(G.os2.nLabelDrive))) {  /* -$:  no fixed */
  1498.                 free(G.os2.buildpathHPFS);
  1499.                 free(G.os2.buildpathFAT);
  1500.                 return IZ_VOL_LABEL;   /* skipping with message */
  1501.             }
  1502.             *G.os2.buildpathHPFS = '\0';
  1503.         } else if (G.os2.renamed_fullpath)   /* pathcomp = valid data */
  1504.             strcpy(G.os2.buildpathHPFS, pathcomp);
  1505.         else if (G.os2.rootlen > 0)
  1506.             strcpy(G.os2.buildpathHPFS, G.os2.rootpath);
  1507.         else
  1508.             *G.os2.buildpathHPFS = '\0';
  1509.         G.os2.endHPFS = G.os2.buildpathHPFS;
  1510.         G.os2.endFAT = G.os2.buildpathFAT;
  1511.         while ((*G.os2.endFAT = *G.os2.endHPFS) != '\0') {
  1512.             ++G.os2.endFAT;
  1513.             ++G.os2.endHPFS;
  1514.         }
  1515.         Trace((stderr, "[%s]\n", G.os2.buildpathHPFS));
  1516.         return 0;
  1517.     }
  1518.  
  1519. /*---------------------------------------------------------------------------
  1520.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  1521.     sary; else assume it's a zipfile member and return.  This path segment
  1522.     gets used in extracting all members from every zipfile specified on the
  1523.     command line.  Note that under OS/2 and MS-DOS, if a candidate extract-to
  1524.     directory specification includes a drive letter (leading "x:"), it is
  1525.     treated just as if it had a trailing '/'--that is, one directory level
  1526.     will be created if the path doesn't exist, unless this is otherwise pro-
  1527.     hibited (e.g., freshening).
  1528.   ---------------------------------------------------------------------------*/
  1529.  
  1530. #if (!defined(SFX) || defined(SFX_EXDIR))
  1531.     if (FUNCTION == ROOT) {
  1532.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  1533.         if (pathcomp == (char *)NULL) {
  1534.             G.os2.rootlen = 0;
  1535.             return 0;
  1536.         }
  1537.         if ((G.os2.rootlen = strlen(pathcomp)) > 0) {
  1538.             int had_trailing_pathsep=FALSE, has_drive=FALSE, xtra=2;
  1539.  
  1540.             if (isalpha(pathcomp[0]) && pathcomp[1] == ':')
  1541.                 has_drive = TRUE;   /* drive designator */
  1542.             if (pathcomp[G.os2.rootlen-1] == '/') {
  1543.                 pathcomp[--G.os2.rootlen] = '\0';
  1544.                 had_trailing_pathsep = TRUE;
  1545.             }
  1546.             if (has_drive && (G.os2.rootlen == 2)) {
  1547.                 if (!had_trailing_pathsep)   /* i.e., original wasn't "x:/" */
  1548.                     xtra = 3;      /* room for '.' + '/' + 0 at end of "x:" */
  1549.             } else if (G.os2.rootlen > 0) {     /* need not check "x:." and "x:/" */
  1550. #ifdef MSC      /* MSC 6.00 bug:  stat(non-existent-dir) == 0 [exists!] */
  1551.                 if (GetFileTime(pathcomp) == -1 ||
  1552.                     SSTAT(pathcomp, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  1553. #else
  1554.                 if (SSTAT(pathcomp, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  1555. #endif
  1556.                 {   /* path does not exist */
  1557.                     if (!G.create_dirs                 /* || iswild(pathcomp) */
  1558.                                        ) {
  1559.                         G.os2.rootlen = 0;
  1560.                         return 2;   /* treat as stored file */
  1561.                     }
  1562.                     /* create directory (could add loop here to scan pathcomp
  1563.                      * and create more than one level, but really necessary?) */
  1564.                     if (MKDIR(pathcomp, 0777) == -1) {
  1565.                         Info(slide, 1, ((char *)slide,
  1566.                           LoadFarString(CantCreateExtractDir), pathcomp));
  1567.                         G.os2.rootlen = 0;   /* path didn't exist, tried to create, */
  1568.                         return 3;  /* failed:  file exists, or need 2+ levels */
  1569.                     }
  1570.                 }
  1571.             }
  1572.             if ((G.os2.rootpath = (char *)malloc(G.os2.rootlen+xtra)) == (char *)NULL) {
  1573.                 G.os2.rootlen = 0;
  1574.                 return 10;
  1575.             }
  1576.             strcpy(G.os2.rootpath, pathcomp);
  1577.             if (xtra == 3)                  /* had just "x:", make "x:." */
  1578.                 G.os2.rootpath[G.os2.rootlen++] = '.';
  1579.             G.os2.rootpath[G.os2.rootlen++] = '/';
  1580.             G.os2.rootpath[G.os2.rootlen] = '\0';
  1581.             Trace((stderr, "rootpath now = [%s]\n", G.os2.rootpath));
  1582.         }
  1583.         return 0;
  1584.     }
  1585. #endif /* !SFX || SFX_EXDIR */
  1586.  
  1587. /*---------------------------------------------------------------------------
  1588.     END:  free rootpath, immediately prior to program exit.
  1589.   ---------------------------------------------------------------------------*/
  1590.  
  1591.     if (FUNCTION == END) {
  1592.         Trace((stderr, "freeing rootpath\n"));
  1593.         if (G.os2.rootlen > 0) {
  1594.             free(G.os2.rootpath);
  1595.             G.os2.rootlen = 0;
  1596.         }
  1597.         return 0;
  1598.     }
  1599.  
  1600.     return 99;  /* should never reach */
  1601.  
  1602. } /* end function checkdir() */
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608. /***********************/
  1609. /* Function isfloppy() */   /* more precisely, is it removable? */
  1610. /***********************/
  1611.  
  1612. static int isfloppy(nDrive)
  1613.     int nDrive;   /* 1 == A:, 2 == B:, etc. */
  1614. {
  1615.     uch ParmList[1] = {0};
  1616.     uch DataArea[1] = {0};
  1617.     char Name[3];
  1618.     HFILE handle;
  1619. #ifdef __32BIT__
  1620.     ULONG rc;
  1621.     ULONG action;
  1622. #else
  1623.     USHORT rc;
  1624.     USHORT action;
  1625. #endif
  1626.  
  1627.  
  1628.     Name[0] = (char) (nDrive + 'A' - 1);
  1629.     Name[1] = ':';
  1630.     Name[2] = 0;
  1631.  
  1632.     rc = DosOpen(Name, &handle, &action, 0L, FILE_NORMAL, FILE_OPEN,
  1633.                  OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  1634.                  OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0L);
  1635.  
  1636.     if (rc == ERROR_NOT_READY)   /* must be removable */
  1637.       return TRUE;
  1638.     else if (rc) {   /* other error:  do default a/b heuristic instead */
  1639.       Trace((stderr, "error in DosOpen(DASD):  guessing...\n", rc));
  1640.       return (nDrive == 1 || nDrive == 2)? TRUE : FALSE;
  1641.     }
  1642.  
  1643.     rc = DosDevIOCtl(DataArea, sizeof(DataArea), ParmList, sizeof(ParmList),
  1644.                      DSK_BLOCKREMOVABLE, IOCTL_DISK, handle);
  1645.     DosClose(handle);
  1646.  
  1647.     if (rc) {   /* again, just check for a/b */
  1648.         Trace((stderr, "error in DosDevIOCtl category IOCTL_DISK, function "
  1649.           "DSK_BLOCKREMOVABLE\n  (rc = 0x%04x):  guessing...\n", rc));
  1650.         return (nDrive == 1 || nDrive == 2)? TRUE : FALSE;
  1651.     } else {
  1652.         return DataArea[0] ? FALSE : TRUE;
  1653.     }
  1654. } /* end function isfloppy() */
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660. static int IsFileNameValid(const char *name)
  1661. {
  1662.   HFILE hf;
  1663. #ifdef __32BIT__
  1664.   ULONG uAction;
  1665. #else
  1666.   USHORT uAction;
  1667. #endif
  1668.  
  1669.   switch( DosOpen(name, &hf, &uAction, 0, 0, FILE_OPEN,
  1670.                   OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0) )
  1671.   {
  1672.   case ERROR_INVALID_NAME:
  1673.   case ERROR_FILENAME_EXCED_RANGE:
  1674.     return FALSE;
  1675.   case NO_ERROR:
  1676.     DosClose(hf);
  1677.   default:
  1678.     return TRUE;
  1679.   }
  1680. }
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686. /**********************/
  1687. /* Function map2fat() */
  1688. /**********************/
  1689.  
  1690. static void map2fat(pathcomp, pEndFAT)
  1691.     char *pathcomp, **pEndFAT;
  1692. {
  1693.     char *ppc = pathcomp;          /* variable pointer to pathcomp */
  1694.     char *pEnd = *pEndFAT;         /* variable pointer to buildpathFAT */
  1695.     char *pBegin = *pEndFAT;       /* constant pointer to start of this comp. */
  1696.     char *last_dot = (char *)NULL; /* last dot not converted to underscore */
  1697.     int dotname = FALSE;           /* flag:  path component begins with dot */
  1698.                                    /*  ("." and ".." don't count) */
  1699.     register unsigned workch;      /* hold the character being tested */
  1700.  
  1701.  
  1702.     /* Only need check those characters which are legal in HPFS but not
  1703.      * in FAT:  to get here, must already have passed through mapname.
  1704.      * (GRR:  oops, small bug--if char was quoted, no longer have any
  1705.      * knowledge of that.)  Also must truncate path component to ensure
  1706.      * 8.3 compliance...
  1707.      */
  1708.     while ((workch = (uch)*ppc++) != 0) {
  1709.         switch (workch) {
  1710.             case '[':               /* add  '"'  '+'  ','  '='  ?? */
  1711.             case ']':
  1712.                 *pEnd++ = '_';      /* convert brackets to underscores */
  1713.                 break;
  1714.  
  1715.             case '.':
  1716.                 if (pEnd == *pEndFAT) {   /* nothing appended yet... */
  1717.                     if (*ppc == '\0')     /* don't bother appending a */
  1718.                         break;            /*  "./" component to the path */
  1719.                     else if (*ppc == '.' && ppc[1] == '\0') {   /* "../" */
  1720.                         *pEnd++ = '.';    /* add first dot, unchanged... */
  1721.                         ++ppc;            /* skip second dot, since it will */
  1722.                     } else {              /*  be "added" at end of if-block */
  1723.                         *pEnd++ = '_';    /* FAT doesn't allow null filename */
  1724.                         dotname = TRUE;   /*  bodies, so map .exrc -> _.exrc */
  1725.                     }                     /*  (extra '_' now, "dot" below) */
  1726.                 } else if (dotname) {     /* found a second dot, but still */
  1727.                     dotname = FALSE;      /*  have extra leading underscore: */
  1728.                     *pEnd = '\0';         /*  remove it by shifting chars */
  1729.                     pEnd = *pEndFAT + 1;  /*  left one space (e.g., .p1.p2: */
  1730.                     while (pEnd[1]) {     /*  __p1 -> _p1_p2 -> _p1.p2 when */
  1731.                         *pEnd = pEnd[1];  /*  finished) [opt.:  since first */
  1732.                         ++pEnd;           /*  two chars are same, can start */
  1733.                     }                     /*  shifting at second position] */
  1734.                 }
  1735.                 last_dot = pEnd;    /* point at last dot so far... */
  1736.                 *pEnd++ = '_';      /* convert dot to underscore for now */
  1737.                 break;
  1738.  
  1739.             default:
  1740.                 *pEnd++ = (char)workch;
  1741.  
  1742.         } /* end switch */
  1743.     } /* end while loop */
  1744.  
  1745.     *pEnd = '\0';                 /* terminate buildpathFAT */
  1746.  
  1747.     /* NOTE:  keep in mind that pEnd points to the end of the path
  1748.      * component, and *pEndFAT still points to the *beginning* of it...
  1749.      * Also note that the algorithm does not try to get too fancy:
  1750.      * if there are no dots already, the name either gets truncated
  1751.      * at 8 characters or the last underscore is converted to a dot
  1752.      * (only if more characters are saved that way).  In no case is
  1753.      * a dot inserted between existing characters.
  1754.      */
  1755.     if (last_dot == (char *)NULL) {  /* no dots:  check for underscores... */
  1756.         char *plu = strrchr(pBegin, '_');    /* pointer to last underscore */
  1757.  
  1758.         if (plu == (char *)NULL) { /* no dots, no underscores:  truncate at 8 */
  1759.             *pEndFAT += 8;         /* chars (could insert '.' and keep 11...) */
  1760.             if (*pEndFAT > pEnd)
  1761.                 *pEndFAT = pEnd;   /* oops...didn't have 8 chars to truncate */
  1762.             else
  1763.                 **pEndFAT = '\0';
  1764.         } else if (MIN(plu - pBegin, 8) + MIN(pEnd - plu - 1, 3) > 8) {
  1765.             last_dot = plu;        /* be lazy:  drop through to next if-block */
  1766.         } else if ((pEnd - *pEndFAT) > 8) {
  1767.             *pEndFAT += 8;         /* more fits into just basename than if */
  1768.             **pEndFAT = '\0';      /*  convert last underscore to dot */
  1769.         } else
  1770.             *pEndFAT = pEnd;       /* whole thing fits into 8 chars or less */
  1771.     }
  1772.  
  1773.     if (last_dot != (char *)NULL) {   /* one dot (or two, in the case of */
  1774.         *last_dot = '.';              /*  "..") is OK:  put it back in */
  1775.  
  1776.         if ((last_dot - pBegin) > 8) {
  1777.             char *p, *q;
  1778.             int i;
  1779.  
  1780.             p = last_dot;
  1781.             q = last_dot = pBegin + 8;
  1782.             for (i = 0;  (i < 4) && *p;  ++i)  /* too many chars in basename: */
  1783.                 *q++ = *p++;                   /*  shift ".ext" left and */
  1784.             *q = '\0';                         /*  truncate/terminate it */
  1785.             *pEndFAT = q;
  1786.         } else if ((pEnd - last_dot) > 4) {    /* too many chars in extension */
  1787.             *pEndFAT = last_dot + 4;
  1788.             **pEndFAT = '\0';
  1789.         } else
  1790.             *pEndFAT = pEnd;   /* filename is fine; point at terminating zero */
  1791.  
  1792.         if ((last_dot - pBegin) > 0 && last_dot[-1] == ' ')
  1793.             last_dot[-1] = '_';                /* NO blank in front of '.'! */
  1794.     }
  1795. } /* end function map2fat() */
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801. static int SetLongNameEA(char *name, char *longname)
  1802. {
  1803.   EAOP eaop;
  1804.   FEALST fealst;
  1805.  
  1806.   eaop.fpFEAList = (PFEALIST) &fealst;
  1807.   eaop.fpGEAList = NULL;
  1808.   eaop.oError = 0;
  1809.  
  1810.   strcpy((char *) fealst.szName, ".LONGNAME");
  1811.   strcpy((char *) fealst.szValue, longname);
  1812.  
  1813.   fealst.cbList  = sizeof(fealst) - CCHMAXPATH + strlen((char *) fealst.szValue);
  1814.   fealst.cbName  = (BYTE) strlen((char *) fealst.szName);
  1815.   fealst.cbValue = sizeof(USHORT) * 2 + strlen((char *) fealst.szValue);
  1816.  
  1817. #ifdef __32BIT__
  1818.   fealst.oNext   = 0;
  1819. #endif
  1820.   fealst.fEA     = 0;
  1821.   fealst.eaType  = 0xFFFD;
  1822.   fealst.eaSize  = strlen((char *) fealst.szValue);
  1823.  
  1824.   return DosSetPathInfo(name, FIL_QUERYEASIZE,
  1825.                         (PBYTE) &eaop, sizeof(eaop), 0);
  1826. }
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832. /****************************/
  1833. /* Function close_outfile() */
  1834. /****************************/
  1835.  
  1836.            /* GRR:  need to return error level!! */
  1837.  
  1838. void close_outfile(__G)   /* only for extracted files, not directories */
  1839.     __GDEF
  1840. {
  1841.     fclose(G.outfile);
  1842.  
  1843.     /* set extra fields, both stored-in-zipfile and .LONGNAME flavors */
  1844.     if (G.extra_field) {    /* zipfile extra field may have extended attribs */
  1845.         int err = EvalExtraFields(__G__ G.filename, G.extra_field,
  1846.                                   G.lrec.extra_field_length);
  1847.  
  1848.         if (err == IZ_EF_TRUNC) {
  1849.             if (G.qflag)
  1850.                 Info(slide, 1, ((char *)slide, "%-22s ", G.filename));
  1851.             Info(slide, 1, ((char *)slide, LoadFarString(TruncEAs),
  1852.               makeword(G.extra_field+2)-10, G.qflag? "\n" : ""));
  1853.         }
  1854.     }
  1855.  
  1856.     if (G.os2.longnameEA) {
  1857. #ifdef DEBUG
  1858.         int e =
  1859. #endif
  1860.           SetLongNameEA(G.filename, G.os2.lastpathcomp);
  1861.         Trace((stderr, "close_outfile:  SetLongNameEA() returns %d\n", e));
  1862.         free(G.os2.lastpathcomp);
  1863.     }
  1864.  
  1865.     /* set date/time and permissions */
  1866.     SetPathAttrTimes(__G__ G.pInfo->file_attr, 0);
  1867.  
  1868. } /* end function close_outfile() */
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874. /******************************/
  1875. /* Function check_for_newer() */
  1876. /******************************/
  1877.  
  1878. int check_for_newer(__G__ filename)   /* return 1 if existing file newer or equal; */
  1879.     __GDEF
  1880.     char *filename;             /*  0 if older; -1 if doesn't exist yet */
  1881. {
  1882.     ulg existing, archive;
  1883. #ifdef USE_EF_UT_TIME
  1884.     iztimes z_utime;
  1885. #endif
  1886.  
  1887.     if ((existing = (ulg)GetFileTime(filename)) == (ulg)-1)
  1888.         return DOES_NOT_EXIST;
  1889.  
  1890. #ifdef USE_EF_UT_TIME
  1891.     if (G.extra_field &&
  1892.         (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  1893.                           G.lrec.last_mod_file_date, &z_utime, NULL)
  1894.          & EB_UT_FL_MTIME))
  1895.     {
  1896.         TTrace((stderr, "check_for_newer:  using Unix extra field mtime\n"));
  1897.         archive = Utime2DosDateTime(z_utime.mtime);
  1898.     } else {
  1899.         archive = ((ulg) G.lrec.last_mod_file_date) << 16 |
  1900.                   G.lrec.last_mod_file_time;
  1901.     }
  1902. #else /* !USE_EF_UT_TIME */
  1903.     archive = ((ulg) G.lrec.last_mod_file_date) << 16 |
  1904.               G.lrec.last_mod_file_time;
  1905. #endif /* ?USE_EF_UT_TIME */
  1906.  
  1907.     return (existing >= archive);
  1908. } /* end function check_for_newer() */
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914. #ifndef SFX
  1915.  
  1916. /*************************/
  1917. /* Function dateformat() */
  1918. /*************************/
  1919.  
  1920. int dateformat()
  1921. {
  1922. /*-----------------------------------------------------------------------------
  1923.   For those operating systems which support it, this function returns a value
  1924.   which tells how national convention says that numeric dates are displayed.
  1925.   Return values are DF_YMD, DF_DMY and DF_MDY.
  1926.  -----------------------------------------------------------------------------*/
  1927.  
  1928.     switch (GetCountryInfo()) {
  1929.         case 0:
  1930.             return DF_MDY;
  1931.         case 1:
  1932.             return DF_DMY;
  1933.         case 2:
  1934.             return DF_YMD;
  1935.     }
  1936.     return DF_MDY;   /* default if error */
  1937.  
  1938. } /* end function dateformat() */
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944. /************************/
  1945. /*  Function version()  */
  1946. /************************/
  1947.  
  1948. void version(__G)
  1949.     __GDEF
  1950. {
  1951.     int len;
  1952. #if defined(__IBMC__) || defined(__WATCOMC__) || defined(_MSC_VER)
  1953.     char buf[80];
  1954. #endif
  1955.  
  1956.     len = sprintf((char *)slide, LoadFarString(CompiledWith),
  1957.  
  1958. #if defined(__GNUC__)
  1959. #  ifdef __EMX__  /* __EMX__ is defined as "1" only (sigh) */
  1960.       "emx+gcc ", __VERSION__,
  1961. #  else
  1962.       "gcc/2 ", __VERSION__,
  1963. #  endif
  1964. #elif defined(__IBMC__)
  1965.       "IBM ",
  1966. #  if (__IBMC__ < 200)
  1967.       (sprintf(buf, "C Set/2 %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  1968. #  elif (__IBMC__ < 300)
  1969.       (sprintf(buf, "C Set++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  1970. #  else
  1971.       (sprintf(buf, "Visual Age C++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  1972. #  endif
  1973. #elif defined(__WATCOMC__)
  1974.       "Watcom C", (sprintf(buf, " (__WATCOMC__ = %d)", __WATCOMC__), buf),
  1975. #elif defined(__TURBOC__)
  1976. #  ifdef __BORLANDC__
  1977.       "Borland C++",
  1978. #    if (__BORLANDC__ < 0x0460)
  1979.         " 1.0",
  1980. #    elif (__BORLANDC__ == 0x0460)
  1981.         " 1.5",                     /* from Kai Uwe:  three less than DOS */
  1982. #    else
  1983.         " 2.0",                     /* (__BORLANDC__ == 0x0500)? */
  1984. #    endif
  1985. #  else
  1986.       "Turbo C",                    /* these are probably irrelevant */
  1987. #    if (__TURBOC__ >= 661)
  1988.        "++ 1.0 or later",
  1989. #    elif (__TURBOC__ == 661)
  1990.        " 3.0?",
  1991. #    elif (__TURBOC__ == 397)
  1992.        " 2.0",
  1993. #    else
  1994.        " 1.0 or 1.5?",
  1995. #    endif
  1996. #  endif
  1997. #elif defined(MSC)
  1998.       "Microsoft C ",
  1999. #  ifdef _MSC_VER
  2000.       (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),
  2001. #  else
  2002.       "5.1 or earlier",
  2003. #  endif
  2004. #else
  2005.       "unknown compiler", "",
  2006. #endif /* ?compilers */
  2007.  
  2008.       "OS/2",
  2009.  
  2010. /* GRR:  does IBM C/2 identify itself as IBM rather than Microsoft? */
  2011. #if (defined(MSC) || (defined(__WATCOMC__) && !defined(__386__)))
  2012. #  if defined(M_I86HM) || defined(__HUGE__)
  2013.       " (16-bit, huge)",
  2014. #  elif defined(M_I86LM) || defined(__LARGE__)
  2015.       " (16-bit, large)",
  2016. #  elif defined(M_I86MM) || defined(__MEDIUM__)
  2017.       " (16-bit, medium)",
  2018. #  elif defined(M_I86CM) || defined(__COMPACT__)
  2019.       " (16-bit, compact)",
  2020. #  elif defined(M_I86SM) || defined(__SMALL__)
  2021.       " (16-bit, small)",
  2022. #  elif defined(M_I86TM) || defined(__TINY__)
  2023.       " (16-bit, tiny)",
  2024. #  else
  2025.       " (16-bit)",
  2026. #  endif
  2027. #else
  2028.       " 2.x/3.x (32-bit)",
  2029. #endif
  2030.  
  2031. #ifdef __DATE__
  2032.       " on ", __DATE__
  2033. #else
  2034.       "", ""
  2035. #endif
  2036.     );
  2037.  
  2038.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  2039.                                 /* MSC can't handle huge macro expansions */
  2040.  
  2041.     /* temporary debugging code for Borland compilers only */
  2042.     /* __TCPLUSPLUS__, __BCPLUSPLUS__ not defined for v1.5 */
  2043. #if (defined(__TURBOC__) && defined(DEBUG))
  2044.     Info(slide, 0, ((char *)slide, "\t(__TURBOC__ = 0x%04x = %d)\n", __TURBOC__,
  2045.       __TURBOC__));
  2046. #ifdef __BORLANDC__
  2047.     Info(slide, 0, ((char *)slide, "\t(__BORLANDC__ = 0x%04x)\n",__BORLANDC__));
  2048. #else
  2049.     Info(slide, 0, ((char *)slide, "\tdebug(__BORLANDC__ not defined)\n"));
  2050. #endif
  2051. #endif /* __TURBOC__ && DEBUG */
  2052.  
  2053. } /* end function version() */
  2054.  
  2055. #endif /* !SFX */
  2056.  
  2057.  
  2058.  
  2059. /* This table can be static because it is pseudo-constant */
  2060. static unsigned char cUpperCase[256], cLowerCase[256];
  2061. static BOOL bInitialized=FALSE;
  2062.  
  2063. /* Initialize the tables of upper- and lowercase characters, including
  2064.    handling of country-dependent characters. */
  2065.  
  2066. static void InitNLS(void)
  2067. {
  2068.   unsigned nCnt, nU;
  2069.   COUNTRYCODE cc;
  2070.  
  2071.   if (bInitialized == FALSE) {
  2072.     bInitialized = TRUE;
  2073.  
  2074.     for ( nCnt = 0; nCnt < 256; nCnt++ )
  2075.       cUpperCase[nCnt] = cLowerCase[nCnt] = (unsigned char) nCnt;
  2076.  
  2077.     cc.country = cc.codepage = 0;
  2078.     DosMapCase(sizeof(cUpperCase), &cc, (PCHAR) cUpperCase);
  2079.  
  2080.     for ( nCnt = 0; nCnt < 256; nCnt++ ) {
  2081.       nU = cUpperCase[nCnt];
  2082.       if (nU != nCnt && cLowerCase[nU] == (unsigned char) nU)
  2083.         cLowerCase[nU] = (unsigned char) nCnt;
  2084.     }
  2085.  
  2086.     for ( nCnt = 'A'; nCnt <= 'Z'; nCnt++ )
  2087.       cLowerCase[nCnt] = (unsigned char) (nCnt - 'A' + 'a');
  2088.   }
  2089. }
  2090.  
  2091.  
  2092. int IsUpperNLS(int nChr)
  2093. {
  2094.   return (cUpperCase[nChr] == (unsigned char) nChr);
  2095. }
  2096.  
  2097.  
  2098. int ToLowerNLS(int nChr)
  2099. {
  2100.   return cLowerCase[nChr];
  2101. }
  2102.  
  2103.  
  2104. char *StringLower(char *szArg)
  2105. {
  2106.   unsigned char *szPtr;
  2107.  
  2108.   for ( szPtr = (unsigned char *) szArg; *szPtr; szPtr++ )
  2109.     *szPtr = cLowerCase[*szPtr];
  2110.   return szArg;
  2111. }
  2112.  
  2113.  
  2114. #if defined(__IBMC__) && defined(__DEBUG_ALLOC__)
  2115. void DebugMalloc(void)
  2116. {
  2117.   _dump_allocated(0); /* print out debug malloc memory statistics */
  2118. }
  2119. #endif
  2120.  
  2121.  
  2122. #if defined(REENTRANT) && defined(USETHREADID)
  2123. ulg GetThreadId(void)
  2124. {
  2125.   PTIB   pptib;       /* Address of a pointer to the
  2126.                          Thread Information Block */
  2127.   PPIB   pppib;       /* Address of a pointer to the
  2128.                          Process Information Block */
  2129.  
  2130.   DosGetInfoBlocks(&pptib, &pppib);
  2131.   return pptib->tib_ptib2->tib2_ultid;
  2132. }
  2133. #endif /* defined(REENTRANT) && defined(USETHREADID) */
  2134.  
  2135.  
  2136. void os2GlobalsCtor(__GPRO)
  2137. {
  2138.   G.os2.nLastDrive = (USHORT)(-1);
  2139.   G.os2.firstcall = TRUE;
  2140.  
  2141. #ifdef OS2DLL
  2142.   G.os2.rexx_mes = "0";
  2143. #endif
  2144.  
  2145.   InitNLS();
  2146. }
  2147.