dkgetsz.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*-
  2. * Copyright © 2010
  3. * Waldemar Brodkorb <wbx@openadk.org>
  4. * Thorsten Glaser <tg@mirbsd.org>
  5. *
  6. * Provided that these terms and disclaimer and all copyright notices
  7. * are retained or reproduced in an accompanying document, permission
  8. * is granted to deal in this work without restriction, including un‐
  9. * limited rights to use, publicly perform, distribute, sell, modify,
  10. * merge, give away, or sublicence.
  11. *
  12. * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
  13. * the utmost extent permitted by applicable law, neither express nor
  14. * implied; without malicious intent or gross negligence. In no event
  15. * may a licensor, author or contributor be held liable for indirect,
  16. * direct, other damage, loss, or other issues arising in any way out
  17. * of dealing in the work, even if advised of the possibility of such
  18. * damage or existence of a defect, except proven that it results out
  19. * of said person’s immediate fault when using the work as intended.
  20. *
  21. * Alternatively, this work may be distributed under the terms of the
  22. * General Public License, any version, as published by the Free Soft-
  23. * ware Foundation.
  24. *-
  25. * Display the size of a block device (e.g. USB stick, CF/SF/MMC card
  26. * or hard disc) in 512-byte sectors.
  27. */
  28. #define _FILE_OFFSET_BITS 64
  29. #include <sys/param.h>
  30. #include <sys/types.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/mount.h>
  33. #if defined(__APPLE__)
  34. #include <sys/disk.h>
  35. #endif
  36. #if defined(DIOCGDINFO)
  37. #include <sys/disklabel.h>
  38. #endif
  39. #include <err.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. unsigned long long numsecs(int);
  44. int
  45. main(int argc, char *argv[]) {
  46. int fd;
  47. if (argc != 2)
  48. errx(255, "Syntax: dkgetsz /dev/sda");
  49. if ((fd = open(argv[1], O_RDONLY)) == -1)
  50. err(1, "open");
  51. printf("%llu\n", numsecs(fd));
  52. close(fd);
  53. return (0);
  54. }
  55. unsigned long long
  56. numsecs(int fd)
  57. {
  58. #if defined(BLKGETSIZE) || defined(DKIOCGETBLOCKCOUNT)
  59. /*
  60. * note: BLKGETSIZE64 returns bytes, not sectors, but the return
  61. * type is size_t which is 32 bits on an ILP32 platform, so it
  62. * fails interestingly here… thus we use BLKGETSIZE instead.
  63. */
  64. #if defined(DKIOCGETBLOCKCOUNT)
  65. uint64_t nsecs;
  66. #define THEIOCTL DKIOCGETBLOCKCOUNT
  67. #define STRIOCTL "DKIOCGETBLOCKCOUNT"
  68. #else
  69. unsigned long nsecs;
  70. #define THEIOCTL BLKGETSIZE
  71. #define STRIOCTL "BLKGETSIZE"
  72. #endif
  73. if (ioctl(fd, THEIOCTL, &nsecs) == -1)
  74. err(1, "ioctl %s", STRIOCTL);
  75. return ((unsigned long long)nsecs);
  76. #elif defined(DIOCGDINFO)
  77. struct disklabel dl;
  78. if (ioctl(fd, DIOCGDINFO, &dl) == -1)
  79. err(1, "ioctl DIOCGDINFO");
  80. return ((unsigned long long)dl.d_secperunit);
  81. #else
  82. #error PLEASE DO IMPLEMENT numsecs FOR THIS PLATFORM.
  83. #endif
  84. }