Browse Source

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.
OPENBSD_2_7
millert 24 years ago
parent
commit
c67af3de41
2 changed files with 40 additions and 37 deletions
  1. +6
    -4
      src/lib/libutil/opendev.3
  2. +34
    -33
      src/lib/libutil/opendev.c

+ 6
- 4
src/lib/libutil/opendev.3 View File

@ -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


+ 34
- 33
src/lib/libutil/opendev.c View File

@ -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);
}

Loading…
Cancel
Save