一個具自我複製功能的程式

來源:互聯網
上載者:User

/*******************************************************************
 *
 *此程式是一具自我複製程式,它將以自己為模板,複製許多不同名字的程式,
 *這此複製的程式同樣都有自我複製的能力類似於病毒,但有不是病毒,因為
 *沒有破壞性,程式中也沒有具有破壞性的代碼。
 *
 *程式在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);
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.