Skip to content
Advertisement

How do I find the size of mounted USB flash drive in C?

I have a flash drive device (/dev/sda1) mounted to /mnt on an embedded linux system (kernel 2.6.23). Using C how do I work out the size of the drive?

Advertisement

Answer

On Linux, if you’re not worried about portability (C doesn’t know about drives, so any such specific code will be unportable), use statfs():

  struct statfs fsb;

  if(statfs("/mnt", &fsb) == 0)
    printf("device has %ld blocks, each %ld bytesn", fsb.f_blocks, fsb.f_bsize);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement