Skip to content

Instantly share code, notes, and snippets.

@kernigh
Created June 8, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernigh/09ced7c9018272812d2ebf7c5b0b68da to your computer and use it in GitHub Desktop.
Save kernigh/09ced7c9018272812d2ebf7c5b0b68da to your computer and use it in GitHub Desktop.
size of OpenBSD disk partition
#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