X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1012a6,29a923fbbd8ba89c X-Google-Attributes: gid1012a6,public X-Google-Thread: f996b,1bf4e46a52a72b7b X-Google-Attributes: gidf996b,public X-Google-ArrivalTime: 1994-12-19 07:16:21 PST Path: nntp.gmd.de!dearn!blekul11!idefix.CS.kuleuven.ac.be!ub4b!EU.net!Germany.EU.net! howland.reston.ans.net!news2.near.net!bloom-beacon.mit.edu!boulder!csnews!mox!tchrist From: Tom Christiansen Newsgroups: alt.ascii-art,comp.unix.shell Subject: Re: Anyway to randomize sig files? Date: 19 Dec 1994 15:16:21 GMT Organization: Perl Consulting and Training Lines: 144 Message-ID: <3d4845$8c5@csnews.cs.Colorado.EDU> References: <34550.stroheck@student.msu.edu> <3cppfl$mc9@cronkite.seas.gwu.edu> <3d0c28$62s@cronkite.seas.gwu.edu> Reply-To: tchrist@mox.perl.com (Tom Christiansen) NNTP-Posting-Host: perl.com Originator: tchrist@mox Xref: nntp.gmd.de alt.ascii-art:18268 comp.unix.shell:17898 #!/usr/bin/perl # # sigrand -- supply random fortunes for .signature file # Tom Christiansen -- tchrist@perl.com ################################################################ # begin configuration section $MKNOD = "/etc/mknod"; $SIG{'PIPE'} = 'IGNORE'; &gethome; # for rec/humor/funny instead of rec.humor.funny $NG_IS_DIR = 1; $FULLNAME = "$HOME/.fullname"; $FIFO = "$HOME/.signature"; $ART = "$HOME/.article"; $NEWS = "$HOME/News"; $SIGS = "$NEWS/SIGNATURES"; $SEMA = "$HOME/.sigrandpid"; $GLOBRAND = 1/4; # chance to use global sigs anyway # end configuration section -- HOME and FORTUNE get autoconf'd ################################################################ &setup; &justme; #fork && exit; # be a daemon open (SEMA, ">$SEMA") || die "can't write $SEMA: $!"; print SEMA "$$\n"; close SEMA || die "can't close $SEMA: $!"; MAIN: while (1) { open (FIFO, "> $FIFO") || die "can't write $FIFO: $!"; &load_sigs; $sig = $sig[rand @sig]; print FIFO $name, "\n" x (3 - ($sig =~ tr/\n//)), $sig; close FIFO; sleep 2; # to avoid dup sigs } die "NOT REACHED"; ################################################################ sub setup { if (-e $FULLNAME) { $name = `cat $FULLNAME`; die "$FULLNAME should contain only 1 line, aborting" if $name =~ tr/\n// > 1; } else { chop($host = `hostname`); ($host) = gethostbyname($host) unless $host =~ /\./; $user = $ENV{USER} || $ENV{LOGNAME} || $pw[0] || die "intruder alert"; ($name = $pw[6]) =~ s/,.*//; $name =~ s/&/\u\L$user/g; # can't believe some folks still do this $name = "\t$name\t$user\@$host\n"; } &check_fortunes if !-e $SIGS; unless (-p $FIFO) { if (!-e _) { system("$MKNOD $FIFO p") && die "can't mknod $FIFO"; warn "created $FIFO as a named pipe\n"; } else { die "$0: won't overwrite file .signature\n"; } } srand(time|$$); } sub load_sigs { local($sigfile) = &ng; if (!-e $sigfile) { @sig = (&fortune); return; } open (SIGS,$sigfile) || die "can't open $sigfile"; local($/) = "%%\n"; undef @sig; while () { s!$/$!!o; push(@sig,$_); } close SIGS; } sub ng { (rand(1.0) > ($GLOBRAND) && open ART) || return $SIGS; local($/) = ''; $_ = ; ($ng) = /Newsgroups:\s*([^,\s]*)/; $ng =~ s!\.!/!g if $NG_IS_DIR; # if rn -/, or SAVEDIR=%p/%c $ng = "$NEWS/$ng/SIGNATURES"; -f $ng ? $ng : $SIGS; } sub fortune { local($_, $tries); do { $_ = `$FORTUNE -s`; } until tr/\n// < 4 || $tries++ > 20; local($*) = 1; s/^/ /g; $_ || " SIGRAND: deliver random signals to all processes.\n"; } sub check_fortunes { return if $FORTUNE; foreach $dir (split(/:/, $ENV{PATH}), '/usr/games') { return if -x ($FORTUNE = "$dir/fortune"); } die "Need either $SIGS or a fortune program, bailing out"; } sub gethome { @pw = getpwuid($<); $HOME = $ENV{HOME} || $ENV{LOGDIR} || $pw[7] || die "no home directory for user $<"; } # "There can be only one." --the Highlander sub justme { if (open SEMA) { chop($pid = ); kill(0, $pid) && die "$0 already running (pid $pid)\n"; #warn "COOL: no $pid: $!"; close SEMA; } } -- Tom Christiansen Perl Consultant, Gamer, Hiker tchrist@mox.perl.com stab_val(stab)->str_nok = 1; /* what a wonderful hack! */ --Larry Wall in stab.c from the perl source code