darwin: read disk block sizes and counts

Fixes cgpt add, create, etc.

Change-Id: I3017c2d3f3fccfdd4d761cf8c3631db8a350fee2
diff --git a/cgpt/cgpt_common.c b/cgpt/cgpt_common.c
index 1a6bd3c..4e33629 100644
--- a/cgpt/cgpt_common.c
+++ b/cgpt/cgpt_common.c
@@ -18,6 +18,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef HAVE_MACOS
+#include <sys/disk.h>
+#endif
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -304,22 +307,27 @@
   if (fstat(fd, &stat) == -1) {
     return -1;
   }
-#ifndef HAVE_MACOS
   if ((stat.st_mode & S_IFMT) != S_IFREG) {
+#ifndef HAVE_MACOS
     if (ioctl(fd, BLKGETSIZE64, size) < 0) {
       return -1;
     }
     if (ioctl(fd, BLKSSZGET, sector_bytes) < 0) {
       return -1;
     }
+#else
+    if (ioctl(fd, DKIOCGETBLOCKSIZE, sector_bytes) < 0) {
+      return -1;
+    }
+    if (ioctl(fd, DKIOCGETBLOCKCOUNT, size) < 0) {
+      return -1;
+    }
+    *size = *size * *sector_bytes;
+#endif
   } else {
     *sector_bytes = 512;  /* bytes */
     *size = stat.st_size;
   }
-#else
-  *sector_bytes = 512;  /* bytes */
-  *size = stat.st_size;
-#endif
   return 0;
 }
 
@@ -333,11 +341,11 @@
   // Clear struct for proper error handling.
   memset(drive, 0, sizeof(struct drive));
 
-  drive->fd = open(drive_path, mode | 
 #ifndef HAVE_MACOS
-		               O_LARGEFILE |
+  mode |= O_LARGEFILE;
 #endif
-			       O_NOFOLLOW);
+
+  drive->fd = open(drive_path, mode | O_NOFOLLOW);
   if (drive->fd == -1) {
     Error("Can't open %s: %s\n", drive_path, strerror(errno));
     return CGPT_FAILED;