How to obtain block device size (c/c++)

for eg. with (l)stat i discovered, that /dev/sda1 is a valid block device. What it's capacity?
C/C++ example please.
LVL 43
ravenplAsked:
Who is Participating?
 
Duncan RoeSoftware DeveloperCommented:
"strace fdisk /dev/hda" shows the same ioctls. "man ioctl_list" identifies the structures associated with each
0
 
ravenplAuthor Commented:
Apart from opening it and doing lseek() to the end of device ?
0
 
ahoffmannCommented:
(l)stat is for files and dirs, I guess you need to use somethng like sd()
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Dragon_KromeCommented:
Try stat()ing the file  (http://www.die.net/doc/linux/man/man2/stat.2.html):


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
struct stat st;

stat("/dev/sda1", &st);

printf("\nSize in bytes: %lu", st.st_size);

return 0;
}
0
 
ravenplAuthor Commented:
Dragon_Krome: if You read the Q carefully, You would notice that I already tried stat(). Have You compiled and run Your programm ?
0
 
Duncan RoeSoftware DeveloperCommented:
"man statfs" tells you how to find usage of a *partition*. If you really want to know about a block device, best would be to look at the source of fdisk. "strace fdisk /dev/sda" showed me ioctls BLKSSZGET, HDIO_GETGEO, BLKGETSIZE, & BLKGETSIZE64 on the block device, which is also read from (for the partition table, I imagine
0
 
ravenplAuthor Commented:
statfs is useless - we are talking about devices, not filesystems.
those ioctls are mentioned in ahoffman's 'man sd', but I haven't time o check it yet. But I suppose that's what I need.
0
 
ravenplAuthor Commented:
The asisted should be accepted and vice versa ;)
0
 
ahoffmannCommented:
glad to have helped, even a bit unspecific
Future readers will see how to exchange "acepted" and "assisted"
;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.