devname.c revision 298226
13229Spst/* 23229Spst * Copyright (c) 1989, 1993 33229Spst * The Regents of the University of California. All rights reserved. 43229Spst * 53229Spst * Redistribution and use in source and binary forms, with or without 63229Spst * modification, are permitted provided that the following conditions 73229Spst * are met: 83229Spst * 1. Redistributions of source code must retain the above copyright 93229Spst * notice, this list of conditions and the following disclaimer. 103229Spst * 2. Redistributions in binary form must reproduce the above copyright 113229Spst * notice, this list of conditions and the following disclaimer in the 123229Spst * documentation and/or other materials provided with the distribution. 133229Spst * 4. Neither the name of the University nor the names of its contributors 143229Spst * may be used to endorse or promote products derived from this software 153229Spst * without specific prior written permission. 163229Spst * 173229Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 183229Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 193229Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 203229Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 213229Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 223229Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 233229Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 243229Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 253229Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 263229Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 273229Spst * SUCH DAMAGE. 283229Spst */ 293229Spst 303229Spst#if defined(LIBC_SCCS) && !defined(lint) 313229Spststatic char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; 323229Spst#endif /* LIBC_SCCS and not lint */ 333229Spst#include <sys/cdefs.h> 343229Spst__FBSDID("$FreeBSD: head/lib/libc/gen/devname.c 298226 2016-04-18 21:05:15Z avos $"); 353229Spst 363229Spst#include <sys/param.h> 373229Spst#include <sys/sysctl.h> 383229Spst 393229Spst#include <stdio.h> 403229Spst#include <stdint.h> 413229Spst#include <stdlib.h> 423229Spst#include <string.h> 433229Spst#include <sys/stat.h> 443229Spst 453229Spstchar * 463229Spstdevname_r(dev_t dev, mode_t type, char *buf, int len) 473229Spst{ 483229Spst int i; 493229Spst size_t j; 503229Spst 513229Spst if (dev == NODEV || !(S_ISCHR(type) || S_ISBLK(dev))) { 523229Spst strlcpy(buf, "#NODEV", len); 533229Spst return (buf); 543229Spst } 553229Spst 563229Spst if (S_ISCHR(type)) { 573229Spst j = len; 583229Spst i = sysctlbyname("kern.devname", buf, &j, &dev, sizeof (dev)); 593229Spst if (i == 0) 603229Spst return (buf); 613229Spst } 623229Spst 633229Spst /* Finally just format it */ 643229Spst snprintf(buf, len, "#%c:%#jx", 653229Spst S_ISCHR(type) ? 'C' : 'B', (uintmax_t)dev); 663229Spst return (buf); 673229Spst} 683229Spst 693229Spstchar * 703229Spstdevname(dev_t dev, mode_t type) 713229Spst{ 723229Spst static char buf[SPECNAMELEN + 1]; 733229Spst 743229Spst return (devname_r(dev, type, buf, sizeof(buf))); 753229Spst} 763229Spst