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