home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vdmutils.zip / EASLURPR.C next >
Text File  |  1997-10-19  |  5KB  |  127 lines

  1. /* DOS program to "slurp" the entire exetnded attrbibutes structure   */
  2. /* file from within an OS/2 VDM session.                              */
  3. /* Author: David W. Noon, October 1997                                */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. #include <malloc.h>
  10. #include <dos.h>
  11.  
  12. #include "vdmutils.h"
  13.  
  14. /* This is a hack among hacks */
  15. static void far *PtrAdd(void far *p, short o)
  16. {
  17.    return MK_FP(FP_SEG(p),FP_OFF(p)+o);
  18. }
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.    if (_osmajor < 20)
  23.       fputs("This does not look like an OS/2 VDM session.",stderr);
  24.    else if (argc != 2)
  25.       fputs("You must supply a path/filename.",stderr);
  26.    else
  27.    {
  28.       FILESTATUS Base_attrs;
  29.       USHORT rc;
  30.  
  31.       /* Obtain size of EA structure required */
  32.       rc = DwnQPathInfo(argv[1],FIL_QUERYEASIZE,&Base_attrs,sizeof(FILESTATUS));
  33.       if (rc == 0)
  34.       {
  35.          if (Base_attrs.cbList >= 4)
  36.          {
  37.             PEAOP EA_struct_ptr;
  38.             PFEA  EA_ptr;
  39.             USHORT Bytes_done;
  40.             char *EA_name, *EA_value;
  41.             int i;
  42.             unsigned short *EA_short;
  43.  
  44.             /* We will allocate the EA_name area on the near heap, since the
  45.                Watcom run-time library has a bug in which, if the first heap
  46.                allocation call is _fmalloc(), the memory area abutting the
  47.                current DGROUP is consumed, thus preventing creation of a near
  48.                heap. Allocating some near storage first prevents this theft. */
  49.  
  50.             EA_name = malloc(32);  /* Name length max of 32 bytes */
  51.             if (EA_name != NULL)
  52.             {
  53.                /* Allocate both FEAList and GEAList as full size */
  54.                EA_struct_ptr = (PEAOP) _fmalloc(Base_attrs.cbList*2+sizeof(EAOP));
  55.                if (EA_struct_ptr != NULL)
  56.                {
  57.                   /* Prime the pointers and lengths that anchor the 2 lists */
  58.                   EA_struct_ptr->fpFEAList = (PFEALIST) PtrAdd(EA_struct_ptr,sizeof(EAOP));
  59.                   EA_struct_ptr->fpFEAList->cbList = Base_attrs.cbList;
  60.                   EA_struct_ptr->fpGEAList = (PGEALIST) PtrAdd(EA_struct_ptr->fpFEAList,Base_attrs.cbList);
  61.                   EA_struct_ptr->fpGEAList->cbList = Base_attrs.cbList;
  62.  
  63.                   /* Read all the EA's in a single request to the file system */
  64.                   rc = DwnQPathInfo(argv[1],FIL_QUERYALLEAS,EA_struct_ptr,Base_attrs.cbList+sizeof(EAOP));
  65.                   if (rc == 0)
  66.                   {
  67.                      printf("EA structure for %s\n\nName\tHexadecimal Value\n",argv[1]);
  68.  
  69.                      /* We will now "run" the FEAList structure array */
  70.                      Bytes_done = 4U;
  71.                      EA_ptr = &(EA_struct_ptr->fpFEAList->list[0]);
  72.                      do
  73.                      {
  74.                         /* Copy EA name to near storage for printing */
  75.                         _fstrcpy(EA_name,EA_ptr->szName);
  76.  
  77.                         /* Copy EA value to near storage for printing */
  78.                         EA_value = malloc(EA_ptr->cbValue);
  79.                         /* Alias the bytes with unsigned short integers */
  80.                         EA_short = (unsigned short *) EA_value;
  81.                         if (EA_value != NULL)
  82.                         {
  83.                            _fmemcpy(EA_value,PtrAdd(EA_ptr,EA_ptr->cbName+5),EA_ptr->cbValue);
  84.  
  85.                            fputs(EA_name,stdout);
  86.                            putchar('\t');
  87.                            for (i = 0; i < EA_ptr->cbValue/sizeof(short); i++)
  88.                               printf("%04X ",EA_short[i]);
  89.                            if (EA_ptr->cbValue > i*sizeof(short))
  90.                               printf("%0*X",(EA_ptr->cbValue-i*sizeof(short))*2,EA_short[i]);
  91.                            putchar('\n');
  92.  
  93.                            /* Release near memory */
  94.                            free(EA_value);
  95.                         }
  96.                         else
  97.                            fprintf(stderr,"Unable to allocate %u bytes for EA %s.\n",
  98.                                  EA_ptr->cbValue,EA_name);
  99.  
  100.                         Bytes_done += (EA_ptr->cbName + EA_ptr->cbValue + 7U) & 0xFFFCU;
  101.  
  102.                         EA_ptr = (PFEA) PtrAdd(EA_ptr,(EA_ptr->cbName+EA_ptr->cbValue+7U)&0xFFFCU);
  103.                      } while (Bytes_done < EA_struct_ptr->fpFEAList->cbList);
  104.                   }
  105.                   else
  106.                      fprintf(stderr,"Return code %u when slurping EA structure of %s",rc,argv[1]);
  107.  
  108.                   _ffree(EA_struct_ptr);
  109.                }
  110.                else
  111.                   fprintf(stderr,"Unable to allocate %u bytes for EA structure.\n",Base_attrs.cbList);
  112.  
  113.                free(EA_name);
  114.             }
  115.             else
  116.                fputs("No near heap storage available at all!",stderr);
  117.          }
  118.          else
  119.             printf("Path %s has no extended attributes.",argv[1]);
  120.  
  121.       }
  122.       else
  123.          fprintf(stderr,"Return code %u when querying EA size of %s",rc,argv[1]);
  124.    }
  125.    return 0;
  126. }
  127.