|
| 1 | +#if !defined(__linux__) && !defined(__APPLE__) |
| 2 | +#error \ |
| 3 | + "Do not manage to build this file unless your platform is Linux or macOS." |
| 4 | +#endif |
| 5 | + |
1 | 6 | #include <fcntl.h> |
2 | | -#include <linux/fs.h> |
| 7 | +#if defined(__linux__) |
| 8 | +#include <linux/fs.h> /* BLKGETSIZE64 */ |
| 9 | +#elif defined(__APPLE__) |
| 10 | +#include <libkern/OSByteOrder.h> |
| 11 | +#include <sys/disk.h> /* DKIOCGETBLOCKCOUNT and DKIOCGETBLOCKSIZE */ |
| 12 | +#define htole32(x) OSSwapHostToLittleInt32(x) |
| 13 | +#define le32toh(x) OSSwapLittleToHostInt32(x) |
| 14 | +#define htole64(x) OSSwapHostToLittleInt64(x) |
| 15 | +#define le64toh(x) OSSwapLittleToHostInt64(x) |
| 16 | +#endif |
3 | 17 | #include <stdint.h> |
4 | 18 | #include <stdio.h> |
5 | 19 | #include <stdlib.h> |
@@ -278,12 +292,31 @@ int main(int argc, char **argv) |
278 | 292 | /* Get block device size */ |
279 | 293 | if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) { |
280 | 294 | long int blk_size = 0; |
| 295 | +#if defined(__linux__) |
281 | 296 | ret = ioctl(fd, BLKGETSIZE64, &blk_size); |
282 | 297 | if (ret != 0) { |
283 | 298 | perror("BLKGETSIZE64:"); |
284 | 299 | ret = EXIT_FAILURE; |
285 | 300 | goto fclose; |
286 | 301 | } |
| 302 | +#elif defined(__APPLE__) |
| 303 | + uint64_t block_count = 0; |
| 304 | + uint32_t sector_size = 0; |
| 305 | + |
| 306 | + ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count); |
| 307 | + if (ret) { |
| 308 | + perror("DKIOCGETBLOCKCOUNT"); |
| 309 | + ret = EXIT_FAILURE; |
| 310 | + goto fclose; |
| 311 | + } |
| 312 | + ret = ioctl(fd, DKIOCGETBLOCKSIZE, §or_size); |
| 313 | + if (ret) { |
| 314 | + perror("DKIOCGETBLOCKSIZE"); |
| 315 | + ret = EXIT_FAILURE; |
| 316 | + goto fclose; |
| 317 | + } |
| 318 | + blk_size = block_count * sector_size; |
| 319 | +#endif |
287 | 320 | stat_buf.st_size = blk_size; |
288 | 321 | } |
289 | 322 |
|
|
0 commit comments