f



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

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

0
jmshahen (14)
9/3/2006 9:49:51 PM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

10 Replies
564 Views

Similar Articles

[PageSpeed] 58

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
0
jacob (2541)
9/3/2006 10:16:39 PM
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.

0
jmshahen (14)
9/4/2006 12:34:49 AM
Fork?

"jon" <jmshahen@hotmail.com> wrote in message 
news:1157330089.951726.180740@m73g2000cwd.googlegroups.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.
> 


0
9/4/2006 2:11:56 AM
On 3 Sep 2006 17:34:49 -0700, in comp.lang.c , "jon"
<jmshahen@hotmail.com> wrote:

>
>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
0
markmcintyre (4555)
9/4/2006 9:50:51 PM
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.
0
jmcgill (426)
9/4/2006 11:46:42 PM
In article <44fb5446$0$5095$ba4acef3@news.orange.fr>,
jacob navia  <jacob@jacob.remcomp.fr> wrote:
>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
0
roberson2 (8602)
9/5/2006 4:06:53 PM
Walter Roberson wrote:
> In article <44fb5446$0$5095$ba4acef3@news.orange.fr>,
> jacob navia  <jacob@jacob.remcomp.fr> wrote:
> 
>>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.
0
jacob (2541)
9/5/2006 10:22:47 PM
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.)

<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>

-- 
goose
Have I offended you? Send flames to root@localhost
real email: lelanthran at gmail dot com
website   : www.lelanthran.com
0
9/7/2006 9:13:34 PM
In article <edpuf8$4su$2@ctb-nnrp2.saix.net>,
goose  <lkman-spamtrap@webmail.co.za> wrote:
>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>

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
0
roberson2 (8602)
9/7/2006 9:15:41 PM
>> 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.

><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.

0
9/7/2006 11:35:18 PM
Reply:

Similar Artilces:

Trial of Musatov's theory: double spaced 'e x p a n d s' and single spaced 'c o n t r a c t s'. Does the spacing post-processing appear equal? If so, what force was lost and where did it g
Musatov's theory 'The space between spaces expands and contracts.' Would this give time a direction? Musatov ...

It's on-it's off-it's on, it's off -WHY?
IS THIS THE CORRECT FORUM? Hope so. We have three computer networked at home...all with Windows XP. one of those is a laptop. Some mornings all 3 can see each other and transfer files...then mysteriously one or more will disappear. No contact....then later it's back. The laptop is the worst. Most often it won't let us in...but sometimes it will. No obvious reason. The most INFURIATING part is the message "see your administrator". Dammit Bill Gates...don't you realize that most networks are small offices without a Majestic Royal Administrator sitting his i...

More factory sealed FPGA's with C of C's
I have more FPGA's with C of C's that I can supply below facotry direct pricing. Small quantities ok. These have all been purchased thru factrory direct channels and in most cases I can tell you exactly where they came from. We have aquired these from VMI and SMI programs that we have with major OEM's and CM's. Jon E. Hansen Pyramid Technologies Inc. jon@pyramidemail.com www.pyramidtechnologiesinc.com (949)864-7745 Direct (949)636-1416 Cell CERTIFIED ISO 9001:2000 XC5VFX70T-1FF1136C XC5VFX70T-1FFG1136C XC5VLX50-1FF676C XC5VLX330-1FF1760C XC4VLX100-10FFG1148C XC2V3000-4FG...

What's the C++ community's feeling about lambda(or closure) in C++?
I know this topic sounds kind of old and dry(and shallow, maybe :)), but, you see, lately I found that there's all the fuss about closure in Java, so I poked around for a little, and did find some valuable information. And I started to remember that there were a proposal(actually two) about lambda/closure for C++(see http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/n1968.pdf and http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/n1958.pdf) They're all pretty well-formed and sound, though I found Bjarne's a little more comprehensive. It simply seems that the closure id...

How to link obj's from c-source with obj's from c++-source
Hi, I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c. test.c contains just a simple function, which is declared in test.h. test.h is included in test.c and in main.cpp. When I try to link the objects with link main.cpp test.c -out:prog.exe I get the error: "main.obj : error LNK2019: unresolved external Symbol "int __cdecl greater(int,int)" (?greater@@YAHHH@Z), called in function _main prog.exe : fatal error LNK1120: 1 unresolved external Symbol" What have I to do, to be able to link those objects together? Guido Belligoi "Guido Belligoi" ...

Q: C macro's for lvalue statements ? Any C marco Guru's out there ?
Hi everybody, I've got a problem implementing some macros that would make my code much more readable. The idea: I have a set of macros that create variable names depending of the content of some other define. An example (of how it should look like in the end): // --------------- cut #define BASE_NAME module1 // the macro(s) which I need help for :-) #define MAKE_VARNAME2(base, var,val) int var#_#base = val #define MAKE_VARNAME(var, val) MAKE_VARNAME2(BASE_NAME, var, val) // the usage example MAKE_VARNAME(status, 0); // --------------- cut which should expand to int status_module1...

Q: C macro's for lvalue statements ? Any C marco Guru's out there ?
Hi everybody, I've got a problem implementing some macros that would make my code much more readable. The idea: I have a set of macros that create variable names depending of the content of some other define. An example (of how it should look like in the end): // --------------- cut #define BASE_NAME module1 // the macro(s) which I need help for :-) #define MAKE_VARNAME2(base, var,val) int var#_#base = val #define MAKE_VARNAME(var, val) MAKE_VARNAME2(BASE_NAME, var, val) // the usage example MAKE_VARNAME(status, 0); // --------------- cut which should expand to int status_module1...

Any XL C/C++'s option to determine the compiler's predefined macros
Hi, AIX 5.3 $ xlc++ -qversion IBM XL C/C++ Enterprise Edition V8.0 for AIX Version: 08.00.0000.0014 Is there any XL C/C++'s option to determine the compiler's predefined macros? Alex Vinokur In article <2e672e57-f577-4134-9e81-03eb6b3097b7@35g2000pry.googlegroups.com>, Alex Vinokur <alexvn@users.sourceforge.net> writes: > Hi, > > AIX 5.3 > > $ xlc++ -qversion > IBM XL C/C++ Enterprise Edition V8.0 for AIX > Version: 08.00.0000.0014 > > Is there any XL C/C++'s option to determine the compiler's predefined > macros? > > Ale...

Error? s = assignin('base','s',tf('s'));
Hi, I need a little help here. Could anyone please tell me what is wrong with this command? s = assignin('base','s',tf('s')); The matlab syntax is -- assignin(ws, 'var', val) Basically I just want to add s=tf('s') into the base workspace. Jack "Jack" <Jack@Jill.com> wrote in message news:ef3e449.-1@webcrossing.raydaftYaTP... > Hi, > I need a little help here. Could anyone please tell me what is wrong > with this command? > > s = assignin('base','s',tf('s')); > > The matlab syntax is -- as...

Calling 'foo.c' or 'foo2.c' from my 'main_code.c'
Hello. I don't know if following is possible. I've got 'main_code.c': ............................ char * another_code; another_code = "foo.c"; ............................ I've got 'foo.c': ............................ #include <stdio.h> int main() { printf ("Hello world from 'foo.c'!\n"); return 0; } ............................ I would like to call 'foo.c' main function from 'main_code.c'. Is this possible? Thank you very much and best regards. Francesco Moi <francescomoi@europe.com> scribbled the ...

It's primitive; it's dumb; it's brittle--but it's AI.
http://www.scn.org/~mentifex/aisource.html is a pathway to the http://code.google.com/p/mindforth AI project at Google Code. The AI is primitive because it has only enough features to demon- strate thinking. It is "dumb" inasmuch as it has barely any IQ. It is extremely brittle because the slightest change in the code can destroy the artificial mind crafted so painstakingly in Forth: Arthur -- http://agi-roadmap.org/Milestones -- ten years of progress. ...

It's not C# we have to worry about
Tiobe publishes a monthly ranking of the popularity of programming platforms. It's based on google, so it has to be taken with some grain of salt. Java has been number one since a while now after eclipsing C++, but there are some languages that are rapidly gaining popularity (and no, it's not C#, which has been flatline for a long time now). In fact, it's PHP that has been exploding in popularity, and it now ranks only below Java, C, and C++ in the rankings. http://www.tiobe.com/tiobe_index/tekst.htm The title I used was only somewhat tongue in cheek. PHP, like ASP, ...

if ('A:B:C' =~ /:(.*?)$/) then why the heck is $1 'B:C' and not just 'C'
To repeat the title, in case it is munged by Google Groups: if ('A:B:C' =~ /:(.*?)$/) then why the heck is $1 'B:C' and not just 'C' I've been developing with perl for years; but even simple things in it still sometimes throw up surprises. The regexp /:(.*?)$/ is anchored on the right by $, then comes a non- greedy match which, AIUI, is the "shortest string it can get away with", preceded by a colon. So I would expect this to pick up just the "C", as it does with /([^:]*)$/. Am I assuming/doing something silly? It is frid...

S*S'-> make a code program of DIFF (S) for S to excute expression:S*S, ( S=vector, S'=dS/dt). ideas to all?
hello everyone, now i need a help of ideas for a program style from all to find :S*S'. here we would not be able to use directly Diff( S) cuz of error of unsame size of S & diff( S). soo i dlove to get any code PROGRAM STYLEs for function S' ( dS/dt) to Find S*S', with S: vector, for example as a begining input: t=0:10; S=2*t.^3 +3*t; Now make a code program to DEFINE S' by ALGORITHM of MATH. dear all, my question going well for understanding?? iam waiting for all. ( hey, give a trial into the world of Math of Algorithm for function & diveritative)-) ...

Web resources about - how to write C program that copy's it's self onto the hard drive, while its running? - comp.lang.c

Resources last updated: 2/4/2016 1:41:39 AM