home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / diskacc.zip / diskacc.c next >
C/C++ Source or Header  |  2002-07-08  |  3KB  |  163 lines

  1. /* DISKACC.C
  2.  *
  3.  * Autor:    Kai Uwe Rommel
  4.  * Datum:    Thu 28-Dec-1989
  5.  * Stand:    Fri 21-Sep-1990
  6.  *
  7.  * Compiler: MS C ab 5.00
  8.  * System:   OS/2 ab 1.1
  9.  *
  10.  * Direct disk access library for OS/2 protected mode and MS-DOS.
  11.  *
  12.  * - DLL for OS/2.
  13.  */
  14.  
  15.  
  16. #define INCL_DOSDEVICES
  17. #define INCL_DOSDEVIOCTL
  18. #define INCL_NOPM
  19. #include <os2.h>
  20.  
  21. #include "diskacc.h" 
  22.  
  23. int _acrtused = 0;
  24.  
  25.  
  26. typedef struct
  27. {
  28.   BYTE   bCommand;
  29.   USHORT usHead;
  30.   USHORT usCylinder;
  31.   USHORT usFirstSector;
  32.   USHORT cSectors;
  33.   struct
  34.   {
  35.     USHORT usSectorNumber;
  36.     USHORT usSectorSize;
  37.   }
  38.   TrackTable[64];
  39. }
  40. TRACK;
  41.  
  42.  
  43. static int test_sector(int handle, int side, int track, int sector)
  44. {
  45.   char buffer[1024];
  46.   TRACK trk;
  47.  
  48.   trk.bCommand      = 0;
  49.   trk.usHead        = side;
  50.   trk.usCylinder    = track;
  51.   trk.usFirstSector = 0;
  52.   trk.cSectors      = 1;
  53.  
  54.   trk.TrackTable[0].usSectorNumber = sector;
  55.   trk.TrackTable[0].usSectorSize   = 512;
  56.  
  57.   return (DosDevIOCtl(buffer, &trk, DSK_READTRACK, IOCTL_DISK, handle) == 0);
  58. }
  59.  
  60.  
  61. INT APIENTRY DskOpen(USHORT drive, PUSHORT sides,
  62.                      PUSHORT tracks, PUSHORT sectors)
  63. {
  64.   BIOSPARAMETERBLOCK bpb;
  65.   HFILE handle;
  66.   USHORT action;
  67.   BYTE cmd = 0;
  68.   char name[3];
  69.  
  70.   name[0] = (char) (drive + 'A');
  71.   name[1] = ':';
  72.   name[2] = 0;
  73.  
  74.   if ( DosOpen(name, &handle, &action, 0L, FILE_NORMAL, FILE_OPEN,
  75.                OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  76.                OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE, 0L) != 0 )
  77.     return -1;
  78.  
  79.   if ( DosDevIOCtl(0L, &cmd, DSK_LOCKDRIVE, IOCTL_DISK, handle) != 0 )
  80.   {
  81.     DosClose(handle);
  82.     return -1;
  83.   }
  84.  
  85.   if ( DosDevIOCtl(&bpb, &cmd, DSK_GETDEVICEPARAMS, IOCTL_DISK, handle) != 0 )
  86.   {
  87.     DosDevIOCtl(0L, &cmd, DSK_UNLOCKDRIVE, IOCTL_DISK, handle);
  88.     DosClose(handle);
  89.     return -1;
  90.   }
  91.  
  92.   *sectors = bpb.usSectorsPerTrack;
  93.   *tracks  = bpb.cCylinders;
  94.   *sides   = bpb.cHeads;
  95.  
  96.   if ( *sectors >= 15 )
  97.     if ( !test_sector(handle, 0, 0, 15) )
  98.     {
  99.       if ( *sectors == 15 )
  100.         *tracks = 40;
  101.  
  102.       *sectors = 9;
  103.     }
  104.  
  105.   return handle;
  106. }
  107.  
  108.  
  109. VOID APIENTRY DskClose(USHORT handle)
  110. {
  111.   BYTE cmd = 0;
  112.  
  113.   DosDevIOCtl(0L, &cmd, DSK_UNLOCKDRIVE, IOCTL_DISK, handle);
  114.   DosClose(handle);
  115. }
  116.  
  117.  
  118. INT APIENTRY DskRead(USHORT handle, USHORT side, USHORT  track,
  119.                      USHORT sector, USHORT nsects, PVOID buf)
  120. {
  121.   TRACK trk;
  122.   USHORT cnt;
  123.  
  124.   trk.bCommand      = 0;
  125.   trk.usHead        = side;
  126.   trk.usCylinder    = track;
  127.   trk.usFirstSector = 0;
  128.   trk.cSectors      = nsects;
  129.  
  130.   for ( cnt = 0; cnt < nsects; cnt++ )
  131.   {
  132.     trk.TrackTable[cnt].usSectorNumber = sector + cnt;
  133.     trk.TrackTable[cnt].usSectorSize   = 512;
  134.   }
  135.  
  136.   return DosDevIOCtl(buf, &trk, DSK_READTRACK, IOCTL_DISK, handle);
  137. }
  138.  
  139.  
  140. INT APIENTRY DskWrite(USHORT handle, USHORT side, USHORT  track,
  141.                       USHORT sector, USHORT nsects, PVOID buf)
  142. {
  143.   TRACK trk;
  144.   USHORT cnt;
  145.  
  146.   trk.bCommand      = 0;
  147.   trk.usHead        = side;
  148.   trk.usCylinder    = track;
  149.   trk.usFirstSector = 0;
  150.   trk.cSectors      = nsects;
  151.  
  152.   for ( cnt = 0; cnt < nsects; cnt++ )
  153.   {
  154.     trk.TrackTable[cnt].usSectorNumber = sector + cnt;
  155.     trk.TrackTable[cnt].usSectorSize   = 512;
  156.   }
  157.  
  158.   return DosDevIOCtl(buf, &trk, DSK_WRITETRACK, IOCTL_DISK, handle);
  159. }
  160.  
  161.  
  162. /* Ende DISKACC.C */
  163.