/*
 * $Id: log.c,v 1.1 2000/09/08 16:13:04 mhill Exp mhill $
 */

/************************************************************************** 

LOG - Generate Hexdumpt to a text file
Copyright (C) 2000  Mike Hill

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

**************************************************************************/

#include<stdio.h>

FILE *src, *dst;

// Usage is 'log srcfile dstfile count offset'

// srcfile is the file to read from (or device)
// dstfile is the destination file to write the hexdump ascii file to
// count   is number of blocks of 16 bytes to read
// offset  is the number of bytes to seek forward in the file before logging

int main (int argc, char *argv[])
{
  int x, y, count;
  int c, offset;
  unsigned char buff[16];

  if (argc != 5)
    return;

  src = fopen (argv[1], "rb");
  dst = fopen (argv[2], "wb");
  count = atoi (argv[3]);
  offset = atoi (argv[4]);

  if (src == NULL) {
    fprintf (dst, "couldnt open %s\n");
    fclose (dst);
    return;
  }

  fseek (src, offset, 0);

  for (y = 0; y < count; y++) {
    fprintf (dst, "%08x  ", y * 0x10);

    fread (buff, 16, 1, src);

    for (x = 0; x < 16; x++) {
      fprintf (dst, "%02x ", buff[x]);
    }
    fprintf (dst, "  ");

    for (x = 0; x < 16; x++) {
      if (isprint (buff[x]))
	fprintf (dst, "%c", buff[x]);
      else
	fprintf (dst, ".");
    }

    fprintf (dst, "\n");
  }

  fclose (src);
  fclose (dst);
}
