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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ravenplAuthor Commented:
Apart from opening it and doing lseek() to the end of device ?
(l)stat is for files and dirs, I guess you need to use somethng like sd()
Try stat()ing the file  (

#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;
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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 ?
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
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.
Duncan RoeSoftware DeveloperCommented:
"strace fdisk /dev/hda" shows the same ioctls. "man ioctl_list" identifies the structures associated with each

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ravenplAuthor Commented:
The asisted should be accepted and vice versa ;)
glad to have helped, even a bit unspecific
Future readers will see how to exchange "acepted" and "assisted"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.