473,766 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5153
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******@hotma il.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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******@hotma il.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.re mcomp.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.re mcomp.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**********@c tb-nnrp2.saix.net> ,
goose <lk************ @webmail.co.zaw rote:
>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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3024
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole* file, not for the *remaining* data to be written. It happens both under 2.3 and 2.2.3. Any ideas?
4
2136
by: GO | last post by:
I have a custom Perl application (programmed by myself) that is used to rename files. For some reason, within the last month, it is no longer working properly. Prior to this I've been using it for several years without problem. It's a relatively simple program that reads the file names from the current directory and replaces strings of text issued from command-line parameters. But now, for some unknown reason, it looks in the root of...
18
4893
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
18
3710
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only be written to, since its contents are undefined. The program is allocating a new piece of data, and the previous contents aren't relevant. This memory
5
2584
by: news.microsoft.com | last post by:
Hello, what is the most performant size for the byte array for reading/writing using 2 filestreams? example code: Dim bytearrayinput(4095) As Byte Dim rdlen As Long = 0 Dim totlen As Long = fsInput.Length
2
2136
by: 1388-2/HB | last post by:
On a W2K3 box running IIS 6 I have a web app that implements Forms Authentication mixed with AD (the login page autheticates users against AD & impersonates them with each page request). This means my process identity varies, and is not necessarily "ASPNET" or "IUSR...". My process identity for any given page request will be user1, user2, user3, etc. (there's a lot of them) This web app needs to copy files (Word documents) from our old...
34
29897
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
0
3308
by: dot | last post by:
I spent a few headache filled days trying to use GMP on windows (XP pro) I finally got it to work and since I found little help on the Web I thought someone might find what i did useful. Creating an environment suitable for GMP programming. 1) Create 'cygwin' environment (see below) 2)add GMP to the cygwin directory (see below)
1
2287
by: thanawala27 | last post by:
Hi, I have mapped a server to my computer (called it H drive). all my perl scripts run on the server. Now, i would like to copy a file from my local hard disk (C drive) to this server. As my scripts are running on the server, so while copying the file, the server is not able to recognize the location of the C drive (which is my local computer) So, how do I make the server recognize my local computer and hence copy the file to the...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.