]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 2000, Todd C. Miller. All rights reserved. | |
3 | * Copyright (c) 1996, Jason Downs. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
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. | |
13 | * | |
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 | |
24 | * SUCH DAMAGE. | |
25 | */ | |
26 | ||
27 | #include <ctype.h> | |
28 | #include <errno.h> | |
29 | #include <fcntl.h> | |
30 | #include <limits.h> | |
31 | #include <paths.h> | |
32 | #include <stdio.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include "util.h" | |
36 | ||
37 | int | |
38 | opendev(path, oflags, dflags, realpath) | |
39 | char *path; | |
40 | int oflags; | |
41 | int dflags; | |
42 | char **realpath; | |
43 | { | |
44 | int fd; | |
45 | char *slash, *prefix; | |
46 | static char namebuf[PATH_MAX]; | |
47 | ||
48 | /* Initial state */ | |
49 | if (realpath) | |
50 | *realpath = path; | |
51 | fd = -1; | |
52 | errno = ENOENT; | |
53 | ||
54 | if (dflags & OPENDEV_BLCK) | |
55 | prefix = ""; /* block device */ | |
56 | else | |
57 | prefix = "r"; /* character device */ | |
58 | ||
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)) { | |
64 | char *slice; | |
65 | while ((slice = strrchr(namebuf, 's')) && | |
66 | isdigit(*(slice-1))) *slice = '\0'; | |
67 | fd = open(namebuf, oflags); | |
68 | if (realpath) | |
69 | *realpath = namebuf; | |
70 | } else | |
71 | errno = ENAMETOOLONG; | |
72 | } | |
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); | |
77 | if (realpath) | |
78 | *realpath = namebuf; | |
79 | } else | |
80 | errno = ENAMETOOLONG; | |
81 | } | |
82 | return (fd); | |
83 | } |