summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorKjell Wooding <kjell@cvs.openbsd.org>2008-06-13 18:45:42 +0000
committerKjell Wooding <kjell@cvs.openbsd.org>2008-06-13 18:45:42 +0000
commit1a9466aff5e77554ccb005209154838280aaffae (patch)
tree74bda59bf0c9c4d75e2740a0e45ae12e77dfed92 /usr.bin
parent0d7b4351fc9f31bc17854edbe9bda8efbee71615 (diff)
Fix debian bug #432656
'Prints root directory as "//" instead of "/" for root files.' Issue was with dirname, which strips the trailing slash, except when given "/". Wrap it in a cover function to fix. Also helps with portability to data-munging dirname glibc.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mg/file.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index af1361e1a4e..ae4036a020e 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.65 2008/03/21 08:01:20 pyr Exp $ */
+/* $OpenBSD: file.c,v 1.66 2008/06/13 18:45:41 kjell Exp $ */
/* This file is in the public domain. */
@@ -10,6 +10,8 @@
#include "def.h"
+static char *xdirname(const char *);
+
/*
* Insert a file into the current buffer. Real easy - just call the
* insertfile routine with the file name.
@@ -288,6 +290,7 @@ insertfile(char *fname, char *newname, int replacebuf)
int nbytes, s, nline = 0, siz, x, x2;
int opos; /* offset we started at */
int oline; /* original line number */
+ char *dp;
if (replacebuf == TRUE)
x = undo_enable(FALSE);
@@ -306,9 +309,10 @@ insertfile(char *fname, char *newname, int replacebuf)
bp = curbp;
if (newname != NULL) {
(void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
- (void)strlcpy(bp->b_cwd, dirname(newname),
- sizeof(bp->b_cwd));
+ dp = xdirname(newname);
+ (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
+ free(dp);
}
/* hard file open */
@@ -335,8 +339,10 @@ insertfile(char *fname, char *newname, int replacebuf)
curbp = bp;
return (showbuffer(bp, curwp, WFFULL | WFMODE));
} else {
- (void)strlcpy(bp->b_cwd, dirname(fname), sizeof(bp->b_cwd));
+ dp = xdirname(fname);
+ (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
+ free(dp);
}
opos = curwp->w_doto;
oline = curwp->w_dotline;
@@ -642,3 +648,21 @@ upmodes(struct buffer *bp)
if (bp == NULL || curwp->w_bufp == bp)
wp->w_flag |= WFMODE;
}
+
+/*
+ * Same as dirname, except an empty string is returned in
+ * place of "/". This means we can always add a trailing
+ * slash and be correct.
+ * Unlike dirname, we allocate. Caller must free.
+ */
+static char *
+xdirname(const char *path)
+{
+ char *dp;
+
+ dp = dirname(path);
+ if (*dp && dp[0] == '/' && dp[1] == '\0')
+ return (strdup(""));
+
+ return (strdup(dp));
+}