Novell Home

CLIB remote filesystem access

From Developer Community

Shows how to creat and write to a file on a remote system. (using AttachToFileServer)

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 <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;

   if(argc != 4)
   {
	  printf("usage: %s <remote server/volume:\\file> <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");   


   fh = creat(path, 0);
   if((-1) == fh)
   {
	  printf("creat failed with errno = %d: nwerrno = %d\r\n", errno, NetWareErrno);
	  goto ERR;
   }
   
   err = write(fh, "hello", 6);
   if((-1) == err)
   {
	  printf("Write failed with errno = %d: NetWareErrno = %d\r\n", errno, NetWareErrno);
   }

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.