#include <stdio.h>         // general I/O
#include <fcntl.h>         // options for open()
#define O_LARGEFILE    0100000
#include <sys/ioctl.h>     // provides ioctl()
#include <linux/fs.h>      // BLKRRPART

int main(int argc, char** argv, char** env)
{
 int fd = -1;
 int err = 0;
 char* filename = NULL;

   if (argc > 1)
   {
      filename = argv[1];
   } 
   else
   {
      printf("Usage example: reset_disk /dev/hda\n");
      printf("Calls the BLKRRPART ioctl() to make kernel re-read and re-interpret partition table.\n");
      printf("This only works if no devices (filesystems) are currently mounted off that disk.\n");
      exit(1);
   }

   fd = open(filename, O_RDONLY|O_LARGEFILE);
   if (fd <= 0)
   {
      printf("Unable to open %s. Exiting\n", filename);
      exit(1);
   }

   err = ioctl(3, BLKRRPART, NULL);
   if (err)
   {
      printf("Error re-reading partition tables using the BLKRRPART ioctl().\n");
      exit(1);
   }

   close(fd);
   
   return(0);
}
