Getting tty name without using ttyname(3)

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Sat Feb 17 07:34:06 AEST 1990


In article <17954 at rpp386.cactus.org> jfh at rpp386.cactus.org (John F. Haugh II) writes:
: How about using DBM?  Write a program which takes filenames as arguments
: and saves the names away using the device-inode as the index.  To find the
: name of your tty you would just make a request of the database.  You could
: then feed all of the tty device names to this program at boot-time to
: create the index.

You mean a little program like this?

#!/usr/bin/perl
chdir '/dev' || die "Can't cd to /dev: $!";
unlink 'devices.dir', 'devices.pag';
dbmopen(DEV,'devices',0644) || die "Can't dbmopen /dev/devices: $!";
foreach $file (grep( -c $_ || -b _, <*>)) {
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev) = stat $file;
    $DEV{sprintf("%d,%d", int($rdev / 256), $rdev % 256)} = $file;
}

It could also be done with makedbm, if you've got it.  In fact, you could
write makedbm in perl if you don't have it...

In fact, here's a version that does everything but the YP claptrap.

#!/usr/bin/perl

$usage = <<EOM;
Usage:
    $0 infile outfile
    $0 -u dbmfile
EOM

for ($_ = shift; /^-\w/; $_ = shift) {
    /^-u/ && (++$undo,next);
    die "Unrecognized switch: $_\n$usage";
}
unshift(@ARGV,$_);

$dbmfile = pop(@ARGV);

$#ARGV == 0 - $undo || die $usage;

if ($undo) {
    -f "$dbmfile.dir" || die "Can't find $dbmfile\n";
    -f "$dbmfile.pag" || die "Can't find $dbmfile\n";
    dbmopen(DBM,$dbmfile,0644) || die "Can't open $dbmfile: $!\n";
    while (($key,$val) = each(%DBM)) {
	print $key," ",$val,"\n";
    }
}
else {
    unlink "$dbmfile.dir", "$dbmfile.pag";
    dbmopen(DBM,$dbmfile,0644) || die "Can't create $dbmfile: $!";
    while (<>) {
	chop;
	while (/\\$/) {
	    chop;
	    $_ .= <>;
	    chop;
	}
	($key,$val) = split(/\s/,$_,2);
	$DBM{$key} = $val;
    }
}

You'd use it like this:
	ls -l /dev | \
	sed -e '/^[^cb]/d' \
	    -e 's/.*\([0-9][0-9]*,\) *\([0-9][0-9]*\).* \(.*\)/\1\2 \3/' | \
	makedbm - /dev/devices

To list it out:
	makedbm -u /dev/devices

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.wizards mailing list