xref: /openbsd-src/lib/libutil/opendev.c (revision ce7e0fc6a9d74d25b78fb6ad846387717f5172b6)
1 /*	$OpenBSD: opendev.c,v 1.6 2000/04/30 17:37:46 millert Exp $ */
2 
3 /*
4  * Copyright (c) 2000, Todd C. Miller.  All rights reserved.
5  * Copyright (c) 1996, Jason Downs.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <paths.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include "util.h"
37 
38 /*
39  * This routine is a generic rewrite of the original code found in
40  * disklabel(8).
41  */
42 
43 int
44 opendev(path, oflags, dflags, realpath)
45 	char *path;
46 	int oflags;
47 	int dflags;
48 	char **realpath;
49 {
50 	int fd;
51 	char *slash, *prefix;
52 	static char namebuf[PATH_MAX];
53 
54 	/* Initial state */
55 	if (realpath)
56 		*realpath = path;
57 	fd = -1;
58 	errno = ENOENT;
59 
60 	if (dflags & OPENDEV_BLCK)
61 		prefix = "";			/* block device */
62 	else
63 		prefix = "r";			/* character device */
64 
65 	if ((slash = strchr(path, '/')))
66 		fd = open(path, oflags);
67 	else if (dflags & OPENDEV_PART) {
68 		/*
69 		 * First try raw partition (for removable drives)
70 		 */
71 		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c",
72 		    _PATH_DEV, prefix, path, 'a' + getrawpartition())
73 		    < sizeof(namebuf)) {
74 			fd = open(namebuf, oflags);
75 			if (realpath)
76 				*realpath = namebuf;
77 		} else
78 			errno = ENAMETOOLONG;
79 	}
80 	if (!slash && fd == -1 && errno == ENOENT) {
81 		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
82 		    _PATH_DEV, prefix, path) < sizeof(namebuf)) {
83 			fd = open(namebuf, oflags);
84 			if (realpath)
85 				*realpath = namebuf;
86 		} else
87 			errno = ENAMETOOLONG;
88 	}
89 	return (fd);
90 }
91