Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/fdisk.tproj/opendev.c

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)
66char *path;
67int oflags;
68int dflags;
69char **realpath;
70{
71int fd;
72char *slash, *prefix;
73static char namebuf[PATH_MAX];
74
75/* Initial state */
76if (realpath)
77*realpath = path;
78fd = -1;
79errno = ENOENT;
80
81if (dflags & OPENDEV_BLCK)
82prefix = "";/* block device */
83else
84prefix = "r";/* character device */
85
86if ((slash = strchr(path, '/')))
87fd = open(path, oflags);
88else if (dflags & OPENDEV_PART) {
89/*
90 * First try raw partition (for removable drives)
91 */
92if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c",
93 _PATH_DEV, prefix, path, 'a' + getrawpartition())
94 < sizeof(namebuf)) {
95fd = open(namebuf, oflags);
96if (realpath)
97*realpath = namebuf;
98} else
99errno = ENAMETOOLONG;
100}
101if (!slash && fd == -1 && errno == ENOENT) {
102if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
103 _PATH_DEV, prefix, path) < sizeof(namebuf)) {
104fd = open(namebuf, oflags);
105if (realpath)
106*realpath = namebuf;
107} else
108errno = ENAMETOOLONG;
109}
110return (fd);
111}
112

Archive Download this file

Revision: HEAD