Novell Home

CLIB's opendir & readdir on a remote file system

From Developer Community

Demonstrates how to setup the connection to do an opendir/readdir on a remote file system. This sample must be run against a server in the same tree as the server that you are running this on.

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <nwcalls.h>
#include <nwnet.h>
#include <errno.h>
#include <nwerrno.h>
#include <fcntl.h>
#include <nwdsapi.h> 
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <nwcntask.h>
#include <nwclxcon.h>
#include <dirent.h>
#include <nwconn.h>

int main(int argc, char **argv)
{
   NWDSContextHandle    dContext;
   NWDSCCODE            err;
   NWCONN_HANDLE     connHandle;
   struct stat statbuf;
   char *serverName, *path;
   char buff[255];
   int fh;
   WORD serverID;
   DIR *dirEnt, *fileEnt;

   if(argc != 4)
   {
      printf("usage: %s <remote server/volume:\\dir> <username> <password>\r\n", argv[0]);
      return (-1);
   }

   err = NWCallsInit(NULL, NULL);
   
   if (err)
   {
      printf("\nCall to NWCallsInit returned: %04X", err);
      return 1;
   }

   
   serverName = buff;
   path = argv[1];
   
   memset(buff, 0, sizeof(buff));
   while(*path != '\\' && *path != '/')
   {
     *serverName = *path;
     serverName++;
     path++;
   }

   serverName = buff;
   path = argv[1];

   err = NWDSCreateContextHandle(&dContext);
   
   if (err == ERR_CONTEXT_CREATION)
   {
      printf("NWDSCreateContext returned: %04X\n", err);
      goto ERR;
   }

   err = NWDSLogin(dContext, 0, argv[2], argv[3], 0);
   if(err)
   {
      printf("Error %d with NWDSLogin\r\n", err);
      goto ERR;
   }

   
   err = AttachToFileServer(serverName, &serverID);
   if(err)
   {
      printf("Error with AttachToFileServer errno = %d, NetWareErrno = %d\r\n", errno, NetWareErrno);
      goto ERR;
   }
   
   connHandle = GetCurrentConnection();
   //-----Have DS authenticate our connection to the server.
   err=NWDSAuthenticateConn(dContext, connHandle);
   if(err != 0){
      printf("\nERROR:  NWDSAuthenticate() returned error: %d", err);
      goto ERR;
   } 
   printf("NWDSAuthenticate success\r\n");   

   printf("path = %s\r\n", path);

   dirEnt = opendir(path);
   if(NULL == dirEnt)
   {
      printf("Error %d\r\n", errno);
      goto ERR;
   }

   while((fileEnt = readdir(dirEnt)) != NULL)   
      {   
         
         printf("%s\r\n", fileEnt->d_name);   
      }   

ERR:
   printf("press any key to begin destruction\r\n");
   getch();
   NWCCCloseConn(connHandle);
   NWDSLogout(dContext);
   NWDSFreeContext(dContext);

   return 0;
}

--Benjamin Fjeldsted

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.