]>
git.saurik.com Git - apple/libc.git/blob - util/opendev.c
2 * Copyright (c) 2000, Todd C. Miller. All rights reserved.
3 * Copyright (c) 1996, Jason Downs. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 opendev(path
, oflags
, dflags
, realpath
)
46 static char namebuf
[PATH_MAX
];
54 if (dflags
& OPENDEV_BLCK
)
55 prefix
= ""; /* block device */
57 prefix
= "r"; /* character device */
59 if ((slash
= strchr(path
, '/')))
60 fd
= open(path
, oflags
);
61 else if (dflags
& OPENDEV_PART
) {
62 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
63 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
65 while ((slice
= strrchr(namebuf
, 's')) &&
66 isdigit(*(slice
-1))) *slice
= '\0';
67 fd
= open(namebuf
, oflags
);
73 if (!slash
&& fd
== -1 && errno
== ENOENT
) {
74 if (snprintf(namebuf
, sizeof(namebuf
), "%s%s%s",
75 _PATH_DEV
, prefix
, path
) < sizeof(namebuf
)) {
76 fd
= open(namebuf
, oflags
);