Created
June 8, 2018 17:22
-
-
Save kernigh/09ced7c9018272812d2ebf7c5b0b68da to your computer and use it in GitHub Desktop.
size of OpenBSD disk partition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/disklabel.h> | |
#include <sys/dkio.h> /* DIOCGDINFO */ | |
#include <sys/ioctl.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
/* | |
* Shows the size in bytes of a disk partition in OpenBSD. | |
*/ | |
int main(int argc, char **argv) { | |
struct disklabel label; | |
struct partition *part; | |
struct stat st; | |
unsigned long long size; | |
int fd; | |
char *path; | |
if (argc != 2) { | |
fprintf(stderr, "usage: %s special\n", argv[0]); | |
return 1; | |
} | |
path = argv[1]; | |
fd = open(path, O_RDONLY); | |
if (fd < 0 || ioctl(fd, DIOCGDINFO, &label) < 0 || | |
fstat(fd, &st) < 0) { | |
perror(path); | |
return 1; | |
} | |
part = &label.d_partitions[DISKPART(st.st_rdev)]; | |
size = label.d_secsize * DL_GETPSIZE(part); | |
printf("%s: %llu bytes\n", path, size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment