Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/util/fdisk/opendev.c

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

Archive Download this file

Revision: 2045