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