Novell Home

NSS12

From Developer Community

This code demonstrates how to convert an NSS file key to a file name with NSS APIs (NetWare). Please note that a file on an NSS volume may have more than one full name if symbolic links are used.

Sample Code

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

static void PrintFilename( NINT task_id, Key_t file_key) {
  zInfo_s info;
  unsigned char fname[ 256];
  size_t actual_len;
  Key_t parent_key;
  STATUS ccode;
  if (( ccode = zGetInfo( file_key, zGET_NAME, sizeof( info), zINFO_VERSION_A, &info)) != zOK) {
    printf( "zGetInfo failed, ccode = %d\n", ccode);
    return;
  }
  ccode = NWUSUnicodeToByte( fname, sizeof( fname), ( unicode_t*)((( char*) &info) + info.nameStart), &actual_len);
  if ( ccode != 0) {
    printf( "NWUSUnicodeToByte failed, ccode = %x\n", ccode);
    return;
  }
  if ( actual_len == 0) return;
  if (( ccode = zOpen( file_key, task_id, zNSPACE_LONG|zMODE_UTF8, "..", zRR_READ_ACCESS, &parent_key)) != zOK) {
    printf( "zOpen failed, ccode = %d\n", ccode);
    return;
  }
  PrintFilename( task_id, parent_key);
  if (( ccode = zClose( parent_key)) != zOK) {
    printf( "zClose failed, ccode = %d\n", ccode);
    return;
  }
  printf( "/%s", fname);
}
int main( int argc, char* argv[]) {
  Key_t root_key;
  NINT task_id;
  Key_t file_key;
  STATUS ccode;
  if ( argc != 2) {
    printf( "Usage: %s <filename>\n", argv[ 0]);
    return EXIT_FAILURE;
  }
  ccode = NWUSStandardUnicodeInit();
  if ( ccode != 0) {
    printf( "NWUSStandardUnicodeInit failed, ccode = %x\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zRootKey( 0, &root_key)) != zOK) {
    NWUSStandardUnicodeRelease();
    printf( "zRootKey failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zBeginTask( root_key, 0, &task_id)) != zOK) {
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zBeginTask failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zOpen( root_key, task_id, zNSPACE_LONG|zMODE_UTF8, argv[ 1], zRR_READ_ACCESS, &file_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zOpen failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  PrintFilename( task_id, file_key);
  if (( ccode = zClose( file_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zClose failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zEndTask( root_key, task_id)) != zOK) {
    zClose( root_key);
    NWUSStandardUnicodeRelease();
    printf( "zEndTask failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zClose( root_key)) != zOK) {
    NWUSStandardUnicodeRelease();
    printf( "zClose failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  NWUSStandardUnicodeRelease();
  return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.