A self-replication program

Source: Internet
Author: User

/*************************************** ****************************
*
* This program is a self-replication program. It will copy many programs with different names using its own template,
* This replication program also has the ability to replicate itself like a virus, but it is not a virus, because
* No Destructive code exists in the program.
*
* The program is compiled and approved in turboc2.0
*
* Programming: Zheng Shaohui 2004.3.1
*
**************************************** ****************************/
# Include <stdio. h>
# Include <conio. h>
# Include <string. h>

# Define OK 1
# Define error 0
Typedef int bool;

Void getprefixname (INT idx, char * Name ){
/* Generate a file name prefix Based on Data idx, which is only 4 bytes long ,*
* Use it as the first four characters of the executable file name ,*
* And save it in name */
Int temp;
Int CNT = 0;
While (CNT <4 ){
Name [CNT ++] = idx % 26 + 97;
Idx/= 26;
}/* While */
Name [4] = '/0 ';
}/* Getname */

Void getfullname (char * prefix, int idx, char * fullname ){
/* Generate the full name of an executable file, which is prefixed *
* Prefix is composed of a number idx and saved in fullname */
Int CNT = 4, temp;
Strcpy (fullname, prefix );
While (idx> 0 ){
Fullname [CNT ++] = idx % 10 + 48;
Idx/= 10;
}
Fullname [CNT] = '/0 ';
Strcat (fullname, ". EXE ");
}/* Getfullname */

Bool writefile (char * filename, unsigned char * buffer, Long SIZE ){
/* Put the buffer content into the file named filename */
File * FP;
Long Count = 0;
Fp = fopen (filename, "WB ");
If (FP = NULL) return error;
While (count <size)
Fputc (buffer [count ++], FP );
Fclose (FP );
Return OK;
}

Bool readfile (char * filename, unsigned char * buffer, long * size ){
/* Put the content in the file named filename in the buffer */
File * FP;
Long Count = 0;
Fp = fopen (filename, "rb ");
If (FP = NULL) return error;
Cprintf ("Wait a moment please, this program is reading soure file ...");
While (! Feof (FP) {/* When the file is not finished */
Buffer [count ++] = fgetc (FP );
}/* While */
* Size = count;
Fclose (FP );
Return OK;
}/* Readfile */

Void showinfo (void ){
Textbackground (lightblue );
Clrscr ();
Window (15,1, 65,7 );
Textbackground (Cyan );
Clrscr ();
Window (17,2, 63,6 );
Textbackground (black );
Textcolor (red );
Clrscr ();
Cprintf ("<breeding>/n/R Author: fancyboy fancyboy1983@tom.com.cn ");
Textcolor (lightblue );
Cprintf ("/n/r this file will breed program files, these breeded file have the same function .");
Cprintf ("/n/r the following are the breeding file list :");
Window (5, 9, 75, 24 );
Textbackground (brown );
Textcolor (yellow );
Clrscr ();
}/* Showinfo */

Void main (INT argc, char * argv []) {

Char prefixname [5];
Char filename [50];
Char buffer [30000];
Int COUNT = 0, prefix;
Long fsize;
File * FP;

(Void) argc;
Showinfo ();
Srand (Time (null ));
Prefix = rand ();
Getprefixname (prefix, prefixname );
If (readfile (argv [0], buffer, & fsize) = Error ){
Cprintf ("Sorry, can't read file % S.", argv [0]);
Exit (0 );
}

While (count ++ <10000) {/* exit after 10000 files are generated */
Getfullname (prefixname, Count, filename);/* obtain a file name as the name of the file to be generated */
If (writefile (filename, buffer, fsize) = Error)/* if a file write error occurs */
Printf ("/n/rcan't Create File % S.", filename );
Cprintf ("% s,", filename );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.