tholm@uvicctr.UUCP (Terrence W. Holm) (08/26/88)
EFTH MINIX report #33 - August 1988 - ttyname(3)
Ttyname(3) seems to be missing in the distribution of
MINIX 1.3. I can not find the original posting [PH?],
and Dr. Tanenbaum doesn't have it either - so, here
is what I currently use.
----------------------------------------------------------
echo x - ttyname.3
gres '^X' '' > ttyname.3 << '/'
XSUBROUTINES
X ttyname(3) - look up the filename of a terminal device
X
XINVOCATION
X char *ttyname( desc )
X int desc;
X
XEXPLANATION
X The argument <desc> must be a file descriptor for a character
X device. This routine returns a pointer to the filename within
X "/dev" which is associated with that file descriptor. For
X example, the command tty(1) is almost as simple as
X "puts( ttyname(0) )".
X
X The string space for the filename is static storage within
X ttyname(3), it is overwritten by subsequent calls.
X
XRESULTS
X NULL : No filename found in /dev.
X o/w : Pointer to string name of device.
X
XFILES
X /dev/*
X
XREFERENCES
X tty(1), fstat(2), isatty(2), ctermid(3)
/
echo x - ttyname.c
gres '^X' '' > ttyname.c << '/'
X/* ttyname(3)
X */
X
X#include <sys/types.h>
X#include <sys/dir.h>
X#include <sys/stat.h>
X
X#define NULL (char *) 0
X#define DEV "/dev/"
X
Xstatic char file_name[40];
X
X
Xchar *ttyname( tty_file_desc )
X int tty_file_desc;
X
X {
X int dev_dir_desc;
X struct direct dir_entry;
X struct stat tty_stat;
X struct stat dev_stat;
X
X /* Make sure the supplied file descriptor is for a terminal */
X
X if ( fstat(tty_file_desc, &tty_stat) < 0 )
X return( NULL );
X
X if ( (tty_stat.st_mode & S_IFMT) != S_IFCHR )
X return( NULL );
X
X
X /* Open /dev for reading */
X
X if ( (dev_dir_desc = open(DEV,0)) < 0 )
X return( NULL );
X
X while ( read(dev_dir_desc, (char *) &dir_entry, sizeof (struct direct)) > 0 )
X {
X /* Find an entry in /dev with the same inode number */
X
X if ( tty_stat.st_ino != dir_entry.d_ino )
X continue;
X
X /* Put the name of the device in static storage */
X
X strcpy( file_name, DEV );
X strncat( file_name, dir_entry.d_name, sizeof (dir_entry.d_name) );
X
X /* Be absolutely certain the inodes match */
X
X if ( stat(file_name, &dev_stat) < 0 )
X continue;
X
X if ( (dev_stat.st_mode & S_IFMT) != S_IFCHR )
X continue;
X
X if ( tty_stat.st_ino == dev_stat.st_ino &&
X tty_stat.st_dev == dev_stat.st_dev &&
X tty_stat.st_rdev == dev_stat.st_rdev )
X {
X close( dev_dir_desc );
X return( file_name );
X }
X }
X
X close( dev_dir_desc );
X return( NULL );
X }
/
----------------------------------------------------------
Edwin L. Froese
uw-beaver!ubc-cs!mprg!handel!froese
Terrence W. Holm
uw-beaver!uvicctr!tholm