+#if defined(__APPLE__)
+ if (!S_ISREG(stbuf.st_mode))
+ {
+ uint32_t block_size = 0;
+ uint64_t blocks = 0;
+
+ if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
+ {
+ close(dev->fd);
+ free(dev);
+ exfat_error("failed to get block size");
+ return NULL;
+ }
+ if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
+ {
+ close(dev->fd);
+ free(dev);
+ exfat_error("failed to get blocks count");
+ return NULL;
+ }
+ dev->size = blocks * block_size;
+ }
+ else
+#elif defined(__OpenBSD__)
+ if (!S_ISREG(stbuf.st_mode))
+ {
+ struct disklabel lab;
+ struct partition* pp;
+ char* partition;
+
+ if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
+ {
+ close(dev->fd);
+ free(dev);
+ exfat_error("failed to get disklabel");
+ return NULL;
+ }
+
+ /* Don't need to check that partition letter is valid as we won't get
+ this far otherwise. */
+ partition = strchr(spec, '\0') - 1;
+ pp = &(lab.d_partitions[*partition - 'a']);
+ dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
+
+ if (pp->p_fstype != FS_NTFS)
+ exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
+ "you can fix this with fdisk(8)");
+ }
+ else
+#endif
+ {
+ /* works for Linux, FreeBSD, Solaris */
+ dev->size = exfat_seek(dev, 0, SEEK_END);
+ if (dev->size <= 0)
+ {
+ close(dev->fd);
+ free(dev);
+ exfat_error("failed to get size of '%s'", spec);
+ return NULL;
+ }
+ if (exfat_seek(dev, 0, SEEK_SET) == -1)
+ {
+ close(dev->fd);
+ free(dev);
+ exfat_error("failed to seek to the beginning of '%s'", spec);
+ return NULL;
+ }
+ }
+