/** GameBoy Cartridge Lister *********************************/
/**                                                         **/
/**                        listall.c                        **/
/**                                                         **/
/** This program written by Pascal Felber will list all of  **/
/** your cartridges making a list of internal names         **/
/** contained in them.                                      **/
/**                                                         **/
/** Copyright (C) Pascal Felber 1996                        **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/   
/**     changes to this file.                               **/
/*************************************************************/

#include <stdio.h>
#include <sys/stat.h>

#define NAMEFIELD 20

char HDR[0x150];

void main(int argc,char *argv[])
{
  FILE *s;
  struct stat st;
  int arg, i;

  if(argc < 2) {
    fprintf(stderr, "Usage: %s files...\n", argv[0]);
    exit(1);
  }

  printf("+---------------------+---------+---------\n");
  printf("| FileName            | Size    | CartName\n");
  printf("+---------------------+---------+---------\n");
  for(arg = 1; arg < argc; arg++) {
    if(!(s = fopen(argv[arg], "rb"))) {
      perror("fopen");
      exit(1);
    }
    if(stat(argv[arg], &st)) {
      perror("fstat");
      exit(1);
    }
    if(fread(HDR, 1, 0x150, s) != 0x150) {
      perror("fread");
      exit(1);
    }
    HDR[0x146] = '\0';
    printf("| %s", &HDR[0x134]);
    for(i = strlen(&HDR[0x134]); i < NAMEFIELD; i++)
      putchar(' ');
    printf("| %7d | ", st.st_size);
    printf("%s", argv[arg]);
    putchar('\n');
    fclose(s);
  }
  printf("+---------------------+---------+---------\n");
}
