// $Id: rellif.c 3777 2010-11-27 13:53:57Z flaterco $
// Validate results of filler.c.
// gcc -Wall -Wextra -O2 -static -s -o rellif rellif.c

#define __STDC_FORMAT_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

void usage () {
  fprintf (stderr, "Usage: rellif theChar\n");
  fprintf (stderr,"   Each byte of standard input is compared with theChar\n");
  fprintf (stderr,"   theChar should be hexadecimal (e.g., 0, 1, F0, 0F)\n");
  exit (-1);
}

int main (int argc, char **argv) {
  uintmax_t bcount = 0;
  uint8_t theChar = 0;

  if (argc != 2)
    usage ();

  char const fmt[] = "%" SCNx8;
  if (sscanf (argv[1], fmt, &theChar) != 1)
    usage ();

  int charIn;
  while ((charIn = getchar()) != EOF) {
    if ((uint8_t)charIn != theChar)
      printf ("Expected %02X got %02X at byte %" PRIuMAX " in 512B-block %" PRIuMAX " or 4k-block %" PRIuMAX "\n",
	      theChar, charIn, bcount, bcount/512, bcount/4096);
    ++bcount;
  }

  fprintf (stderr, "%" PRIuMAX " bytes compared\n", bcount);
  return 0;
}
