1*429b3653Sderaadt /* $OpenBSD: opendisk.c,v 1.7 2014/06/30 00:26:22 deraadt Exp $ */
29046b325Scsapuntz /* $NetBSD: opendisk.c,v 1.4 1997/09/30 17:13:50 phil Exp $ */
39046b325Scsapuntz
49046b325Scsapuntz /*-
59046b325Scsapuntz * Copyright (c) 1997 The NetBSD Foundation, Inc.
69046b325Scsapuntz * All rights reserved.
79046b325Scsapuntz *
89046b325Scsapuntz * This code is derived from software contributed to The NetBSD Foundation
99046b325Scsapuntz * by Luke Mewburn.
109046b325Scsapuntz *
119046b325Scsapuntz * Redistribution and use in source and binary forms, with or without
129046b325Scsapuntz * modification, are permitted provided that the following conditions
139046b325Scsapuntz * are met:
149046b325Scsapuntz * 1. Redistributions of source code must retain the above copyright
159046b325Scsapuntz * notice, this list of conditions and the following disclaimer.
169046b325Scsapuntz * 2. Redistributions in binary form must reproduce the above copyright
179046b325Scsapuntz * notice, this list of conditions and the following disclaimer in the
189046b325Scsapuntz * documentation and/or other materials provided with the distribution.
199046b325Scsapuntz *
209046b325Scsapuntz * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219046b325Scsapuntz * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229046b325Scsapuntz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239046b325Scsapuntz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249046b325Scsapuntz * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259046b325Scsapuntz * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269046b325Scsapuntz * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279046b325Scsapuntz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289046b325Scsapuntz * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299046b325Scsapuntz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309046b325Scsapuntz * POSSIBILITY OF SUCH DAMAGE.
319046b325Scsapuntz */
329046b325Scsapuntz
33*429b3653Sderaadt #include <sys/types.h>
349046b325Scsapuntz
359046b325Scsapuntz #include <errno.h>
369046b325Scsapuntz #include <fcntl.h>
379046b325Scsapuntz #include <paths.h>
389046b325Scsapuntz #include <stdio.h>
399046b325Scsapuntz #include <string.h>
409046b325Scsapuntz
4179822b59Smillert #include "util.h"
4279822b59Smillert
439046b325Scsapuntz int
opendisk(const char * path,int flags,char * buf,size_t buflen,int iscooked)441477552aSderaadt opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
459046b325Scsapuntz {
469046b325Scsapuntz int f, rawpart;
479046b325Scsapuntz
489046b325Scsapuntz snprintf(buf, buflen, "%s", path);
499046b325Scsapuntz
509046b325Scsapuntz if ((flags & O_CREAT) != 0) {
519046b325Scsapuntz errno = EINVAL;
529046b325Scsapuntz return (-1);
539046b325Scsapuntz }
549046b325Scsapuntz
559046b325Scsapuntz rawpart = getrawpartition();
569046b325Scsapuntz if (rawpart < 0)
579046b325Scsapuntz return (-1); /* sysctl(3) in getrawpartition sets errno */
589046b325Scsapuntz
599046b325Scsapuntz f = open(buf, flags);
609046b325Scsapuntz if (f != -1 || errno != ENOENT)
619046b325Scsapuntz return (f);
629046b325Scsapuntz
639046b325Scsapuntz snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
649046b325Scsapuntz f = open(buf, flags);
659046b325Scsapuntz if (f != -1 || errno != ENOENT)
669046b325Scsapuntz return (f);
679046b325Scsapuntz
689046b325Scsapuntz if (strchr(path, '/') != NULL)
699046b325Scsapuntz return (-1);
709046b325Scsapuntz
719046b325Scsapuntz snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
729046b325Scsapuntz f = open(buf, flags);
739046b325Scsapuntz if (f != -1 || errno != ENOENT)
749046b325Scsapuntz return (f);
759046b325Scsapuntz
769046b325Scsapuntz snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
779046b325Scsapuntz 'a' + rawpart);
789046b325Scsapuntz f = open(buf, flags);
799046b325Scsapuntz return (f);
809046b325Scsapuntz }
81