I got a reply from the author and worked a "little" on it this morning. I'm down to 470 characters over 6 lines. There's a few bugs and several security holes fixed. Fixing them cost a few bytes, but I think it's worth it, overall. I mean, what use is showing Python we can beat them in program size if the program sucks? We have to show them we can write
secure network software in 6 lines or less. *grins*
The author is working on revamping the while() loop and moving to use read() instead of <> for the socket. But, I don't have that version, so I'll work with what I have.
sub f{for$w(keys%k){&t}}$/=$_;($,,$w=$a,%k)=splice@ARGV,0,3;socket S,2
+,1,6;bind
S,&a;sub i{$k{$w}=1}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub
+t{socket
X,2,1,6;if(connect X,a){print X "$, $_[0] $_[1]/$_[2]";close}else{dele
+te$k{$w}}}
listen S,5;while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&c
+lose){
m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$')}sub e{open F,'>',$
+3;print F
$';close}sub g{open(F,'<',$3)&&t$a,"e$3", <F>;close F}sub h{t$_,i for
+keys%k}
This code is sooo completely untested. :-)
The readable version ...
sub f{
for$w(keys%k){&t}
}
$/=$_;
($,,$w=$a,%k)=splice@ARGV,0,3;
socket S,2,1,6;
bind S,&a;
sub i{
$k{$w}=1
}
sub a{
$w=~/:/;
pack'CxnC4x8',2,$',split'\.',$`
}
sub t{
socket X,2,1,6;
if(connect X,a){
print X "$, $_[0] $_[1]/$_[2]";
close
}else{
delete$k{$w}
}
}
listen S,5;
while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&close){
m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$')
}
sub e{
open F,'>',$3;
print F $';
close
}
sub g{
open(F,'<',$3)&&t$a,"e$3", <F>;
close F
}
sub h{
t$_,i for keys%k
}
Update: After jonadab's suggestions and some reorganization, we save 11 characters (down to 459 over 6 lines):
sub f{for$w(keys%k){&t}}$/=$_;($,,$w=$a,%k)=splice@ARGV,0,3;socket S,2
+,1,6;bind
S,&a;sub i{$k{$w}=1}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub
+t{socket
X,2,1,6;connect(X,a)?print(X "$, $_[0] $_[1]/$_[2]")&&close:delete$k{$
+w}}listen
S,5;while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&close){s
+ub h{t$_,i
for keys%k}m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$')}sub e{o
+pen
F,'>',$3;print F $';close}sub g{open(F,'<',$3)&&t$a,"e$3",<F>;close F}
}
Yes, there's a subroutine definition in the while-loop. "perl -MO=Deparse" tells me it's ok.
Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.