diff --git a/src/lib/libutil/opendev.3 b/src/lib/libutil/opendev.3 index 0cb2eca2..7ceb063a 100644 --- a/src/lib/libutil/opendev.3 +++ b/src/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/src/lib/libutil/opendev.c b/src/lib/libutil/opendev.c index add26d7d..00426632 100644 --- a/src/lib/libutil/opendev.c +++ b/src/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 -#include #include #include +#include #include +#include +#include #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); }