4.7 Listing in Reverse Order: Example

  /* itrprev.c 
     Example of processing a list in reverse order using NWDSItrPrev. 
  */ 
   
  #include <stdio.h> 
  #include <stdlib.h> 
  #include <nwnet.h> 
  #include <nwcalls.h> 
   
  void main(int argc, char *argv[]) 
  { 
     NWDSContextHandle context =
          (NWDSContextHandle)ERR_CONTEXT_CREATION; 
     nstr8             strAbbreviatedName[MAX_DN_CHARS+1]; 
     pBuf_T            pb = NULL; 
     NWDSCCODE         ccode; 
     nuint32           flags; 
     nuint32           iter=0; 
     nint32            iterHandle = -1; 
   
  /* Check arguments */ 
     if(argc != 3) 
     { 
        printf("\nUsage:    itrprev  <base object>  <object class>"); 
        printf("\nExample:  itrprev  myou.myorg  user"); 
        exit(1); 
     } 
   
     /* Initialize NWCalls and unicode tables */ 
     ccode = NWCallsInit(NULL,NULL); 
        if(ccode) exit(1); 
   
     /* Create NDS Context */ 
     ccode = NWDSCreateContextHandle(&context); 
     if(ccode) exit(1); 
   
     /* Set flags to get object names in typeless format. */ 
     NWDSGetContext(context, DCK_FLAGS, &flags); 
     flags |= DCV_TYPELESS_NAMES; 
     NWDSSetContext(context, DCK_FLAGS, &flags); 
   
     printf("itrprev:  Sample program for NDS 8 Iterator. List in
             reverse.\n"); 
     printf("Container to list:    %s\n",   argv[1]); 
     printf("Class:                %s\n",   argv[2]); 
   
     /* Convert the directory name (passed in as first arg) to its 
        shortest form relative to the name context */ 
     ccode = NWDSAbbreviateName(context, argv[1], strAbbreviatedName); 
     if(ccode) goto error; 
   
     /* Allocate the output buffer.  */ 
     ccode = NWDSAllocBuf(MAX_MESSAGE_LEN, &pb); 
     if(ccode) goto error; 
   
     /* ———— Create the Iterator ————- 
        A list iterator is ordered first by Base Class, 
        then by RDN within a base class. 
     */ 
   
     ccode = NWDSItrCreateList( 
                   context,              /* context for this search */ 
                   strAbbreviatedName,   /* container to search     */ 
                   argv[2],              /* class name filter       */ 
                   NULL,                 /* subordinate name filter */ 
                   DS_ITR_ANY_SERVER,    /* scalability requirement */ 
                   0,                    /* no timeout              */ 
                   &iter);               /* returned Iterator ptr   */ 
     if (ccode) 
        goto error; 
   
     /* Position the Iterator at the EOF position. */ 
     ccode = NWDSItrSetPosition(iter, DS_ITR_EOF, 0); 
     if (ccode) goto error; 
   
     /* 
        Read all entries in the result set and dump the results. 
        If the search result is empty, -765 (ERR_EOF_HIT) is returned. 
     */ 
     do                /* Loop until Iteration handle returns -1 */ 
     { 
        nuint          i; 
        nstr8          objName[MAX_DN_CHARS+1]; 
        nuint32        objCount, attrCount; 
        Object_Info_T  objectInfo; 
   
        ccode = NWDSItrGetPrev(
                        iter,         /* Iterator object handle      */ 
                         0,            /* # of entries to read. 0=all */ 
                         0,            /* no timeout                  */ 
                         &iterHandle,  /* Iteration handle            */ 
                         pb);          /* Returned data               */ 
        if (ccode) goto error; 
   
        ccode = NWDSGetObjectCount (context,pb,&objCount); 
        if (ccode) goto error; 
        printf("\nNext buffer.  Object Count = %d\n", objCount); 
   
        for (i=0; i<objCount; i++) 
        { 
           ccode = NWDSGetObjectName (context, 
                                      pb, 
                                      objName, 
                                      &attrCount, 
                                      &objectInfo); 
           if (ccode) goto error; 
           printf("  %-20s     Class: %s\n",
                   objName,objectInfo.baseClass); 
   
        }  /* For each object */ 
     } while (iterHandle != -1); 
   
  /* Clean up, normal or error termination. */ 
  error: 
     printf("\n\nccode = %d.",ccode); 
     if (iter) 
        NWDSItrDestroy(iter);      /* Destroy the Iterator when done. */ 
     if (pb) 
        NWDSFreeBuf(pb); 
     NWDSFreeContext(context); 
  }