#!/usr/bin/env perl # Usage: idle [tmax] =head1 NAME idle - display idle times =head1 SYNOPSIS idle [tmax] =head1 DESCRIPTION B displays idle times for miscellaneous devices (B, B, BI, BI, BI and BI). The format is: C where I is the number of days, I is the number of hours, I is the number of minutes and I is the number of seconds. If I is 0, it is not displayed. An argument can be given. It is the maximum number of seconds for which the idle time is displayed. =head1 NOTE B was originally written for Solaris, then updated to work under Linux. It may work on other systems, perhaps not completely. It has been tested on some machines (but the results may change from one configuration to another, these are only examples): =over 4 =item * Solaris: B = keyboard, B = mouse, BI = text terminals. =item * Linux/x86: B = mouse (no longer seems to work), B to B = Linux consoles, B = X server, BI = text terminals, BI = ssh connections. =item * Linux/PowerPC: B to B = Linux consoles, B = X server, BI = some text terminals, BI = some text terminals and ssh connections. =item * OSF: BI and BI = text terminals, but BI and BI give the same results, so you may want to filter one of them. =item * Darwin (Mac OS X): BI = text terminals and ssh connections. =back =head1 AUTHOR Vincent Lefevre . Mail comments and bug reports about B to . =head1 AVAILABILITY The latest version is available at . =cut ######################################################################## use integer; use strict; # $Id: idle 11969 2006-04-23 09:40:47Z lefevre $ my ($tmax,%t,%u); $ARGV[0] =~ /^\d+$/ and $tmax = $ARGV[0]; my $len = 5; foreach ('kbd', 'mouse') { my @s = stat "/dev/$_" or next; $t{$_} = $s[8]; $u{$_} = $s[4]; } opendir DEV, '/dev' or die "idle: can't open directory /dev\n"; while (defined($_ = readdir DEV)) { /^(tty[p-z]?|vt)\d+$/ or next; my @s = stat "/dev/$_" or next; $s[4] or next; $t{$_} = $s[8]; $u{$_} = $s[4]; } closedir DEV or die "idle: can't close directory /dev\n"; my $pts = '/dev/pts'; if (opendir PTS, $pts) { while (defined($_ = readdir PTS)) { /^\d+$/ or next; my @s = stat "$pts/$_" or next; $s[5] or next; $t{"pts/$_"} = $s[8]; $u{"pts/$_"} = $s[4]; } closedir PTS or die "idle: can't close directory $pts\n"; } my $time = time; foreach (sort { $t{$b} <=> $t{$a} } keys %t) { my $t = $time - $t{$_}; defined $tmax && $t > $tmax and next; printf "%-8s %12s (%s)\n", $_, &disptime($t), &user($u{$_}); } sub disptime { my $t = $_[0]; my $s = $t % 60; $t /= 60; my $m = $t % 60; $t /= 60; my $h = $t % 24; $t /= 24; sprintf "%s%02d:%02d:%02d", ($t ? "$t+" : ""), $h, $m, $s } sub user { my $u = (getpwuid $_[0])[0]; return $u ne '' ? $u : $_[0]; }