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