4.2 Counting with eDirectory Iterators: Example

  
  /* itrcount.c 
     Example of NDS 8 Iterator NWDSItrCount function. 
  */ 
   
  #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]; 
     NWDSCCODE    ccode; 
     nuint32      flags; 
     nuint32      iter=0; 
     nint32       iterHandle = -1; 
     nuint32      timeout = 0;    /* Timeout=0 means no time limit. */ 
     nuint32      limit= 0; 
     nuint32      count; 
   
  /* Check arguments */ 
     if(argc < 2) 
     { 
        printf("\nUsage:    itrcount  <base object>  [count limit]\n"); 
        printf("\nExample:  itrcount  myou.myorg  1000\n"); 
        exit(1); 
     } 
   
     if (argc >= 3) 
        sscanf(argv[2], "%d", &limit); 
   
     /* 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("itrcount:   Sample program for NWDSItrCount\n"); 
     printf("Container:    %s\n", argv[1]); 
     printf("Count limit:  %d\n\n", limit); 
   
     /* 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; 
   
     /* ———— Create the Iterator ————- */ 
   
     ccode = NWDSItrCreateList( 
                   context,              /* context for this search */ 
                   strAbbreviatedName,   /* container to search     */ 
                   NULL,                 /* class name filter       */ 
                   NULL,                 /* subordinate name filter */ 
                   DS_ITR_ANY_SERVER,    /* scalability requirement */ 
                   timeout,              /* 0 = no timeout          */ 
                   &iter);               /* returned Iterator ptr   */ 
     if (ccode) 
        goto error; 
   
   
     /* Count until we reach the max count limit, timeout, or end 
        of list. Do not update the current position while counting. 
     */ 
     ccode = NWDSItrCount(iter, timeout, limit, N_FALSE, &count); 
     if (ccode == ERR_QUERY_TIMEOUT) 
        printf("Count timed out after counting %d objects\n",count); 
     else if (ccode == ERR_ITR_MAX_COUNT) 
        printf("Count reached max count limit: %d\n", count); 
     else 
        printf("Count reached the end of the list.  %d
                objects.\n",count); 
   
   
  /* Clean up, normal or error termination. */ 
  error: 
     printf("\n\nccode = %d.",ccode); 
     if (iter) 
        NWDSItrDestroy(iter);      /* Destroy the Iterator when done. */ 
     NWDSFreeContext(context); 
  }