#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/disk.h>

int main(int argc, char * argv[]) {
  off_t size;
  int   error;
  int   fd;

  if (argc != 2) {
    printf("Usage: getsize <device>\n");
    return 1;
  }

  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }

  error = ioctl(fd, DIOCGMEDIASIZE, &size);
  if (error == -1) {
    perror("ioctl");
  } else {
    printf("Size: %lld\n", size);
  }
 
  close(fd);
  return 0;
}
