diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2000-04-30 17:37:47 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2000-04-30 17:37:47 +0000 |
commit | 3b0f0e6c1237e8eed8cdc313f5a3f10e06faedcd (patch) | |
tree | ce7caf953bdcd7b0cffb10ef46a56d9936fbaec1 | |
parent | 5a19e612433c6ed34c1b787c6cfc01b7efeb58c1 (diff) |
Add OPENDEV_BLCK flag for opening the block devices (the character
device is opened by default).
Don't open the file w/o adding a /dev/ prefix unless the file has
a '/' in it. This prevents problems where someone says "disklabel
sd0" with a file called "sd0" in the cwd.
The OPENDEV_DRCT flag has been deprecated as it is the the default
behavior and always has been.
Add checks for >= MAXPATHLEN and return ENAMETOOLONG in that case.
-rw-r--r-- | lib/libutil/opendev.3 | 10 | ||||
-rw-r--r-- | lib/libutil/opendev.c | 67 |
2 files changed, 40 insertions, 37 deletions
diff --git a/lib/libutil/opendev.3 b/lib/libutil/opendev.3 index 0cb2eca2535..7ceb063a100 100644 --- a/lib/libutil/opendev.3 +++ b/lib/libutil/opendev.3 @@ -1,5 +1,6 @@ -.\" $OpenBSD: opendev.3,v 1.8 1999/07/07 10:50:06 aaron Exp $ +.\" $OpenBSD: opendev.3,v 1.9 2000/04/30 17:37:46 millert Exp $ .\" +.\" Copyright (c) 2000, Todd C. Miller. All rights reserved. .\" Copyright (c) 1996, Jason Downs. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -23,7 +24,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 17, 1996 +.Dd February 15, 2000 .Dt OPENDEV 3 .Os .Sh NAME @@ -61,7 +62,7 @@ the following values: .Pp .Bd -literal -offset indent -compact OPENDEV_PART attempt to open the raw partition during expansion -OPENDEV_DRCT attempt to open the device itself during expansion +OPENDEV_BLCK open the block device (default is character device) .Ed .Pp If @@ -75,7 +76,8 @@ The return value and errors are the same as the return value and errors of .Xr open 2 . .Sh SEE ALSO -.Xr open 2 +.Xr open 2 , +.Xr getrawpartition 3 .Sh HISTORY The .Fn opendev diff --git a/lib/libutil/opendev.c b/lib/libutil/opendev.c index add26d7dcd5..0042663299f 100644 --- a/lib/libutil/opendev.c +++ b/lib/libutil/opendev.c @@ -1,6 +1,7 @@ -/* $OpenBSD: opendev.c,v 1.5 1996/09/16 02:40:51 tholo Exp $ */ +/* $OpenBSD: opendev.c,v 1.6 2000/04/30 17:37:46 millert Exp $ */ /* + * Copyright (c) 2000, Todd C. Miller. All rights reserved. * Copyright (c) 1996, Jason Downs. All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,11 +26,12 @@ * SUCH DAMAGE. */ -#include <stdio.h> -#include <string.h> #include <errno.h> #include <fcntl.h> +#include <limits.h> #include <paths.h> +#include <stdio.h> +#include <string.h> #include "util.h" @@ -46,44 +48,43 @@ opendev(path, oflags, dflags, realpath) char **realpath; { int fd; - static char namebuf[256]; + char *slash, *prefix; + static char namebuf[PATH_MAX]; + /* Initial state */ if (realpath) *realpath = path; + fd = -1; + errno = ENOENT; - fd = open(path, oflags); - if ((fd < 0) && (errno == ENOENT)) { - if (path[0] != '/') { - if (dflags & OPENDEV_PART) { - /* - * First try raw partition (for removable - * drives) - */ - (void)snprintf(namebuf, sizeof(namebuf), - "%sr%s%c", _PATH_DEV, path, - 'a' + getrawpartition()); - fd = open(namebuf, oflags); - } - - if ((dflags & OPENDEV_DRCT) && (fd < 0) && - (errno == ENOENT)) { - /* ..and now no partition (for tapes) */ - namebuf[strlen(namebuf) - 1] = '\0'; - fd = open(namebuf, oflags); - } + if (dflags & OPENDEV_BLCK) + prefix = ""; /* block device */ + else + prefix = "r"; /* character device */ + if ((slash = strchr(path, '/'))) + fd = open(path, oflags); + else if (dflags & OPENDEV_PART) { + /* + * First try raw partition (for removable drives) + */ + if (snprintf(namebuf, sizeof(namebuf), "%s%s%s%c", + _PATH_DEV, prefix, path, 'a' + getrawpartition()) + < sizeof(namebuf)) { + fd = open(namebuf, oflags); if (realpath) *realpath = namebuf; - } + } else + errno = ENAMETOOLONG; } - if ((fd < 0) && (errno == ENOENT) && (path[0] != '/')) { - (void)snprintf(namebuf, sizeof(namebuf), "%sr%s", - _PATH_DEV, path); - fd = open(namebuf, oflags); - - if (realpath) - *realpath = namebuf; + if (!slash && fd == -1 && errno == ENOENT) { + if (snprintf(namebuf, sizeof(namebuf), "%s%s%s", + _PATH_DEV, prefix, path) < sizeof(namebuf)) { + fd = open(namebuf, oflags); + if (realpath) + *realpath = namebuf; + } else + errno = ENAMETOOLONG; } - return (fd); } |