An Extended Attribute Example: CHKEA.C

From OS2World.Com Wiki
Jump to navigation Jump to search

CHKEA.C shows a way to find out if the file object has Extended Attributes associated with it. If so, then the query is made as to the size of all of the Extended Attributes that are attached. Lastly, the names of the types of the Extended Attributes are displayed. One can go further and modify this example to actually display the data stored in the Extended Attributes. This was left as an exercise for the reader. Most of the error checking in this and other examples is minimal due to the fact that clarity was the intention here, not necessarily robustness of the code. Error recovery is a topic that can easily demand a separate book by itself, and thus was mostly left out of these examples.

CHKEA.C

***********************************************************
*                       CHKEA.C                           *   
***********************************************************

#define INCL_DOSERRORS
#define INCL_DOSFILEMGR

#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

INT main (USHORT usNumArgs,
          PCHAR apchArgs [] )
{

   CHAR           achPath [CCHMAXPATHCOMP] ;
   PCHAR          pchPath ;
   ULONG          ulCount ;
   HDIR           hdFile ;
   APIRET         arReturn ;
   FILEFINDBUF4   ffbFile ;
   CHAR           achFile [CCHMAXPATHCOMP] ;
   PBYTE          pbBuffer ;
   PDENA2         pdAttribute ;

   if ( usNumArgs != 2 ) {

      puts ( "Syntax:  CHKEA [filename]" ) ;
      puts ( "" ) ;
      puts ( "where \'filename\' is the name of a " ) ;
      puts ( "file/directory and can contain wildcards." ) ;
      return 1 ;

   } /* endif */

   DosQueryPathInfo ( apchArgs[1],
                      FIL_QUERYFULLNAME,
                      achPath,
                      CCHMAXPATHCOMP ) ;

   pchPath = strrchr ( achPath, '\\' ) ;

   if ( pchPath != NULL ) {

      pchPath++ ;
      *pchPath = 0 ;

   } /* endif */

   ulCount = 1 ;
   hdFile = HDIR_SYSTEM ;

   arReturn = DosFindFirst ( apchArgs[1],
                             &hdFile,
                             FILE_DIRECTORY,
                             &ffbFile,
                             sizeof ( FILEFINDBUF4 ),
                             &ulCount,
                             FIL_QUERYEASIZE ) ;

   while ( arReturn == 0 ) {

      sprintf ( achFile, "%s%s", achPath, ffbFile.achName ) ;

      printf ( "\nFile name: %s\n", achFile ) ;
      printf ( "\nTotal bytes allocated for EAs: %ld bytes.\n",
               ffbFile.cbList ) ;

      pbBuffer = malloc ( ffbFile.cbList ) ;

      ulCount = -1 ;

      arReturn = DosEnumAttribute ( ENUMEA_REFTYPE_PATH,
                                    achFile,
                                    1,
                                    pbBuffer,
                                    ffbFile.cbList,
                                    &ulCount,
                                    ENUMEA_LEVEL_NO_VALUE ) ;

      printf ( "\nThis object contains %ld EAs.\n", ulCount ) ;

      pdAttribute = (PDENA2) pbBuffer ;

      while ( ulCount != 0) {

         printf ( "Found EA with name \"%s\"\n",
                  pdAttribute->szName ) ;

         ulCount-- ;
         pdAttribute = (PDENA2) (((PBYTE) pdAttribute ) +
                        pdAttribute->oNextEntryOffset ) ;

      } /* endwhile */

      free ( pbBuffer ) ;

      ulCount = 1 ;
      arReturn = DosFindNext ( hdFile,
                               &ffbFile,
                               sizeof ( ffbFile ),
                               &ulCount ) ;

   } /* endwhile */

   if (( arReturn != 0 ) &&
        ( arReturn != ERROR_NO_MORE_FILES )) {

      printf ( "\nError %ld encountered\n", arReturn ) ;

   } /* endif */

   return arReturn ;
}

CHKEA.MAK

***********************************************************
*                       CHKEA.MAK                         *
***********************************************************

CHKEA.EXE:                      CHKEA.OBJ
        LINK386 @<<
CHKEA
CHKEA
CHKEA
OS2386
CHKEA
<<

CHKEA.OBJ:                      CHKEA.C
        ICC -C+ -Kb+ -Ss+ CHKEA.C

CHKEA.DEF

***********************************************************
*                       CHKEA.DEF                         *
***********************************************************

NAME CHKEA WINDOWCOMPAT NEWFILES
DESCRIPTION 'Extended Attribute Example
            Copyright (c) 1993 by Arthur Panov
            All rights reserved.'
PROTMODE

***********************************************************

CHKEA.EXE expects a command line input argument that is the name of the file of interest. Wildcard characters are accepted. Firstly, a determination is made if the file object can be located on the hard disk, and if successful, the full name of the object is constructed.

The DosFindFirst API is the most useful function call available to a programmer when attempting to locate file objects.

NOTICE

The CHKEA example is from The Art of OS/2 C Programming by Kathleen Panov, Arthur Panov, and Larry Salomon, Jr., August 1993, published by QED Publishing Group, ISBN 0-89435-446-9. The example was uploaded by the publisher with the assistance of the authors for use by Forum members.