xref: /netbsd-src/lib/libutil/opendisk.c (revision 395941f2ce613dd2839278a646d63781c97bed20)
1*395941f2Schristos /*	$NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $	*/
21e372f6cSlukem 
31e372f6cSlukem /*-
41e372f6cSlukem  * Copyright (c) 1997 The NetBSD Foundation, Inc.
51e372f6cSlukem  * All rights reserved.
61e372f6cSlukem  *
71e372f6cSlukem  * This code is derived from software contributed to The NetBSD Foundation
81e372f6cSlukem  * by Luke Mewburn.
91e372f6cSlukem  *
101e372f6cSlukem  * Redistribution and use in source and binary forms, with or without
111e372f6cSlukem  * modification, are permitted provided that the following conditions
121e372f6cSlukem  * are met:
131e372f6cSlukem  * 1. Redistributions of source code must retain the above copyright
141e372f6cSlukem  *    notice, this list of conditions and the following disclaimer.
151e372f6cSlukem  * 2. Redistributions in binary form must reproduce the above copyright
161e372f6cSlukem  *    notice, this list of conditions and the following disclaimer in the
171e372f6cSlukem  *    documentation and/or other materials provided with the distribution.
181e372f6cSlukem  *
191e372f6cSlukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201e372f6cSlukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211e372f6cSlukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221e372f6cSlukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231e372f6cSlukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241e372f6cSlukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251e372f6cSlukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261e372f6cSlukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271e372f6cSlukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281e372f6cSlukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291e372f6cSlukem  * POSSIBILITY OF SUCH DAMAGE.
301e372f6cSlukem  */
311e372f6cSlukem 
32c4dd2a85Schristos #if HAVE_NBTOOL_CONFIG_H
33c4dd2a85Schristos #include "nbtool_config.h"
34c4dd2a85Schristos #endif
35c4dd2a85Schristos 
361e372f6cSlukem #include <sys/cdefs.h>
371e372f6cSlukem #if defined(LIBC_SCCS) && !defined(lint)
38*395941f2Schristos __RCSID("$NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $");
391e372f6cSlukem #endif
401e372f6cSlukem 
411e372f6cSlukem #include <sys/param.h>
421e372f6cSlukem 
43b48252f3Slukem #include <assert.h>
441e372f6cSlukem #include <errno.h>
45*395941f2Schristos #include <stdarg.h>
461e372f6cSlukem #include <fcntl.h>
47c4dd2a85Schristos #ifndef HAVE_NBTOOL_CONFIG_H
48f0061ca4Slukem #include <util.h>
491e372f6cSlukem #include <paths.h>
50c4dd2a85Schristos #else
51c4dd2a85Schristos #include "opendisk.h"
52c4dd2a85Schristos #endif
531e372f6cSlukem #include <stdio.h>
541e372f6cSlukem #include <string.h>
551e372f6cSlukem 
56*395941f2Schristos static int __printflike(5, 6)
opd(char * buf,size_t len,int (* ofn)(const char *,int,...),int flags,const char * fmt,...)57*395941f2Schristos opd(char *buf, size_t len, int (*ofn)(const char *, int, ...),
58*395941f2Schristos     int flags, const char *fmt, ...)
59*395941f2Schristos {
60*395941f2Schristos 	va_list ap;
61*395941f2Schristos 
62*395941f2Schristos 	va_start(ap, fmt);
63*395941f2Schristos 	vsnprintf(buf, len, fmt, ap);
64*395941f2Schristos 	va_end(ap);
65*395941f2Schristos 
66*395941f2Schristos 	return (*ofn)(buf, flags, 0);
67*395941f2Schristos }
68*395941f2Schristos 
69aaaf93f1Spooka static int
__opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))70aaaf93f1Spooka __opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
71363b1455Spooka 	int (*ofn)(const char *, int, ...))
721e372f6cSlukem {
73*395941f2Schristos 	int f, part;
741e372f6cSlukem 
751e372f6cSlukem 	if (buf == NULL) {
761e372f6cSlukem 		errno = EFAULT;
77*395941f2Schristos 		return -1;
781e372f6cSlukem 	}
79f0061ca4Slukem 
80e6b7ef96Slukem 	if ((flags & O_CREAT) != 0) {
81e6b7ef96Slukem 		errno = EINVAL;
82*395941f2Schristos 		return -1;
83e6b7ef96Slukem 	}
841e372f6cSlukem 
85*395941f2Schristos 	part = getrawpartition();
86*395941f2Schristos 	if (part < 0)
87*395941f2Schristos 		return -1;	/* sysctl(3) in getrawpartition sets errno */
88*395941f2Schristos 	part += 'a';
891e372f6cSlukem 
90*395941f2Schristos 	/*
91*395941f2Schristos 	 * If we are passed a plain name, first try /dev to avoid accidents
92*395941f2Schristos 	 * with files in the same directory that happen to match disk names.
93*395941f2Schristos 	 */
94*395941f2Schristos 	if (strchr(path, '/') == NULL) {
95*395941f2Schristos 		const char *r = iscooked ? "" : "r";
96*395941f2Schristos 		const char *d = _PATH_DEV;
97*395941f2Schristos 
98*395941f2Schristos 		f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
99f0061ca4Slukem 		if (f != -1 || errno != ENOENT)
100*395941f2Schristos 			return f;
101f0061ca4Slukem 
102*395941f2Schristos 		f = opd(buf, buflen, ofn, flags, "%s%s%s%c", d, r, path, part);
103f0061ca4Slukem 		if (f != -1 || errno != ENOENT)
104*395941f2Schristos 			return f;
105*395941f2Schristos 	}
106f0061ca4Slukem 
107*395941f2Schristos 	f = opd(buf, buflen, ofn, flags, "%s", path);
108f0061ca4Slukem 	if (f != -1 || errno != ENOENT)
109*395941f2Schristos 		return f;
110f0061ca4Slukem 
111*395941f2Schristos 	return opd(buf, buflen, ofn, flags, "%s%c", path, part);
1121e372f6cSlukem }
113aaaf93f1Spooka 
114aaaf93f1Spooka int
opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked)115aaaf93f1Spooka opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
116aaaf93f1Spooka {
117aaaf93f1Spooka 
118363b1455Spooka 	return __opendisk(path, flags, buf, buflen, iscooked, open);
119aaaf93f1Spooka }
120aaaf93f1Spooka 
121aaaf93f1Spooka int
opendisk1(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))122aaaf93f1Spooka opendisk1(const char *path, int flags, char *buf, size_t buflen, int iscooked,
123363b1455Spooka 	int (*ofn)(const char *, int, ...))
124aaaf93f1Spooka {
125aaaf93f1Spooka 
126aaaf93f1Spooka 	return __opendisk(path, flags, buf, buflen, iscooked, ofn);
127aaaf93f1Spooka }
128