Read drive number from the command line.
[reactos.git] / reactos / apps / utils / partinfo / partinfo.c
index c7e95fd..56dbd48 100644 (file)
@@ -54,6 +54,13 @@ void HexDump(char *buffer, ULONG size)
 }
 #endif
 
+
+void Usage(void)
+{
+  puts("Usage: partinfo <drive number>");
+}
+
+
 int main (int argc, char *argv[])
 {
   HANDLE hDisk;
@@ -62,8 +69,53 @@ int main (int argc, char *argv[])
   char *Buffer;
   DRIVE_LAYOUT_INFORMATION *LayoutBuffer;
   DISK_GEOMETRY DiskGeometry;
+  ULONG ulDrive;
+  CHAR DriveName[40];
+  SYSTEM_DEVICE_INFORMATION DeviceInfo;
+  NTSTATUS Status;
 
-  hDisk = CreateFile("\\\\.\\PHYSICALDRIVE0",
+  if (argc != 2)
+    {
+      Usage();
+      return(0);
+    }
+
+  ulDrive = strtoul(argv[1], NULL, 10);
+  if (errno != 0)
+    {
+      printf("Error: Malformed drive number\n", errno);
+      return(0);
+    }
+
+  /* Check drive number */
+  Status = NtQuerySystemInformation(SystemDeviceInformation,
+                                   &DeviceInfo,
+                                   sizeof(SYSTEM_DEVICE_INFORMATION),
+                                   &i);
+  if (!NT_SUCCESS(Status))
+    {
+      printf("NtQuerySystemInformation() failed (Status %lx)\n", Status);
+      return(0);
+    }
+
+  if (DeviceInfo.NumberOfDisks == 0)
+    {
+      printf("No disk drive installed!\n");
+      return(0);
+    }
+
+  if (ulDrive >= DeviceInfo.NumberOfDisks)
+    {
+      printf("Invalid disk drive number! Valid drive numbers [0-%lu]\n",
+            DeviceInfo.NumberOfDisks-1);
+      return(0);
+    }
+
+  /* Build full drive name */
+  sprintf(DriveName, "\\\\.\\PHYSICALDRIVE%lu", ulDrive);
+
+  /* Open drive */
+  hDisk = CreateFile(DriveName,
                     GENERIC_READ,
                     FILE_SHARE_READ | FILE_SHARE_WRITE,
                     NULL,
@@ -76,8 +128,7 @@ int main (int argc, char *argv[])
       return 0;
     }
 
-
-  /* get drive geometry */
+  /* Get drive geometry */
   if (!DeviceIoControl(hDisk,
                       IOCTL_DISK_GET_DRIVE_GEOMETRY,
                       NULL,
@@ -97,6 +148,7 @@ int main (int argc, char *argv[])
 #ifdef DUMP_DATA
   HexDump((char*)&DiskGeometry, dwRead);
 #endif
+  printf("Drive number: %lu\n", ulDrive);
   printf("Cylinders: %I64u\nMediaType: %lx\nTracksPerCylinder: %lu\n"
         "SectorsPerTrack: %lu\nBytesPerSector: %lu\n\n",
         DiskGeometry.Cylinders.QuadPart,
@@ -125,7 +177,7 @@ int main (int argc, char *argv[])
                       NULL))
     {
       CloseHandle(hDisk);
-      printf("DeviceIoControl failed! Error: %u\n",
+      printf("DeviceIoControl(IOCTL_DISK_GET_DRIVE_LAYOUT) failed! Error: %u\n",
             GetLastError());
       free(Buffer);
       return 0;
@@ -154,10 +206,6 @@ int main (int argc, char *argv[])
             LayoutBuffer->PartitionEntry[i].PartitionLength.QuadPart);
     }
 
-#ifdef DUMP_SIZE_INFO
-  printf("\nsizeof(PARTITION_INFORMATION): %lu\n", sizeof(PARTITION_INFORMATION));
-#endif
-
   free(Buffer);
 
   return 0;