/*
 * $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	"$Header: devtolin.c 1.6 89/10/10 $@(#)acct:lib/devtolin.c	1.4"
/*
 *	convert device to linename (as in /dev/linename)
 *	return ptr to LSZ-byte string, "?" if not found
 *	device must be character device
 *	maintains small list in tlist for speed
 */

#include <sys/types.h>
#include "acctdef.h"
#include <stdio.h>
#include <dirent.h>

#define TSIZE1	50	/* # distinct names, for speed only */
static	tsize1;
static struct tlist {
	char	tname[LSZ];	/* linename */
	dev_t	tdev;		/* device */
} tl[TSIZE1];

static struct dirent d;

dev_t	lintodev();
char	*dev_to_ttyname();

char *
devtolin(device)
dev_t device;
{
	register struct tlist *tp;
	DIR *dirf;
	struct dirent *dp;
	char *line;
	
	line = dev_to_ttyname(device) + sizeof("/dev/") - 1;
	if (lintodev(line) == device)
		return(line);

	for (tp = tl; tp < &tl[tsize1]; tp++)
		if (device == tp->tdev)
			return(tp->tname);
		
	if ((dirf = opendir("/dev")) == NULL)
		return("?");

	while ((dp = readdir(dirf)) != NULL) {
		if (dp->d_ino != 0 && lintodev(dp->d_name) == device) {
			if (tsize1 < TSIZE1) {
				tp->tdev = device;
				CPYN(tp->tname, dp->d_name);
				tsize1++;
			}
			CPYN(d.d_name, dp->d_name);
			closedir(dirf);
			return(d.d_name);
		}
	}
	closedir(dirf);
	return("?");
}
