home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xdfs1.exe / XDFS1.C next >
C/C++ Source or Header  |  1995-04-07  |  9KB  |  304 lines

  1. /**************************************************************************
  2. ** File: xdfs1.c
  3. **
  4. ** Desc: Example program for making Direct File System calls.
  5. **
  6. **       This program demonstrates a simple implementation of
  7. **       Direct File System calls.
  8. **
  9. **
  10. **   DISCLAIMER  
  11. **  
  12. **   Novell, Inc. makes no representations or warranties with respect to
  13. **   any NetWare software, and specifically disclaims any express or
  14. **   implied warranties of merchantability, title, or fitness for a
  15. **   particular purpose.  
  16. **
  17. **   Distribution of any NetWare software is forbidden without the
  18. **   express written consent of Novell, Inc.  Further, Novell reserves
  19. **   the right to discontinue distribution of any NetWare software.
  20. **   
  21. **   Novell is not responsible for lost profits or revenue, loss of use
  22. **   of the software, loss of data, costs of re-creating lost data, the
  23. **   cost of any substitute equipment or program, or claims by any party
  24. **   other than you.  Novell strongly recommends a backup be made before
  25. **   any software is installed.   Technical support for this software
  26. **   may be provided at the discretion of Novell.
  27. **
  28. **    QMK386 options used:
  29. **
  30. **        None
  31. **
  32. ** Programmers:
  33. **
  34. **    Ini   Who                  Firm
  35. **    ---------------------------------------------------------------------
  36. **    DWH   Dirk W. Howard       Novell Developer Support
  37. **
  38. **
  39. ** History:
  40. **
  41. **    When        Who      What
  42. **    ---------------------------------------------------------------------
  43. **    2-21-1995    DWH      First code.
  44. **    4-7-1995     DWH      Made changes for portability to Power PC.
  45. **
  46. */
  47.  
  48.  
  49. /**************************************************************************
  50. ** Prototypes and macro definitions
  51. */
  52.  
  53. #ifndef PPC
  54.    /*------------------------------------------------
  55.    ** NetWare Intel include files
  56.    */
  57.    #include <dfs.h>
  58.    #include <fcntl.h>
  59.    #include <string.h>
  60.    #include <signal.h>
  61.    #include <nwdir.h>
  62.    #include <process.h>
  63.    #include <conio.h>
  64.    #include <stdio.h>
  65.    #include <errno.h>
  66.    #include <niterror.h>
  67. #else
  68.    /*------------------------------------------------
  69.    ** NetWare Power PC include files
  70.    */
  71.    #include <nwtypes.h>
  72.    #include <nwdfs.h>
  73.    #include <fcntl.h>
  74.    #include <string.h>
  75.    #include <signal.h>
  76.    #include <nwdir.h>
  77.    #include <nwthread.h>
  78.    #include <nwconio.h>
  79.    #include <stdio.h>
  80.    #include <errno.h>
  81.    #include <nwerrno.h>
  82. #endif
  83.  
  84.    /*------------------------------------------------
  85.    ** Macro substitutions
  86.    */
  87.    #define  bytesPerSector    512
  88.    #define  SIZEOFBUF         4096
  89.  
  90.    /*------------------------------------------------
  91.    ** Function Prototypes
  92.    */
  93.    void WeWereUnloaded(int sigtype);
  94.    void genericThread(void *arg);
  95.  
  96. /**************************************************************************
  97. ** Global variables
  98. */
  99. LONG  lNLMThreadCount;
  100. LONG  lNLMShutdown;
  101. LONG  lBytesPerBlock;
  102. LONG  lBlockingFactor;
  103. LONG  lSectorsInBuf;
  104.  
  105.  
  106. /**************************************************************************
  107. ** main procedure
  108. */
  109. void main()
  110. {
  111.    VOLUME_INFO    volInfo;
  112.    int            iCompCode;
  113.    int            iCount;
  114.  
  115.    /*------------------------------------------------
  116.    ** Register for SIGTERM
  117.    */
  118.    signal(SIGTERM, WeWereUnloaded);
  119.  
  120.  
  121.    /*------------------------------------------------------------------------
  122.    ** Get volume stats.
  123.    */
  124.    iCompCode=GetVolumeStatistics(
  125.       /* I- fileServerID               */ 0,
  126.       /* I- volumeNumber               */ 0,     /* SYS: */
  127.       /* I- structSize                 */ sizeof(volInfo),
  128.       /* -O returnedVolumeStatistics   */ &volInfo
  129.       );
  130.    switch(iCompCode)
  131.    {
  132.       case ESUCCESS:
  133.          break;
  134.  
  135.       case ERR_INVALID_VOLUME:
  136.          printf( "XDFS1: Error  GetVolumeStatistics() reports: Invalid "
  137.                  "volume number.\n\r");
  138.          exit( 0 );
  139.  
  140.       default:
  141.          printf( "XDFS1: Error  GetVolumeStatistics() reports error "
  142.                  "code: %d\n\r", iCompCode);
  143.          exit( 0 );
  144.    }
  145.  
  146.    lBytesPerBlock = bytesPerSector * volInfo.sectorsPerBlock;
  147.    lSectorsInBuf = SIZEOFBUF / bytesPerSector;
  148.    lBlockingFactor = lBytesPerBlock / SIZEOFBUF;
  149.  
  150.    /*------------------------------------------------
  151.    ** Spin off ten threads working on DFS calls.
  152.    */
  153.    for ( iCount=0; iCount<10; iCount++ )
  154.    {
  155.    iCompCode = BeginThread( genericThread, NULL, 0, (void *)iCount );
  156.       if( iCompCode == EFAILURE ) 
  157.          switch( errno )
  158.          {
  159.          case ENOMEM:
  160.             printf( "XDFS1: Error  BeginThread() indicated \"Not enough "
  161.                     "memory\"\n\r");
  162.             return;
  163.  
  164.          case EINVAL:
  165.             printf( "XDFS1: Error  BeginThread() indicated \"Invalid "
  166.                     "arg was passed in.\"\n\r");
  167.             return;
  168.  
  169.          default:
  170.             printf( "XDFS1: Error  BeginThread() returned unknown "
  171.                     "error: %d.\n\r", iCompCode);
  172.             return;
  173.          }
  174.    }
  175.  
  176.    /*------------------------------------------------
  177.    ** Do a thread switch to allow the spawned thread to startup
  178.    */
  179.    ThreadSwitchWithDelay();
  180.  
  181.    /*------------------------------------------------
  182.    ** Wait for threads to complete
  183.    */
  184.    while ( lNLMThreadCount != 0 )
  185.       ThreadSwitchWithDelay();
  186.    SetAutoScreenDestructionMode( TRUE );
  187.    exit( 0 );
  188. }
  189.  
  190.  
  191. /**************************************************************************
  192. ** WeWereUnloaded Procedure
  193. */
  194. void WeWereUnloaded(int sigtype)
  195. {
  196.    sigtype = sigtype;
  197.    lNLMShutdown = TRUE;
  198.    
  199.    while(lNLMThreadCount > 0)
  200.       ThreadSwitchWithDelay();
  201. }
  202.  
  203.  
  204. /**************************************************************************
  205. ** Generic Thread
  206. */
  207. void genericThread(void *arg)
  208. {
  209.    LONG  lCount;
  210.    LONG  lThreadNo = (LONG)arg;
  211.    LONG  lFH;
  212.    LONG  lCompCode;
  213.    char  caFilename[80];
  214.    char  caBuf[ SIZEOFBUF ];
  215.  
  216.    lNLMThreadCount++;
  217.  
  218.    sprintf( caFilename, "DFS%ld.DWH", lThreadNo );
  219.    lFH = DFScreat( 
  220.             /* I- filename */    (BYTE *)caFilename, 
  221.             /* I- access   */    O_RDWR | O_CREAT | O_TRUNC, 
  222.             /* I- flagBits */    0x0001
  223.             );
  224.    if ( lFH == -1 )
  225.    {
  226.       printf( "XDFS1: Error in DFScreat in thread %ld\n\r", lThreadNo );
  227.       perror( "DFScreat" );
  228.       lNLMThreadCount--;
  229.       return;
  230.    }
  231.    printf( "Opened file %s in thread %ld.\n\r", caFilename, lThreadNo );
  232.    
  233.    /*------------------------------------------------
  234.    ** Write caBuf to the file 64 times
  235.    */
  236.    for ( lCount=0; lCount<64; lCount++ )
  237.    {
  238.       memset( caBuf, '!'+lCount, SIZEOFBUF );
  239.  
  240.       /*------------------------------------------------
  241.       ** Check to see if we are exiting
  242.       */
  243.       if ( lNLMShutdown == TRUE )
  244.       {
  245.          DFSSetEndOfFile( lFH, (LONG)( sizeof(caBuf) * lCount ), 1 );
  246.          DFSclose( lFH );
  247.          lNLMThreadCount--;
  248.          return;
  249.       }
  250.  
  251.       /*------------------------------------------------
  252.       ** Allocated disk storage
  253.       */
  254.       if ( (lCount % lBlockingFactor) == 0 )
  255.       {
  256.          lCompCode = DFSExpandFile(
  257.                         /* I- fileHandle        */  lFH,
  258.                         /* I- fileBlockNumber   */  lCount/lBlockingFactor,
  259.                         /* I- numberOfBlocks    */  1,
  260.                         /* I- volumeBlockNumber */ -1,
  261.                         /* I- segmentNumber     */ -1
  262.                         );
  263.          if ( lCompCode != 0 )
  264.          {
  265.             printf( "XDFS1: Error in DFSExpandFile in thread %ld\n\r"
  266.                     "Error Code %X\n\r\n\r", lThreadNo, lCompCode );
  267.             DFSSetEndOfFile( lFH, (LONG)( sizeof(caBuf) * lCount ), 1 );
  268.             DFSclose( lFH );
  269.             lNLMThreadCount--;
  270.             return;
  271.          }
  272.          printf( "Expanded file %s in thread %ld.\n\r", 
  273.                  caFilename, lThreadNo );
  274.       }
  275.  
  276.       /*------------------------------------------------
  277.       ** Write buffer to file
  278.       */
  279.       lCompCode = DFSWrite(
  280.                      /* I- fileHandle     */    lFH,
  281.                      /* I- startingSector */    lCount * lSectorsInBuf,
  282.                      /* I- sectorCount    */    lSectorsInBuf,
  283.                      /* I- buffer         */    (BYTE *)caBuf
  284.                      );
  285.       if ( lCompCode != 0 )
  286.       {
  287.          printf( "XDFS1: Error %ld during DFSWrite in thread %ld\n\r\n\r", 
  288.                  lCompCode, lThreadNo );
  289.          DFSSetEndOfFile( lFH, (LONG)( sizeof(caBuf) * lCount ), 1 );
  290.          DFSclose( lFH );
  291.          lNLMThreadCount--;
  292.          return;
  293.       }
  294.       printf( "Wrote data to %s beginning at sector %ld in thread %ld.\n\r",
  295.               caFilename, lCount * lSectorsInBuf, lThreadNo );
  296.  
  297.    }
  298.    DFSSetEndOfFile( lFH, (LONG)( sizeof(caBuf) * lCount ), 1 );
  299.    DFSclose( lFH );
  300.    lNLMThreadCount--;
  301.    return;
  302. }
  303.  
  304.