Created
September 4, 2014 20:07
-
-
Save itiut/a3bc25be104cf69e8be9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <fcntl.h> | |
#include <linux/fs.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/ioctl.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
printf("Usage: %s device\n", argv[0]); | |
return 0; | |
} | |
const char *device = argv[1]; | |
int fd = open(device, O_RDONLY); | |
if (fd == -1) { | |
perror("open(2)"); | |
exit(EXIT_FAILURE); | |
} | |
unsigned long long block_size = 0; | |
if (ioctl(fd, BLKSSZGET, &block_size) == -1) { | |
perror("ioctl(2)"); | |
exit(EXIT_FAILURE); | |
} | |
unsigned long long n_of_blocks = 0; | |
if (ioctl(fd, BLKGETSIZE, &n_of_blocks) == -1) { | |
perror("ioctl(2)"); | |
exit(EXIT_FAILURE); | |
} | |
close(fd); | |
printf("device name: %s\n", device); | |
printf("block size: %lld bytes\n", block_size); | |
printf("# of blocks: %lld\n", n_of_blocks); | |
printf("volume: %lld GiB\n", block_size * n_of_blocks / (1 << 30)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment