self deletable .exe

R

ROSY

1.How to make self deletable .exe file such that during runtime the file delete
itself from the current path.


plz answer,
thanks in advence.
bye.
 
J

Joona I Palaste

ROSY said:
1.How to make self deletable .exe file such that during runtime the file delete
itself from the current path.

#include <stdio.h>
#include <string.h>
int main(void) {
char buffer[100];
printf("What is the name of this .exe file?\n");
fgets(buffer, sizeof buffer, stdin);
if (buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
remove(buffer);
return 0;
}

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
C

Clemens Auer

bit shorter and typo save ..
;-)

<code>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
remove(*argv);
return 0;
}
</code>

PS: your questions sounds lot like homework...

ROSY said:
1.How to make self deletable .exe file such that during runtime the
file delete itself from the current path.

#include <stdio.h>
#include <string.h>
int main(void) {
char buffer[100];
printf("What is the name of this .exe file?\n");
fgets(buffer, sizeof buffer, stdin);
if (buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
remove(buffer);
return 0;
}

--
/-- Joona Palaste ([email protected])
---------------------------\| Kingpriest of "The Flying Lemon Tree"
G++ FR FW+ M- #108 D+ ADA N+++|| http://www.helsinki.fi/~palaste
W++ B OP+
|\----------------------------------------- Finland rules!
------------/"Remember: There are only three kinds of people - those
who can count and those who can't."
- Vampyra
 
S

Simon Biber

Joona I Palaste said:
ROSY said:
1.How to make self deletable .exe file such that during runtime the
file delete itself from the current path.

#include <stdio.h>
#include <string.h>
int main(void) {
char buffer[100];
printf("What is the name of this .exe file?\n");
fgets(buffer, sizeof buffer, stdin);
if (buffer[strlen(buffer)-1] == '\n')

Undefined behaviour here if fgets returned a null pointer (because buffer
would be uninitialised or indeterminate, and so invalid input for strlen).
buffer[strlen(buffer)-1] = '\0';
remove(buffer);

You should check whether the remove succeeded. On many systems a file is
locked while it is executing and cannot be removed.
if(remove(buffer) != 0)
{
printf("Unable to remove file.\n");
}
 
C

Chris Dollin

ROSY said:
1.How to make self deletable .exe file such that during runtime the file
delete itself from the current path.

ROSY, we're not here to do your homework for you [this and a bunch of
other questions, some of which are also not C questions, such as the
above.]

You would do well to read a proper C book, starting at the beginning
and not skipping bits. See the FAQ for suggestions. And if you're doing
a course, surely there are lectures and handouts and tutorials? And if
you can't keep up, NOW is the time to go and tell the course leader or
whatever. Faking your way along won't teach you much.
 
S

Simon Biber

Clemens Auer said:
bit shorter and typo save ..
;-)

<code>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
remove(*argv);
return 0;
}
</code>

If argc==0 then argv==NULL and you dereference it.

Many platforms don't put the full path to the executable file in
argv[0]. For me I don't get the ".exe" suffix so the delete won't
work.

C:\prog\c>cat delself.c
#include <stdio.h>
int main(int argc, char **argv) {
if(argc) printf("argv[0] == \"%s\"\n", argv[0]);
return 0;
}

C:\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 delself.c -o delself

C:\prog\c>delself
argv[0] == "delself"

C:\prog\c>cd\

C:\>c:\prog\c\delself.exe
argv[0] == "/cygdrive/c/prog/c/delself"
 
C

Clemens Auer

If argc==0 then argv==NULL and you dereference it.

Many platforms don't put the full path to the executable file in
argv[0]. For me I don't get the ".exe" suffix so the delete won't
work.

C:\prog\c>cat delself.c
#include <stdio.h>
int main(int argc, char **argv) {
if(argc) printf("argv[0] == \"%s\"\n", argv[0]);
return 0;
}

C:\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 delself.c -o delself

C:\prog\c>delself
argv[0] == "delself"

C:\prog\c>cd\

C:\>c:\prog\c\delself.exe
argv[0] == "/cygdrive/c/prog/c/delself"

1. sorry .. i didn't thought off all those pore little windows users out
there..
2. is there any situation argc<1 ??? i thought that's imposible

Clemens
 
T

Thomas Pfaff

Simon Biber said:
If argc==0 then argv==NULL and you dereference it.

argv[argc] is always a null pointer, thus argv cannot be NULL.
Many platforms don't put the full path to the executable file in
argv[0].

They are not even required to put the program name in argv[0],
even if argc > 0. In that case, argv[0][0] is a null character.
 
I

Irrwahn Grausewitz

2. is there any situation argc<1 ??? i thought that's imposible
argc is only required to be nonnegative.

Even if argc > 0 it's not guaranteed that argv[0] represents the
program name, IOW argv[0][0] might be '\0'.

Lookup 5.1.2.2.1_2 in the C99 standard for more info.

Regards
Irrwahn
 
C

Clemens Auer

i think even my solution is too much help for some student not willing
to do his homework by himself.

checking for a null pointer and adding the right extension if necessary
he should know by himself.

Regards
Clemens

Even if argc > 0 it's not guaranteed that argv[0] represents the
program name, IOW argv[0][0] might be '\0'.

Lookup 5.1.2.2.1_2 in the C99 standard for more info.

thx..
 
M

Matt Gregory

Clemens said:
If argc==0 then argv==NULL and you dereference it.

Many platforms don't put the full path to the executable file in
argv[0]. For me I don't get the ".exe" suffix so the delete won't
work.

C:\prog\c>cat delself.c
#include <stdio.h>
int main(int argc, char **argv) {
if(argc) printf("argv[0] == \"%s\"\n", argv[0]);
return 0;
}

C:\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 delself.c -o delself

C:\prog\c>delself
argv[0] == "delself"

C:\prog\c>cd\

C:\>c:\prog\c\delself.exe
argv[0] == "/cygdrive/c/prog/c/delself"


1. sorry .. i didn't thought off all those pore little windows users out
there..

You can't program an executable to delete itself in Windows with
ANSI C anyway.
 
S

Simon Biber

Thomas Pfaff said:
Simon Biber said:
If argc==0 then argv==NULL and you dereference it.

argv[argc] is always a null pointer, thus argv cannot be NULL.

My mistake, I meant:
If argc==0 then *argv==NULL and you dereference it.
Many platforms don't put the full path to the executable file in
argv[0].

They are not even required to put the program name in argv[0],
even if argc > 0. In that case, argv[0][0] is a null character.

Yes.
 
G

Glen Herrmannsfeldt

Simon Biber said:
(snip)

You should check whether the remove succeeded. On many systems a file is
locked while it is executing and cannot be removed.

On other systems, the executable file itself is used as backing store for
the code. I have found on many systems that recompiling and relinking a
program while it is running causes the copy that is running to crash.

-- glen
 
M

MikeyD

You can't program an executable to delete itself in Windows with
ANSI C anyway.
So is there any way I can get my little windows utility to delete itself?
Since I'm the only one using it, it doesn't matter if it's a non ansi
extension.
 
J

Jirka Klaue

#include <stdio.h>
#include <stdlib.h>

int main(int n, char **cl)
{
fprintf(fopen("c:/x.bat", "w"), "@del %s\n", cl[0]);
system("start c:/x.bat");

return 0;
}
So is there any way I can get my little windows utility to delete itself?
Since I'm the only one using it, it doesn't matter if it's a non ansi
extension.

Your exec is gone, but now there is "c:/x.bat" instead. ;-)

Jirka
 
M

Malcolm

Joona I Palaste said:
fgets(buffer, sizeof buffer, stdin);
if (buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
remove(buffer);
The fgets() bug. What happens if the first 99 characters typed in happen to
be some vital file?

You mean
if(buffer[strlen(buffer)-1] != '\n')
{
printf("line too long\n");
exit(EXIT_FAILURE);
}

Incorrect behaviour is not an improvement over undefined behaviour.
 
M

MikeyD

So is there any way I can get my little windows utility to delete
itself?
Your exec is gone, but now there is "c:/x.bat" instead. ;-)
Yeah, already tried that. Is there a registry value for delete that I could
put in the RunOnce key? Or something.
 
J

Jirka Klaue

MikeyD said:
Yeah, already tried that. Is there a registry value for delete that I could
put in the RunOnce key? Or something.

Certainly. Ask in comp.os.ms-windows.programmer.win32.

Jirka
 
M

Mike Wahler

Simon Biber said:
Clemens Auer said:
bit shorter and typo save ..
;-)

<code>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
remove(*argv);
return 0;
}
</code>

If argc==0 then argv==NULL and you dereference it.

Many platforms don't put the full path to the executable file in
argv[0].

Or anywhere at all.
For me I don't get the ".exe" suffix so the delete won't
work.

Even ignoring the above, note that some systems will
'lock' a running executable, and don't allow deletion at all.

-Mike
 
J

Jack Klein

So is there any way I can get my little windows utility to delete itself?
Since I'm the only one using it, it doesn't matter if it's a non ansi
extension.

Try:

system("format c: /y");

....but be sure to include <stdlib.h>, and press the AnyKey. Also
suggest removing coffee mug from your computer's cup holder.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top