home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / WINLBSRC.ZIP / IOCTL.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  4.9 KB  |  150 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - ioctl.cas
  3.  *
  4.  * function(s)
  5.  *    ioctl - control I/O device
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. #pragma inline
  17.  
  18. #define __IN_IOCTL
  19.  
  20. #include <asmrules.h>
  21. #include <io.h>
  22. #include <_io.h>
  23. #include <errno.h>
  24.  
  25. extern int _protected;  // Needed until _protected is in a header file.
  26.  
  27. /*-----------------------------------------------------------------------*
  28.  
  29. Name            ioctl - controls I/O device
  30.  
  31. Usage           int ioctl(int handle, int cmd [, void *argdx, int argcx]);
  32.  
  33. Prototype in    io.h
  34.  
  35. Description     This is a direct interface to the MS-DOS call 0x44
  36.                 (IOCTL).
  37.                 The exact function depends on the value of cmd as follows:
  38.  
  39.                     0   Get device information
  40.                     1   Set device information (in argdx)
  41.                     2   Read argcx bytes into the address pointed to by
  42.                         argdx
  43.                     3   Write argcx bytes from the address pointed to
  44.                         by argdx
  45.                     4   Same as 2, except handle is treated as a drive number
  46.                         (0 = default, 1 = A, etc.)
  47.                     5   Same as 3, except handle is a drive number
  48.                         (0 = default, 1 = A, etc.)
  49.                     6   Get input status
  50.                     7   Get output status
  51.                     8   Test removability; DOS 3.x only
  52.                     11  Set sharing conflict retry count; DOS 3.x only
  53.  
  54.                 ioctl can be used to get information about device channels.
  55.                 Regular files can also be used, but only cmd values 0, 6, and
  56.                 7 are defined for them. All other calls return an EINVAL error
  57.                 for files.
  58.  
  59.                 See the documentation for system call 0x44 in the MS-DOS
  60.                 Programmer's Reference Manual for detailed information on
  61.                 argument or return values.
  62.  
  63.                 The arguments argdx and argcx are optional.
  64.  
  65.                 ioctl provides a direct interface to DOS 2.0 device drivers for
  66.                 special functions. As a result, the exact behavior of this
  67.                 function will vary across different vendors' hardware and in
  68.                 different devices. Also, several vendors do not follow the
  69.                 interfaces described here. Refer to the vendor BIOS
  70.                 documentation for exact use of ioctl.
  71.  
  72. Return value    For cmd 0 or 1, the return value is the device
  73.                 information (DX of the IOCTL call).
  74.  
  75.                 For cmd values of 2 through 5, the return value is the
  76.                 number of bytes actually transferred.
  77.  
  78.                 For cmd values of 6 or 7, the return value is the device
  79.                 status.
  80.  
  81.                 In any event, if an error is detected, a value of -1 is
  82.                 returned, and errno is set to one of the following:
  83.  
  84.                     EINVAL  Invalid argument
  85.                     EBADF   Bad file number
  86.                     EINVDAT Invalid data
  87.  
  88. NOTE:           ioctl has old style definition since prototype has ... ;
  89.                 argdx and argcx are optional
  90. *------------------------------------------------------------------------*/
  91. int ioctl (fd, func, argdx, argcx)
  92.     int fd; int func; void *argdx; int argcx;
  93. {
  94. #if LDATA
  95.     int UsesDX;
  96.  
  97.     switch (func)
  98.         {
  99.         case  0:            // These functions do not take an argument in DX
  100.         case  6:
  101.         case  7:
  102.         case  8:
  103.         case  9:
  104.         case 10:
  105.         case 14:
  106.         case 15:    UsesDX = 0;
  107.                     break;
  108.  
  109.         default:    UsesDX = 1;
  110.                     break;
  111.         }
  112.  
  113.     if (!UsesDX)
  114.         goto AfterDX;
  115.  
  116.     asm .286P
  117.     asm mov ax,W1(argdx)    // Load segment/selector into DX
  118.     asm cmp _protected,0
  119.     asm je  SelectorOK      // Real mode, no check on segment
  120.     asm verr ax             // Check whether segment to load is readable
  121.     asm jz  SelectorOK      // Zero Flag set if selector is OK
  122.     asm mov ax,EINVAL       // Otherwise invalid argument
  123.     asm jmp ioctlFailed
  124. SelectorOK:
  125.     asm push ds             // Save DS
  126.     asm mov ds,ax           // Load selector
  127. #endif
  128.  
  129.     asm mov dx,argdx        // Ignore segment, already loaded if present
  130. AfterDX:
  131.     asm mov ah,44h
  132.     asm mov al,func
  133.     asm mov bx,fd
  134.     asm mov cx,argcx
  135.     asm int 21h
  136.  
  137. #if LDATA
  138.     if (UsesDX)
  139.         asm pop ds;         // Restore DS
  140. #endif
  141.  
  142.     asm jc  ioctlFailed
  143.     if (func == 0)
  144.         return(_DX);        // Dev info is in dx
  145.     return(_AX);
  146.  
  147. ioctlFailed:
  148.     return  __IOerror (_AX);
  149. }
  150.