home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_02 / 2n02060a < prev    next >
Text File  |  1991-01-02  |  5KB  |  159 lines

  1.  
  2. /* ---------------------------------------------------
  3.     TESTRELO.C  -  Microsoft C ver 5.1
  4.  
  5.    A driver used to validate the assembly language
  6.    function relocate_pfht().
  7.  
  8.    NOTE:
  9.       The routines open_file() and close_file() are
  10.       included here because the limit imposed by
  11.       Microsoft on the number of open streams and/or
  12.       handles using their library functions.
  13.  
  14.    Commands to produce an executable from the code as
  15.    shown in the listings:
  16.        masm relocate;         (listing 5)
  17.        cl -c testrelo.c       (listing 6)
  18.        link testrelo relocate;
  19.    -------------------------------------------------- */
  20. #include <stdio.h>
  21. #include <stdlib.h>     /* errno definition */
  22. #include <dos.h>        /* file create attributes */
  23.  
  24. /* define the maximum attempts to open NUL */
  25. #define MAX_TRIES 30
  26.  
  27.                /* FUNCTION PROTOTYPES */
  28. void useage(void);
  29. int open_file(char far *, unsigned);
  30. int close_file(unsigned);
  31. extern int relocate_pfht(void);
  32.  
  33. /* a place to store the handles returned by open_file() */
  34. int handles[MAX_TRIES];
  35.  
  36.  
  37. /* ----------------------------------------------------
  38.  *  MAIN()
  39.    ----------------------------------------------------  */
  40. int main( int argc, char *argv[] )
  41. {
  42. int i, num;
  43.                     /* handle some simple exceptions */
  44.   if( argc < 2 || *(argv[1]) == '?' )
  45.      {
  46.      useage();
  47.      exit(1);
  48.      }
  49.  
  50.   num = atoi(argv[1]);
  51.   if( num > 30 )
  52.      {
  53.      useage();
  54.      exit(1);
  55.      }
  56.  
  57.   relocate_pfht();  /* move the PFHT */
  58.  
  59.                     /* attempt to open the NUL device */
  60.   for(i = 0; i < num; i++)
  61.      {
  62.      handles[i] = open_file((char far *)"nul", _A_NORMAL);
  63.      if( handles[i] == -1 )
  64.         {
  65.         printf("open not successful.  errno = %x hex\n",
  66.                errno);
  67.         printf("failed attempting to open #%d\n", i+1);
  68.         break;
  69.         }
  70.      }
  71.                     /* close what was opened */
  72.   while( i >= 0 )
  73.      close_file(handles[i--]);
  74. }
  75.  
  76. /* ----------------------------------------------------
  77.  *  USEAGE()
  78.  * Let the user know what is expected as input and what
  79.  * the output will be.
  80.    ----------------------------------------------------  */
  81. void useage()
  82. {
  83.   printf("Pass a command line parameter less than or\n");
  84.   printf("equal to %d.  This driver will then attempt\n",
  85.          MAX_TRIES);
  86.   printf("to open the NUL device %d times.\n\n", MAX_TRIES);
  87.   printf("The driver will report the number on which\n");
  88.   printf("open failed.\n");
  89. }
  90.  
  91. /* ----------------------------------------------------
  92.  *  OPEN_FILE()
  93.  * use function 0x3c, int 0x21 to open a file/device
  94.  * PARAMETERS:
  95.  *      name    - pointer to the file/device name
  96.  *      attr    - open mode attributes (same as MSC
  97.  *                function open())
  98.  * RETURNS:
  99.  *      success:  the handle to the file/device
  100.  *      failure:  -1 and global variable errno set to
  101.  *                the error code returned by DOS.
  102.  * NOTE the far pointer to the filespec.
  103.    ----------------------------------------------------  */
  104. int open_file( char far *name, unsigned attr )
  105. {
  106. union REGS inregs, outregs;
  107. struct SREGS segs;
  108. unsigned ret_val;
  109.                     /* setup for DOS open call */
  110.   segs.ds = FP_SEG(name);
  111.   inregs.x.dx = FP_OFF(name);
  112.   inregs.h.ah = 0x3c;
  113.   inregs.x.cx = attr;
  114.                     /* DOS open */
  115.   int86x(0x21, &inregs, &outregs, &segs);
  116.  
  117.   if( outregs.x.cflag )
  118.      {              /* open failed */
  119.      errno = outregs.x.ax;
  120.      ret_val = -1;
  121.      }
  122.   else              /* open successful */
  123.      ret_val = outregs.x.ax;
  124.  
  125.   return(ret_val);
  126. }
  127.  
  128. /* ----------------------------------------------------
  129.  *  CLOSE_FILE()
  130.  * use function 0x3e, int 0x21 to close a file/device
  131.  * PARAMETERS:
  132.  *      handle  - the handle of the open file/device
  133.  * RETURNS:
  134.  *      success:  zero
  135.  *      failure:  -1 and global variable errno set to
  136.  *                the error code returned by DOS.
  137.    ----------------------------------------------------  */
  138. int close_file( unsigned handle )
  139. {
  140. union REGS inregs, outregs;
  141. unsigned ret_val;
  142.                     /* setup for the DOS close call */
  143.   inregs.h.ah = 0x3e;
  144.   inregs.x.bx = handle;
  145.                     /* DOS close */
  146.   int86(0x21, &inregs, &outregs);
  147.                     /* failed close */
  148.   if( outregs.x.cflag )
  149.      {
  150.      errno = outregs.x.ax;
  151.      ret_val = -1;
  152.      }
  153.   else              /* successful close */
  154.      ret_val = 0;
  155.  
  156.   return(ret_val);
  157. }
  158.  
  159.