1662909a7SPeter Wemm /* 2662909a7SPeter Wemm * Copyright (c) 1989, 1993 3662909a7SPeter Wemm * The Regents of the University of California. All rights reserved. 4662909a7SPeter Wemm * 5662909a7SPeter Wemm * Redistribution and use in source and binary forms, with or without 6662909a7SPeter Wemm * modification, are permitted provided that the following conditions 7662909a7SPeter Wemm * are met: 8662909a7SPeter Wemm * 1. Redistributions of source code must retain the above copyright 9662909a7SPeter Wemm * notice, this list of conditions and the following disclaimer. 10662909a7SPeter Wemm * 2. Redistributions in binary form must reproduce the above copyright 11662909a7SPeter Wemm * notice, this list of conditions and the following disclaimer in the 12662909a7SPeter Wemm * documentation and/or other materials provided with the distribution. 13662909a7SPeter Wemm * 3. All advertising materials mentioning features or use of this software 14662909a7SPeter Wemm * must display the following acknowledgement: 15662909a7SPeter Wemm * This product includes software developed by the University of 16662909a7SPeter Wemm * California, Berkeley and its contributors. 17662909a7SPeter Wemm * 4. Neither the name of the University nor the names of its contributors 18662909a7SPeter Wemm * may be used to endorse or promote products derived from this software 19662909a7SPeter Wemm * without specific prior written permission. 20662909a7SPeter Wemm * 21662909a7SPeter Wemm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22662909a7SPeter Wemm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23662909a7SPeter Wemm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24662909a7SPeter Wemm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25662909a7SPeter Wemm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26662909a7SPeter Wemm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27662909a7SPeter Wemm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28662909a7SPeter Wemm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29662909a7SPeter Wemm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30662909a7SPeter Wemm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31662909a7SPeter Wemm * SUCH DAMAGE. 32662909a7SPeter Wemm */ 33662909a7SPeter Wemm 34662909a7SPeter Wemm #if defined(LIBC_SCCS) && !defined(lint) 35662909a7SPeter Wemm static char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; 36662909a7SPeter Wemm #endif /* LIBC_SCCS and not lint */ 37ea8d448aSDavid E. O'Brien #include <sys/cdefs.h> 38ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39662909a7SPeter Wemm 40662909a7SPeter Wemm #include <sys/types.h> 418d25eb2cSPoul-Henning Kamp #include <sys/sysctl.h> 42662909a7SPeter Wemm 43662909a7SPeter Wemm #include <err.h> 44662909a7SPeter Wemm #include <fcntl.h> 45662909a7SPeter Wemm #include <stdio.h> 46662909a7SPeter Wemm #include <string.h> 473cea5952SPoul-Henning Kamp #include <sys/param.h> 489d4ee2f2SPoul-Henning Kamp #include <sys/stat.h> 49662909a7SPeter Wemm 509d4ee2f2SPoul-Henning Kamp char * 51529ac587SPoul-Henning Kamp devname_r(dev_t dev, mode_t type, char *buf, int len) 529d4ee2f2SPoul-Henning Kamp { 53d35bce02SThomas Moestl int i; 54d35bce02SThomas Moestl size_t j; 559d4ee2f2SPoul-Henning Kamp char *r; 569d4ee2f2SPoul-Henning Kamp 578d25eb2cSPoul-Henning Kamp if ((type & S_IFMT) == S_IFCHR) { 58529ac587SPoul-Henning Kamp j = len; 598d25eb2cSPoul-Henning Kamp i = sysctlbyname("kern.devname", buf, &j, &dev, sizeof (dev)); 608d25eb2cSPoul-Henning Kamp if (i == 0) 618d25eb2cSPoul-Henning Kamp return (buf); 628d25eb2cSPoul-Henning Kamp } 638d25eb2cSPoul-Henning Kamp 648d25eb2cSPoul-Henning Kamp /* Finally just format it */ 65c6dd496cSPoul-Henning Kamp if (dev == NODEV) 66c6dd496cSPoul-Henning Kamp r = "#NODEV"; 673cea5952SPoul-Henning Kamp else 68c6dd496cSPoul-Henning Kamp r = "#%c:%d:0x%x"; 69529ac587SPoul-Henning Kamp snprintf(buf, len, r, 703cea5952SPoul-Henning Kamp (type & S_IFMT) == S_IFCHR ? 'C' : 'B', major(dev), minor(dev)); 713cea5952SPoul-Henning Kamp return (buf); 729d4ee2f2SPoul-Henning Kamp } 73529ac587SPoul-Henning Kamp 74529ac587SPoul-Henning Kamp char * 75529ac587SPoul-Henning Kamp devname(dev_t dev, mode_t type) 76529ac587SPoul-Henning Kamp { 77529ac587SPoul-Henning Kamp static char buf[SPECNAMELEN + 1]; 78529ac587SPoul-Henning Kamp 79529ac587SPoul-Henning Kamp return(devname_r(dev, type, buf, sizeof(buf))); 80529ac587SPoul-Henning Kamp } 81