#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

// Intel is little-endian
#define FATsize_F16(bpb) ((((unsigned int)bpb[23])<<8) + (unsigned int)bpb[22])

char usage_format[] = "Usage: %s fatfs-file\n" ;

int main(int argc, char *argv[]) {
  unsigned char BPBsector[512] ;
  unsigned short FATSz16 ;
  if (argc<2) {
    fprintf(stderr, "Usage: %s fatfs-file\n", argv[0]) ;
    exit(1) ;
  }
  FILE *fatFile = fopen(argv[1], "r") ;
  if (fatFile == NULL) {
    fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno)) ;
    exit(1) ;
  }
  if (fread((void *)BPBsector, 512, 1L, fatFile) != 1) {
    fprintf(stderr, "Unable to read sector 0 of %s\n", argv[1]) ;
    exit(1) ;
  }
  printf("FAT16 size is %hu\n", FATsize_F16(BPBsector)) ;
  return 0;
}
