1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  *	convert device to linename (as in /dev/linename)
30*0Sstevel@tonic-gate  *	return ptr to LSZ-byte string, "?" if not found
31*0Sstevel@tonic-gate  *	device must be character device
32*0Sstevel@tonic-gate  *	maintains small list in tlist structure for speed
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/param.h>
38*0Sstevel@tonic-gate #include "acctdef.h"
39*0Sstevel@tonic-gate #include <dirent.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static	tsize1;
44*0Sstevel@tonic-gate static struct tlist {
45*0Sstevel@tonic-gate 	char	tname[LSZ];	/* linename */
46*0Sstevel@tonic-gate 	dev_t	tdev;		/* device */
47*0Sstevel@tonic-gate } tl[TSIZE1];
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate char	*strncpy();
50*0Sstevel@tonic-gate dev_t	lintodev();
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static char dev_dir[] = "/dev";
53*0Sstevel@tonic-gate static char *def_srch_dirs[] = { "/dev/term",
54*0Sstevel@tonic-gate 				 "/dev/pts",
55*0Sstevel@tonic-gate 				 "/dev/xt",
56*0Sstevel@tonic-gate 				 NULL };
57*0Sstevel@tonic-gate char file_name[MAX_DEV_PATH];	/* name being returned */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate char *
60*0Sstevel@tonic-gate devtolin(device)
61*0Sstevel@tonic-gate   dev_t device;
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	register struct tlist *tp;
64*0Sstevel@tonic-gate 	char **srch_dirs;	/* priority directories to search first */
65*0Sstevel@tonic-gate 	int found = 0;
66*0Sstevel@tonic-gate 	int dirno = 0;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	for (tp = tl; tp < &tl[tsize1]; tp++)
69*0Sstevel@tonic-gate 		if (device == tp->tdev)
70*0Sstevel@tonic-gate 			return(tp->tname);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	srch_dirs = def_srch_dirs;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	while ((!found) && (srch_dirs[dirno] != NULL)) {
75*0Sstevel@tonic-gate 		/* if /dev is one of the priority directories we should only
76*0Sstevel@tonic-gate 		   search its top level for now (set depth = MAX_SEARCH_DEPTH) */
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		found = srch_dir(device, srch_dirs[dirno],
79*0Sstevel@tonic-gate 				 ((strcmp(srch_dirs[dirno], dev_dir) == 0) ?
80*0Sstevel@tonic-gate 				  MAX_SRCH_DEPTH : 1),
81*0Sstevel@tonic-gate 				 NULL);
82*0Sstevel@tonic-gate 		dirno++;
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* if not yet found search remaining /dev directory skipping the
86*0Sstevel@tonic-gate 	   priority directories */
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (!found)
89*0Sstevel@tonic-gate 		found = srch_dir(device, dev_dir, 0, srch_dirs);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/* if found then put it (without the "/dev/" prefix) in the tlist
92*0Sstevel@tonic-gate 	   structure and return the path name without the "/dev/" prefix */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (found) {
95*0Sstevel@tonic-gate 		if (tsize1 < TSIZE1) {
96*0Sstevel@tonic-gate 			tp->tdev = device;
97*0Sstevel@tonic-gate 			CPYN(tp->tname, file_name+5);
98*0Sstevel@tonic-gate 			tsize1++;
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 		return(file_name+5);
101*0Sstevel@tonic-gate 	} else
102*0Sstevel@tonic-gate 	/* if not found put "?" in the tlist structure for that device and
103*0Sstevel@tonic-gate 	   return "?" */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		if (tsize1 < TSIZE1) {
106*0Sstevel@tonic-gate 			tp->tdev = device;
107*0Sstevel@tonic-gate 			CPYN(tp->tname, "?");
108*0Sstevel@tonic-gate 			tsize1++;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 		return("?");
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static int
115*0Sstevel@tonic-gate srch_dir(device, path, depth, skip_dirs)
116*0Sstevel@tonic-gate   dev_t device;	/* device we are looking for */
117*0Sstevel@tonic-gate   char *path;	/* current path */
118*0Sstevel@tonic-gate   int depth;	/* current depth */
119*0Sstevel@tonic-gate   char *skip_dirs[];	/* directories that don't need searched */
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	DIR *fdev;
122*0Sstevel@tonic-gate 	struct dirent *d;
123*0Sstevel@tonic-gate 	int dirno = 0;
124*0Sstevel@tonic-gate 	int found = 0;
125*0Sstevel@tonic-gate 	int path_len;
126*0Sstevel@tonic-gate 	char *last_comp;
127*0Sstevel@tonic-gate 	struct stat sb;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/* do we need to search this directory? */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if ((skip_dirs != NULL) && (depth != 0))
132*0Sstevel@tonic-gate 		while (skip_dirs[dirno] != NULL)
133*0Sstevel@tonic-gate 			if (strcmp(skip_dirs[dirno++], path) == 0)
134*0Sstevel@tonic-gate 				return(0);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	/* open the directory */
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if ((fdev = opendir(path)) == NULL)
140*0Sstevel@tonic-gate 		return(0);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	/* initialize file name using path name */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	path_len = strlen(path);
145*0Sstevel@tonic-gate 	strcpy(file_name, path);
146*0Sstevel@tonic-gate 	last_comp = file_name + path_len;
147*0Sstevel@tonic-gate 	*last_comp++ = '/';
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* start searching this directory */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	while ((!found) && ((d = readdir(fdev)) != NULL))
152*0Sstevel@tonic-gate 		if (d->d_ino != 0) {
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 			/* if name would not be too long append it to
155*0Sstevel@tonic-gate 			   directory name, otherwise skip this entry */
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 			if ((int) (path_len + strlen(d->d_name) + 2) > MAX_DEV_PATH)
158*0Sstevel@tonic-gate 				continue;
159*0Sstevel@tonic-gate 			else
160*0Sstevel@tonic-gate 				strcpy(last_comp, d->d_name);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 			/* if this directory entry has the device number we need,
163*0Sstevel@tonic-gate 			   then the name is found.  Otherwise if it's a directory
164*0Sstevel@tonic-gate 			   (not . or ..) and we haven't gone too deep, recurse. */
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 			if (lintodev(file_name+5) == device) {
167*0Sstevel@tonic-gate 				found = 1;
168*0Sstevel@tonic-gate 				break;
169*0Sstevel@tonic-gate 			} else if ((depth < MAX_SRCH_DEPTH) &&
170*0Sstevel@tonic-gate 					  (strcmp(d->d_name, ".") != 0) &&
171*0Sstevel@tonic-gate 					  (strcmp(d->d_name, "..") != 0) &&
172*0Sstevel@tonic-gate 					  (stat(file_name, &sb) != -1) &&
173*0Sstevel@tonic-gate 					  ((sb.st_mode & S_IFMT) == S_IFDIR))
174*0Sstevel@tonic-gate 				found = srch_dir(device, file_name, depth+1, skip_dirs);
175*0Sstevel@tonic-gate 		}
176*0Sstevel@tonic-gate 	closedir(fdev);
177*0Sstevel@tonic-gate 	return(found);
178*0Sstevel@tonic-gate }
179