468,457 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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

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

Sep 3 '06 #1
10 4792
jon wrote:
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
Sep 3 '06 #2
jon

jacob navia wrote:
jon wrote:
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

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

Sep 4 '06 #3
Fork?

"jon" <jm******@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
jacob navia wrote:
>jon wrote:
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


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

Sep 4 '06 #4
On 3 Sep 2006 17:34:49 -0700, in comp.lang.c , "jon"
<jm******@hotmail.comwrote:
>
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
Sep 4 '06 #5
jon wrote:
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.
Sep 4 '06 #6
In article <44**********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:
>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.)
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Sep 5 '06 #7
Walter Roberson wrote:
In article <44**********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:
>>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.
Sep 5 '06 #8
Walter Roberson wrote:

<snipped>
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.)
<OTThey 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>

--
goose
Have I offended you? Send flames to root@localhost
real email: lelanthran at gmail dot com
website : www.lelanthran.com
Sep 7 '06 #9
In article <ed**********@ctb-nnrp2.saix.net>,
goose <lk************@webmail.co.zawrote:
>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.)
><OTThey 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.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Sep 7 '06 #10
>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.)
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.
><OTThey 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.

Sep 7 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Patrick Useldinger | last post: by
18 posts views Thread by jas | last post: by
18 posts views Thread by jacob navia | last post: by
5 posts views Thread by news.microsoft.com | last post: by
2 posts views Thread by 1388-2/HB | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.