Novell Home

Sample code for qwrite()

From Developer Community

This is a sample of qwrite(). Like the sample for gwrite(), it uses AsyncRead(), so don't blindly use this code 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;
   size_t                  pos;
   size_t                  i;
   if ( argc != 3) {
      puts( "Usage: LOAD QWRITE <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);
   for ( pos = 0, i = 0; i < num_of_buffers; ++i) {
      if (( ccode = qwrite( dstfile,
                            cache_bufs[ i].cacheBufferPointer,
                            cache_bufs[ i].cacheBufferLength,
                            pos)) == EFAILURE) {
         printf( "qwrite failed, errno = %d ( %s ), NetWareErrno = %d\n",
                 errno, strerror( errno), NetWareErrno);
         break;
      }
      pos += cache_bufs[ i].cacheBufferLength;
   }
   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.