1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25/*
26 * Copyright (c) 2000, Todd C. Miller.  All rights reserved.
27 * Copyright (c) 1996, Jason Downs.  All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 *    notice, this list of conditions and the following disclaimer in the
36 *    documentation and/or other materials provided with the distribution.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
39 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 */
50
51#include <errno.h>
52#include <fcntl.h>
53#include <limits.h>
54#include <paths.h>
55#include <stdio.h>
56#include <string.h>
57
58#include "util.h"
59/*
60 * This routine is a generic rewrite of the original code found in
61 * disklabel(8).
62 */
63
64int
65opendev(path, oflags, dflags, realpath)
66	char *path;
67	int oflags;
68	int dflags;
69	char **realpath;
70{
71	int fd;
72	char *slash, *prefix;
73	static char namebuf[PATH_MAX];
74
75	/* Initial state */
76	if (realpath)
77		*realpath = path;
78	fd = -1;
79	errno = ENOENT;
80
81	if (dflags & OPENDEV_BLCK)
82		prefix = "";			/* block device */
83	else
84		prefix = "r";			/* character device */
85
86	if ((slash = strchr(path, '/')))
87		fd = open(path, oflags);
88	else if (dflags & OPENDEV_PART) {
89		/*
90		 * First try raw partition (for removable drives)
91		 */
92		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c",
93		    _PATH_DEV, prefix, path, 'a' + getrawpartition())
94		    < sizeof(namebuf)) {
95			fd = open(namebuf, oflags);
96			if (realpath)
97				*realpath = namebuf;
98		} else
99			errno = ENAMETOOLONG;
100	}
101	if (!slash && fd == -1 && errno == ENOENT) {
102		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
103		    _PATH_DEV, prefix, path) < sizeof(namebuf)) {
104			fd = open(namebuf, oflags);
105			if (realpath)
106				*realpath = namebuf;
107		} else
108			errno = ENAMETOOLONG;
109	}
110	return (fd);
111}
112