Thread: Making a program copy itself

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Making a program copy itself

    Is it possible to make a program copy itself somewhere in c? What I meen is say if I compile my program to example.exe, when it runs I want it to copy itself somewhere else say like c:\bob.exe or somthing. Don't know if my question made sence but thanks.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    and why would you want to do that exactly?

  3. #3
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Why not? Its just a (binary) file. Some Operating Systems might not allow it under certain conditions though.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by fischerandom
    Why not? Its just a (binary) file. Some Operating Systems might not allow it under certain conditions though.
    When someone starts asking questions about code which sounds suspicious (i.e, self replicating executables, etc) he is not likely to get much help. Further, it is against the board rules to post on 'illegal actvities,' and, therefore, a question or two regarding the nature of his business with self replicating code is definitely not out of line.
    Last edited by kermit; 05-28-2006 at 02:56 PM.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by fischerandom
    Why not? Its just a (binary) file. Some Operating Systems might not allow it under certain conditions though.
    Go Away.
    Woop?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You'll need to learn binary code for your platform type (eg Win32 + x86) and write a quine in it. That's all really
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Um...yeah that's simple. I have no idea why you would want to do this, but hey go for it.

    A couple of simple C block (array-based) I/O functions will do the trick.

    Since I do not see the harm there is in creating a copy of an exe I do not deem this code to be unsafe.

    And since you obviously cannot perform this simple task I do not see you as a threat or as someone who could inject assembly opcodes into the correct places in the final exe in a way as to do any damage at all.

    Code:
    ...
    int source_handle=_open(source_filename,_O_BINARY | _O_RDONLY,_S_IREAD);
    if (source_handle==-1) return;
    
    int target_handle=_open(target_filename,_O_BINARY | _O_WRONLY,_S_IWRITE);
    if (target_handle==-1) return;
    
    DWORD dwLength=filelength(source_handle);
    
    BYTE *pBuffer=new BYTE[dwLength];
    if (!pBuffer) return;
    memset(pBuffer,dwLength,0);
    
    _read(source_handle,(BYTE *)pBuffer,dwLength);
    _write(target_handle,(BYTE *)pBuffer,dwLength);
    
    _close(source_handle);
    _close(target_handle);
    
    delete [] pBuffer;
    
    ...
    Oh and if you don't like the use of new on the C board then replace it with malloc. I could care less really.

    This is a far cry (no pun intended) from self-modifying code as is. Should you want to do that then I won't help you blow up your system anymore.
    Last edited by VirtualAce; 05-29-2006 at 03:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help making a program keep going.
    By ashinms in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2009, 10:12 AM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  3. Replies: 2
    Last Post: 01-21-2008, 02:07 AM
  4. Making a program take a variable number of arguments
    By subtled in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 05:38 AM
  5. C program to copy log file frm database
    By nirmala.s in forum C Programming
    Replies: 15
    Last Post: 11-27-2006, 11:05 AM