#!/usr/bin/php
<?php
/**
 * Tiny script that simply redirects from STDIN to STDOUT for a
 * given amount of seconds.
 * I used this script to abort a network stream after a known count of seconds
 * 
 * @author Julius Beckmann
 * @link http://juliusbeckmann.de/
 * @license GPL
 */

function usage() {
  global 
$argv;
  
fwrite(STDOUT"Usage: ".basename($argv[0])." NUM_SECS\n");
  
fwrite(STDOUT"NUM_SECS: Number of seconds to redirect STDIN.\n");
}

// Check argument count.
if($argc != 2) {
  
usage();
  exit(
1);
}

// Calculate when we should stop.
$time_end = (int)$argv[1] + time();

// Simply move stream.
while($time_end >= time()) {
  
fwrite(STDOUTfread(STDIN4096));
  
// This is important. Without it, we would have CPU-intensive busy-waiting.
  
usleep(10);
}