xref: /csrg-svn/lib/libc/gen/devname.c (revision 40966)
1*40966Smarc /*
2*40966Smarc  * Copyright (c) 1989 The Regents of the University of California.
3*40966Smarc  * All rights reserved.
4*40966Smarc  *
5*40966Smarc  * Redistribution and use in source and binary forms are permitted
6*40966Smarc  * provided that the above copyright notice and this paragraph are
7*40966Smarc  * duplicated in all such forms and that any documentation,
8*40966Smarc  * advertising materials, and other materials related to such
9*40966Smarc  * distribution and use acknowledge that the software was developed
10*40966Smarc  * by the University of California, Berkeley.  The name of the
11*40966Smarc  * University may not be used to endorse or promote products derived
12*40966Smarc  * from this software without specific prior written permission.
13*40966Smarc  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*40966Smarc  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*40966Smarc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*40966Smarc  */
17*40966Smarc 
18*40966Smarc #if defined(LIBC_SCCS) && !defined(lint)
19*40966Smarc static char sccsid[] = "@(#)devname.c	5.2 (Berkeley) 04/18/90";
20*40966Smarc #endif /* LIBC_SCCS and not lint */
21*40966Smarc 
2240258Smarc #include <sys/types.h>
2340258Smarc #include <sys/stat.h>
24*40966Smarc #include <sys/file.h>
25*40966Smarc #include <dirent.h>
26*40966Smarc #include <paths.h>
2740258Smarc 
28*40966Smarc static struct devs {
29*40966Smarc 	struct	devs *next;
3040258Smarc 	dev_t	dev;
3140258Smarc 	char	name[MAXNAMLEN+1];
32*40966Smarc 	char	type;
3340258Smarc };
3440258Smarc 
35*40966Smarc #define	hash(x)	((x)&0xff)
36*40966Smarc static struct devs *devhash[minor(~0)];
3740258Smarc 
38*40966Smarc static int devinit;
3940258Smarc 
4040258Smarc char *
41*40966Smarc devname(dev, type)
4240258Smarc 	dev_t dev;
4340258Smarc {
4440258Smarc 	struct devs *devp;
4540258Smarc 
4640258Smarc 	if (devinit == 0) {
47*40966Smarc 		register struct devs *devpp;
48*40966Smarc 		register struct dirent *entry;
49*40966Smarc 		struct stat sb;
5040258Smarc 		DIR *dp = opendir(_PATH_DEV);
51*40966Smarc 		int savewd = open(".", O_RDONLY, 0);
52*40966Smarc 		int specialtype;
5340258Smarc 
54*40966Smarc 		if (savewd == -1 || dp == NULL || chdir(_PATH_DEV) == -1)
5540258Smarc 			return (NULL);
5640258Smarc 		while ((entry = readdir(dp)) != NULL) {
5740258Smarc 			if (stat(entry->d_name, &sb) == -1)
5840258Smarc 				continue;
59*40966Smarc 			switch(sb.st_mode&S_IFMT) {
60*40966Smarc 			case S_IFCHR:
61*40966Smarc 				specialtype = 1;
62*40966Smarc 				break;
63*40966Smarc 			case S_IFBLK:
64*40966Smarc 				specialtype = 0;
65*40966Smarc 				break;
66*40966Smarc 			default:
6740258Smarc 				continue;
68*40966Smarc 			}
6940258Smarc 			devp = (struct devs *)malloc(sizeof (struct devs));
7040258Smarc 			if (devp == NULL)
7140258Smarc 				return (NULL);
72*40966Smarc 			devp->type = specialtype;
7340258Smarc 			devp->dev = sb.st_rdev;
7440258Smarc 			strcpy(devp->name, entry->d_name);
7540258Smarc 			devp->next = NULL;
76*40966Smarc 			if ((devpp = devhash[hash(sb.st_rdev)]) == NULL)
77*40966Smarc 				devhash[hash(sb.st_rdev)] = devp;
7840258Smarc 			else {
7940258Smarc 				for (;devpp->next != NULL; devpp = devpp->next)
8040258Smarc 					;
8140258Smarc 				devpp->next = devp;
8240258Smarc 			}
8340258Smarc 		}
84*40966Smarc 		fchdir(savewd);
85*40966Smarc 		close(savewd);
86*40966Smarc 		closedir(dp);
87*40966Smarc 		devinit = 1;
8840258Smarc 	}
89*40966Smarc 	for (devp = devhash[hash(dev)]; devp != NULL; devp = devp->next)
90*40966Smarc 		if (dev == devp->dev && type == devp->type)
9140258Smarc 			return(devp->name);
9240258Smarc 
9340258Smarc 	return (NULL);
9440258Smarc }
9540258Smarc 
9640258Smarc #ifdef TEST
9740258Smarc main() {
9840258Smarc 	printf(" %s \n", devname(0));
9940258Smarc }
10040258Smarc #endif
101