xref: /csrg-svn/lib/libc/gen/devname.c (revision 40971)
140966Smarc /*
240966Smarc  * Copyright (c) 1989 The Regents of the University of California.
340966Smarc  * All rights reserved.
440966Smarc  *
540966Smarc  * Redistribution and use in source and binary forms are permitted
640966Smarc  * provided that the above copyright notice and this paragraph are
740966Smarc  * duplicated in all such forms and that any documentation,
840966Smarc  * advertising materials, and other materials related to such
940966Smarc  * distribution and use acknowledge that the software was developed
1040966Smarc  * by the University of California, Berkeley.  The name of the
1140966Smarc  * University may not be used to endorse or promote products derived
1240966Smarc  * from this software without specific prior written permission.
1340966Smarc  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1440966Smarc  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1540966Smarc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1640966Smarc  */
1740966Smarc 
1840966Smarc #if defined(LIBC_SCCS) && !defined(lint)
19*40971Smarc static char sccsid[] = "@(#)devname.c	5.3 (Berkeley) 04/18/90";
2040966Smarc #endif /* LIBC_SCCS and not lint */
2140966Smarc 
2240258Smarc #include <sys/types.h>
2340258Smarc #include <sys/stat.h>
2440966Smarc #include <sys/file.h>
2540966Smarc #include <dirent.h>
2640966Smarc #include <paths.h>
2740258Smarc 
2840966Smarc static struct devs {
2940966Smarc 	struct	devs *next;
3040258Smarc 	dev_t	dev;
3140258Smarc 	char	name[MAXNAMLEN+1];
32*40971Smarc 	mode_t	type;
3340258Smarc };
3440258Smarc 
3540966Smarc #define	hash(x)	((x)&0xff)
3640966Smarc static struct devs *devhash[minor(~0)];
3740258Smarc 
3840966Smarc static int devinit;
3940258Smarc 
4040258Smarc char *
4140966Smarc devname(dev, type)
4240258Smarc 	dev_t dev;
43*40971Smarc 	mode_t type;
4440258Smarc {
4540258Smarc 	struct devs *devp;
4640258Smarc 
4740258Smarc 	if (devinit == 0) {
4840966Smarc 		register struct devs *devpp;
4940966Smarc 		register struct dirent *entry;
5040966Smarc 		struct stat sb;
5140258Smarc 		DIR *dp = opendir(_PATH_DEV);
5240966Smarc 		int savewd = open(".", O_RDONLY, 0);
53*40971Smarc 		mode_t specialtype;
5440258Smarc 
5540966Smarc 		if (savewd == -1 || dp == NULL || chdir(_PATH_DEV) == -1)
5640258Smarc 			return (NULL);
5740258Smarc 		while ((entry = readdir(dp)) != NULL) {
5840258Smarc 			if (stat(entry->d_name, &sb) == -1)
5940258Smarc 				continue;
6040966Smarc 			switch(sb.st_mode&S_IFMT) {
6140966Smarc 			case S_IFCHR:
62*40971Smarc 				specialtype = S_IFCHR;
6340966Smarc 				break;
6440966Smarc 			case S_IFBLK:
65*40971Smarc 				specialtype = S_IFBLK;
6640966Smarc 				break;
6740966Smarc 			default:
6840258Smarc 				continue;
6940966Smarc 			}
7040258Smarc 			devp = (struct devs *)malloc(sizeof (struct devs));
7140258Smarc 			if (devp == NULL)
7240258Smarc 				return (NULL);
7340966Smarc 			devp->type = specialtype;
7440258Smarc 			devp->dev = sb.st_rdev;
7540258Smarc 			strcpy(devp->name, entry->d_name);
7640258Smarc 			devp->next = NULL;
7740966Smarc 			if ((devpp = devhash[hash(sb.st_rdev)]) == NULL)
7840966Smarc 				devhash[hash(sb.st_rdev)] = devp;
7940258Smarc 			else {
8040258Smarc 				for (;devpp->next != NULL; devpp = devpp->next)
8140258Smarc 					;
8240258Smarc 				devpp->next = devp;
8340258Smarc 			}
8440258Smarc 		}
8540966Smarc 		fchdir(savewd);
8640966Smarc 		close(savewd);
8740966Smarc 		closedir(dp);
8840966Smarc 		devinit = 1;
8940258Smarc 	}
9040966Smarc 	for (devp = devhash[hash(dev)]; devp != NULL; devp = devp->next)
9140966Smarc 		if (dev == devp->dev && type == devp->type)
9240258Smarc 			return(devp->name);
9340258Smarc 
9440258Smarc 	return (NULL);
9540258Smarc }
9640258Smarc 
9740258Smarc #ifdef TEST
9840258Smarc main() {
9940258Smarc 	printf(" %s \n", devname(0));
10040258Smarc }
10140258Smarc #endif
102