]>
git.saurik.com Git - apple/libc.git/blob - util/opendev.c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $OpenBSD: opendev.c,v 1.7 2002/06/09 22:18:43 fgsch Exp $ */
28 * Copyright (c) 2000, Todd C. Miller. All rights reserved.
29 * Copyright (c) 1996, Jason Downs. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
41 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
44 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 opendev(path
, oflags
, dflags
, realpath
)
72 static char namebuf
[PATH_MAX
];
80 if (dflags
& OPENDEV_BLCK
)
81 prefix
= ""; /* block device */
83 prefix
= "r"; /* character device */
85 if ((slash
= strchr(path
, '/')))
86 fd
= open(path
, oflags
);
87 else if (dflags
& OPENDEV_PART
) {
88 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
89 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
91 while ((slice
= strrchr(namebuf
, 's')) &&
92 isdigit(*(slice
-1))) *slice
= '\0';
93 fd
= open(namebuf
, oflags
);
99 if (!slash
&& fd
== -1 && errno
== ENOENT
) {
100 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
101 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
102 fd
= open(namebuf
, oflags
);
106 errno
= ENAMETOOLONG
;