48 lines
1.2 KiB
C
48 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
#include <dirent.h>
|
|
#include <string.h>
|
|
|
|
|
|
const char fileext[] = "flag";
|
|
const char magic[] = "flag";
|
|
#define MAGICLEN 4
|
|
|
|
const int maxlen = 256;
|
|
|
|
|
|
int main (void)
|
|
{
|
|
DIR *dirp;
|
|
struct dirent *entryp;
|
|
dirp = opendir("./");
|
|
|
|
if(dirp != NULL){
|
|
while(entryp = readdir(dirp)){
|
|
char *filename = entryp->d_name;
|
|
int fextpos = strnlen(filename, maxlen) - strlen(fileext);
|
|
if(0 == strncmp(fileext, &filename[fextpos], maxlen)){
|
|
printf("flagfile: ");
|
|
FILE *file = fopen(filename, "r");
|
|
if(file != NULL){
|
|
char sample[MAGICLEN];
|
|
fread(&sample , MAGICLEN, 1, file);
|
|
fclose(file);
|
|
if(0 == strncmp(magic, sample, MAGICLEN)){
|
|
printf("[valid] ");
|
|
}
|
|
}else{
|
|
perror("Couldn't open file. ");
|
|
}
|
|
}
|
|
printf("%s\t\n", filename);
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
}else{
|
|
perror("Couldn't open the directory");
|
|
}
|
|
|
|
return 0;
|
|
}
|