Novell Home

How to scan open files by connection

From Developer Community

This is an example of how to obtain open files for a connection by using NWScanOpenFilesByConn2() API.

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __MWERKS__
#pragma ANSI_strict off
#endif
#include <nwcalls.h>
#include <nwclxcon.h>
#ifdef __MWERKS__
#pragma ANSI_strict reset
#endif

#if defined( __MWERKS__) && !defined( N_PLAT_NLM)
#define strupr(s) (_strupr(s),s)
#endif

int main( int argc, char* argv[]) {
  char const*   server_name;
#ifdef N_PLAT_NLM
  char const*   login_user_name;
  char const*   password;
#endif
  NWRCODE             rcode;
  NWCCODE             ccode;
  NWCONN_HANDLE       conn_handle;
  NWCONN_NUM          conn_num;
  nint16              iter_handle = 0;
  OPEN_FILE_CONN_CTRL open_ctrl;
  OPEN_FILE_CONN      open_file;
#ifdef N_PLAT_NLM
  if ( argc != 5) {
    printf( "Usage: %s <servername> <username> <password> <connection number>\n", *argv);
    return EXIT_FAILURE;
  }
#else
  if ( argc != 3) {
    printf( "Usage: %s <servername> <connection number>\n", *argv);
    return EXIT_FAILURE;
  }
#endif
  ccode = NWCallsInit( NULL, NULL);
  if ( ccode != 0) {
    printf( "NWCallsInit failed, ccode = %x", ccode);
    return EXIT_FAILURE;
  }
  server_name     = strupr( *++argv);
#ifdef N_PLAT_NLM
  login_user_name = strupr( *++argv);
  password        = strupr( *++argv);
#endif
  conn_num        = ( NWCONN_NUM) atoi( *++argv);
  printf( "conn_num = %u\n", conn_num);
  rcode = NWCCOpenConnByName( 0,
                              server_name,
                              NWCC_NAME_FORMAT_BIND,
                              NWCC_OPEN_LICENSED,
                              NWCC_TRAN_TYPE_WILD,
                              &conn_handle);
  if ( rcode != 0) {
    printf( "NWCCOpenConnByName failed, rcode = %x", rcode);
    return EXIT_FAILURE;
  }
#ifdef N_PLAT_NLM
  ccode = NWLoginToFileServer( conn_handle, login_user_name, OT_USER, password);
  if ( ccode != 0) {
    printf( "NWLoginToFileServer failed, ccode = %x", ccode);
    NWCCCloseConn( conn_handle);
    return EXIT_FAILURE;
  }
#endif
  while ( 1) {
    char   vol_name[ 17];
    char   path_name[ 50];
    nuint8 len = sizeof( path_name);
    int    i;
    ccode = NWScanOpenFilesByConn2( conn_handle, conn_num, &iter_handle, &open_ctrl, &open_file);
    printf( "iter_handle = %d\n", iter_handle);
    if ( iter_handle == -1) break;
    if( ccode != 0) {
      printf( "NWScanOpenFilesByConn2 failed, ccode = %x", ccode);
      NWCCCloseConn( conn_handle);
      return EXIT_FAILURE;
    }
    ccode = NWGetVolumeName( conn_handle, open_file.volNumber, vol_name);
    if( ccode != 0) {
      printf( "NWGetVolumeName failed, ccode = %x", ccode);
      NWCCCloseConn( conn_handle);
      return EXIT_FAILURE;
    }
    printf( "open_file.nameSpace = %u\n", open_file.nameSpace);
    ccode = NWGetPathFromDirectoryBase( conn_handle, open_file.volNumber, open_file.dirEntry, 0 /* open_file.nameSpace */, &len, path_name);
    if( ccode != 0) {
      printf( "NWGetPathFromDirectoryBase failed, ccode = %x", ccode);
      NWCCCloseConn( conn_handle);
      return EXIT_FAILURE;
    }
    printf( "%s:", vol_name);
    for ( i = 0; i < len; i += path_name[ i] + 1) {
      printf( "/%.*s", path_name[ i], path_name + i + 1);
    }
    printf( "\nopen_file.fileName = %s\n", open_file.fileName);
  }
  NWCCCloseConn( conn_handle);
  NWCallsTerm( NULL);
  return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.