]> git.saurik.com Git - apple/libc.git/blame - util/opendev.c
Libc-391.2.3.tar.gz
[apple/libc.git] / util / opendev.c
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9385eb3d
A
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* $OpenBSD: opendev.c,v 1.7 2002/06/09 22:18:43 fgsch Exp $ */
24
25/*
26 * Copyright (c) 2000, Todd C. Miller. All rights reserved.
27 * Copyright (c) 1996, Jason Downs. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
37 *
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
48 * SUCH DAMAGE.
49 */
50
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <limits.h>
55#include <paths.h>
56#include <stdio.h>
57#include <string.h>
58
59#include "util.h"
60
61int
62opendev(path, oflags, dflags, realpath)
63 char *path;
64 int oflags;
65 int dflags;
66 char **realpath;
67{
68 int fd;
69 char *slash, *prefix;
70 static char namebuf[PATH_MAX];
71
72 /* Initial state */
73 if (realpath)
74 *realpath = path;
75 fd = -1;
76 errno = ENOENT;
77
78 if (dflags & OPENDEV_BLCK)
79 prefix = ""; /* block device */
80 else
81 prefix = "r"; /* character device */
82
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)) {
88 char *slice;
89 while ((slice = strrchr(namebuf, 's')) &&
90 isdigit(*(slice-1))) *slice = '\0';
91 fd = open(namebuf, oflags);
92 if (realpath)
93 *realpath = namebuf;
94 } else
95 errno = ENAMETOOLONG;
96 }
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);
101 if (realpath)
102 *realpath = namebuf;
103 } else
104 errno = ENAMETOOLONG;
105 }
106 return (fd);
107}