|
|
1.1 ! root 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: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #if defined(LIBC_SCCS) && !defined(lint) ! 21: static char sccsid[] = "@(#)devname.c 5.5 (Berkeley) 5/31/90"; ! 22: #endif /* LIBC_SCCS and not lint */ ! 23: ! 24: #include <sys/types.h> ! 25: #include <sys/stat.h> ! 26: #include <sys/file.h> ! 27: #include <dirent.h> ! 28: #include <paths.h> ! 29: ! 30: static struct devs { ! 31: struct devs *next; ! 32: dev_t dev; ! 33: char name[MAXNAMLEN+1]; ! 34: mode_t type; ! 35: }; ! 36: ! 37: #define hash(x) ((x)&0xff) ! 38: static struct devs *devhash[0xff]; ! 39: ! 40: static int devinit; ! 41: ! 42: char * ! 43: devname(dev, type) ! 44: dev_t dev; ! 45: mode_t type; ! 46: { ! 47: struct devs *devp; ! 48: ! 49: if (devinit == 0) { ! 50: register struct devs *devpp; ! 51: register struct dirent *entry; ! 52: struct stat sb; ! 53: DIR *dp = opendir(_PATH_DEV); ! 54: int savewd = open(".", O_RDONLY, 0); ! 55: mode_t specialtype; ! 56: ! 57: if (savewd == -1 || dp == NULL || chdir(_PATH_DEV) == -1) ! 58: return (NULL); ! 59: while ((entry = readdir(dp)) != NULL) { ! 60: if (stat(entry->d_name, &sb) == -1) ! 61: continue; ! 62: switch(sb.st_mode&S_IFMT) { ! 63: case S_IFCHR: ! 64: specialtype = S_IFCHR; ! 65: break; ! 66: case S_IFBLK: ! 67: specialtype = S_IFBLK; ! 68: break; ! 69: default: ! 70: continue; ! 71: } ! 72: devp = (struct devs *)malloc(sizeof (struct devs)); ! 73: if (devp == NULL) ! 74: return (NULL); ! 75: devp->type = specialtype; ! 76: devp->dev = sb.st_rdev; ! 77: strcpy(devp->name, entry->d_name); ! 78: devp->next = NULL; ! 79: if ((devpp = devhash[hash(sb.st_rdev)]) == NULL) ! 80: devhash[hash(sb.st_rdev)] = devp; ! 81: else { ! 82: for (;devpp->next != NULL; devpp = devpp->next) ! 83: ; ! 84: devpp->next = devp; ! 85: } ! 86: } ! 87: fchdir(savewd); ! 88: close(savewd); ! 89: closedir(dp); ! 90: devinit = 1; ! 91: } ! 92: for (devp = devhash[hash(dev)]; devp != NULL; devp = devp->next) ! 93: if (dev == devp->dev && type == devp->type) ! 94: return(devp->name); ! 95: ! 96: return (NULL); ! 97: } ! 98: ! 99: #ifdef TEST ! 100: main() { ! 101: printf(" %s \n", devname(0)); ! 102: } ! 103: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.