Home | History | Annotate | Line # | Download | only in libutil
opendisk.c revision 1.12.28.1
      1 /*	$NetBSD: opendisk.c,v 1.12.28.1 2017/03/13 07:41:25 skrll 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 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: opendisk.c,v 1.12.28.1 2017/03/13 07:41:25 skrll Exp $");
     35 #endif
     36 
     37 #include <sys/param.h>
     38 
     39 #include <assert.h>
     40 #include <errno.h>
     41 #include <stdarg.h>
     42 #include <fcntl.h>
     43 #include <util.h>
     44 #include <paths.h>
     45 #include <stdio.h>
     46 #include <string.h>
     47 
     48 static int __printflike(5, 6)
     49 opd(char *buf, size_t len, int (*ofn)(const char *, int, ...),
     50     int flags, const char *fmt, ...)
     51 {
     52 	va_list ap;
     53 
     54 	va_start(ap, fmt);
     55 	vsnprintf(buf, len, fmt, ap);
     56 	va_end(ap);
     57 
     58 	return (*ofn)(buf, flags, 0);
     59 }
     60 
     61 static int
     62 __opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
     63 	int (*ofn)(const char *, int, ...))
     64 {
     65 	int f, part;
     66 
     67 	if (buf == NULL) {
     68 		errno = EFAULT;
     69 		return -1;
     70 	}
     71 
     72 	if ((flags & O_CREAT) != 0) {
     73 		errno = EINVAL;
     74 		return -1;
     75 	}
     76 
     77 	part = getrawpartition();
     78 	if (part < 0)
     79 		return -1;	/* sysctl(3) in getrawpartition sets errno */
     80 	part += 'a';
     81 
     82 	/*
     83 	 * If we are passed a plain name, first try /dev to avoid accidents
     84 	 * with files in the same directory that happen to match disk names.
     85 	 */
     86 	if (strchr(path, '/') == NULL) {
     87 		const char *r = iscooked ? "" : "r";
     88 		const char *d = _PATH_DEV;
     89 
     90 		f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
     91 		if (f != -1 || errno != ENOENT)
     92 			return f;
     93 
     94 		f = opd(buf, buflen, ofn, flags, "%s%s%s%c", d, r, path, part);
     95 		if (f != -1 || errno != ENOENT)
     96 			return f;
     97 	}
     98 
     99 	f = opd(buf, buflen, ofn, flags, "%s", path);
    100 	if (f != -1 || errno != ENOENT)
    101 		return f;
    102 
    103 	return opd(buf, buflen, ofn, flags, "%s%c", path, part);
    104 }
    105 
    106 int
    107 opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
    108 {
    109 
    110 	return __opendisk(path, flags, buf, buflen, iscooked, open);
    111 }
    112 
    113 int
    114 opendisk1(const char *path, int flags, char *buf, size_t buflen, int iscooked,
    115 	int (*ofn)(const char *, int, ...))
    116 {
    117 
    118 	return __opendisk(path, flags, buf, buflen, iscooked, ofn);
    119 }
    120