home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / chkea.zip / CHKEA.TXT
Text File  |  1993-08-23  |  6KB  |  178 lines

  1.  
  2. An Extended Attribute Example: CHKEA.C
  3.  
  4. CHKEA.C shows a way to find out if the file object has
  5. Extended Attributes associated with it. If so, then the query is made 
  6. as to the size of all of the Extended Attributes that are attached. 
  7. Lastly, the names of the types of the Extended Attributes are 
  8. displayed. One can go further and modify this example to actually display 
  9. the data stored in the Extended Attributes. This was left as an exercise 
  10. for the reader. Most of the error checking in this and other examples is 
  11. minimal due to the fact that clarity was the intention here, not 
  12. necessarily robustness of the code. Error recovery is a topic that can 
  13. easily demand a separate book by itself, and thus was mostly left out 
  14. of these examples.
  15.  
  16. ***********************************************************
  17. *                       CHKEA.C                           *   
  18. ***********************************************************
  19.  
  20. #define INCL_DOSERRORS
  21. #define INCL_DOSFILEMGR
  22.  
  23. #include <os2.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. INT main (USHORT usNumArgs,
  29.           PCHAR apchArgs [] )
  30. {
  31.  
  32.    CHAR           achPath [CCHMAXPATHCOMP] ;
  33.    PCHAR          pchPath ;
  34.    ULONG          ulCount ;
  35.    HDIR           hdFile ;
  36.    APIRET         arReturn ;
  37.    FILEFINDBUF4   ffbFile ;
  38.    CHAR           achFile [CCHMAXPATHCOMP] ;
  39.    PBYTE          pbBuffer ;
  40.    PDENA2         pdAttribute ;
  41.  
  42.    if ( usNumArgs != 2 ) {
  43.  
  44.       puts ( "Syntax:  CHKEA [filename]" ) ;
  45.       puts ( "" ) ;
  46.       puts ( "where \'filename\' is the name of a " ) ;
  47.       puts ( "file/directory and can contain wildcards." ) ;
  48.       return 1 ;
  49.  
  50.    } /* endif */
  51.  
  52.    DosQueryPathInfo ( apchArgs[1],
  53.                       FIL_QUERYFULLNAME,
  54.                       achPath,
  55.                       CCHMAXPATHCOMP ) ;
  56.  
  57.    pchPath = strrchr ( achPath, '\\' ) ;
  58.  
  59.    if ( pchPath != NULL ) {
  60.  
  61.       pchPath++ ;
  62.       *pchPath = 0 ;
  63.  
  64.    } /* endif */
  65.  
  66.    ulCount = 1 ;
  67.    hdFile = HDIR_SYSTEM ;
  68.  
  69.    arReturn = DosFindFirst ( apchArgs[1],
  70.                              &hdFile,
  71.                              FILE_DIRECTORY,
  72.                              &ffbFile,
  73.                              sizeof ( FILEFINDBUF4 ),
  74.                              &ulCount,
  75.                              FIL_QUERYEASIZE ) ;
  76.  
  77.    while ( arReturn == 0 ) {
  78.  
  79.       sprintf ( achFile, "%s%s", achPath, ffbFile.achName ) ;
  80.  
  81.       printf ( "\nFile name: %s\n", achFile ) ;
  82.       printf ( "\nTotal bytes allocated for EAs: %ld bytes.\n",
  83.                ffbFile.cbList ) ;
  84.  
  85.       pbBuffer = malloc ( ffbFile.cbList ) ;
  86.  
  87.       ulCount = -1 ;
  88.  
  89.       arReturn = DosEnumAttribute ( ENUMEA_REFTYPE_PATH,
  90.                                     achFile,
  91.                                     1,
  92.                                     pbBuffer,
  93.                                     ffbFile.cbList,
  94.                                     &ulCount,
  95.                                     ENUMEA_LEVEL_NO_VALUE ) ;
  96.  
  97.       printf ( "\nThis object contains %ld EAs.\n", ulCount ) ;
  98.  
  99.       pdAttribute = (PDENA2) pbBuffer ;
  100.  
  101.       while ( ulCount != 0) {
  102.  
  103.          printf ( "Found EA with name \"%s\"\n",
  104.                   pdAttribute->szName ) ;
  105.  
  106.          ulCount-- ;
  107.          pdAttribute = (PDENA2) (((PBYTE) pdAttribute ) +
  108.                         pdAttribute->oNextEntryOffset ) ;
  109.  
  110.       } /* endwhile */
  111.  
  112.       free ( pbBuffer ) ;
  113.  
  114.       ulCount = 1 ;
  115.       arReturn = DosFindNext ( hdFile,
  116.                                &ffbFile,
  117.                                sizeof ( ffbFile ),
  118.                                &ulCount ) ;
  119.  
  120.    } /* endwhile */
  121.  
  122.    if (( arReturn != 0 ) &&
  123.         ( arReturn != ERROR_NO_MORE_FILES )) {
  124.  
  125.       printf ( "\nError %ld encountered\n", arReturn ) ;
  126.  
  127.    } /* endif */
  128.  
  129.    return arReturn ;
  130. }
  131.  
  132. ***********************************************************
  133. *                       CHKEA.MAK                         *
  134. ***********************************************************
  135.  
  136. CHKEA.EXE:                      CHKEA.OBJ
  137.         LINK386 @<<
  138. CHKEA
  139. CHKEA
  140. CHKEA
  141. OS2386
  142. CHKEA
  143. <<
  144.  
  145. CHKEA.OBJ:                      CHKEA.C
  146.         ICC -C+ -Kb+ -Ss+ CHKEA.C
  147.  
  148. ***********************************************************
  149. *                       CHKEA.DEF                         *
  150. ***********************************************************
  151.  
  152. NAME CHKEA WINDOWCOMPAT NEWFILES
  153. DESCRIPTION 'Extended Attribute Example
  154.             Copyright (c) 1993 by Arthur Panov
  155.             All rights reserved.'
  156. PROTMODE
  157.  
  158. ***********************************************************
  159.  
  160. CHKEA.EXE expects a command line input argument that is the 
  161. name of the file of interest. Wildcard characters are 
  162. accepted. Firstly, a determination is made if the file 
  163. object can be located on the hard disk, and if successful, 
  164. the full name of the object is constructed.
  165.  
  166. The DosFindFirst API is the most useful function call available 
  167. to a programmer when attempting to locate file objects.
  168.  
  169. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  170. +                       NOTICE                            +
  171. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  172.  
  173. The CHKEA example is from The Art of OS/2 C Programming by
  174. Kathleen Panov, Arthur Panov, and Larry Salomon, Jr., 
  175. August 1993, published by QED Publishing Group, ISBN 
  176. 0-89435-446-9. The example was uploaded by the publisher 
  177. with the assistance of the authors for use by Forum members.
  178.