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