Browse Source

Always return a pointer to the static buffer in realpath, even if

path and realpath are the same.  This matches the manual and avoids
a bogus cast to non-const.  OK jsing@, previous version OK kettenis@
OPENBSD_4_9
millert 14 years ago
parent
commit
aab35837b7
1 changed files with 10 additions and 13 deletions
  1. +10
    -13
      src/lib/libutil/opendev.c

+ 10
- 13
src/lib/libutil/opendev.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: opendev.c,v 1.12 2010/12/17 19:35:34 millert Exp $ */
/* $OpenBSD: opendev.c,v 1.13 2010/12/21 15:47:52 millert Exp $ */
/* /*
* Copyright (c) 2000, Todd C. Miller. All rights reserved. * Copyright (c) 2000, Todd C. Miller. All rights reserved.
@ -54,8 +54,6 @@ opendev(const char *path, int oflags, int dflags, char **realpath)
int fd; int fd;
/* Initial state */ /* Initial state */
if (realpath)
*realpath = (char *)path;
fd = -1; fd = -1;
errno = ENOENT; errno = ENOENT;
@ -64,12 +62,13 @@ opendev(const char *path, int oflags, int dflags, char **realpath)
else else
prefix = "r"; /* character device */ prefix = "r"; /* character device */
if ((slash = strchr(path, '/')))
fd = open(path, oflags);
else if (isduid(path, dflags)) {
if ((slash = strchr(path, '/'))) {
strlcpy(namebuf, path, sizeof(namebuf));
fd = open(namebuf, oflags);
} else if (isduid(path, dflags)) {
strlcpy(namebuf, path, sizeof(namebuf));
if ((fd = open("/dev/diskmap", oflags)) != -1) { if ((fd = open("/dev/diskmap", oflags)) != -1) {
bzero(&dm, sizeof(struct dk_diskmap)); bzero(&dm, sizeof(struct dk_diskmap));
strlcpy(namebuf, path, sizeof(namebuf));
dm.device = namebuf; dm.device = namebuf;
dm.fd = fd; dm.fd = fd;
if (dflags & OPENDEV_PART) if (dflags & OPENDEV_PART)
@ -81,8 +80,7 @@ opendev(const char *path, int oflags, int dflags, char **realpath)
close(fd); close(fd);
fd = -1; fd = -1;
errno = ENOENT; errno = ENOENT;
} else if (realpath)
*realpath = namebuf;
}
} else if (errno != ENOENT) { } else if (errno != ENOENT) {
errno = ENXIO; errno = ENXIO;
return -1; return -1;
@ -96,8 +94,6 @@ opendev(const char *path, int oflags, int dflags, char **realpath)
_PATH_DEV, prefix, path, 'a' + getrawpartition()) _PATH_DEV, prefix, path, 'a' + getrawpartition())
< sizeof(namebuf)) { < sizeof(namebuf)) {
fd = open(namebuf, oflags); fd = open(namebuf, oflags);
if (realpath)
*realpath = namebuf;
} else } else
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
} }
@ -105,10 +101,11 @@ opendev(const char *path, int oflags, int dflags, char **realpath)
if (snprintf(namebuf, sizeof(namebuf), "%s%s%s", if (snprintf(namebuf, sizeof(namebuf), "%s%s%s",
_PATH_DEV, prefix, path) < sizeof(namebuf)) { _PATH_DEV, prefix, path) < sizeof(namebuf)) {
fd = open(namebuf, oflags); fd = open(namebuf, oflags);
if (realpath)
*realpath = namebuf;
} else } else
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
} }
if (realpath)
*realpath = namebuf;
return (fd); return (fd);
} }

Loading…
Cancel
Save