summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorsten Glaser <tg@mirbsd.org>2010-11-20 15:59:19 +0000
committerWaldemar Brodkorb <wbx@openadk.org>2010-11-20 20:22:04 +0100
commita4f2ed3645d6b825e769f557e95bbf708e033da2 (patch)
tree267200315041d5726286e22359ba1ed14feeafc3
parentd4bab9e306b4b9fbf02bbd737261db34ee99479f (diff)
add tool to display the size of a HDD/CF/SD/MMC/USB in 512-byte sectors
Signed-off-by: Thorsten Glaser <tg@mirbsd.org>
-rw-r--r--tools/adk/Makefile5
-rw-r--r--tools/adk/dkgetsz.c95
2 files changed, 99 insertions, 1 deletions
diff --git a/tools/adk/Makefile b/tools/adk/Makefile
index 0de5b0291..6d02b0d62 100644
--- a/tools/adk/Makefile
+++ b/tools/adk/Makefile
@@ -15,7 +15,10 @@ ${TOPDIR}/bin/tools/pkgrebuild: $(TOPDIR)/bin/tools
${TOPDIR}/bin/tools/pt: $(TOPDIR)/bin/tools
$(HOSTCC) -o $(TOPDIR)/bin/tools/pt pt.c
+${TOPDIR}/bin/tools/dkgetsz: ${TOPDIR}/bin/tools
+ ${HOSTCC} -O2 -Wall -o $@ dkgetsz.c
+
install: ${TOPDIR}/bin/tools/depmaker ${TOPDIR}/bin/tools/pkgrebuild \
- $(TOPDIR)/bin/tools/pt
+ $(TOPDIR)/bin/tools/pt ${TOPDIR}/bin/tools/dkgetsz
include $(TOPDIR)/mk/tools.mk
diff --git a/tools/adk/dkgetsz.c b/tools/adk/dkgetsz.c
new file mode 100644
index 000000000..fd9d07f22
--- /dev/null
+++ b/tools/adk/dkgetsz.c
@@ -0,0 +1,95 @@
+/*-
+ * Copyright © 2010
+ * Waldemar Brodkorb <wbx@openadk.org>
+ * Thorsten Glaser <tg@mirbsd.org>
+ *
+ * Provided that these terms and disclaimer and all copyright notices
+ * are retained or reproduced in an accompanying document, permission
+ * is granted to deal in this work without restriction, including un‐
+ * limited rights to use, publicly perform, distribute, sell, modify,
+ * merge, give away, or sublicence.
+ *
+ * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
+ * the utmost extent permitted by applicable law, neither express nor
+ * implied; without malicious intent or gross negligence. In no event
+ * may a licensor, author or contributor be held liable for indirect,
+ * direct, other damage, loss, or other issues arising in any way out
+ * of dealing in the work, even if advised of the possibility of such
+ * damage or existence of a defect, except proven that it results out
+ * of said person’s immediate fault when using the work as intended.
+ *
+ * Alternatively, this work may be distributed under the terms of the
+ * General Public License, any version, as published by the Free Soft-
+ * ware Foundation.
+ *-
+ * Display the size of a block device (e.g. USB stick, CF/SF/MMC card
+ * or hard disc) in 512-byte sectors.
+ */
+
+#define _FILE_OFFSET_BITS 64
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+
+#if defined(__APPLE__)
+#include <sys/disk.h>
+#endif
+
+#if defined(DIOCGDINFO)
+#include <sys/disklabel.h>
+#endif
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+unsigned long long numsecs(int);
+
+int
+main(int argc, char *argv[]) {
+ int fd;
+
+ if (argc != 2)
+ errx(255, "Syntax: dkgetsz /dev/sda");
+
+ if ((fd = open(argv[1], O_RDONLY)) == -1)
+ err(1, "open");
+ printf("%llu\n", numsecs(fd));
+ close(fd);
+ return (0);
+}
+
+unsigned long long
+numsecs(int fd)
+{
+#if defined(BLKGETSIZE) || defined(DKIOCGETBLOCKCOUNT)
+/*
+ * note: BLKGETSIZE64 returns bytes, not sectors, but the return
+ * type is size_t which is 32 bits on an ILP32 platform, so it
+ * fails interestingly here… thus we use BLKGETSIZE instead.
+ */
+#if defined(DKIOCGETBLOCKCOUNT)
+ uint64_t nsecs;
+#define THEIOCTL DKIOCGETBLOCKCOUNT
+#define STRIOCTL "DKIOCGETBLOCKCOUNT"
+#else
+ unsigned long nsecs;
+#define THEIOCTL BLKGETSIZE
+#define STRIOCTL "BLKGETSIZE"
+#endif
+ if (ioctl(fd, THEIOCTL, &nsecs) == -1)
+ err(1, "ioctl %s", STRIOCTL);
+ return ((unsigned long long)nsecs);
+#elif defined(DIOCGDINFO)
+ struct disklabel dl;
+
+ if (ioctl(fd, DIOCGDINFO, &dl) == -1)
+ err(1, "ioctl DIOCGDINFO");
+ return ((unsigned long long)dl.d_secperunit);
+#else
+#error PLEASE DO IMPLEMENT numsecs FOR THIS PLATFORM.
+#endif
+}