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.4 (Berkeley) 05/03/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 mode_t type; 33 }; 34 35 #define hash(x) ((x)&0xff) 36 static struct devs *devhash[0xff]; 37 38 static int devinit; 39 40 char * 41 devname(dev, type) 42 dev_t dev; 43 mode_t type; 44 { 45 struct devs *devp; 46 47 if (devinit == 0) { 48 register struct devs *devpp; 49 register struct dirent *entry; 50 struct stat sb; 51 DIR *dp = opendir(_PATH_DEV); 52 int savewd = open(".", O_RDONLY, 0); 53 mode_t specialtype; 54 55 if (savewd == -1 || dp == NULL || chdir(_PATH_DEV) == -1) 56 return (NULL); 57 while ((entry = readdir(dp)) != NULL) { 58 if (stat(entry->d_name, &sb) == -1) 59 continue; 60 switch(sb.st_mode&S_IFMT) { 61 case S_IFCHR: 62 specialtype = S_IFCHR; 63 break; 64 case S_IFBLK: 65 specialtype = S_IFBLK; 66 break; 67 default: 68 continue; 69 } 70 devp = (struct devs *)malloc(sizeof (struct devs)); 71 if (devp == NULL) 72 return (NULL); 73 devp->type = specialtype; 74 devp->dev = sb.st_rdev; 75 strcpy(devp->name, entry->d_name); 76 devp->next = NULL; 77 if ((devpp = devhash[hash(sb.st_rdev)]) == NULL) 78 devhash[hash(sb.st_rdev)] = devp; 79 else { 80 for (;devpp->next != NULL; devpp = devpp->next) 81 ; 82 devpp->next = devp; 83 } 84 } 85 fchdir(savewd); 86 close(savewd); 87 closedir(dp); 88 devinit = 1; 89 } 90 for (devp = devhash[hash(dev)]; devp != NULL; devp = devp->next) 91 if (dev == devp->dev && type == devp->type) 92 return(devp->name); 93 94 return (NULL); 95 } 96 97 #ifdef TEST 98 main() { 99 printf(" %s \n", devname(0)); 100 } 101 #endif 102