lobase

Linux port of OpenBSDs userland.
Log | Files | Refs | README

commit 41baeebc284edb331f96fb43abe25f533a0ea1f3
parent 8802ca1f8106321a86224f78d73f0141fe22356d
Author: Duncaen <mail@duncano.de>
Date:   Fri, 19 May 2017 18:14:21 +0200

lib/libopenbsd: add devname(3)

Diffstat:
include/stdlib.h | 2+-
lib/libopenbsd/gen/Makefile.inc | 7++++---
lib/libopenbsd/gen/devname.3 | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/gen/devname.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 182 insertions(+), 4 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h @@ -42,7 +42,7 @@ uint32_t arc4random(void); uint32_t arc4random_uniform(uint32_t); void arc4random_buf(void *, size_t) __attribute__((__bounded__ (__string__,1,2))); - +char *devname(dev_t, mode_t); void *reallocarray(void *optr, size_t nmemb, size_t size); int heapsort(void *, size_t, size_t, int (*)(const void *, const void *)); int mergesort(void *, size_t, size_t, int (*)(const void *, const void *)); diff --git a/lib/libopenbsd/gen/Makefile.inc b/lib/libopenbsd/gen/Makefile.inc @@ -3,8 +3,9 @@ # stdio sources VPATH+= ${LIBCSRCDIR}/gen -SRCS+= basename.c dirname.c errc.c fts.c getmntinfo.c glob.c setmode.c \ - signame.c unvis.c verrc.c vis.c vwarnc.c warnc.c +SRCS+= basename.c devname.c dirname.c errc.c fts.c getmntinfo.c glob.c \ + setmode.c signame.c unvis.c verrc.c vis.c vwarnc.c warnc.c -MAN+= basename.3 dirname.3 fts.3 getmntinfo.3 glob.3 setmode.3 unvis.3 vis.3 +MAN+= basename.3 devname.3 dirname.3 fts.3 getmntinfo.3 glob.3 setmode.3 \ + unvis.3 vis.3 diff --git a/lib/libopenbsd/gen/devname.3 b/lib/libopenbsd/gen/devname.3 @@ -0,0 +1,66 @@ +.\" $OpenBSD: devname.3,v 1.8 2013/06/05 03:39:22 tedu Exp $ +.\" +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd $Mdocdate: June 5 2013 $ +.Dt DEVNAME 3 +.Os +.Sh NAME +.Nm devname +.Nd get device name +.Sh SYNOPSIS +.In sys/stat.h +.In stdlib.h +.Ft char * +.Fn devname "dev_t dev" "mode_t type" +.Sh DESCRIPTION +The +.Fn devname +function returns a pointer to the name of the block or character +device in +.Pa /dev +with a device number of +.Fa dev , +and a file type matching the one encoded in +.Fa type +which must be one of +.Dv S_IFBLK +or +.Dv S_IFCHR . +If no device matches the specified values, or no information is +available, a pointer to the string +.Qq ?? +is returned. +.Sh SEE ALSO +.Xr stat 2 , +.Xr dev_mkdb 8 +.Sh HISTORY +The +.Nm devname +function call appeared in +.Bx 4.4 . diff --git a/lib/libopenbsd/gen/devname.c b/lib/libopenbsd/gen/devname.c @@ -0,0 +1,111 @@ +/* $OpenBSD: devname.c,v 1.13 2016/07/06 04:35:12 guenther Exp $ */ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/stat.h> +#include <sys/types.h> + +#if 0 +#include <db.h> +#endif +#include <dirent.h> +#include <fcntl.h> +#include <limits.h> +#include <paths.h> +#include <stdbool.h> +#include <string.h> +#include <stdlib.h> + +static char * +devname_nodb(dev_t dev, mode_t type) +{ + static char buf[NAME_MAX + 1]; + char *name = NULL; + struct dirent *dp; + struct stat sb; + DIR *dirp; + + if ((dirp = opendir(_PATH_DEV)) == NULL) + return (NULL); + while ((dp = readdir(dirp)) != NULL) { + if (dp->d_type != DT_UNKNOWN && DTTOIF(dp->d_type) != type) + continue; + if (fstatat(dirfd(dirp), dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) + || sb.st_rdev != dev || (sb.st_mode & S_IFMT) != type) + continue; + strlcpy(buf, dp->d_name, sizeof(buf)); + name = buf; + break; + } + closedir(dirp); + return (name); +} + +/* + * Keys in dev.db are a mode_t followed by a dev_t. The former is the + * type of the file (mode & S_IFMT), the latter is the st_rdev field. + * Note that the structure may contain padding. + */ +char * +devname(dev_t dev, mode_t type) +{ +#if 0 + static DB *db; + static bool failure; + struct { + mode_t type; + dev_t dev; + } bkey; + DBT data, key; + char *name = NULL; + + if (!db && !failure) { + if (!(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) + failure = true; + } + if (!failure) { + /* Be sure to clear any padding that may be found in bkey. */ + memset(&bkey, 0, sizeof(bkey)); + bkey.dev = dev; + bkey.type = type; + key.data = &bkey; + key.size = sizeof(bkey); + if ((db->get)(db, &key, &data, 0) == 0) + name = data.data; + } else { + name = devname_nodb(dev, type); + } + return (name ? name : "??"); +#else + char *name = NULL; + name = devname_nodb(dev, type); + return (name ? name : "??"); +#endif +} +DEF_WEAK(devname);