#include <stdio.h>
#include <stdlib.h>
#include <DirectoryService/DirectoryService.h>

#ifndef bool
typedef enum    { false = 0, true = 1 } bool;
#endif  /* bool */

// 32k buffer
#define BUFFERSIZE  32 * 1024  

int main( int argc, char *argv[] )
{
    int                    ret = 0 ;
    long             dirStatus = eDSNoErr ;

    tDirReference       dirRef = 0 ;
    unsigned long    nodeCount = 0 ;

    tDataBufferPtr  dataBuffer = NULL ;
    unsigned long    dataCount = 0    ;
    tContextData   listContext = NULL ;

  // open OD
    dirStatus = dsOpenDirService( &dirRef );
    if( dirStatus != eDSNoErr ) goto errexit ;

  // node  count 
    dirStatus = dsGetDirNodeCount( dirRef, &nodeCount ); 
    if( dirStatus != eDSNoErr ) goto errexit ;

    if( nodeCount == 0 )
    {
        fprintf( stderr, "node count = 0\n") ;
        goto toexit;
    }

  // allocate buffer
    dataBuffer = dsDataBufferAllocate( dirRef, BUFFERSIZE );
    if( dataBuffer == NULL ) // allocate error
    {
        fprintf( stderr, "data buffer allocate error\n") ;
        ret = 1; 
        goto toexit;
    }

    do {
        int                 i = 1   ;
        char        *nodePath = NULL;
        tDataListPtr nodeName = NULL;

        dirStatus = dsGetDirNodeList( dirRef, dataBuffer, &dataCount, &listContext );
        if( dirStatus != eDSNoErr ) goto errexit ;

        for( i = 1 ; i <= dataCount; i++ )
        {
            nodeName = dsDataListAllocate( dirRef );

            if( nodeName == NULL )
            {
                fprintf( stderr, "name buffer allocate error\n") ;
                ret = 1; 
                goto toexit;
            }
            dirStatus = dsGetDirNodeName( dirRef, dataBuffer, i, &nodeName );
            if( dirStatus != eDSNoErr ) goto errexit ;

            nodePath = dsGetPathFromList( dirRef, nodeName, "/" );
            printf("Node No. %02d, %s\n", i, nodePath );
            free( nodePath );
            free( nodeName );
        }

    }while( listContext != NULL );

    goto toexit;

errexit:
    ret = 1;
    fprintf( stderr, "errocode = %d\n", dirStatus );
toexit:
    if( listContext != NULL )
        dsReleaseContinueData( dirRef, listContext );
    if( dataBuffer != NULL )
        dsDataBufferDeAllocate( dirRef, dataBuffer );
    if( dirRef != 0 )
        dsCloseDirService( dirRef );
    exit(ret);
}