program copying itself (ideas)

This is a discussion on program copying itself (ideas) within the C Programming forums, part of the General Programming Boards category; Just a thought........for a program to copy itself (i saw this post earlier and I just want to see if ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    56

    program copying itself (ideas)

    Just a thought........for a program to copy itself (i saw this post earlier and I just want to see if my brain is capable of thinking of this).


    To do so (inefficiently), could an array within the program hold the exact characters of the compiled exe. And then open an .exe file in binary mode and simply write the new characters to that file...and it would be able to run?

    I thought of this because I realized that in my downloader program...all it does when it downloads an exe is read in one character at a time and then write it to the same extension.

    Any thoughts? This is for ideal purposes...I would have already tried writing this if I wanted to misuse it.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    of course that works.
    wheter you copy an exe from file to file or from memory to file doesn't matter - its just data.
    signature under construction

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Both of you need to put your logic hats on...

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well, it would grow to infinate size trying to store the program in itself... If you have an array that holds what the .exe would contain when compiled... that array would have to have inside of the program the contents of the array... it goes on infinately.

    But you can open the exe in binary read only mode then read it to copy it.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    No no no....like it doesn't read in the compiled code....since it's not intaking new information, it's already there....So i would compile the code already...and it would store it in a previously defined array....or a structure....something simliar of the sort.

    But, I see where the problem is......how would i get that compiled code into the array in the first place? to get the original compiled code of the final program....wouldn't i need to compile the program WITH the already predefined array? (which I can't get in the first place)....

    That's quite a pickle, any way around that?


    And can you open the file you are running for reading?

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Test opening a file that is running... Ask the question of your self, then prove your answer.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Fine fine fine.....I only asked cus i can never copy a file that is in use in any scenario.....I'll try

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Are you opening in read mode or just opening?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    read mode...so it would be able to copy and write a new one.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    The only thing this mentions is that
    The fopen() function may fail if:
    [ETXTBSY]
    [CX] [Option Start] The file is a pure procedure (shared text) file that is being executed and mode requires write access.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    and if not fopen...........
    I'd rather use fread to read the program in one character at a time........but i can't even imagine how that would work if it's reading and writing itself at the same time.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Well, try something like this and see if it works, I guess:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int c;
        FILE *fp;
    
        if(!(fp = fopen(argv[0], "rb"))) {
            perror("Can't open self");
            return 1;
        }
    
        while((c = getc(fp)) != EOF) putchar(c);
    
        fclose(fp);
    
        return 0;
    }
    Code:
    C:\>self > other.exe
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    sure works....thanks a bunch.

    So I guess that makes things much simpler....so correct me if I'm wrong...but in conclusion....

    A copying device would simply write the characters to a new file after reading itself.

    For virus purposes (sorry it came into thought).....it would somehow increment the number in the name so it could make a new file rather than recopy itself....and it's offspring would have the codes to run itself.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I hope you aren't trying to make a virus.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Quote Originally Posted by johnchain
    For virus purposes (sorry it came into thought).....it would somehow increment the number in the name so it could make a new file rather than recopy itself....and it's offspring would have the codes to run itself.
    Sorry, you lose at Cboard, don't try again.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  3. Program Idea's
    By devour89 in forum C++ Programming
    Replies: 11
    Last Post: 12-04-2002, 03:57 AM
  4. Program ideas...
    By code987 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 10:41 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21