#!/usr/bin/perl -T -w # # $Id: slow-filter,v 1.2 2003/10/07 00:41:07 suter Exp $ # Copyright (C) 1998,2003 Mark Suter # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # [MJS 29 Jul 1998] Original version. # [MJS 22 Mar 2003] Re-worked to use Getopt::Long and Pod::Usage use strict; use Getopt::Long; use Pod::Usage; $ENV{'PATH'} = '/bin:/usr/bin'; my %opt = (man => 0, help => 0, block => 8, delay => 100); GetOptions(\%opt, "man", "help", "block", "delay") or pod2usage(0); $opt{man} and pod2usage(-exitval => 0, -verbose => 2); $opt{help} and pod2usage(0); my ($len, $buf, $offset, $written); while($len = sysread STDIN, $buf, $opt{block}) { if (!defined $len) { next if $! =~ /^Interrupted/; die "System read error: $!\n"; } $offset = 0; while ($len) { # Handle partial writes. $written = syswrite STDOUT, $buf, $len, $offset; die "System write error: $!\n" unless defined $written; $len -= $written; $offset += $written; }; select undef, undef, undef, ($opt{delay} / 1000); # Sleep for $opt{delay} ms. } __END__ =head1 NAME slow-filter - A "slow" version of cat =head1 SYNOPSIS slow-filter [ --block ] [ --delay ] =head1 OPTIONS =over 8 =item B<--block> Number of octets to read in each sysread. =item B<--delay> Delay in milliseconds between reads. =item B<--man> Print the manual page and exit. =item B<--help> Print a brief help message and exit. =back =head1 DESCRIPTION B is a simple filter based on code from page 231 of _Programming Perl_, under the summary of 'syswrite'. It will delay its output according to the paramaters provided. =head1 EXIT CODES If B exits with a zero exit status and the correct output is on standard output. Nothing else is ever printed to standard error. B will exit with a non-zero exit status if there was a fatal error. Both fatal and non-fatal errors will cause output on standard error. =head1 AUTHOR Mark Suter EFE =head1 COPYRIGHT Copyright (C) 1998,2003 Mark Suter EFE This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or from the following webpage. http://www.gnu.org/licenses/gpl.txt =cut