Novell Home

Sample code for AsyncRead() (NetWare)

From Developer Community

This is a simple sample of reading files with AsyncRead().

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                     fhandle;
   LONG                    loc_sem;
   int                     ccode;
   LONG                    bytes_read;
   T_cacheBufferStructure* cache_bufs;
   LONG                    num_of_buffers;
   LONG                    num_of_bytes_to_print;
   char*                   ptr;
   size_t                  i;
   if ( argc != 2) {
      puts( "Usage: LOAD ASYNCRD <filename>\n"
            "where <filename> - file name to read");
      return EXIT_FAILURE;
   }
   if (( fhandle = open( argv[ 1], O_RDONLY)) == -1) {
      printf( "open failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      return EXIT_FAILURE;
   }
   if (( loc_sem = OpenLocalSemaphore( 0)) == 0) {
      printf( "OpenLocalSemaphore failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      close( fhandle);
      return EXIT_FAILURE;
   }
   if (( ccode = AsyncRead( fhandle,
                            0,
                            2 * 1024,
                            &bytes_read,
                            loc_sem,
                            &cache_bufs,
                            &num_of_buffers)) != 0) {
      printf( "AsyncRead failed, errno = %d ( %s ), NetWareErrno = %d\n",
              errno, strerror( errno), NetWareErrno);
      CloseLocalSemaphore( loc_sem);
      close( fhandle);
      return EXIT_FAILURE;
   }
   WaitOnLocalSemaphore( loc_sem);
   printf( "bytes_read = %d, num_of_buffers = %d\n", bytes_read, num_of_buffers);
   printf( "buffer #1: completionCode     = %d,\n"
           "           cacheBufferLength  = %d,\n"
           "           cacheBufferPointer = %x\n",
           cache_bufs -> completionCode,
           cache_bufs -> cacheBufferLength,
           cache_bufs -> cacheBufferPointer);
   num_of_bytes_to_print = cache_bufs -> cacheBufferLength < 1024 ?
                           cache_bufs -> cacheBufferLength : 1024;
   ptr                   = cache_bufs -> cacheBufferPointer;
   for ( i = 0; i < num_of_bytes_to_print; ++i, ++ptr) {
      printf( "%c", isprint( *ptr) ? *ptr : '.');
   }
   AsyncRelease( cache_bufs);
   CloseLocalSemaphore( loc_sem);
   close( fhandle);
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.