Skip to content

Instantly share code, notes, and snippets.

@cbscribe
Last active October 28, 2020 17:19
Show Gist options
  • Save cbscribe/ea3ae2991880a348722cd92480d5ffcb to your computer and use it in GitHub Desktop.
Save cbscribe/ea3ae2991880a348722cd92480d5ffcb to your computer and use it in GitHub Desktop.
#include <stdio.h>
// Every GIF must have this first 6 bytes
char *gif_header = "GIF89a";
int main(int argc, char *argv[])
{
// check for correct number of arguments
if (argc != 2)
{
printf("Usage: ./is_gif filename\n");
return 1;
}
// Attempt to open the file
FILE *f = fopen(argv[1], "r");
// If the file can't be opened, quit
if (!f)
{
printf("Can't open file.\n");
return 1;
}
// Read the first 6 bytes from file
char buffer[6];
// Gets 6 chunks of 1 byte each from file
int bytes = fread(buffer, 1, 6, f);
// If there weren't 6 bytes, file is too small
if (bytes != 6)
{
printf("File is too small\n");
return 1;
}
// Compare each byte with the gif header
for (int i = 1; i < 6; i++)
{
if (buffer[i] != gif_header[i])
{
printf("Not a GIF file.\n");
return 0;
}
}
printf("Is a GIF file.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment