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

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

#define FATSz16_pos 22

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) ;
  }
  memcpy((void *)&FATSz16, (void *)&BPBsector[FATSz16_pos], sizeof(FATSz16)) ;
  printf("FAT16 size is %hu\n", FATSz16) ;
  return 0;
}
