xref: /minix3/lib/libutil/opendisk.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: opendisk.c,v 1.13 2014/09/29 21:04:52 christos Exp $	*/
20c3983b2SBen Gras 
30c3983b2SBen Gras /*-
40c3983b2SBen Gras  * Copyright (c) 1997 The NetBSD Foundation, Inc.
50c3983b2SBen Gras  * All rights reserved.
60c3983b2SBen Gras  *
70c3983b2SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
80c3983b2SBen Gras  * by Luke Mewburn.
90c3983b2SBen Gras  *
100c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
110c3983b2SBen Gras  * modification, are permitted provided that the following conditions
120c3983b2SBen Gras  * are met:
130c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
140c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
150c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
160c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
170c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
180c3983b2SBen Gras  *
190c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200c3983b2SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210c3983b2SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220c3983b2SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230c3983b2SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240c3983b2SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250c3983b2SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260c3983b2SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270c3983b2SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280c3983b2SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290c3983b2SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
300c3983b2SBen Gras  */
310c3983b2SBen Gras 
32*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
33*0a6a1f1dSLionel Sambuc #include "nbtool_config.h"
34*0a6a1f1dSLionel Sambuc #endif
35*0a6a1f1dSLionel Sambuc 
360c3983b2SBen Gras #include <sys/cdefs.h>
370c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
38*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: opendisk.c,v 1.13 2014/09/29 21:04:52 christos Exp $");
390c3983b2SBen Gras #endif
400c3983b2SBen Gras 
410c3983b2SBen Gras #include <sys/param.h>
420c3983b2SBen Gras 
430c3983b2SBen Gras #include <assert.h>
440c3983b2SBen Gras #include <errno.h>
450c3983b2SBen Gras #include <fcntl.h>
46*0a6a1f1dSLionel Sambuc #ifndef HAVE_NBTOOL_CONFIG_H
470c3983b2SBen Gras #include <util.h>
480c3983b2SBen Gras #include <paths.h>
49*0a6a1f1dSLionel Sambuc #else
50*0a6a1f1dSLionel Sambuc #include "opendisk.h"
51*0a6a1f1dSLionel Sambuc #endif
520c3983b2SBen Gras #include <stdio.h>
530c3983b2SBen Gras #include <string.h>
540c3983b2SBen Gras 
550c3983b2SBen Gras static int
__opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))560c3983b2SBen Gras __opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
570c3983b2SBen Gras 	int (*ofn)(const char *, int, ...))
580c3983b2SBen Gras {
5984d9c625SLionel Sambuc #if !defined(__minix)
600c3983b2SBen Gras 	int f, rawpart;
61f14fb602SLionel Sambuc #else
62f14fb602SLionel Sambuc 	int f;
6384d9c625SLionel Sambuc #endif /* !defined(__minix) */
640c3983b2SBen Gras 
6584d9c625SLionel Sambuc #if defined(__minix)
66de27460eSDavid van Moolenbroek 	/*
67de27460eSDavid van Moolenbroek 	 * MINIX does not have the cooked/raw distinction.  Do not prepend 'r'
68de27460eSDavid van Moolenbroek 	 * to the device name when generating a full path.
69de27460eSDavid van Moolenbroek 	 */
70de27460eSDavid van Moolenbroek 	iscooked = 1;
7184d9c625SLionel Sambuc #endif /* defined(__minix) */
72de27460eSDavid van Moolenbroek 
730c3983b2SBen Gras 	if (buf == NULL) {
740c3983b2SBen Gras 		errno = EFAULT;
750c3983b2SBen Gras 		return (-1);
760c3983b2SBen Gras 	}
770c3983b2SBen Gras 	snprintf(buf, buflen, "%s", path);
780c3983b2SBen Gras 
790c3983b2SBen Gras 	if ((flags & O_CREAT) != 0) {
800c3983b2SBen Gras 		errno = EINVAL;
810c3983b2SBen Gras 		return (-1);
820c3983b2SBen Gras 	}
830c3983b2SBen Gras 
8484d9c625SLionel Sambuc #if !defined(__minix)
850c3983b2SBen Gras 	rawpart = getrawpartition();
860c3983b2SBen Gras 	if (rawpart < 0)
870c3983b2SBen Gras 		return (-1);	/* sysctl(3) in getrawpartition sets errno */
8884d9c625SLionel Sambuc #endif /* !defined(__minix) */
890c3983b2SBen Gras 
900c3983b2SBen Gras 	f = ofn(buf, flags, 0);
910c3983b2SBen Gras 	if (f != -1 || errno != ENOENT)
920c3983b2SBen Gras 		return (f);
930c3983b2SBen Gras 
9484d9c625SLionel Sambuc #if !defined(__minix)
950c3983b2SBen Gras 	snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
960c3983b2SBen Gras 	f = ofn(buf, flags, 0);
970c3983b2SBen Gras 	if (f != -1 || errno != ENOENT)
980c3983b2SBen Gras 		return (f);
9984d9c625SLionel Sambuc #endif /* !defined(__minix) */
1000c3983b2SBen Gras 
1010c3983b2SBen Gras 	if (strchr(path, '/') != NULL)
1020c3983b2SBen Gras 		return (-1);
1030c3983b2SBen Gras 
1040c3983b2SBen Gras 	snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
1050c3983b2SBen Gras 	f = ofn(buf, flags, 0);
1060c3983b2SBen Gras 	if (f != -1 || errno != ENOENT)
1070c3983b2SBen Gras 		return (f);
1080c3983b2SBen Gras 
10984d9c625SLionel Sambuc #if !defined(__minix)
1100c3983b2SBen Gras 	snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
1110c3983b2SBen Gras 	    'a' + rawpart);
1120c3983b2SBen Gras 	f = ofn(buf, flags, 0);
11384d9c625SLionel Sambuc #endif /* !defined(__minix) */
1140c3983b2SBen Gras 	return (f);
1150c3983b2SBen Gras }
1160c3983b2SBen Gras 
1170c3983b2SBen Gras int
opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked)1180c3983b2SBen Gras opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
1190c3983b2SBen Gras {
1200c3983b2SBen Gras 
1210c3983b2SBen Gras 	return __opendisk(path, flags, buf, buflen, iscooked, open);
1220c3983b2SBen Gras }
1230c3983b2SBen Gras 
1240c3983b2SBen Gras int
opendisk1(const char * path,int flags,char * buf,size_t buflen,int iscooked,int (* ofn)(const char *,int,...))1250c3983b2SBen Gras opendisk1(const char *path, int flags, char *buf, size_t buflen, int iscooked,
1260c3983b2SBen Gras 	int (*ofn)(const char *, int, ...))
1270c3983b2SBen Gras {
1280c3983b2SBen Gras 
1290c3983b2SBen Gras 	return __opendisk(path, flags, buf, buflen, iscooked, ofn);
1300c3983b2SBen Gras }
131