Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/ftp Factor out common string processing code elimina...
details: https://anonhg.NetBSD.org/src/rev/5b0ce21e3826
branches: trunk
changeset: 573789:5b0ce21e3826
user: christos <christos%NetBSD.org@localhost>
date: Wed Feb 09 23:17:27 2005 +0000
description:
Factor out common string processing code eliminating static buffers,
making functions that should be static be static, and cleaning up
const usage. Added a guard against buffer overflow, but the domap function
is a bit too complicated for me to tackle right now. I will leave it
to the author; hi luke!
diffstat:
usr.bin/ftp/cmds.c | 237 ++++++++++++++++++++++++++------------------------
usr.bin/ftp/extern.h | 5 +-
2 files changed, 124 insertions(+), 118 deletions(-)
diffs (truncated from 487 to 300 lines):
diff -r 4a1a4b929118 -r 5b0ce21e3826 usr.bin/ftp/cmds.c
--- a/usr.bin/ftp/cmds.c Wed Feb 09 23:02:10 2005 +0000
+++ b/usr.bin/ftp/cmds.c Wed Feb 09 23:17:27 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cmds.c,v 1.109 2005/01/03 09:50:09 lukem Exp $ */
+/* $NetBSD: cmds.c,v 1.110 2005/02/09 23:17:27 christos Exp $ */
/*-
* Copyright (c) 1996-2005 The NetBSD Foundation, Inc.
@@ -103,7 +103,7 @@
#if 0
static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
#else
-__RCSID("$NetBSD: cmds.c,v 1.109 2005/01/03 09:50:09 lukem Exp $");
+__RCSID("$NetBSD: cmds.c,v 1.110 2005/02/09 23:17:27 christos Exp $");
#endif
#endif /* not lint */
@@ -150,6 +150,11 @@
static int confirm(const char *, const char *);
+static const char *doprocess(char *, size_t, const char *, int, int, int);
+static const char *domap(char *, size_t, const char *);
+static const char *docase(char *, size_t, const char *);
+static const char *dotrans(char *, size_t, const char *);
+
static int
confirm(const char *cmd, const char *file)
{
@@ -391,9 +396,11 @@
void
put(int argc, char *argv[])
{
+ char buf[MAXPATHLEN];
char *cmd;
int loc = 0;
- char *locfile, *remfile;
+ char *locfile;
+ const char *remfile;
if (argc == 2) {
argc++;
@@ -417,15 +424,26 @@
if (loc) /* If argv[2] is a copy of the old argv[1], update it */
remfile = locfile;
cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
- if (loc && ntflag)
- remfile = dotrans(remfile);
- if (loc && mapflag)
- remfile = domap(remfile);
+ remfile = doprocess(buf, sizeof(buf), remfile,
+ 0, loc && ntflag, loc && mapflag);
sendrequest(cmd, locfile, remfile,
locfile != argv[1] || remfile != argv[2]);
free(locfile);
}
+static const char *
+doprocess(char *dst, size_t dlen, const char *src,
+ int casef, int transf, int mapf)
+{
+ if (casef)
+ src = docase(dst, dlen, src);
+ if (transf)
+ src = dotrans(dst, dlen, src);
+ if (mapf)
+ src = domap(dst, dlen, src);
+ return src;
+}
+
/*
* Send multiple files.
*/
@@ -435,7 +453,7 @@
int i;
sigfunc oldintr;
int ointer;
- char *tp;
+ const char *tp;
if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-files"))) {
fprintf(ttyout, "usage: %s local-files\n", argv[0]);
@@ -456,13 +474,9 @@
continue;
}
if (mflag && confirm(argv[0], cp)) {
- tp = cp;
- if (mcase)
- tp = docase(tp);
- if (ntflag)
- tp = dotrans(tp);
- if (mapflag)
- tp = domap(tp);
+ char buf[MAXPATHLEN];
+ tp = doprocess(buf, sizeof(buf), cp,
+ mcase, ntflag, mapflag);
sendrequest((sunique) ? "STOU" : "STOR",
cp, tp, cp != tp || !interactive);
if (!mflag && fromatty) {
@@ -484,8 +498,9 @@
if (!doglob) {
if (mflag && confirm(argv[0], argv[i])) {
- tp = (ntflag) ? dotrans(argv[i]) : argv[i];
- tp = (mapflag) ? domap(tp) : tp;
+ char buf[MAXPATHLEN];
+ tp = doprocess(buf, sizeof(buf), argv[i],
+ 0, ntflag, mapflag);
sendrequest((sunique) ? "STOU" : "STOR",
argv[i], tp, tp != argv[i] || !interactive);
if (!mflag && fromatty) {
@@ -510,8 +525,10 @@
for (cpp = gl.gl_pathv; cpp && *cpp != NULL && connected;
cpp++) {
if (mflag && confirm(argv[0], *cpp)) {
- tp = (ntflag) ? dotrans(*cpp) : *cpp;
- tp = (mapflag) ? domap(tp) : tp;
+ char buf[MAXPATHLEN];
+ tp = *cpp;
+ tp = doprocess(buf, sizeof(buf), *cpp,
+ 0, ntflag, mapflag);
sendrequest((sunique) ? "STOU" : "STOR",
*cpp, tp, *cpp != tp || !interactive);
if (!mflag && fromatty) {
@@ -554,7 +571,9 @@
getit(int argc, char *argv[], int restartit, const char *mode)
{
int loc, rval;
- char *remfile, *locfile, *olocfile;
+ char *remfile, *olocfile;
+ const char *locfile;
+ char buf[MAXPATHLEN];
loc = rval = 0;
if (argc == 2) {
@@ -576,13 +595,8 @@
code = -1;
return (0);
}
- locfile = olocfile;
- if (loc && mcase)
- locfile = docase(locfile);
- if (loc && ntflag)
- locfile = dotrans(locfile);
- if (loc && mapflag)
- locfile = domap(locfile);
+ locfile = doprocess(buf, sizeof(buf), olocfile,
+ loc && mcase, loc && ntflag, loc && mapflag);
if (restartit) {
struct stat stbuf;
int ret;
@@ -662,7 +676,8 @@
{
sigfunc oldintr;
int ointer;
- char *cp, *tp;
+ char *cp;
+ const char *tp;
int restartit;
if (argc == 0 ||
@@ -687,6 +702,7 @@
if (sigsetjmp(jabort, 1))
mabort();
while ((cp = remglob(argv, proxy, NULL)) != NULL) {
+ char buf[MAXPATHLEN];
if (*cp == '\0' || !connected) {
mflag = 0;
continue;
@@ -700,13 +716,7 @@
}
if (!confirm(argv[0], cp))
continue;
- tp = cp;
- if (mcase)
- tp = docase(tp);
- if (ntflag)
- tp = dotrans(tp);
- if (mapflag)
- tp = domap(tp);
+ tp = doprocess(buf, sizeof(buf), cp, mcase, ntflag, mapflag);
if (restartit) {
struct stat stbuf;
@@ -1881,26 +1891,25 @@
* convert the given name to lower case if it's all upper case, into
* a static buffer which is returned to the caller
*/
-char *
-docase(char *name)
+static const char *
+docase(char *dst, size_t dlen, const char *src)
{
- static char new[MAXPATHLEN];
- int i, dochange;
+ size_t i;
+ int dochange = 1;
- dochange = 1;
- for (i = 0; name[i] != '\0' && i < sizeof(new) - 1; i++) {
- new[i] = name[i];
- if (islower((unsigned char)new[i]))
+ for (i = 0; src[i] != '\0' && i < dlen - 1; i++) {
+ dst[i] = src[i];
+ if (islower((unsigned char)dst[i]))
dochange = 0;
}
- new[i] = '\0';
+ dst[i] = '\0';
if (dochange) {
- for (i = 0; new[i] != '\0'; i++)
- if (isupper((unsigned char)new[i]))
- new[i] = tolower((unsigned char)new[i]);
+ for (i = 0; dst[i] != '\0'; i++)
+ if (isupper((unsigned char)dst[i]))
+ dst[i] = tolower((unsigned char)dst[i]);
}
- return (new);
+ return dst;
}
void
@@ -1935,22 +1944,24 @@
(void)strlcpy(ntout, argv[2], sizeof(ntout));
}
-char *
-dotrans(char *name)
+static const char *
+dotrans(char *dst, size_t dlen, const char *src)
{
- static char new[MAXPATHLEN];
- char *cp1, *cp2 = new;
- int i, ostop, found;
+ const char *cp1;
+ char *cp2 = dst;
+ size_t i, ostop;
for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
continue;
- for (cp1 = name; *cp1; cp1++) {
- found = 0;
+ for (cp1 = src; *cp1; cp1++) {
+ int found = 0;
for (i = 0; *(ntin + i) && i < 16; i++) {
if (*cp1 == *(ntin + i)) {
found++;
if (i < ostop) {
*cp2++ = *(ntout + i);
+ if (cp2 - dst >= dlen - 1)
+ goto out;
}
break;
}
@@ -1959,8 +1970,9 @@
*cp2++ = *cp1;
}
}
+out:
*cp2 = '\0';
- return (new);
+ return dst;
}
void
@@ -1996,12 +2008,12 @@
(void)strlcpy(mapout, cp, MAXPATHLEN);
}
-char *
-domap(char *name)
+static const char *
+domap(char *dst, size_t dlen, const char *src)
{
- static char new[MAXPATHLEN];
- char *cp1 = name, *cp2 = mapin;
- char *tp[9], *te[9];
+ const char *cp1 = src;
+ char *cp2 = mapin;
+ const char *tp[9], *te[9];
int i, toks[9], toknum = 0, match = 1;
for (i=0; i < 9; ++i) {
@@ -2044,130 +2056,127 @@
{
toks[toknum] = 0;
}
- cp1 = new;
- *cp1 = '\0';
- cp2 = mapout;
- while (*cp2) {
+ cp2 = dst;
+ *cp2 = '\0';
+ cp1 = mapout;
+ while (*cp1) {
match = 0;
- switch (*cp2) {
+ switch (*cp1) {
Home |
Main Index |
Thread Index |
Old Index