10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
230Sstevel@tonic-gate /* All Rights Reserved */
240Sstevel@tonic-gate
25*428Ssl108498 /*
26*428Ssl108498 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27*428Ssl108498 * Use is subject to license terms.
28*428Ssl108498 */
29*428Ssl108498 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.9 */
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * convert device to linename (as in /dev/linename)
330Sstevel@tonic-gate * return ptr to LSZ-byte string, "?" if not found
340Sstevel@tonic-gate * device must be character device
350Sstevel@tonic-gate * maintains small list in tlist structure for speed
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include "acctdef.h"
420Sstevel@tonic-gate #include <dirent.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate
46*428Ssl108498 static int tsize1;
470Sstevel@tonic-gate static struct tlist {
480Sstevel@tonic-gate char tname[LSZ]; /* linename */
490Sstevel@tonic-gate dev_t tdev; /* device */
500Sstevel@tonic-gate } tl[TSIZE1];
510Sstevel@tonic-gate
520Sstevel@tonic-gate char *strncpy();
530Sstevel@tonic-gate dev_t lintodev();
540Sstevel@tonic-gate
550Sstevel@tonic-gate static char dev_dir[] = "/dev";
560Sstevel@tonic-gate static char *def_srch_dirs[] = { "/dev/term",
570Sstevel@tonic-gate "/dev/pts",
580Sstevel@tonic-gate "/dev/xt",
590Sstevel@tonic-gate NULL };
600Sstevel@tonic-gate char file_name[MAX_DEV_PATH]; /* name being returned */
610Sstevel@tonic-gate
62*428Ssl108498 static int srch_dir();
63*428Ssl108498
640Sstevel@tonic-gate char *
devtolin(device)650Sstevel@tonic-gate devtolin(device)
660Sstevel@tonic-gate dev_t device;
670Sstevel@tonic-gate {
680Sstevel@tonic-gate register struct tlist *tp;
690Sstevel@tonic-gate char **srch_dirs; /* priority directories to search first */
700Sstevel@tonic-gate int found = 0;
710Sstevel@tonic-gate int dirno = 0;
720Sstevel@tonic-gate
730Sstevel@tonic-gate for (tp = tl; tp < &tl[tsize1]; tp++)
740Sstevel@tonic-gate if (device == tp->tdev)
750Sstevel@tonic-gate return(tp->tname);
760Sstevel@tonic-gate
770Sstevel@tonic-gate srch_dirs = def_srch_dirs;
780Sstevel@tonic-gate
790Sstevel@tonic-gate while ((!found) && (srch_dirs[dirno] != NULL)) {
800Sstevel@tonic-gate /* if /dev is one of the priority directories we should only
810Sstevel@tonic-gate search its top level for now (set depth = MAX_SEARCH_DEPTH) */
820Sstevel@tonic-gate
830Sstevel@tonic-gate found = srch_dir(device, srch_dirs[dirno],
840Sstevel@tonic-gate ((strcmp(srch_dirs[dirno], dev_dir) == 0) ?
850Sstevel@tonic-gate MAX_SRCH_DEPTH : 1),
860Sstevel@tonic-gate NULL);
870Sstevel@tonic-gate dirno++;
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate /* if not yet found search remaining /dev directory skipping the
910Sstevel@tonic-gate priority directories */
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (!found)
940Sstevel@tonic-gate found = srch_dir(device, dev_dir, 0, srch_dirs);
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* if found then put it (without the "/dev/" prefix) in the tlist
970Sstevel@tonic-gate structure and return the path name without the "/dev/" prefix */
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (found) {
1000Sstevel@tonic-gate if (tsize1 < TSIZE1) {
1010Sstevel@tonic-gate tp->tdev = device;
1020Sstevel@tonic-gate CPYN(tp->tname, file_name+5);
1030Sstevel@tonic-gate tsize1++;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate return(file_name+5);
1060Sstevel@tonic-gate } else
1070Sstevel@tonic-gate /* if not found put "?" in the tlist structure for that device and
1080Sstevel@tonic-gate return "?" */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (tsize1 < TSIZE1) {
1110Sstevel@tonic-gate tp->tdev = device;
1120Sstevel@tonic-gate CPYN(tp->tname, "?");
1130Sstevel@tonic-gate tsize1++;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate return("?");
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static int
srch_dir(device,path,depth,skip_dirs)1200Sstevel@tonic-gate srch_dir(device, path, depth, skip_dirs)
1210Sstevel@tonic-gate dev_t device; /* device we are looking for */
1220Sstevel@tonic-gate char *path; /* current path */
1230Sstevel@tonic-gate int depth; /* current depth */
1240Sstevel@tonic-gate char *skip_dirs[]; /* directories that don't need searched */
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate DIR *fdev;
1270Sstevel@tonic-gate struct dirent *d;
1280Sstevel@tonic-gate int dirno = 0;
1290Sstevel@tonic-gate int found = 0;
1300Sstevel@tonic-gate int path_len;
1310Sstevel@tonic-gate char *last_comp;
1320Sstevel@tonic-gate struct stat sb;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /* do we need to search this directory? */
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if ((skip_dirs != NULL) && (depth != 0))
1370Sstevel@tonic-gate while (skip_dirs[dirno] != NULL)
1380Sstevel@tonic-gate if (strcmp(skip_dirs[dirno++], path) == 0)
1390Sstevel@tonic-gate return(0);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /* open the directory */
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if ((fdev = opendir(path)) == NULL)
1450Sstevel@tonic-gate return(0);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* initialize file name using path name */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate path_len = strlen(path);
1500Sstevel@tonic-gate strcpy(file_name, path);
1510Sstevel@tonic-gate last_comp = file_name + path_len;
1520Sstevel@tonic-gate *last_comp++ = '/';
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /* start searching this directory */
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate while ((!found) && ((d = readdir(fdev)) != NULL))
1570Sstevel@tonic-gate if (d->d_ino != 0) {
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate /* if name would not be too long append it to
1600Sstevel@tonic-gate directory name, otherwise skip this entry */
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if ((int) (path_len + strlen(d->d_name) + 2) > MAX_DEV_PATH)
1630Sstevel@tonic-gate continue;
1640Sstevel@tonic-gate else
1650Sstevel@tonic-gate strcpy(last_comp, d->d_name);
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /* if this directory entry has the device number we need,
1680Sstevel@tonic-gate then the name is found. Otherwise if it's a directory
1690Sstevel@tonic-gate (not . or ..) and we haven't gone too deep, recurse. */
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (lintodev(file_name+5) == device) {
1720Sstevel@tonic-gate found = 1;
1730Sstevel@tonic-gate break;
1740Sstevel@tonic-gate } else if ((depth < MAX_SRCH_DEPTH) &&
1750Sstevel@tonic-gate (strcmp(d->d_name, ".") != 0) &&
1760Sstevel@tonic-gate (strcmp(d->d_name, "..") != 0) &&
1770Sstevel@tonic-gate (stat(file_name, &sb) != -1) &&
1780Sstevel@tonic-gate ((sb.st_mode & S_IFMT) == S_IFDIR))
1790Sstevel@tonic-gate found = srch_dir(device, file_name, depth+1, skip_dirs);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate closedir(fdev);
1820Sstevel@tonic-gate return(found);
1830Sstevel@tonic-gate }
184