Novell Home

Sample code for gwrite() (NetWare)

From Developer Community

This is a simple sample of gwrite(). It reads a file with AsyncRead() and creates a copy of it with gwrite(). Because AsyncRead() can read only 64 KB at a time (or at least the documentation says so), you should modify this sample accordingly to copy large files.

Sample Code

#include <ctype.h>
#include <errno.h>
#include <nwerrno.h>
#include <nwfileio.h>
#include <nwsemaph.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char* argv[]) {
   int                     srcfile;
   int                     dstfile;
   LONG                    loc_sem;
   int                     ccode;
   LONG                    bytes_read;
   T_cacheBufferStructure* cache_bufs;
   LONG                    num_of_buffers;
   if ( argc != 3) {
      puts( "Usage: LOAD GWRITE <src> <dst>\n"
            "where <src> - source file name,\n"
            "      <dst> - destination file name");
      return EXIT_FAILURE;
   }
   if (( srcfile = open( argv[ 1], O_RDONLY)) == -1) {
      printf( "open failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      return EXIT_FAILURE;
   }
   if (( dstfile = creat( argv[ 2], 0)) == -1) {
      printf( "creat failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      close( srcfile);
      return EXIT_FAILURE;
   }
   if (( loc_sem = OpenLocalSemaphore( 0)) == 0) {
      printf( "OpenLocalSemaphore failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      close( dstfile);
      close( srcfile);
      return EXIT_FAILURE;
   }
   if (( ccode = AsyncRead( srcfile,
                            0,
                            filelength( srcfile),
                            &bytes_read,
                            loc_sem,
                            &cache_bufs,
                            &num_of_buffers)) != 0) {
      printf( "AsyncRead failed, ccode = %d, errno = %d ( %s ), NetWareErrno = %d\n",
              ccode, errno, strerror( errno), NetWareErrno);
      CloseLocalSemaphore( loc_sem);
      close( dstfile);
      close( srcfile);
      return EXIT_FAILURE;
   }
   WaitOnLocalSemaphore( loc_sem);
   printf( "bytes_read = %d, num_of_buffers = %d\n", bytes_read, num_of_buffers);
   ccode = gwrite( dstfile,
                   ( T_mwriteBufferStructure*) cache_bufs,
                   num_of_buffers,
                   &num_of_buffers);
   printf( "ccode = %d, written %d buffer(s)\n", ccode, num_of_buffers);
   AsyncRelease( cache_bufs);
   CloseLocalSemaphore( loc_sem);
   close( dstfile);
   close( srcfile);
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.