summaryrefslogtreecommitdiff
path: root/usr.bin/mg
diff options
context:
space:
mode:
authorKjell Wooding <kjell@cvs.openbsd.org>2011-01-23 00:45:04 +0000
committerKjell Wooding <kjell@cvs.openbsd.org>2011-01-23 00:45:04 +0000
commit307a2a0cc3f0272edc54cc84f2aac346bdf6f89a (patch)
tree75e5dae2ce16decf87b0dd934cd3176ff822532f /usr.bin/mg
parentf0c0cd6043d28df3fc5f831fc45fb40bf80cfcdc (diff)
Wrap basename,dirname with xbasename, xdirname funtions,
which have fewer special cases, and use the strlcpy/strlcat src/dst/buffer len semantic. More portable to boot. Discussion and feedback from Henri Kemppainen
Diffstat (limited to 'usr.bin/mg')
-rw-r--r--usr.bin/mg/buffer.c5
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/dired.c34
-rw-r--r--usr.bin/mg/file.c59
4 files changed, 62 insertions, 39 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index acb0712c366..2b863db2d01 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.76 2011/01/21 19:10:13 kjell Exp $ */
+/* $OpenBSD: buffer.c,v 1.77 2011/01/23 00:45:03 kjell Exp $ */
/* This file is in the public domain. */
@@ -666,8 +666,7 @@ augbname(char *bn, const char *fn, size_t bs)
int count;
size_t remain, len;
- len = strlcpy(bn, basename(fn), bs);
- if (len >= bs)
+ if ((len = xbasename(bn, fn, bs)) >= bs)
return (FALSE);
remain = bs - len;
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index ef26daa3595..9366d71bc64 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.115 2011/01/18 16:25:40 kjell Exp $ */
+/* $OpenBSD: def.h,v 1.116 2011/01/23 00:45:03 kjell Exp $ */
/* This file is in the public domain. */
@@ -357,6 +357,7 @@ int buffsave(struct buffer *);
int makebkfile(int, int);
int writeout(struct buffer *, char *);
void upmodes(struct buffer *);
+size_t xbasename(char *, const char *, size_t);
/* line.c X */
struct line *lalloc(int);
diff --git a/usr.bin/mg/dired.c b/usr.bin/mg/dired.c
index 03e60bb358f..3bf9eec4b32 100644
--- a/usr.bin/mg/dired.c
+++ b/usr.bin/mg/dired.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dired.c,v 1.47 2011/01/18 17:35:42 lum Exp $ */
+/* $OpenBSD: dired.c,v 1.48 2011/01/23 00:45:03 kjell Exp $ */
/* This file is in the public domain. */
@@ -335,7 +335,7 @@ int
d_expunge(int f, int n)
{
struct line *lp, *nlp;
- char fname[NFILEN];
+ char fname[NFILEN], sname[NFILEN];
for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
nlp = lforw(lp);
@@ -346,15 +346,16 @@ d_expunge(int f, int n)
return (FALSE);
case FALSE:
if (unlink(fname) < 0) {
- ewprintf("Could not delete '%s'",
- basename(fname));
+ (void)xbasename(sname, fname, NFILEN);
+ ewprintf("Could not delete '%s'", sname);
return (FALSE);
}
break;
case TRUE:
if (rmdir(fname) < 0) {
- ewprintf("Could not delete directory '%s'",
- basename(fname));
+ (void)xbasename(sname, fname, NFILEN);
+ ewprintf("Could not delete directory "
+ "'%s'", sname);
return (FALSE);
}
break;
@@ -371,7 +372,7 @@ d_expunge(int f, int n)
int
d_copy(int f, int n)
{
- char frname[NFILEN], toname[NFILEN], *bufp;
+ char frname[NFILEN], toname[NFILEN], sname[NFILEN], *bufp;
int ret;
size_t off;
struct buffer *bp;
@@ -385,8 +386,10 @@ d_copy(int f, int n)
ewprintf("Directory name too long");
return (FALSE);
}
- if ((bufp = eread("Copy %s to: ", toname, sizeof(toname),
- EFDEF | EFNEW | EFCR, basename(frname))) == NULL)
+ (void)xbasename(sname, frname, NFILEN);
+ bufp = eread("Copy %s to: ", toname, sizeof(toname),
+ EFDEF | EFNEW | EFCR, sname);
+ if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
@@ -405,6 +408,7 @@ d_rename(int f, int n)
int ret;
size_t off;
struct buffer *bp;
+ char sname[NFILEN];
if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
ewprintf("Not a file");
@@ -415,8 +419,10 @@ d_rename(int f, int n)
ewprintf("Directory name too long");
return (FALSE);
}
- if ((bufp = eread("Rename %s to: ", toname,
- sizeof(toname), EFDEF | EFNEW | EFCR, basename(frname))) == NULL)
+ (void)xbasename(sname, frname, NFILEN);
+ bufp = eread("Rename %s to: ", toname,
+ sizeof(toname), EFDEF | EFNEW | EFCR, sname);
+ if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
@@ -452,6 +458,7 @@ d_shell_command(int f, int n)
struct buffer *bp;
struct mgwin *wp;
FILE *fin;
+ char sname[NFILEN];
bp = bfind("*Shell Command Output*", TRUE);
if (bclear(bp) != TRUE)
@@ -463,8 +470,9 @@ d_shell_command(int f, int n)
}
command[0] = '\0';
- if ((bufp = eread("! on %s: ", command, sizeof(command), EFNEW,
- basename(fname))) == NULL)
+ (void)xbasename(sname, fname, NFILEN);
+ bufp = eread("! on %s: ", command, sizeof(command), EFNEW, sname);
+ if (bufp == NULL)
return (ABORT);
infd = open(fname, O_RDONLY);
if (infd == -1) {
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index 3d7e23660c1..df1c81fadb8 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.74 2011/01/21 19:10:13 kjell Exp $ */
+/* $OpenBSD: file.c,v 1.75 2011/01/23 00:45:03 kjell Exp $ */
/* This file is in the public domain. */
@@ -10,7 +10,7 @@
#include <libgen.h>
-static char *xdirname(const char *);
+size_t xdirname(char *, const char *, size_t);
/*
* Insert a file into the current buffer. Real easy - just call the
@@ -292,7 +292,6 @@ 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(FFRAND, 0);
@@ -311,10 +310,8 @@ insertfile(char *fname, char *newname, int replacebuf)
bp = curbp;
if (newname != NULL) {
(void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
- dp = xdirname(newname);
- (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
+ (void)xdirname(bp->b_cwd, newname, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
- free(dp);
}
/* hard file open */
@@ -335,16 +332,15 @@ insertfile(char *fname, char *newname, int replacebuf)
goto cleanup;
}
killbuffer(bp);
- if ((bp = dired_(fname)) == NULL)
- return (FALSE);
+ bp = dired_(fname);
undo_enable(FFRAND, x);
+ if (bp == NULL)
+ return (FALSE);
curbp = bp;
return (showbuffer(bp, curwp, WFFULL | WFMODE));
} else {
- dp = xdirname(fname);
- (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd));
+ (void)xdirname(bp->b_cwd, fname, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
- free(dp);
}
opos = curwp->w_doto;
oline = curwp->w_dotline;
@@ -661,19 +657,38 @@ upmodes(struct buffer *bp)
}
/*
- * Same as dirname, except an empty string is returned in
+ * dirname using strlcpy semantic.
+ * Like 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.
+ * Address portability issues by copying argument
+ * before using. Some implementations modify the input string.
*/
-static char *
-xdirname(const char *path)
+size_t
+xdirname(char *dp, const char *path, size_t dplen)
{
- char *dp;
-
- dp = dirname(path);
- if (*dp && dp[0] == '/' && dp[1] == '\0')
- return (strdup(""));
-
- return (strdup(dp));
+ char ts[NFILEN];
+ size_t len;
+
+ (void)strlcpy(ts, path, NFILEN);
+ len = strlcpy(dp, dirname(ts), dplen);
+ if (dplen > 0 && dp[0] == '/' && dp[1] == '\0') {
+ dp[0] = '\0';
+ len = 0;
+ }
+ return (len);
+}
+
+/*
+ * basename using strlcpy/strlcat semantic.
+ * Address portability issue by copying argument
+ * before using: some implementations modify the input string.
+ */
+size_t
+xbasename(char *bp, const char *path, size_t bplen)
+{
+ char ts[NFILEN];
+
+ (void)strlcpy(ts, path, NFILEN);
+ return (strlcpy(bp, basename(ts), bplen));
}