summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkjell <kjell@openbsd.org>2006-04-06 05:28:17 +0000
committerkjell <kjell@openbsd.org>2006-04-06 05:28:17 +0000
commitf5827a28f77b7df63f2fb34c39c719dfa1188fa0 (patch)
tree70beaa96edc394757f9ea2e764265893ac44cdc7
parentkudos to djm for finding an embarrassing bug. using the same variable (diff)
downloadwireguard-openbsd-f5827a28f77b7df63f2fb34c39c719dfa1188fa0.tar.xz
wireguard-openbsd-f5827a28f77b7df63f2fb34c39c719dfa1188fa0.zip
Fix a bug whereby a written buffer (^X^W) would not have the correct
trailing buffer number appended (e.g. "file<2>") in case an existing buffer shared its basename().
-rw-r--r--usr.bin/mg/buffer.c31
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/file.c31
3 files changed, 44 insertions, 21 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index 5a2c4644d3e..1d9fc42ef3b 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.55 2006/04/03 00:40:56 deraadt Exp $ */
+/* $OpenBSD: buffer.c,v 1.56 2006/04/06 05:28:17 kjell Exp $ */
/* This file is in the public domain. */
@@ -6,9 +6,11 @@
* Buffer handling.
*/
+#include <libgen.h>
+#include <stdarg.h>
+
#include "def.h"
#include "kbd.h" /* needed for modes */
-#include <stdarg.h>
static struct buffer *makelist(void);
@@ -592,6 +594,31 @@ showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
}
/*
+ * Augment a buffer name with a number, if necessary
+ *
+ * If more than one file of the same basename() is open,
+ * the additional buffers are named "file<2>", "file<3>", and
+ * so forth. This function adjusts a buffer name to
+ * include the number, if necessary.
+ */
+int
+baugname(char *bn, const char *fn, size_t bs)
+{
+ int count;
+ size_t remain, len;
+
+ len = strlcpy(bn, basename(fn), bs);
+ if (len >= bs)
+ return (FALSE);
+
+ remain = bs - len;
+ for (count = 2; bfind(bn, FALSE) != NULL; count++)
+ snprintf(bn + len, remain, "<%d>", count);
+
+ return (TRUE);
+}
+
+/*
* Pop the buffer we got passed onto the screen.
* Returns a status.
*/
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index cc5d93e7a32..a9549d5e195 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.84 2006/04/03 02:43:22 kjell Exp $ */
+/* $OpenBSD: def.h,v 1.85 2006/04/06 05:28:17 kjell Exp $ */
/* This file is in the public domain. */
@@ -386,6 +386,7 @@ int addlinef(struct buffer *, char *, ...);
int anycb(int);
int bclear(struct buffer *);
int showbuffer(struct buffer *, struct mgwin *, int);
+int baugname(char *, const char *, size_t);
struct mgwin *popbuf(struct buffer *);
int bufferinsert(int, int);
int usebuffer(int, int);
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index 1c0e38098b6..2d48a3728cf 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.52 2006/04/03 05:03:34 deraadt Exp $ */
+/* $OpenBSD: file.c,v 1.53 2006/04/06 05:28:17 kjell Exp $ */
/* This file is in the public domain. */
@@ -183,8 +183,6 @@ findbuffer(char *fn)
{
struct buffer *bp;
char bname[NBUFN], fname[NBUFN];
- unsigned int count;
- size_t i, remain;
if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
ewprintf("filename too long");
@@ -195,14 +193,12 @@ findbuffer(char *fn)
if (strcmp(bp->b_fname, fname) == 0)
return (bp);
}
- i = strlcpy(bname, basename(fname), sizeof(bname));
- if (i >= sizeof(bname))
+ /* Not found. Create a new one, adjusting name first */
+ if (baugname(bname, fname, sizeof(bname)) == FALSE)
return (NULL);
- remain = sizeof(bname) - i;
- for (count = 2; bfind(bname, FALSE) != NULL; count++)
- snprintf(&bname[i], remain, "<%d>", count);
- return (bfind(bname, TRUE));
+ bp = bfind(bname, TRUE);
+ return (bp);
}
/*
@@ -475,8 +471,8 @@ int
filewrite(int f, int n)
{
int s;
- char fname[NFILEN];
- char *adjfname, *p, *bufp;
+ char fname[NFILEN], bn[NBUFN];
+ char *adjfname, *bufp;
if ((bufp = eread("Write file: ", fname, NFILEN,
EFNEW | EFCR | EFFILE)) == NULL)
@@ -490,14 +486,13 @@ filewrite(int f, int n)
/* old attributes are no longer current */
bzero(&curbp->b_fi, sizeof(curbp->b_fi));
if ((s = writeout(curbp, adjfname)) == TRUE) {
- (void)strlcpy(curbp->b_fname, adjfname, sizeof curbp->b_fname);
- p = strrchr(curbp->b_fname, '/');
- if (p)
- p++;
- else
- p = curbp->b_fname;
+ (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
free(curbp->b_bname);
- curbp->b_bname = strdup(p);
+ if (baugname(bn, basename(curbp->b_fname), sizeof(bn))
+ == FALSE)
+ return (FALSE);
+ if ((curbp->b_bname = strdup(bn)) == NULL)
+ return (FALSE);
curbp->b_flag &= ~(BFBAK | BFCHG);
upmodes(curbp);
}