Novell Home

NSS10

From Developer Community

This code demonstrates how to list names of all files in a directory in all available namespaces with NSS APIs (NetWare). Uses CLIB's Unicode functions, you will need to replace them with their LibC's equivalents to rebuild this sample for LibC.

Sample Code

#include <nunicode.h>
#include <stdio.h>
#include <stdlib.h>
#include <zPublics.h>

int main( int argc, char* argv[]) {
  Key_t   root_key;
  NINT    task_id;
  Key_t   dir_key;
  zInfo_s info;
  char    fname[ 256];
  size_t  actual_len;
  STATUS  zccode;
  int     ccode;
  size_t  i;
  if ( argc != 2) {
    printf( "Usage: %s <path>\n", argv[ 0]);
    return EXIT_FAILURE;
  }
  ccode = NWUSStandardUnicodeInit();
  if ( ccode != 0) {
    printf( "NWUSStandardUnicodeInit failed, ccode = %x\n", ccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zRootKey( 0, &root_key)) != zOK) {
    NWUSStandardUnicodeRelease();
    printf( "zRootKey failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zBeginTask( root_key, 0, &task_id)) != zOK) {
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zBeginTask failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zOpen( root_key, task_id, zNSPACE_LONG|zMODE_UTF8, argv[ 1], zRR_SCAN_ACCESS, &dir_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zOpen failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zWildRead( dir_key, zPFMT_UTF8, zNTYPE_FILE, 0, 0, zGET_NAME|zGET_ALL_NAMES, sizeof( info), zINFO_VERSION_A, &info)) != zOK) {
    zClose( dir_key);
    zEndTask( root_key, task_id);
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zWildRead failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  do {
    ccode = NWUSUnicodeToByte(( unsigned char*) fname, sizeof( fname), ( unicode_t*)((( char*) &info) + info.nameStart), &actual_len);
    if ( ccode != 0) {
      zClose( dir_key);
      zEndTask( root_key, task_id);
      zClose( root_key);
      NWUSStandardUnicodeRelease();
      printf( "NWUSUnicodeToByte failed, ccode = %x\n", ccode);
      return EXIT_FAILURE;
    }
    puts( fname);
    printf( "  info.names.numEntries        = %u\n", info.names.numEntries);
    printf( "  info.names.fileNameArray     = %u\n", info.names.fileNameArray);
    for ( i = 0; i < info.names.numEntries; ++i) {
      long* nsname_index = ( long*)((( char*) &info) + info.names.fileNameArray);
      printf( "  info.names.fileNameArray[ %u] = %u", i, nsname_index[ i]);
      if ( nsname_index[ i] != 0) {
        ccode = NWUSUnicodeToByte(( unsigned char*) fname, sizeof( fname), ( unicode_t*)((( char*) &info) + nsname_index[ i]), &actual_len);
        if ( ccode != 0) {
          zClose( dir_key);
          zEndTask( root_key, task_id);
          zClose( root_key);
          NWUSStandardUnicodeRelease();
          printf( "NWUSUnicodeToByte failed, ccode = %x\n", ccode);
          return EXIT_FAILURE;
        }
        printf( "    %s\n", fname);
      }
      else puts( "");
    }
  }
  while ( zWildRead( dir_key, zPFMT_UTF8, zNTYPE_FILE, 0, 0, zGET_NAME|zGET_ALL_NAMES, sizeof( info), zINFO_VERSION_A, &info) == zOK);
  if (( zccode = zClose( dir_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zClose failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zEndTask( root_key, task_id)) != zOK) {
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zEndTask failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  if (( zccode = zClose( root_key)) != zOK) {
    NWUSStandardUnicodeRelease();
    printf( "zClose failed, zccode = %d\n", zccode);
    return EXIT_FAILURE;
  }
  NWUSStandardUnicodeRelease();
  return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2009 Novell, Inc. All Rights Reserved.