home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / os2 / os2zip.c < prev    next >
C/C++ Source or Header  |  1996-04-05  |  24KB  |  1,046 lines

  1. /*
  2.  * @(#)dir.c 1.4 87/11/06 Public Domain.
  3.  *
  4.  *  A public domain implementation of BSD directory routines for
  5.  *  MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
  6.  *  August 1897
  7.  *  Ported to OS/2 by Kai Uwe Rommel
  8.  *  December 1989, February 1990
  9.  *  Change for HPFS support, October 1990
  10.  */
  11.  
  12. /* does also contain EA access code for use in ZIP */
  13.  
  14.  
  15. #ifdef OS2
  16.  
  17.  
  18. #if defined(__EMX__) && !defined(__32BIT__)
  19. #  define __32BIT__
  20. #endif
  21.  
  22. #include "zip.h"
  23.  
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #ifndef __BORLANDC__
  28. #include <malloc.h>
  29. #endif
  30.  
  31. #define INCL_NOPM
  32. #define INCL_DOSNLS
  33. #define INCL_DOSERRORS
  34. #include <os2.h>
  35.  
  36. #include "os2zip.h"
  37. #include "os2acl.h"
  38.  
  39.  
  40. #ifndef max
  41. #define max(a, b) ((a) < (b) ? (b) : (a))
  42. #endif
  43.  
  44.  
  45. #ifdef __32BIT__
  46. #define DosFindFirst(p1, p2, p3, p4, p5, p6) \
  47.         DosFindFirst(p1, p2, p3, p4, p5, p6, 1)
  48. #else
  49. #define DosQueryCurrentDisk DosQCurDisk
  50. #define DosQueryFSAttach(p1, p2, p3, p4, p5) \
  51.         DosQFSAttach(p1, p2, p3, p4, p5, 0)
  52. #define DosQueryFSInfo(d, l, b, s) \
  53.         DosQFSInfo(d, l, b, s)
  54. #define DosQueryPathInfo(p1, p2, p3, p4) \
  55.         DosQPathInfo(p1, p2, p3, p4, 0)
  56. #define DosSetPathInfo(p1, p2, p3, p4, p5) \
  57.         DosSetPathInfo(p1, p2, p3, p4, p5, 0)
  58. #define DosEnumAttribute(p1, p2, p3, p4, p5, p6, p7) \
  59.         DosEnumAttribute(p1, p2, p3, p4, p5, p6, p7, 0)
  60. #define DosFindFirst(p1, p2, p3, p4, p5, p6) \
  61.         DosFindFirst(p1, p2, p3, p4, p5, p6, 0)
  62. #define DosMapCase DosCaseMap
  63. #endif
  64.  
  65.  
  66. #ifndef UTIL
  67.  
  68. extern int noisy;
  69.  
  70. #ifndef S_IFMT
  71. #define S_IFMT 0xF000
  72. #endif
  73.  
  74. static int attributes = _A_DIR | _A_HIDDEN | _A_SYSTEM;
  75.  
  76. static char *getdirent(char *);
  77. static void free_dircontents(struct _dircontents *);
  78.  
  79. #ifdef __32BIT__
  80. static HDIR hdir;
  81. static ULONG count;
  82. static FILEFINDBUF3 find;
  83. #else
  84. static HDIR hdir;
  85. static USHORT count;
  86. static FILEFINDBUF find;
  87. #endif
  88.  
  89. DIR *opendir(const char *name)
  90. {
  91.   struct stat statb;
  92.   DIR *dirp;
  93.   char c;
  94.   char *s;
  95.   struct _dircontents *dp;
  96.   char nbuf[MAXPATHLEN + 1];
  97.   int len;
  98.  
  99.   attributes = hidden_files ? (_A_DIR | _A_HIDDEN | _A_SYSTEM) : _A_DIR;
  100.  
  101.   strcpy(nbuf, name);
  102.   if ((len = strlen(nbuf)) == 0)
  103.     return NULL;
  104.  
  105.   if (((c = nbuf[len - 1]) == '\\' || c == '/') && (len > 1))
  106.   {
  107.     nbuf[len - 1] = 0;
  108.     --len;
  109.  
  110.     if (nbuf[len - 1] == ':')
  111.     {
  112.       strcpy(nbuf+len, "\\.");
  113.       len += 2;
  114.     }
  115.   }
  116.   else
  117.     if (nbuf[len - 1] == ':')
  118.     {
  119.       strcpy(nbuf+len, ".");
  120.       ++len;
  121.     }
  122.  
  123. #ifndef __BORLANDC__
  124.   /* when will we ever see a Borland compiler that can properly stat !!! */
  125.   if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  126.     return NULL;
  127. #endif
  128.  
  129.   if ((dirp = malloc(sizeof(DIR))) == NULL)
  130.     return NULL;
  131.  
  132.   if (nbuf[len - 1] == '.' && (len == 1 || nbuf[len - 2] != '.'))
  133.     strcpy(nbuf+len-1, "*.*");
  134.   else
  135.     if (((c = nbuf[len - 1]) == '\\' || c == '/') && (len == 1))
  136.       strcpy(nbuf+len, "*");
  137.     else
  138.       strcpy(nbuf+len, "\\*");
  139.  
  140.   /* len is no longer correct (but no longer needed) */
  141.  
  142.   dirp -> dd_loc = 0;
  143.   dirp -> dd_contents = dirp -> dd_cp = NULL;
  144.  
  145.   if ((s = getdirent(nbuf)) == NULL)
  146.     return dirp;
  147.  
  148.   do
  149.   {
  150.     if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
  151.         ((dp -> _d_entry = malloc(strlen(s) + 1)) == NULL)      )
  152.     {
  153.       if (dp)
  154.         free(dp);
  155.       free_dircontents(dirp -> dd_contents);
  156.  
  157.       return NULL;
  158.     }
  159.  
  160.     if (dirp -> dd_contents)
  161.     {
  162.       dirp -> dd_cp -> _d_next = dp;
  163.       dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  164.     }
  165.     else
  166.       dirp -> dd_contents = dirp -> dd_cp = dp;
  167.  
  168.     strcpy(dp -> _d_entry, s);
  169.     dp -> _d_next = NULL;
  170.  
  171.     dp -> _d_size = find.cbFile;
  172.     dp -> _d_mode = find.attrFile;
  173.     dp -> _d_time = *(unsigned *) &(find.ftimeLastWrite);
  174.     dp -> _d_date = *(unsigned *) &(find.fdateLastWrite);
  175.   }
  176.   while ((s = getdirent(NULL)) != NULL);
  177.  
  178.   dirp -> dd_cp = dirp -> dd_contents;
  179.  
  180.   return dirp;
  181. }
  182.  
  183. void closedir(DIR * dirp)
  184. {
  185.   free_dircontents(dirp -> dd_contents);
  186.   free(dirp);
  187. }
  188.  
  189. struct dirent *readdir(DIR * dirp)
  190. {
  191.   static struct dirent dp;
  192.  
  193.   if (dirp -> dd_cp == NULL)
  194.     return NULL;
  195.  
  196.   dp.d_namlen = dp.d_reclen =
  197.     strlen(strcpy(dp.d_name, dirp -> dd_cp -> _d_entry));
  198.  
  199.   dp.d_ino = 0;
  200.  
  201.   dp.d_size = dirp -> dd_cp -> _d_size;
  202.   dp.d_mode = dirp -> dd_cp -> _d_mode;
  203.   dp.d_time = dirp -> dd_cp -> _d_time;
  204.   dp.d_date = dirp -> dd_cp -> _d_date;
  205.  
  206.   dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  207.   dirp -> dd_loc++;
  208.  
  209.   return &dp;
  210. }
  211.  
  212. void seekdir(DIR * dirp, long off)
  213. {
  214.   long i = off;
  215.   struct _dircontents *dp;
  216.  
  217.   if (off >= 0)
  218.   {
  219.     for (dp = dirp -> dd_contents; --i >= 0 && dp; dp = dp -> _d_next);
  220.  
  221.     dirp -> dd_loc = off - (i + 1);
  222.     dirp -> dd_cp = dp;
  223.   }
  224. }
  225.  
  226. long telldir(DIR * dirp)
  227. {
  228.   return dirp -> dd_loc;
  229. }
  230.  
  231. static void free_dircontents(struct _dircontents * dp)
  232. {
  233.   struct _dircontents *odp;
  234.  
  235.   while (dp)
  236.   {
  237.     if (dp -> _d_entry)
  238.       free(dp -> _d_entry);
  239.  
  240.     dp = (odp = dp) -> _d_next;
  241.     free(odp);
  242.   }
  243. }
  244.  
  245. static char *getdirent(char *dir)
  246. {
  247.   int done;
  248.   static int lower;
  249.  
  250.   if (dir != NULL)
  251.   {                                    /* get first entry */
  252.     hdir = HDIR_SYSTEM;
  253.     count = 1;
  254.     done = DosFindFirst(dir, &hdir, attributes, &find, sizeof(find), &count);
  255.     lower = IsFileSystemFAT(dir);
  256.   }
  257.   else                                 /* get next entry */
  258.     done = DosFindNext(hdir, &find, sizeof(find), &count);
  259.  
  260.   if (done == 0)
  261.   {
  262.     if (lower)
  263.       StringLower(find.achName);
  264.     return find.achName;
  265.   }
  266.   else
  267.   {
  268.     DosFindClose(hdir);
  269.     return NULL;
  270.   }
  271. }
  272.  
  273. /* FAT / HPFS detection */
  274.  
  275. int IsFileSystemFAT(char *dir)
  276. {
  277.   static USHORT nLastDrive = -1, nResult;
  278.   ULONG lMap;
  279.   BYTE bData[64];
  280.   char bName[3];
  281. #ifdef __32BIT__
  282.   ULONG nDrive, cbData;
  283.   PFSQBUFFER2 pData = (PFSQBUFFER2) bData;
  284. #else
  285.   USHORT nDrive, cbData;
  286.   PFSQBUFFER pData = (PFSQBUFFER) bData;
  287. #endif
  288.  
  289.   /* We separate FAT and HPFS+other file systems here.
  290.      at the moment I consider other systems to be similar to HPFS,
  291.      i.e. support long file names and beeing case sensitive */
  292.  
  293.   if (isalpha(dir[0]) && (dir[1] == ':'))
  294.     nDrive = to_up(dir[0]) - '@';
  295.   else
  296.     DosQueryCurrentDisk(&nDrive, &lMap);
  297.  
  298.   if (nDrive == nLastDrive)
  299.     return nResult;
  300.  
  301.   bName[0] = (char) (nDrive + '@');
  302.   bName[1] = ':';
  303.   bName[2] = 0;
  304.  
  305.   nLastDrive = nDrive;
  306.   cbData = sizeof(bData);
  307.  
  308.   if (!DosQueryFSAttach(bName, 0, FSAIL_QUERYNAME, (PVOID) pData, &cbData))
  309.     nResult = !strcmp((char *) pData -> szFSDName + pData -> cbName, "FAT");
  310.   else
  311.     nResult = FALSE;
  312.  
  313.   /* End of this ugly code */
  314.   return nResult;
  315. }
  316.  
  317. /* access mode bits and time stamp */
  318.  
  319. int GetFileMode(char *name)
  320. {
  321. #ifdef __32BIT__
  322.   FILESTATUS3 fs;
  323.   return DosQueryPathInfo(name, 1, &fs, sizeof(fs)) ? -1 : fs.attrFile;
  324. #else
  325.   USHORT mode;
  326.   return DosQFileMode(name, &mode, 0L) ? -1 : mode;
  327. #endif
  328. }
  329.  
  330. long GetFileTime(char *name)
  331. {
  332. #ifdef __32BIT__
  333.   FILESTATUS3 fs;
  334. #else
  335.   FILESTATUS fs;
  336. #endif
  337.   USHORT nDate, nTime;
  338.  
  339.   if (DosQueryPathInfo(name, 1, (PBYTE) &fs, sizeof(fs)))
  340.     return -1;
  341.  
  342.   nDate = * (USHORT *) &fs.fdateLastWrite;
  343.   nTime = * (USHORT *) &fs.ftimeLastWrite;
  344.  
  345.   return ((ULONG) nDate) << 16 | nTime;
  346. }
  347.  
  348. void SetFileTime(char *path, long stamp)
  349. {
  350.   FILESTATUS fs;
  351.   USHORT fd, ft;
  352.  
  353.   if (DosQueryPathInfo(path, FIL_STANDARD, (PBYTE) &fs, sizeof(fs)))
  354.     return;
  355.  
  356.   fd = (USHORT) (stamp >> 16);
  357.   ft = (USHORT) stamp;
  358.   fs.fdateLastWrite = fs.fdateCreation = * (FDATE *) &fd;
  359.   fs.ftimeLastWrite = fs.ftimeCreation = * (FTIME *) &ft;
  360.  
  361.   DosSetPathInfo(path, FIL_STANDARD, (PBYTE) &fs, sizeof(fs), 0);
  362. }
  363.  
  364. /* read volume label */
  365.  
  366. char *getVolumeLabel(int drive, unsigned long *vtime, unsigned long *vmode,
  367.                      time_t *utim)
  368. {
  369.   static FSINFO fi;
  370.  
  371.   if (DosQueryFSInfo(drive ? drive - 'A' + 1 : 0,
  372.              FSIL_VOLSER, (PBYTE) &fi, sizeof(fi)))
  373.     return NULL;
  374.  
  375.   time(utim);
  376.   *vtime = unix2dostime(utim);
  377.   *vmode = _A_VOLID | _A_ARCHIVE;
  378.  
  379.   return (fi.vol.cch > 0) ? fi.vol.szVolLabel : NULL;
  380. }
  381.  
  382. /* FAT / HPFS name conversion stuff */
  383.  
  384. int IsFileNameValid(char *name)
  385. {
  386.   HFILE hf;
  387. #ifdef __32BIT__
  388.   ULONG uAction;
  389. #else
  390.   USHORT uAction;
  391. #endif
  392.  
  393.   switch(DosOpen(name, &hf, &uAction, 0, 0, FILE_OPEN,
  394.          OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0))
  395.   {
  396.   case ERROR_INVALID_NAME:
  397.   case ERROR_FILENAME_EXCED_RANGE:
  398.     return FALSE;
  399.   case NO_ERROR:
  400.     DosClose(hf);
  401.   default:
  402.     return TRUE;
  403.   }
  404. }
  405.  
  406. void ChangeNameForFAT(char *name)
  407. {
  408.   char *src, *dst, *next, *ptr, *dot, *start;
  409.   static char invalid[] = ":;,=+\"[]<>| \t";
  410.  
  411.   if (isalpha(name[0]) && (name[1] == ':'))
  412.     start = name + 2;
  413.   else
  414.     start = name;
  415.  
  416.   src = dst = start;
  417.   if ((*src == '/') || (*src == '\\'))
  418.     src++, dst++;
  419.  
  420.   while (*src)
  421.   {
  422.     for (next = src; *next && (*next != '/') && (*next != '\\'); next++);
  423.  
  424.     for (ptr = src, dot = NULL; ptr < next; ptr++)
  425.       if (*ptr == '.')
  426.       {
  427.         dot = ptr; /* remember last dot */
  428.         *ptr = '_';
  429.       }
  430.  
  431.     if (dot == NULL)
  432.       for (ptr = src; ptr < next; ptr++)
  433.         if (*ptr == '_')
  434.           dot = ptr; /* remember last _ as if it were a dot */
  435.  
  436.     if (dot && (dot > src) &&
  437.     ((next - dot <= 4) ||
  438.      ((next - src > 8) && (dot - src > 3))))
  439.     {
  440.       if (dot)
  441.         *dot = '.';
  442.  
  443.       for (ptr = src; (ptr < dot) && ((ptr - src) < 8); ptr++)
  444.         *dst++ = *ptr;
  445.  
  446.       for (ptr = dot; (ptr < next) && ((ptr - dot) < 4); ptr++)
  447.         *dst++ = *ptr;
  448.     }
  449.     else
  450.     {
  451.       if (dot && (next - src == 1))
  452.         *dot = '.';           /* special case: "." as a path component */
  453.  
  454.       for (ptr = src; (ptr < next) && ((ptr - src) < 8); ptr++)
  455.         *dst++ = *ptr;
  456.     }
  457.  
  458.     *dst++ = *next; /* either '/' or 0 */
  459.  
  460.     if (*next)
  461.     {
  462.       src = next + 1;
  463.  
  464.       if (*src == 0) /* handle trailing '/' on dirs ! */
  465.         *dst = 0;
  466.     }
  467.     else
  468.       break;
  469.   }
  470.  
  471.   for (src = start; *src != 0; ++src)
  472.     if ((strchr(invalid, *src) != NULL) || (*src == ' '))
  473.       *src = '_';
  474. }
  475.  
  476. /* .LONGNAME EA code */
  477.  
  478. typedef struct
  479. {
  480.   ULONG cbList;               /* length of value + 22 */
  481. #ifdef __32BIT__
  482.   ULONG oNext;
  483. #endif
  484.   BYTE fEA;                   /* 0 */
  485.   BYTE cbName;                /* length of ".LONGNAME" = 9 */
  486.   USHORT cbValue;             /* length of value + 4 */
  487.   BYTE szName[10];            /* ".LONGNAME" */
  488.   USHORT eaType;              /* 0xFFFD for length-preceded ASCII */
  489.   USHORT eaSize;              /* length of value */
  490.   BYTE szValue[CCHMAXPATH];
  491. }
  492. FEALST;
  493.  
  494. typedef struct
  495. {
  496.   ULONG cbList;
  497. #ifdef __32BIT__
  498.   ULONG oNext;
  499. #endif
  500.   BYTE cbName;
  501.   BYTE szName[10];            /* ".LONGNAME" */
  502. }
  503. GEALST;
  504.  
  505. char *GetLongNameEA(char *name)
  506. {
  507.   EAOP eaop;
  508.   GEALST gealst;
  509.   static FEALST fealst;
  510.   char *ptr;
  511.  
  512.   eaop.fpGEAList = (PGEALIST) &gealst;
  513.   eaop.fpFEAList = (PFEALIST) &fealst;
  514.   eaop.oError = 0;
  515.  
  516.   strcpy((char *) gealst.szName, ".LONGNAME");
  517.   gealst.cbName  = (BYTE) strlen((char *) gealst.szName);
  518. #ifdef __32BIT__
  519.   gealst.oNext   = 0;
  520. #endif
  521.  
  522.   gealst.cbList  = sizeof(gealst);
  523.   fealst.cbList  = sizeof(fealst);
  524.  
  525.   if (DosQueryPathInfo(name, FIL_QUERYEASFROMLIST,
  526.                (PBYTE) &eaop, sizeof(eaop)))
  527.     return NULL;
  528.  
  529.   if (fealst.cbValue > 4 && fealst.eaType == 0xFFFD)
  530.   {
  531.     fealst.szValue[fealst.eaSize] = 0;
  532.  
  533.     for (ptr = fealst.szValue; *ptr; ptr++)
  534.       if (*ptr == '/' || *ptr == '\\')
  535.     *ptr = '!';
  536.  
  537.     return (char *) fealst.szValue;
  538.   }
  539.  
  540.   return NULL;
  541. }
  542.  
  543. char *GetLongPathEA(char *name)
  544. {
  545.   static char nbuf[CCHMAXPATH + 1];
  546.   char *comp, *next, *ea, sep;
  547.   BOOL bFound = FALSE;
  548.  
  549.   nbuf[0] = 0;
  550.   next = name;
  551.  
  552.   while (*next)
  553.   {
  554.     comp = next;
  555.  
  556.     while (*next != '\\' && *next != '/' && *next != 0)
  557.       next++;
  558.  
  559.     sep = *next;
  560.     *next = 0;
  561.  
  562.     ea = GetLongNameEA(name);
  563.     strcat(nbuf, ea ? ea : comp);
  564.     bFound = bFound || (ea != NULL);
  565.  
  566.     *next = sep;
  567.  
  568.     if (*next)
  569.     {
  570.       strcat(nbuf, "\\");
  571.       next++;
  572.     }
  573.   }
  574.  
  575.   return (nbuf[0] != 0) && bFound ? nbuf : NULL;
  576. }
  577.  
  578. /* general EA code */
  579.  
  580. typedef struct
  581. {
  582.   USHORT nID;
  583.   USHORT nSize;
  584.   ULONG lSize;
  585. }
  586. EFHEADER, *PEFHEADER;
  587.  
  588. #ifdef __32BIT__
  589.  
  590. /* Perhaps due to bugs in the current OS/2 2.0 kernel, the success or
  591.    failure of the DosEnumAttribute() and DosQueryPathInfo() system calls
  592.    depends on the area where the return buffers are allocated. This
  593.    differs for the various compilers, for some alloca() works, for some
  594.    malloc() works, for some, both work. We'll have to live with that. */
  595.  
  596. /* The use of malloc() is not very convenient, because it requires
  597.    backtracking (i.e. free()) at error returns. We do that for system
  598.    calls that may fail, but not for malloc() calls, because they are VERY
  599.    unlikely to fail. If ever, we just leave some memory allocated
  600.    over the usually short lifetime of a zip process ... */
  601.  
  602. #ifdef __GNUC__
  603. #define alloc(x) alloca(x)
  604. #define unalloc(x)
  605. #else
  606. #define alloc(x) malloc(x)
  607. #define unalloc(x) free(x)
  608. #endif
  609.  
  610. void GetEAs(char *path, char **bufptr, size_t *size,
  611.                         char **cbufptr, size_t *csize)
  612. {
  613.   FILESTATUS4 fs;
  614.   PDENA2 pDENA, pFound;
  615.   EAOP2 eaop;
  616.   PGEA2 pGEA;
  617.   PGEA2LIST pGEAlist;
  618.   PFEA2LIST pFEAlist;
  619.   PEFHEADER pEAblock;
  620.   ULONG ulAttributes, ulMemoryBlock;
  621.   ULONG nLength;
  622.   ULONG nBlock;
  623.   char szName[CCHMAXPATH];
  624.  
  625.   *size = *csize = 0;
  626.  
  627.   strcpy(szName, path);
  628.   nLength = strlen(szName);
  629.   if (szName[nLength - 1] == '/')
  630.     szName[nLength - 1] = 0;
  631.  
  632.   if (DosQueryPathInfo(szName, FIL_QUERYEASIZE, (PBYTE) &fs, sizeof(fs)))
  633.     return;
  634.   nBlock = max(fs.cbList, 65535);
  635.   if ((pDENA = alloc((size_t) nBlock)) == NULL)
  636.     return;
  637.  
  638.   ulAttributes = -1;
  639.  
  640.   if (DosEnumAttribute(ENUMEA_REFTYPE_PATH, szName, 1, pDENA, nBlock,
  641.                &ulAttributes, ENUMEA_LEVEL_NO_VALUE)
  642.     || ulAttributes == 0
  643.     || (pGEAlist = alloc((size_t) nBlock)) == NULL)
  644.   {
  645.     unalloc(pDENA);
  646.     return;
  647.   }
  648.  
  649.   pGEA = pGEAlist -> list;
  650.   memset(pGEAlist, 0, nBlock);
  651.   pFound = pDENA;
  652.  
  653.   while (ulAttributes--)
  654.   {
  655.     if (!(strcmp(pFound -> szName, ".LONGNAME") == 0 && use_longname_ea))
  656.     {
  657.       pGEA -> cbName = pFound -> cbName;
  658.       strcpy(pGEA -> szName, pFound -> szName);
  659.  
  660.       nLength = sizeof(GEA2) + strlen(pGEA -> szName);
  661.       nLength = ((nLength - 1) / sizeof(ULONG) + 1) * sizeof(ULONG);
  662.  
  663.       pGEA -> oNextEntryOffset = ulAttributes ? nLength : 0;
  664.       pGEA   = (PGEA2)  ((PCH) pGEA + nLength);
  665.     }
  666.  
  667.     pFound = (PDENA2) ((PCH) pFound + pFound -> oNextEntryOffset);
  668.   }
  669.  
  670.   if (pGEA == pGEAlist -> list) /* no attributes to save */
  671.   {
  672.     unalloc(pDENA);
  673.     unalloc(pGEAlist);
  674.     return;
  675.   }
  676.  
  677.   pGEAlist -> cbList = (PCH) pGEA - (PCH) pGEAlist;
  678.  
  679.   pFEAlist = (PVOID) pDENA;  /* reuse buffer */
  680.   pFEAlist -> cbList = nBlock;
  681.  
  682.   eaop.fpGEA2List = pGEAlist;
  683.   eaop.fpFEA2List = pFEAlist;
  684.   eaop.oError = 0;
  685.  
  686.   if (DosQueryPathInfo(szName, FIL_QUERYEASFROMLIST,
  687.                (PBYTE) &eaop, sizeof(eaop)))
  688.   {
  689.     unalloc(pDENA);
  690.     unalloc(pGEAlist);
  691.     return;
  692.   }
  693.  
  694.   /* The maximum compressed size is (in case of STORE type) the
  695.      uncompressed size plus the size of the compression type field
  696.      plus the size of the CRC field. */
  697.  
  698.   ulAttributes = pFEAlist -> cbList;
  699.   ulMemoryBlock = ulAttributes + sizeof(USHORT) + sizeof(ULONG);
  700.   pEAblock = (PEFHEADER) malloc(sizeof(EFHEADER) + ulMemoryBlock);
  701.  
  702.   if (pEAblock == NULL)
  703.   {
  704.     unalloc(pDENA);
  705.     unalloc(pGEAlist);
  706.     return;
  707.   }
  708.  
  709.   *bufptr = (char *) pEAblock;
  710.   *size = sizeof(EFHEADER);
  711.  
  712.   pEAblock -> nID = EF_OS2EA;
  713.   pEAblock -> nSize = sizeof(pEAblock -> lSize);
  714.   pEAblock -> lSize = ulAttributes; /* uncompressed size */
  715.  
  716.   nLength = memcompress((char *) (pEAblock + 1), ulMemoryBlock,
  717.                         (char *) pFEAlist, ulAttributes);
  718.   *size += nLength;
  719.   pEAblock -> nSize += nLength;
  720.  
  721.   if ((pEAblock = (PEFHEADER) malloc(sizeof(EFHEADER))) == NULL)
  722.   {
  723.     unalloc(pDENA);
  724.     unalloc(pGEAlist);
  725.     return;
  726.   }
  727.  
  728.   *cbufptr = (char *) pEAblock;
  729.   *csize = sizeof(EFHEADER);
  730.  
  731.   pEAblock -> nID = EF_OS2EA;
  732.   pEAblock -> nSize = sizeof(pEAblock -> lSize);
  733.   pEAblock -> lSize = ulAttributes;
  734.  
  735.   if (noisy)
  736.     printf(" (%ld bytes EA's)", ulAttributes);
  737.  
  738.   unalloc(pDENA);
  739.   unalloc(pGEAlist);
  740. }
  741.  
  742. #else /* !__32BIT__ */
  743.  
  744. typedef struct
  745. {
  746.   ULONG oNextEntryOffset;
  747.   BYTE fEA;
  748.   BYTE cbName;
  749.   USHORT cbValue;
  750.   CHAR szName[1];
  751. }
  752. FEA2, *PFEA2;
  753.  
  754. typedef struct
  755. {
  756.   ULONG cbList;
  757.   FEA2 list[1];
  758. }
  759. FEA2LIST, *PFEA2LIST;
  760.  
  761. void GetEAs(char *path, char **bufptr, size_t *size,
  762.                         char **cbufptr, size_t *csize)
  763. {
  764.   FILESTATUS2 fs;
  765.   PDENA1 pDENA, pFound;
  766.   EAOP eaop;
  767.   PGEALIST pGEAlist;
  768.   PGEA pGEA;
  769.   PFEALIST pFEAlist;
  770.   PFEA pFEA;
  771.   PFEA2LIST pFEA2list;
  772.   PFEA2 pFEA2;
  773.   EFHEADER *pEAblock;
  774.   ULONG ulAttributes;
  775.   USHORT nLength, nMaxSize;
  776.   char szName[CCHMAXPATH];
  777.  
  778.   *size = *csize = 0;
  779.  
  780.   strcpy(szName, path);
  781.   nLength = strlen(szName);
  782.   if (szName[nLength - 1] == '/')
  783.     szName[nLength - 1] = 0;
  784.  
  785.   if (DosQueryPathInfo(szName, FIL_QUERYEASIZE, (PBYTE) &fs, sizeof(fs))
  786.       || fs.cbList <= 2 * sizeof(ULONG))
  787.     return;
  788.  
  789.   ulAttributes = -1;
  790.   nMaxSize = (USHORT) min(fs.cbList * 2, 65520L);
  791.  
  792.   if ((pDENA = malloc((size_t) nMaxSize)) == NULL)
  793.     return;
  794.  
  795.   if (DosEnumAttribute(ENUMEA_REFTYPE_PATH, szName, 1, pDENA, fs.cbList,
  796.                &ulAttributes, ENUMEA_LEVEL_NO_VALUE)
  797.     || ulAttributes == 0
  798.     || (pGEAlist = malloc(nMaxSize)) == NULL)
  799.   {
  800.     free(pDENA);
  801.     return;
  802.   }
  803.  
  804.   pGEA = pGEAlist -> list;
  805.   pFound = pDENA;
  806.  
  807.   while (ulAttributes--)
  808.   {
  809.     nLength = strlen(pFound -> szName);
  810.  
  811.     if (!(strcmp(pFound -> szName, ".LONGNAME") == 0 && use_longname_ea))
  812.     {
  813.       pGEA -> cbName = pFound -> cbName;
  814.       strcpy(pGEA -> szName, pFound -> szName);
  815.  
  816.       pGEA++;
  817.       pGEA = (PGEA) (((PCH) pGEA) + nLength);
  818.     }
  819.  
  820.     pFound++;
  821.     pFound = (PDENA1) (((PCH) pFound) + nLength);
  822.   }
  823.  
  824.   if (pGEA == pGEAlist -> list)
  825.   {
  826.     free(pDENA);
  827.     free(pGEAlist);
  828.     return;
  829.   }
  830.  
  831.   pGEAlist -> cbList = (PCH) pGEA - (PCH) pGEAlist;
  832.  
  833.   pFEAlist = (PFEALIST) pDENA; /* reuse buffer */
  834.   pFEAlist -> cbList = fs.cbList;
  835.   pFEA = pFEAlist -> list;
  836.  
  837.   eaop.fpGEAList = pGEAlist;
  838.   eaop.fpFEAList = pFEAlist;
  839.   eaop.oError = 0;
  840.  
  841.   if (DosQueryPathInfo(szName, FIL_QUERYEASFROMLIST,
  842.                (PBYTE) &eaop, sizeof(eaop)))
  843.   {
  844.     free(pDENA);
  845.     free(pGEAlist);
  846.     return;
  847.   }
  848.  
  849.   /* now convert into new OS/2 2.0 32-bit format */
  850.  
  851.   pFEA2list = (PFEA2LIST) pGEAlist;  /* reuse buffer */
  852.   pFEA2 = pFEA2list -> list;
  853.  
  854.   while ((PCH) pFEA - (PCH) pFEAlist < pFEAlist -> cbList)
  855.   {
  856.     nLength = sizeof(FEA) + pFEA -> cbName + 1 + pFEA -> cbValue;
  857.     memcpy((PCH) pFEA2 + sizeof(pFEA2 -> oNextEntryOffset), pFEA, nLength);
  858.     memset((PCH) pFEA2 + sizeof(pFEA2 -> oNextEntryOffset) + nLength, 0, 3);
  859.     pFEA = (PFEA) ((PCH) pFEA + nLength);
  860.  
  861.     nLength = sizeof(FEA2) + pFEA2 -> cbName + 1 + pFEA2 -> cbValue;
  862.     nLength = ((nLength - 1) / sizeof(ULONG) + 1) * sizeof(ULONG);
  863.     /* rounded up to 4-byte boundary */
  864.     pFEA2 -> oNextEntryOffset =
  865.       ((PCH) pFEA - (PCH) pFEAlist < pFEAlist -> cbList) ? nLength : 0;
  866.     pFEA2 = (PFEA2) ((PCH) pFEA2 + nLength);
  867.   }
  868.  
  869.   pFEA2list -> cbList = (PCH) pFEA2 - (PCH) pFEA2list;
  870.   ulAttributes = pFEA2list -> cbList;
  871.  
  872.   pEAblock = (PEFHEADER) pDENA; /* reuse buffer */
  873.  
  874.   *bufptr = (char *) pEAblock;
  875.   *size = sizeof(EFHEADER);
  876.  
  877.   pEAblock -> nID = EF_OS2EA;
  878.   pEAblock -> nSize = sizeof(pEAblock -> lSize);
  879.   pEAblock -> lSize = ulAttributes; /* uncompressed size */
  880.  
  881.   nLength = (USHORT) memcompress((char *) (pEAblock + 1),
  882.     nMaxSize - sizeof(EFHEADER), (char *) pFEA2list, ulAttributes);
  883.  
  884.   *size += nLength;
  885.   pEAblock -> nSize += nLength;
  886.  
  887.   pEAblock = (PEFHEADER) pGEAlist;
  888.  
  889.   *cbufptr = (char *) pEAblock;
  890.   *csize = sizeof(EFHEADER);
  891.  
  892.   pEAblock -> nID = EF_OS2EA;
  893.   pEAblock -> nSize = sizeof(pEAblock -> lSize);
  894.   pEAblock -> lSize = ulAttributes;
  895.  
  896.   if (noisy)
  897.     printf(" (%ld bytes EA's)", ulAttributes);
  898. }
  899.  
  900. #endif /* __32BIT__ */
  901.  
  902. void GetACL(char *path, char **bufptr, size_t *size,
  903.                         char **cbufptr, size_t *csize)
  904. {
  905.   static char *buffer;
  906.   char *cbuffer;
  907.   long bytes, cbytes;
  908.   PEFHEADER pACLblock;
  909.  
  910.   if (buffer == NULL) /* avoid frequent allocation (for every file) */
  911.     if ((buffer = malloc(ACL_BUFFERSIZE)) == NULL)
  912.       return;
  913.  
  914.   if (acl_get(NULL, path, buffer))
  915.     return; /* this will be the most likely case */
  916.  
  917.   bytes = strlen(buffer);
  918.  
  919.   cbytes = bytes + sizeof(USHORT) + sizeof(ULONG);
  920.   if ((*bufptr = realloc(*bufptr, *size + sizeof(EFHEADER) + cbytes)) == NULL)
  921.     return;
  922.  
  923.   pACLblock = (PEFHEADER) (*bufptr + *size);
  924.  
  925.   cbuffer = (char *) (pACLblock + 1);
  926.   cbytes = memcompress(cbuffer, cbytes, buffer, bytes);
  927.  
  928.   *size += sizeof(EFHEADER) + cbytes;
  929.  
  930.   pACLblock -> nID = EF_ACL;
  931.   pACLblock -> nSize = sizeof(pACLblock -> lSize) + cbytes;
  932.   pACLblock -> lSize = bytes; /* uncompressed size */
  933.  
  934.   if ((*cbufptr = realloc(*cbufptr, *csize + sizeof(EFHEADER))) == NULL)
  935.     return;
  936.  
  937.   pACLblock = (PEFHEADER) (*cbufptr + *csize);
  938.   *csize += sizeof(EFHEADER);
  939.  
  940.   pACLblock -> nID = EF_ACL;
  941.   pACLblock -> nSize = sizeof(pACLblock -> lSize);
  942.   pACLblock -> lSize = bytes;
  943.  
  944.   if (noisy)
  945.     printf(" (%ld bytes ACL)", bytes);
  946. }
  947.  
  948. #ifdef USE_EF_UX_TIME
  949.  
  950. void GetExtraTime(struct zlist far *z, ztimbuf *z_utim)
  951. {
  952.   char *eb_l_ptr;
  953.   char *eb_c_ptr;
  954.  
  955.   eb_l_ptr = realloc(z->extra, (z->ext + (EB_HEADLEN+EB_UX_MINLEN)));
  956.   if (eb_l_ptr == NULL)
  957.     return ZE_MEM;
  958.   z->extra = eb_l_ptr;
  959.   eb_l_ptr += z->ext;
  960.   z->ext += (EB_HEADLEN+EB_UX_MINLEN);
  961.  
  962.   eb_l_ptr[0]  = 'U';
  963.   eb_l_ptr[1]  = 'X';
  964.   eb_l_ptr[2]  = EB_UX_MINLEN;          /* length of data part of e.f. */
  965.   eb_l_ptr[3]  = 0;
  966.   eb_l_ptr[4]  = (char)(z_utim->actime);
  967.   eb_l_ptr[5]  = (char)(z_utim->actime >> 8);
  968.   eb_l_ptr[6]  = (char)(z_utim->actime >> 16);
  969.   eb_l_ptr[7]  = (char)(z_utim->actime >> 24);
  970.   eb_l_ptr[8]  = (char)(z_utim->modtime);
  971.   eb_l_ptr[9]  = (char)(z_utim->modtime >> 8);
  972.   eb_l_ptr[10] = (char)(z_utim->modtime >> 16);
  973.   eb_l_ptr[11] = (char)(z_utim->modtime >> 24);
  974.  
  975.   eb_c_ptr = realloc(z->cextra, (z->cext + (EB_HEADLEN+EB_UX_MINLEN)));
  976.   if (eb_c_ptr == NULL)
  977.     return ZE_MEM;
  978.   z->cextra = eb_c_ptr;
  979.   eb_c_ptr += z->cext;
  980.   z->cext += (EB_HEADLEN+EB_UX_MINLEN);
  981.  
  982.   memcpy(eb_c_ptr, eb_l_ptr, (EB_HEADLEN+EB_UX_MINLEN));
  983. }
  984.  
  985. #endif /* USE_EF_UX_TIME */
  986.  
  987. int set_extra_field(struct zlist far *z, ztimbuf *z_utim)
  988. {
  989.   /* store EA data in local header, and size only in central headers */
  990.   GetEAs(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);
  991.  
  992.   /* store ACL data in local header, and size only in central headers */
  993.   GetACL(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);
  994.  
  995. #ifdef USE_EF_UX_TIME
  996.   /* store extended time stamps in both headers */
  997.   GetExtraTime(z, z_utim);
  998. #endif /* USE_EF_UX_TIME */
  999.  
  1000.   return ZE_OK;
  1001. }
  1002.  
  1003. #endif /* UTIL */
  1004.  
  1005. /* Initialize the table of uppercase characters including handling of
  1006.    country dependent characters. */
  1007.  
  1008. void init_upper()
  1009. {
  1010.   COUNTRYCODE cc;
  1011.   unsigned nCnt, nU;
  1012.  
  1013.   for (nCnt = 0; nCnt < sizeof(upper); nCnt++)
  1014.     upper[nCnt] = lower[nCnt] = (unsigned char) nCnt;
  1015.  
  1016.   cc.country = cc.codepage = 0;
  1017.   DosMapCase(sizeof(upper), &cc, (PCHAR) upper);
  1018.  
  1019.   for (nCnt = 0; nCnt < 256; nCnt++)
  1020.   {
  1021.     nU = upper[nCnt];
  1022.     if (nU != nCnt && lower[nU] == (unsigned char) nU)
  1023.       lower[nU] = (unsigned char) nCnt;
  1024.   }
  1025.  
  1026.   for (nCnt = 'A'; nCnt <= 'Z'; nCnt++)
  1027.     lower[nCnt] = (unsigned char) (nCnt - 'A' + 'a');
  1028. }
  1029.  
  1030. char *StringLower(char *szArg)
  1031. {
  1032.   unsigned char *szPtr;
  1033.   for (szPtr = (unsigned char *) szArg; *szPtr; szPtr++)
  1034.     *szPtr = lower[*szPtr];
  1035.   return szArg;
  1036. }
  1037.  
  1038. #if defined(__IBMC__) && defined(__DEBUG_ALLOC__)
  1039. void DebugMalloc(void)
  1040. {
  1041.   _dump_allocated(0); /* print out debug malloc memory statistics */
  1042. }
  1043. #endif
  1044.  
  1045. #endif /* OS2 */
  1046.