Home | History | Annotate | Line # | Download | only in libutil
getdiskrawname.c revision 1.3
      1 /*	$NetBSD: getdiskrawname.c,v 1.3 2014/09/12 07:59:36 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: getdiskrawname.c,v 1.3 2014/09/12 07:59:36 mlelstv Exp $");
     33 
     34 #include <sys/stat.h>
     35 
     36 #include <stdio.h>
     37 #include <string.h>
     38 #include <errno.h>
     39 #include <util.h>
     40 #include <limits.h>
     41 #include <unistd.h>
     42 
     43 static const char *
     44 resolve_link(char *buf, size_t bufsiz, const char *name)
     45 {
     46 	const char *dp;
     47 	ssize_t nlen, dlen;
     48 
     49 	dlen = readlink(name, buf, bufsiz-1);
     50 	if (dlen == -1)
     51 		return name;
     52 
     53 	buf[dlen] = '\0';
     54 
     55 	if (buf[0] != '/') {
     56 		dp = strrchr(name, '/');
     57 		if (dp != NULL) {
     58 			nlen = dp - name + 1;
     59 			if (nlen + 1 > PATH_MAX)
     60 				return NULL;
     61 			if (nlen + dlen + 1 > PATH_MAX)
     62 				return NULL;
     63 
     64 			memmove(buf+nlen, buf, dlen+1);
     65 			memcpy(buf, name, nlen);
     66 		}
     67 	}
     68 
     69 	return buf;
     70 }
     71 
     72 const char *
     73 getdiskrawname(char *buf, size_t bufsiz, const char *name)
     74 {
     75 	const char *dp;
     76 	struct stat st;
     77 	char dest[PATH_MAX];
     78 
     79 	if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
     80 		errno = EINVAL;
     81 		return NULL;
     82 	}
     83 
     84 	dp = strrchr(name, '/');
     85 
     86 	if (stat(name, &st) == -1)
     87 		return NULL;
     88 
     89 	if (!S_ISBLK(st.st_mode)) {
     90 		errno = EFTYPE;
     91 		return NULL;
     92 	}
     93 
     94 	if (dp != NULL)
     95 		(void)snprintf(buf, bufsiz, "%.*s/r%s", (int)(dp - name), name, dp + 1);
     96 	else
     97 		(void)snprintf(buf, bufsiz, "r%s", name);
     98 
     99 	return buf;
    100 }
    101 
    102 const char *
    103 getdiskcookedname(char *buf, size_t bufsiz, const char *name)
    104 {
    105 	const char *dp;
    106 	struct stat st;
    107 	char dest[PATH_MAX];
    108 
    109 	if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
    110 		errno = EINVAL;
    111 		return NULL;
    112 	}
    113 
    114 	dp = strrchr(name, '/');
    115 
    116 	if ((dp != NULL && dp[1] != 'r') || (dp == NULL && name[0] != 'r')) {
    117 		errno = EINVAL;
    118 		return NULL;
    119 	}
    120 
    121 	if (stat(name, &st) == -1)
    122 		return NULL;
    123 
    124 	if (!S_ISCHR(st.st_mode)) {
    125 		errno = EFTYPE;
    126 		return NULL;
    127 	}
    128 
    129 	if (dp != NULL)
    130 		(void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2);
    131 	else
    132 		(void)snprintf(buf, bufsiz, "%s", name + 1);
    133 
    134 	return buf;
    135 }
    136