一个具自我复制功能的程序

来源:互联网 发布:阿里云电视软件下载 编辑:程序博客网 时间:2024/05/01 07:38

/*******************************************************************
 *
 *此程序是一具自我复制程序,它将以自己为模板,复制许多不同名字的程序,
 *这此复制的程序同样都有自我复制的能力类似于病毒,但有不是病毒,因为
 *没有破坏性,程序中也没有具有破坏性的代码。
 *
 *程序在turboc2.0中编绎通过
 *
 *编程:郑绍辉   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){
/*根据数据idx生成一个文件名前缀,仅4个字节,  *
 * 用它作为一个可执行文件的文件名的前4个字符,*
 * 并保存于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){
/*产生一个可执行文件的全名,这个文件名由前缀*
 *prefix和一个数字idx组成,并保存于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){
/*把buffer中内容放入文件名为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){
/*把文件名为filename的文件中的内容放在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)){/*当文件未结束时*/
 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 many 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){/*产生10000个文件后就退出*/
        getFullName(prefixname,count,filename);/*获得一个文件名作为将要产生的文件的名称*/
 if (writeFile(filename,buffer,fsize) == ERROR)/*如果写文件错误*/
            printf("/n/rCan't create file %s.",filename);
        cprintf("%s, ",filename);
    }


}