how to write C program that copy's it's self onto the hard drive, while its running?

J

jon

I am a beginner C programmer, this is actually my first programming
lanugage, besides html, cgi, and javascript. I am looking for a way to
make an .exe file, or a copy of my own file. I have tried writing a
file, compling it, and reading it in a notepad, then writing a program
to write it again, but i have had no luck. I assure you i'm not trying
to create the next big virus, or worm, but only trying to expaned my
knowledge on what programming language i will take. C is my perferred
taste, though i may go into java if this dosen't work out.

aspiring programmer
Jon
15
 
J

jacob navia

jon said:
I am a beginner C programmer, this is actually my first programming
lanugage, besides html, cgi, and javascript. I am looking for a way to
make an .exe file, or a copy of my own file. I have tried writing a
file, compling it, and reading it in a notepad, then writing a program
to write it again, but i have had no luck. I assure you i'm not trying
to create the next big virus, or worm, but only trying to expaned my
knowledge on what programming language i will take. C is my perferred
taste, though i may go into java if this dosen't work out.

aspiring programmer
Jon
15

Mmmm a program that copies itself???

If that isn't a virus... please tell me what you want to achieve.

You know that

copy myexe.exe my-NEW-exe.exe

will work at the command line prompt.

This is quite easy. But no, you want it to copy
it when it is running... Why?

In any case it will be difficult, since when your program
is running, it is locked and fopen will fail... as you
have seen.

Try Java. I am sure there you will find an
easy way to copy a running JAVA program isn't it?

:)

jacob
 
J

jon

jacob said:
Mmmm a program that copies itself???

If that isn't a virus... please tell me what you want to achieve.

You know that

copy myexe.exe my-NEW-exe.exe

will work at the command line prompt.

This is quite easy. But no, you want it to copy
it when it is running... Why?

In any case it will be difficult, since when your program
is running, it is locked and fopen will fail... as you
have seen.

Try Java. I am sure there you will find an
easy way to copy a running JAVA program isn't it?

:)

jacob


Well, The program writes webpages, and when user wants to start another
one, the new copy will be made.
 
M

Mark McIntyre

Well, The program writes webpages, and when user wants to start another
one, the new copy will be made.

Er, you don't want to make a new copy of the programme, you want to
run a new instance of it in memory. That is platform specific, search
your platform documentation or ask in a relevant group.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jmcgill

jon said:
Well, The program writes webpages, and when user wants to start another
one, the new copy will be made.


It's hard to understand what you are trying to do, but it sounds like
you are trying to go straight into a multi-threaded programming model
without learning the basics of the language first.

I would say the Java thread model is *much* easier to work with than any
threads library you will find for C.

If you really want help from others you will need to do a much better
job of explaining what it is you are trying to do.
 
W

Walter Roberson

This is quite easy. But no, you want it to copy
it when it is running... Why?
In any case it will be difficult, since when your program
is running, it is locked and fopen will fail... as you
have seen.

Not necessarily true, Jacob. The OP did not specify a platform,
and in some platforms it is possible to open the running binary:


$ cc -o rr rr.c
$ ./rr
../rr had no problem openning itself for ab+
$ cat rr.c
#include <stdio.h>
int main(int argc, char **argv) {
FILE *me = fopen(argv[0],"ab+");
if (!me) {
printf( "%s was not able to open itself for ab+\n", argv[0] );
} else {
printf( "%s had no problem openning itself for ab+\n", argv[0] );
}
return 0;
}
$ uname -a
IRIX64 origin 6.5 10151453 IP27


Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
i.e., the official Unix specification) language about this situation,
dealing with which version of a swapped-out page that is to be read in
if the binary is rewritten underneath it (oddly, the requirement
is that it is the -new- version of the page that must be swapped in.)
 
J

jacob navia

Walter said:
jacob navia said:
This is quite easy. But no, you want it to copy
it when it is running... Why?

In any case it will be difficult, since when your program
is running, it is locked and fopen will fail... as you
have seen.


Not necessarily true, Jacob. The OP did not specify a platform,
and in some platforms it is possible to open the running binary:


$ cc -o rr rr.c
$ ./rr
./rr had no problem openning itself for ab+
$ cat rr.c
#include <stdio.h>
int main(int argc, char **argv) {
FILE *me = fopen(argv[0],"ab+");
if (!me) {
printf( "%s was not able to open itself for ab+\n", argv[0] );
} else {
printf( "%s had no problem openning itself for ab+\n", argv[0] );
}
return 0;
}
$ uname -a
IRIX64 origin 6.5 10151453 IP27


Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
i.e., the official Unix specification) language about this situation,
dealing with which version of a swapped-out page that is to be read in
if the binary is rewritten underneath it (oddly, the requirement
is that it is the -new- version of the page that must be swapped in.)

Gosh!

You can write self modifying code in there!

That's news for me.

Actually there could be a lot of applications, like
instead of saving the settings in an ASCII file,
then reading them back in the next time the program is run,
you can directly write the settings structure to the executable
and it will be there next time automagically!!!

Thanks for this clarification, I stand corrected.
 
G

goose

Walter Roberson wrote:

Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
i.e., the official Unix specification) language about this situation,
dealing with which version of a swapped-out page that is to be read in
if the binary is rewritten underneath it (oddly, the requirement
is that it is the -new- version of the page that must be swapped in.)

<OT> They don't actually work like that. Removing a link to a
file (filename) still leaves the data happily on disk. Once
the last process (that has an open handle on the file)
closes the file *then* the data space is marked as free.
</OT>
 
W

Walter Roberson

Walter Roberson wrote:
<OT> They don't actually work like that. Removing a link to a
file (filename) still leaves the data happily on disk. Once
the last process (that has an open handle on the file)
closes the file *then* the data space is marked as free.
</OT>

That's a different situation. I'm referring to the situation in which
the contents of the executable file are *updated* while the executable is
running. For example, if you dd a few blocks of /dev/random into the file,
or if you do a binary patch.

Updating in place is sometimes a temptation because it takes less work
to set up the ownership and permissions (and access-control lists if
applicable), especially if the binary is writable by some entity
but that entity doesn't have update permissions on the directory that
contains the binary.
 
G

Gordon Burditt

Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
You're talking about different situations. The above assumes
you opened a running executable for write (some systems prohibit this)
and write new data on it (e.g. apply patch or copy whole new executable
over the old one.
<OT> They don't actually work like that. Removing a link to a
file (filename) still leaves the data happily on disk. Once
the last process (that has an open handle on the file)
closes the file *then* the data space is marked as free.
</OT>

This situation assumes you *delete* the file (under one of its
names, at least), then re-create the file under the same name. This
is a very different situation.

Some systems consider *any* writing on executable code to be symptomatic
of a virus and don't allow it. That would include all compilers and
linkers. Some systems require executable code to be "blessed" before
it's usable as executable code (a weak example here is the "executable"
bit in UNIX. A stronger example would be that executables that aren't
already signed by the vendor can only be created by a compiler signed
by the vendor. In these situations, the answer could be that you
can't portably copy an executable and still use it as an executable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top