]>
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 * 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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $OpenBSD: opendev.c,v 1.7 2002/06/09 22:18:43 fgsch Exp $ */
26 * Copyright (c) 2000, Todd C. Miller. All rights reserved.
27 * Copyright (c) 1996, Jason Downs. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
62 opendev(path
, oflags
, dflags
, realpath
)
70 static char namebuf
[PATH_MAX
];
78 if (dflags
& OPENDEV_BLCK
)
79 prefix
= ""; /* block device */
81 prefix
= "r"; /* character device */
83 if ((slash
= strchr(path
, '/')))
84 fd
= open(path
, oflags
);
85 else if (dflags
& OPENDEV_PART
) {
86 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
87 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
89 while ((slice
= strrchr(namebuf
, 's')) &&
90 isdigit(*(slice
-1))) *slice
= '\0';
91 fd
= open(namebuf
, oflags
);
97 if (!slash
&& fd
== -1 && errno
== ENOENT
) {
98 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
99 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
100 fd
= open(namebuf
, oflags
);
104 errno
= ENAMETOOLONG
;