/*Gifdir.c - Frank Imburgio CIS ID 76666,317
  displays simple info from gif header on colors and size
  written for Turbo C 2.0, but should work ok with MicroSoft 5.1*/
#include <stdio.h>
#include <dir.h>
#include <dos.h>
#include <string.h>

unsigned int getbytes(FILE *dev);

main(int argc, char *argv[])
{
struct   ffblk ffblk;
char     gifspec[10];
char     pathspec[80];
char     drive[MAXDRIVE],dir[MAXDIR],name[MAXFILE],ext[MAXEXT];
unsigned int junk,width,height,byte1,bits_per_pix,colors;
FILE     *f;

/* check for input file or use *.gif as default */
if (argc>1) {
   strcpy(pathspec,argv[1]);
   /* if user sent filename without extension, paste .gif on */
   if (! strchr(pathspec,'.')) {
      strcat(pathspec,".GIF");
      /* check to if the gif file is there - if not, revert to original */
      if (findfirst(pathspec,&ffblk,0)==-1)
	 strcpy(pathspec,argv[1]);
   }
   /* check to see if this is a path without the final backslash */
   if (pathspec[strlen(pathspec)]!='\\' && findfirst(pathspec,&ffblk,0)==-1)
      strcat(pathspec,"\\");
   /* if not a valid filename now, paste *.gif on.... */
   if (findfirst(pathspec,&ffblk,0)==-1)
      strcat(pathspec,"*.GIF");
}
else
   strcpy(pathspec,"*.GIF");
if (findfirst(pathspec,&ffblk,0)==-1) {
   printf("No GIF files in filespec %s!\n",pathspec);
   exit(99);
   }
/* split and save components of filespec for later */
fnsplit(pathspec,drive,dir,name,ext);
while (1) {
   /* build full pathname back from findnext function */
   fnmerge(pathspec,drive,dir,ffblk.ff_name,0);


   /* print filename */
   printf("%13s\t",ffblk.ff_name);

   /* open file */
   f = fopen(pathspec,"r");
   if (! f) {
      printf("* could not open file *\n");
      fclose(f);
      if (findnext(&ffblk))
	  break;
      continue;
   }
   /* read gif header byte*/
   fscanf(f,"%6s",gifspec);
   if (stricmp(gifspec,"gif87a")) {
      printf("*invalid GIF header format - not gif87a*\n");
      fclose(f);
      if (findnext(&ffblk))
	  break;
      continue;
   }

   /* determine the image width */
   width = getbytes(f);

   /* determine the image height */
   height = getbytes(f);

   /* determine the number of colors */
   byte1 = getc(f);
   bits_per_pix = byte1 & 0x07;
   bits_per_pix++;
   colors = 1 << bits_per_pix;

   printf ("%-3d X %-3d X %-4d\n",width,height,colors);
   fclose(f);
   if (findnext(&ffblk))
       break;
   }
}

unsigned int getbytes(FILE *dev)
{
    int byte1;
    int byte2;
    int result;

    byte1 = getc(dev);
    byte2 = getc(dev);

    result = (byte2 << 8) | byte1;

    return result;
}
