1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)devname.c 5.2 (Berkeley) 04/18/90"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include <sys/file.h> 25 #include <dirent.h> 26 #include <paths.h> 27 28 static struct devs { 29 struct devs *next; 30 dev_t dev; 31 char name[MAXNAMLEN+1]; 32 char type; 33 }; 34 35 #define hash(x) ((x)&0xff) 36 static struct devs *devhash[minor(~0)]; 37 38 static int devinit; 39 40 char * 41 devname(dev, type) 42 dev_t dev; 43 { 44 struct devs *devp; 45 46 if (devinit == 0) { 47 register struct devs *devpp; 48 register struct dirent *entry; 49 struct stat sb; 50 DIR *dp = opendir(_PATH_DEV); 51 int savewd = open(".", O_RDONLY, 0); 52 int specialtype; 53 54 if (savewd == -1 || dp == NULL || chdir(_PATH_DEV) == -1) 55 return (NULL); 56 while ((entry = readdir(dp)) != NULL) { 57 if (stat(entry->d_name, &sb) == -1) 58 continue; 59 switch(sb.st_mode&S_IFMT) { 60 case S_IFCHR: 61 specialtype = 1; 62 break; 63 case S_IFBLK: 64 specialtype = 0; 65 break; 66 default: 67 continue; 68 } 69 devp = (struct devs *)malloc(sizeof (struct devs)); 70 if (devp == NULL) 71 return (NULL); 72 devp->type = specialtype; 73 devp->dev = sb.st_rdev; 74 strcpy(devp->name, entry->d_name); 75 devp->next = NULL; 76 if ((devpp = devhash[hash(sb.st_rdev)]) == NULL) 77 devhash[hash(sb.st_rdev)] = devp; 78 else { 79 for (;devpp->next != NULL; devpp = devpp->next) 80 ; 81 devpp->next = devp; 82 } 83 } 84 fchdir(savewd); 85 close(savewd); 86 closedir(dp); 87 devinit = 1; 88 } 89 for (devp = devhash[hash(dev)]; devp != NULL; devp = devp->next) 90 if (dev == devp->dev && type == devp->type) 91 return(devp->name); 92 93 return (NULL); 94 } 95 96 #ifdef TEST 97 main() { 98 printf(" %s \n", devname(0)); 99 } 100 #endif 101