xref: /openbsd-src/lib/libutil/opendev.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: opendev.c,v 1.8 2004/05/28 07:03:47 deraadt 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(char *path, int oflags, int dflags, char **realpath)
45 {
46 	static char namebuf[PATH_MAX];
47 	char *slash, *prefix;
48 	int fd;
49 
50 	/* Initial state */
51 	if (realpath)
52 		*realpath = path;
53 	fd = -1;
54 	errno = ENOENT;
55 
56 	if (dflags & OPENDEV_BLCK)
57 		prefix = "";			/* block device */
58 	else
59 		prefix = "r";			/* character device */
60 
61 	if ((slash = strchr(path, '/')))
62 		fd = open(path, oflags);
63 	else if (dflags & OPENDEV_PART) {
64 		/*
65 		 * First try raw partition (for removable drives)
66 		 */
67 		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c",
68 		    _PATH_DEV, prefix, path, 'a' + getrawpartition())
69 		    < sizeof(namebuf)) {
70 			fd = open(namebuf, oflags);
71 			if (realpath)
72 				*realpath = namebuf;
73 		} else
74 			errno = ENAMETOOLONG;
75 	}
76 	if (!slash && fd == -1 && errno == ENOENT) {
77 		if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
78 		    _PATH_DEV, prefix, path) < sizeof(namebuf)) {
79 			fd = open(namebuf, oflags);
80 			if (realpath)
81 				*realpath = namebuf;
82 		} else
83 			errno = ENAMETOOLONG;
84 	}
85 	return (fd);
86 }
87