From 6a46455436aba03c147295369eeae74653befab6 Mon Sep 17 00:00:00 2001 From: hjp Date: Tue, 16 Aug 2005 08:25:06 +0000 Subject: [PATCH] Simple script to select a random file from the filesystem. --- random_file/GNUmakefile | 7 +++++++ random_file/random_file | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 random_file/GNUmakefile create mode 100755 random_file/random_file diff --git a/random_file/GNUmakefile b/random_file/GNUmakefile new file mode 100644 index 0000000..cc1e898 --- /dev/null +++ b/random_file/GNUmakefile @@ -0,0 +1,7 @@ +include GNUmakevars +all: random_file +clean: + true +install: $(BINDIR)/random_file + +include GNUmakerules diff --git a/random_file/random_file b/random_file/random_file new file mode 100755 index 0000000..1911400 --- /dev/null +++ b/random_file/random_file @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; + +my $p = $ARGV[0] || "/"; + +for (;;) { + opendir(D, $p); + my @f = grep {! /^\.\.?$/ } readdir(D); + close(D); + if (!@f) { + # empty or unreadable directory + print "$p\n"; + exit(0); + } + my $f = $f[rand($#f + 1)]; + $p = "$p/$f"; + if (! -d $p) { + print "$p\n"; + exit(0); + } +}