Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/netbsd-7]: src/lib/libutil Pull up following revision(s) (requested by m...
details: https://anonhg.NetBSD.org/src/rev/178ad2944db9
branches: netbsd-7
changeset: 798377:178ad2944db9
user: martin <martin%NetBSD.org@localhost>
date: Mon Sep 22 11:19:20 2014 +0000
description:
Pull up following revision(s) (requested by mlelstv in ticket #117):
lib/libutil/getdiskrawname.c: revision 1.3
lib/libutil/getdiskrawname.c: revision 1.4
lib/libutil/getdiskrawname.c: revision 1.5
- use a private buffer to resolve symlinks, the previous code was broken
- factored out symlink handling
- handle relative symlinks now
- handle device paths that do not contain a '/'.
use the passed parameter instead of PATH_MAX. Change signedness
of nlen.
KNF, sign cast.
diffstat:
lib/libutil/getdiskrawname.c | 75 ++++++++++++++++++++++++++++++-------------
1 files changed, 52 insertions(+), 23 deletions(-)
diffs (133 lines):
diff -r 50675149bef1 -r 178ad2944db9 lib/libutil/getdiskrawname.c
--- a/lib/libutil/getdiskrawname.c Mon Sep 22 11:15:57 2014 +0000
+++ b/lib/libutil/getdiskrawname.c Mon Sep 22 11:19:20 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: getdiskrawname.c,v 1.2 2013/12/22 14:31:51 mlelstv Exp $ */
+/* $NetBSD: getdiskrawname.c,v 1.2.4.1 2014/09/22 11:19:20 martin Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: getdiskrawname.c,v 1.2 2013/12/22 14:31:51 mlelstv Exp $");
+__RCSID("$NetBSD: getdiskrawname.c,v 1.2.4.1 2014/09/22 11:19:20 martin Exp $");
#include <sys/stat.h>
@@ -37,25 +37,52 @@
#include <string.h>
#include <errno.h>
#include <util.h>
+#include <limits.h>
#include <unistd.h>
+static const char *
+resolve_link(char *buf, size_t bufsiz, const char *name)
+{
+ const char *dp;
+ size_t nlen;
+ ssize_t dlen;
+
+ dlen = readlink(name, buf, bufsiz - 1);
+ if (dlen == -1)
+ return name;
+
+ buf[dlen] = '\0';
+
+ if (buf[0] != '/') {
+ dp = strrchr(name, '/');
+ if (dp != NULL) {
+ nlen = dp - name + 1;
+ if (nlen + 1 > bufsiz)
+ return NULL;
+ if (nlen + dlen + 1 > bufsiz)
+ return NULL;
+
+ memmove(buf + nlen, buf, (size_t)dlen + 1);
+ memcpy(buf, name, nlen);
+ }
+ }
+
+ return buf;
+}
+
const char *
getdiskrawname(char *buf, size_t bufsiz, const char *name)
{
- const char *dp = strrchr(name, '/');
+ const char *dp;
struct stat st;
- ssize_t len;
+ char dest[PATH_MAX];
- if (dp == NULL) {
+ if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
errno = EINVAL;
return NULL;
}
- len = readlink(name, buf, bufsiz-1);
- if (len > 0) {
- buf[len] = '\0';
- name = buf;
- }
+ dp = strrchr(name, '/');
if (stat(name, &st) == -1)
return NULL;
@@ -65,7 +92,10 @@
return NULL;
}
- (void)snprintf(buf, bufsiz, "%.*s/r%s", (int)(dp - name), name, dp + 1);
+ if (dp != NULL)
+ (void)snprintf(buf, bufsiz, "%.*s/r%s", (int)(dp - name), name, dp + 1);
+ else
+ (void)snprintf(buf, bufsiz, "r%s", name);
return buf;
}
@@ -75,17 +105,18 @@
{
const char *dp;
struct stat st;
- ssize_t len;
+ char dest[PATH_MAX];
- if ((dp = strrchr(name, '/')) == NULL) {
+ if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
errno = EINVAL;
return NULL;
}
- len = readlink(name, buf, bufsiz-1);
- if (len > 0) {
- buf[len] = '\0';
- name = buf;
+ dp = strrchr(name, '/');
+
+ if ((dp != NULL && dp[1] != 'r') || (dp == NULL && name[0] != 'r')) {
+ errno = EINVAL;
+ return NULL;
}
if (stat(name, &st) == -1)
@@ -96,12 +127,10 @@
return NULL;
}
- if (dp[1] != 'r') {
- errno = EINVAL;
- return NULL;
- }
-
- (void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2);
+ if (dp != NULL)
+ (void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2);
+ else
+ (void)snprintf(buf, bufsiz, "%s", name + 1);
return buf;
}
Home |
Main Index |
Thread Index |
Old Index