/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libc-port:gen/ttyname.c	1.8"
/*	3.0 SID #	1.2	*/
/*LINTLIBRARY*/
/*
 * ttyname(f): return "/dev/ttyXX" which the the name of the
 * tty belonging to file f.
 *
 * This program works in two passes: the first pass tries to
 * find the device by matching device and inode numbers; if
 * that doesn't work, it tries a second time, this time doing a
 * stat on every file in /dev and trying to match device numbers
 * only. If that fails too, NULL is returned.
 */

#define	NULL	0
#include <sys/types.h>
#include <sys/param.h>
#include <sys/inode.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <dirent.h>

extern int open(), read(), close(), stat(), fstat(), isatty();
extern char *strcpy(), *strcat();
extern char *dev_to_ttyname();

static char rbuf[MAXNAMLEN+1], dev[]="/dev/", *rbufptr;
static char srbuf[MAXNAMLEN+1];

char *
ttyname(f)
int	f;
{
	struct stat fsb, tsb;
	struct dirent *db, *dp;
	DIR *dir = NULL;
	DIR *sdir = NULL;
	register int pass1;

	if(isatty(f) == 0)
		return(NULL);
	if(fstat(f, &fsb) < 0)
		return(NULL);
	if((fsb.st_mode & S_IFMT) != S_IFCHR)
		return(NULL);

	/*
	 * generate a tty name
	 * if this isn't the inode in use, search /dev the old way
	 */
	rbufptr = strcpy(rbuf, dev_to_ttyname(fsb.st_rdev));
	if (stat(rbufptr, &tsb) == 0
	&& tsb.st_rdev == fsb.st_rdev
	&& (tsb.st_mode&S_IFMT) == S_IFCHR
	&& tsb.st_ino == fsb.st_ino) {
		return(rbufptr);
	}

	pass1 = 1;
	do {
		if ((dir = opendir(dev)) == NULL)
			return(NULL);
		for (db = readdir(dir); db != NULL; db = readdir(dir)) {
			if(db->d_ino == 0)
				continue;
			if(pass1 && db->d_ino != fsb.st_ino)
				continue;
			(void) strcpy(rbuf, dev);
			(void) strcat(rbuf, db->d_name);
			if(stat(rbuf, &tsb) < 0)
				continue;
			if(tsb.st_rdev == fsb.st_rdev &&
				(tsb.st_mode&S_IFMT) == S_IFCHR &&
				(!pass1 || tsb.st_ino == fsb.st_ino)) {
				closedir(dir);
				return(rbuf);
			}
			if (pass1 || (tsb.st_mode&S_IFMT) != S_IFDIR)
				continue;
			if (strcmp(db->d_name, ".") == 0 ||
			    strcmp(db->d_name, "..") == 0)
				continue;
			if ((sdir = opendir(rbuf)) == NULL)
				continue;
			/* search subdirectories on 2nd pass */
			for (dp = readdir(sdir); dp != NULL; dp = readdir(sdir)) {
				if (dp->d_ino == 0)
					continue;
				(void) strcpy(srbuf, rbuf);
				(void) strcat(srbuf, "/");
				(void) strcat(srbuf, dp->d_name);
				if(stat(srbuf, &tsb) < 0)
					continue;
				if (tsb.st_rdev == fsb.st_rdev &&
					(tsb.st_mode&S_IFMT) == S_IFCHR &&
					tsb.st_ino == fsb.st_ino) {
					closedir(sdir);
					closedir(dir);
					return(srbuf);
				}
			}
			closedir(sdir);
		}
		closedir(dir);
	} while(pass1--);
	return(NULL);
}

/*
 *	The array 'digit64[]' defines the radix-64 "digits".  Conversion
 *	from decimal is done by simply indexing into this array.  By
 *	using only 6 bits, the index will never be out of range!
 */
static
unsigned
char digit64[] =
{
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '+',
};

/*
 *	The following macros are used to convert a (32 bit) dev_t
 *	into four 6 bit (base 64) values.
 */
#define C_MUNGE(d)	(((d) & 0x3f0000) << 2)
#define D_MUNGE(d)	(((d) & 0x3f00) << 4)
#define E_MUNGE(d)	(((d) & 0x3c00000) >> 14)
#define F_MUNGE(d)	((d) & 0xff)
#define SQUISH(d)	(C_MUNGE(d) | D_MUNGE(d) | E_MUNGE(d) | F_MUNGE(d))

static
char *
dev_to_ttyname(dev)
	dev_t dev;
{
	register int i, div;
	char *ans = "/dev/ttyAA/AAAA";
	long ldev;
#define A_END	14
#define A_DIR	 8

	/* Convert 16 bit dev_t to long to simulate 32 bit dev_t */
	ldev = (dev & 0xff) | ((dev & 0xff00) << 8);
	div = SQUISH(ldev);

	/*
	 * Static data will be changed with each invocation, so
	 * always calculate four places.
	 */
	for(i = A_END; i > A_END-4; i--) {
		ans[i] = digit64[div & 0x3f];	/* div % (int)base; */
		div = (unsigned)div >> 6;	/* div / (int)base; */
	}

	ans[A_DIR] = ans[A_END - 3];
	ans[A_DIR + 1] = ans[A_END - 1];
	return(ans);
}
