A.1. Antivírusový skener / Antivirus scanner
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <elf.h>
#include <fcntl.h>
#include <pwd.h>
#define MAGIC 6585
#define MAX_BUF 1024
static int magic = MAGIC;
int detect(char *filename, int hd)
{
int fd;
struct stat stat;
char *data;
char tmpfile[MAX_BUF];
char cmd[MAX_BUF]="\0";
int tmagic;
int magicloc;
Elf32_Ehdr ehdr;
if(fstat(hd, &stat) < 0) return 1;
magicloc = stat.st_size - sizeof(magic);
if( lseek(hd, magicloc, SEEK_SET) != magicloc ) return 1;
if(read(hd, &tmagic, sizeof(magic)) != sizeof(magic)) return 1;
if(tmagic == MAGIC) return 2;
if(lseek(hd, 0, SEEK_SET) != 0) exit(1);
return 0;
}
void scan_dir(char *directory)
{
int hd;
int r;
DIR *dd;
struct dirent *dirp;
char vfile[256];
dd = opendir(directory);
if(dirp != NULL) {
while (dirp = readdir(dd))
{
r=0;
strncpy(vfile, directory, 255);
strcat(vfile, "/");
strncat(vfile, dirp->d_name, 255-strlen(vfile));
hd=open(vfile, O_RDONLY, 0);
if(hd >= 0) {
r=detect(vfile, hd);
if (r==2)
printf("Subor infikovany: %s\n",vfile);
}
close(hd);
}
closedir(dd);
}
}
int main(int argc, char *argv[], char *envp[])
{
if (argc < 2) {
printf("Pouzitie %s adresar\n",argv[0]);
exit(1);
}
printf("Prehladavam adresar %s\n",argv[1]);
scan_dir(argv[1]);
return 0;
}