xref: /netbsd/lib/libutil/opendisk.c (revision 1492a4e5)
1 /*	$NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Luke Mewburn.
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 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 __RCSID("$NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $");
39 #endif
40 
41 #include <sys/param.h>
42 
43 #include <assert.h>
44 #include <errno.h>
45 #include <stdarg.h>
46 #include <fcntl.h>
47 #ifndef HAVE_NBTOOL_CONFIG_H
48 #include <util.h>
49 #include <paths.h>
50 #else
51 #include "opendisk.h"
52 #endif
53 #include <stdio.h>
54 #include <string.h>
55 
56 static int __printflike(5, 6)
opd(char * buf,size_t len,int (* ofn)(const char *,int,...),int flags,const char * fmt,...)57 opd(char *buf, size_t len, int (*ofn)(const char *, int, ...),
58     int flags, const char *fmt, ...)
59 {
60 	va_list ap;
61 
62 	va_start(ap, fmt);
63 	vsnprintf(buf, len, fmt, ap);
64 	va_end(ap);
65 
66 	return (*ofn)(buf, flags, 0);
67 }
68 
69 static int
__opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))70 __opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
71 	int (*ofn)(const char *, int, ...))
72 {
73 	int f, part;
74 
75 	if (buf == NULL) {
76 		errno = EFAULT;
77 		return -1;
78 	}
79 
80 	if ((flags & O_CREAT) != 0) {
81 		errno = EINVAL;
82 		return -1;
83 	}
84 
85 	part = getrawpartition();
86 	if (part < 0)
87 		return -1;	/* sysctl(3) in getrawpartition sets errno */
88 	part += 'a';
89 
90 	/*
91 	 * If we are passed a plain name, first try /dev to avoid accidents
92 	 * with files in the same directory that happen to match disk names.
93 	 */
94 	if (strchr(path, '/') == NULL) {
95 		const char *r = iscooked ? "" : "r";
96 		const char *d = _PATH_DEV;
97 
98 		f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
99 		if (f != -1 || errno != ENOENT)
100 			return f;
101 
102 		f = opd(buf, buflen, ofn, flags, "%s%s%s%c", d, r, path, part);
103 		if (f != -1 || errno != ENOENT)
104 			return f;
105 	}
106 
107 	f = opd(buf, buflen, ofn, flags, "%s", path);
108 	if (f != -1 || errno != ENOENT)
109 		return f;
110 
111 	return opd(buf, buflen, ofn, flags, "%s%c", path, part);
112 }
113 
114 int
opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked)115 opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
116 {
117 
118 	return __opendisk(path, flags, buf, buflen, iscooked, open);
119 }
120 
121 int
opendisk1(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))122 opendisk1(const char *path, int flags, char *buf, size_t buflen, int iscooked,
123 	int (*ofn)(const char *, int, ...))
124 {
125 
126 	return __opendisk(path, flags, buf, buflen, iscooked, ofn);
127 }
128