home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / diski117.zip / diskacc3.c < prev    next >
C/C++ Source or Header  |  1998-07-05  |  6KB  |  246 lines

  1. /* diskacc3.c - direct disk access library for Win32.
  2.  *
  3.  * Author:  Kai Uwe Rommel <rommel@ars.de>
  4.  * Created: Fri Jul 08 1994
  5.  */
  6.  
  7. static char *rcsid =
  8. "$Id: diskacc3.c,v 1.1 1998/07/05 07:44:17 rommel Exp rommel $";
  9. static char *rcsrev = "$Revision: 1.1 $";
  10.  
  11. /*
  12.  * $Log: diskacc3.c,v $
  13.  * Revision 1.1  1998/07/05 07:44:17  rommel
  14.  * Initial revision
  15.  *
  16.  */
  17.  
  18. #define _INTEGRAL_MAX_BITS 64
  19. #include <windows.h>
  20. #include <winioctl.h>
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. #include "diskacc.h"
  27.  
  28. /* memory allocation for disk buffers */
  29.  
  30. void *DskAlloc(unsigned sectors, unsigned bytespersector)
  31. {
  32.   return VirtualAlloc(NULL, sectors * bytespersector, MEM_COMMIT, PAGE_READWRITE);
  33. }
  34.  
  35. void DskFree(void *ptr)
  36. {
  37.   VirtualFree(ptr, 0, MEM_RELEASE);
  38. }
  39.  
  40. /* logical/physical hard disk and floppy disk access code */
  41.  
  42. int DskCount(void)
  43. {
  44.   char device[1024];
  45.   int devices = 0;
  46.   HANDLE file;
  47.  
  48.   strcpy(device, "\\\\.\\PhysicalDrive0");
  49.  
  50.   for (;;)
  51.   {
  52.     device[17] = '0' + devices;
  53.     file = CreateFile(device, 0, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  54.  
  55.     if (file == INVALID_HANDLE_VALUE)
  56.       break;
  57.  
  58.     CloseHandle(file);
  59.     devices++;
  60.   }
  61.  
  62.   return devices;
  63. }
  64.  
  65. int DskOpen(char *drv, int logical, int lock, unsigned *sector,
  66.         unsigned *sides, unsigned *tracks, unsigned *sectors)
  67. {
  68.   char device[1024];
  69.   HANDLE handle;
  70.   DISK_GEOMETRY geo;
  71.   DWORD bytes;
  72.  
  73.   if (isalpha(drv[0]) && drv[1] == ':' && drv[2] == 0)
  74.   {
  75.     strcpy(device, "\\\\.\\A:");
  76.     device[4] = drv[0];
  77.  
  78.     handle = CreateFile(device, GENERIC_READ|GENERIC_WRITE, 
  79.             FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
  80.             OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
  81.  
  82.     if (handle == INVALID_HANDLE_VALUE)
  83.       return -1;
  84.   }
  85.   else if (drv[0] == '$' && isdigit(drv[1]) && drv[2] == ':' && drv[3] == 0)
  86.   {
  87.     strcpy(device, "\\\\.\\PhysicalDrive0");
  88.     device[17] = drv[1] - 1;
  89.  
  90.     handle = CreateFile(device, GENERIC_READ|GENERIC_WRITE, 
  91.             FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
  92.             OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
  93.  
  94.     if (handle == INVALID_HANDLE_VALUE)
  95.       return -1;
  96.   }
  97.   else
  98.     return -1;
  99.  
  100.   /* do locking here, when implemented */
  101.  
  102.   if (!DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 
  103.                NULL, 0, &geo, sizeof(geo), &bytes, NULL))
  104.     return CloseHandle(handle), -1;
  105.  
  106.   *sectors = geo.SectorsPerTrack;
  107.   *tracks = geo.Cylinders.LowPart;
  108.   *sides = geo.TracksPerCylinder;
  109.   *sector = geo.BytesPerSector;
  110.  
  111.   if (geo.Cylinders.HighPart != 0)
  112.   {
  113.     printf("Error: more than 2^32 tracks not supported\n");
  114.     CloseHandle(handle);
  115.     return -1;
  116.   }
  117.  
  118.   return (int) handle;
  119. }
  120.  
  121. int DskClose(int handle)
  122. {
  123.   return !CloseHandle((HANDLE) handle);
  124. }
  125.  
  126. int DskRead(int handle, unsigned side, unsigned track,
  127.             unsigned sector, unsigned nsects, void *buf)
  128. {
  129.   DISK_GEOMETRY geo;
  130.   LARGE_INTEGER pos;
  131.   DWORD bytes;
  132.  
  133.   if (!DeviceIoControl((HANDLE) handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 
  134.                NULL, 0, &geo, sizeof(geo), &bytes, NULL))
  135.     return -1;
  136.  
  137.   pos.QuadPart = ((track * geo.TracksPerCylinder + side) 
  138.           * geo.SectorsPerTrack + sector) * geo.BytesPerSector;
  139.  
  140.   if (SetFilePointer((HANDLE) handle, pos.LowPart, &pos.HighPart, 
  141.              FILE_BEGIN) == 0xFFFFFFFF)
  142.     return -1;
  143.   
  144.   if (!ReadFile((HANDLE) handle, buf, nsects * geo.BytesPerSector, &bytes, NULL))
  145.     return -1;
  146.  
  147.   return bytes != nsects * geo.BytesPerSector;
  148. }
  149.  
  150. int DskWrite(int handle, unsigned side, unsigned  track,
  151.              unsigned sector, unsigned nsects, void *buf)
  152. {
  153.   DISK_GEOMETRY geo;
  154.   LARGE_INTEGER pos;
  155.   DWORD bytes;
  156.  
  157.   if (!DeviceIoControl((HANDLE) handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 
  158.                NULL, 0, &geo, sizeof(geo), &bytes, NULL))
  159.     return -1;
  160.  
  161.   pos.QuadPart = ((track * geo.TracksPerCylinder + side) 
  162.           * geo.SectorsPerTrack + sector) * geo.BytesPerSector;
  163.  
  164.   if (SetFilePointer((HANDLE) handle, pos.LowPart, &pos.HighPart, 
  165.              FILE_BEGIN) == 0xFFFFFFFF)
  166.     return -1;
  167.   
  168.   if (!WriteFile((HANDLE) handle, buf, nsects * geo.BytesPerSector, &bytes, NULL))
  169.     return -1;
  170.  
  171.   return bytes != nsects * geo.BytesPerSector;
  172. }
  173.  
  174. /* CD-ROM access code */
  175.  
  176. int CDFind(int number)
  177. {
  178.   char buffer[5120], *ptr;
  179.   int devices = 0;
  180.  
  181.   if (GetLogicalDriveStrings(sizeof(buffer), buffer) == 0)
  182.     return -1;
  183.  
  184.   for (ptr = buffer; *ptr; ptr += strlen(ptr) + 1)
  185.     if (GetDriveType(ptr) == DRIVE_CDROM)
  186.     {
  187.       devices++;
  188.  
  189.       if (devices == number)
  190.     return *ptr;
  191.     }
  192.  
  193.   return devices;
  194. }
  195.  
  196. int CDOpen(char *drv, int lock, char *upc, unsigned *sectors)
  197. {
  198.   char device[1024];
  199.   HANDLE handle;
  200.   DWORD Cluster, Sector, FreeClusters, TotalClusters;
  201.  
  202.   strcpy(device, "A:\\");
  203.   device[0] = drv[0];
  204.  
  205.   if (!GetDiskFreeSpace(device, &Cluster, &Sector, &FreeClusters, &TotalClusters))
  206.     return -1;
  207.  
  208.   *sectors = TotalClusters * Cluster;
  209.  
  210.   strcpy(device, "\\\\.\\A:");
  211.   device[4] = drv[0];
  212.  
  213.   handle = CreateFile(device, GENERIC_READ, 
  214.               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
  215.               OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
  216.  
  217.   if (handle == INVALID_HANDLE_VALUE)
  218.     return -1;
  219.  
  220.   return (int) handle;
  221. }
  222.  
  223. int CDClose(int handle)
  224. {
  225.   return !CloseHandle((HANDLE) handle);
  226. }
  227.  
  228. int CDRead(int handle, unsigned sector, unsigned nsects, void *buf)
  229. {
  230.   LARGE_INTEGER pos;
  231.   DWORD bytes;
  232.  
  233.   pos.QuadPart = sector * 2048;
  234.  
  235.   if (SetFilePointer((HANDLE) handle, pos.LowPart, &pos.HighPart, 
  236.              FILE_BEGIN) == 0xFFFFFFFF)
  237.     return -1;
  238.   
  239.   if (!ReadFile((HANDLE) handle, buf, nsects * 2048, &bytes, NULL))
  240.     return -1;
  241.  
  242.   return bytes / 2048;
  243. }
  244.  
  245. /* end of diskacc3.c */
  246.