summaryrefslogtreecommitdiff
path: root/apps/netutils/ftpc
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-06-05 18:53:46 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-06-05 18:53:46 +0000
commit260b4435a85b9f4ae5d70830c1259af7afee256f (patch)
tree37aac92ff9a19ef32dc04b0d9d70c24a8233a278 /apps/netutils/ftpc
parent672c0505c6c10c227c9315ed4f94746462392ea5 (diff)
downloadnuttx-260b4435a85b9f4ae5d70830c1259af7afee256f.tar.gz
nuttx-260b4435a85b9f4ae5d70830c1259af7afee256f.tar.bz2
nuttx-260b4435a85b9f4ae5d70830c1259af7afee256f.zip
More FTP fixes
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3672 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps/netutils/ftpc')
-rw-r--r--apps/netutils/ftpc/ftpc_getreply.c51
-rw-r--r--apps/netutils/ftpc/ftpc_help.c4
-rw-r--r--apps/netutils/ftpc/ftpc_listdir.c8
-rw-r--r--apps/netutils/ftpc/ftpc_putfile.c31
4 files changed, 72 insertions, 22 deletions
diff --git a/apps/netutils/ftpc/ftpc_getreply.c b/apps/netutils/ftpc/ftpc_getreply.c
index 50d392a5f..058eaf213 100644
--- a/apps/netutils/ftpc/ftpc_getreply.c
+++ b/apps/netutils/ftpc/ftpc_getreply.c
@@ -72,7 +72,7 @@
static int ftpc_gets(struct ftpc_session_s *session)
{
int ch;
- int i = 0;
+ int ndx = 0;
/* Start wth an empty response string */
@@ -93,12 +93,18 @@ static int ftpc_gets(struct ftpc_session_s *session)
/* Get the next character from incoming command stream */
ch = ftpc_sockgetc(&session->cmd);
+
+ /* Check if the command stream was closed */
+
if (ch == EOF)
{
ndbg("EOF: Server closed command stream\n");
ftpc_reset(session);
return ERROR;
}
+
+ /* Handle embedded Telnet stuff */
+
else if (ch == TELNET_IAC)
{
/* Handle TELNET commands */
@@ -125,26 +131,43 @@ static int ftpc_gets(struct ftpc_session_s *session)
continue;
}
+
+ /* Deal with carriage returns */
+
else if (ch == ISO_cr)
{
+ /* What follows the carriage return? */
+
ch = ftpc_sockgetc(&session->cmd);
if (ch == '\0')
{
+ /* If it is followed by a NUL then keep it */
+
ch = ISO_cr;
}
+
+ /* If it is followed by a newline then break out of the loop. */
+
else if (ch == ISO_nl)
{
- session->reply[i++] = (char)ch;
+ /* Newline terminates the reply */
+
break;
}
+
+ /* If we did not lose the connection, then push the character
+ * following the carriage back on the "stack" and continue to
+ * examine it from scratch (if could be part of the Telnet
+ * protocol).
+ */
+
else if (ch != EOF)
{
- /* TELNET protocol */
-
ungetc(ch, session->cmd.instream);
continue;
}
}
+
else if (ch == ISO_nl)
{
/* The ISO newline character terminates the string. Just break
@@ -154,21 +177,24 @@ static int ftpc_gets(struct ftpc_session_s *session)
break;
}
- if (i < CONFIG_FTP_MAXREPLY)
+ /* Put the character into the response buffer. Is there space for
+ * another character in the reply buffer?
+ */
+
+ if (ndx < CONFIG_FTP_MAXREPLY)
{
- session->reply[i++] = (char)ch;
+ /* Yes.. put the character in the reply buffer */
+
+ session->reply[ndx++] = (char)ch;
}
-
- if (i >= CONFIG_FTP_MAXREPLY)
+ else
{
ndbg("Reply truncated\n");
- i = CONFIG_FTP_MAXREPLY;
}
}
- session->reply[i] = '\0';
+ session->reply[ndx] = '\0';
session->code = atoi(session->reply);
- ftpc_stripcrlf(session->reply);
return session->code;
}
@@ -233,7 +259,8 @@ int fptc_getreply(struct ftpc_session_s *session)
}
nvdbg("Reply: %s\n", session->reply);
- } while(strncmp(tmp, session->reply, 4) != 0);
+ }
+ while (strncmp(tmp, session->reply, 4) != 0);
}
wd_cancel(session->wdog);
diff --git a/apps/netutils/ftpc/ftpc_help.c b/apps/netutils/ftpc/ftpc_help.c
index 63e3132b8..19041bc24 100644
--- a/apps/netutils/ftpc/ftpc_help.c
+++ b/apps/netutils/ftpc/ftpc_help.c
@@ -92,7 +92,9 @@ int ftpc_help(SESSION handle, FAR const char *arg)
ret = ftpc_cmd(session, "HELP");
}
- /* Logic is missing here to return the help string to the caller */
+ /* Logic is missing here to return the help string to the caller. The caller
+ * needs to call ftpc_getreply.
+ */
return ret;
}
diff --git a/apps/netutils/ftpc/ftpc_listdir.c b/apps/netutils/ftpc/ftpc_listdir.c
index e3af9b122..1bfda8234 100644
--- a/apps/netutils/ftpc/ftpc_listdir.c
+++ b/apps/netutils/ftpc/ftpc_listdir.c
@@ -341,12 +341,15 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
/* Count the number of names in the temporary file */
rewind(filestream);
+ nnames = 0;
+
ftpc_nlstparse(filestream, ftpc_dircount, &nnames);
if (!nnames)
{
ndbg("Nothing found in directory\n");
goto errout;
}
+ nvdbg("nnames: %d\n", nnames);
/* Allocate and initialize a directory container */
@@ -357,12 +360,13 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
ndbg("Failed to allocate dirlist\n");
goto errout;
}
- dirlist->nnames = 0;
/* Then copy all of the directory strings into the container */
rewind(filestream);
- ftpc_nlstparse(filestream, ftpc_dircount, &nnames);
+ dirlist->nnames = 0;
+
+ ftpc_nlstparse(filestream, ftpc_addname, dirlist);
DEBUGASSERT(nnames == dirlist->nnames);
}
diff --git a/apps/netutils/ftpc/ftpc_putfile.c b/apps/netutils/ftpc/ftpc_putfile.c
index 4a15d7410..db4ec9680 100644
--- a/apps/netutils/ftpc/ftpc_putfile.c
+++ b/apps/netutils/ftpc/ftpc_putfile.c
@@ -87,7 +87,18 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
ssize_t nwritten;
int ret = OK;
+ /* Allocate a buffer to hold the binary data */
+
buf = (char *)malloc(CONFIG_FTP_BUFSIZE);
+ if (!buf)
+ {
+ ndbg("Failed to allocate an I/O buffer\n");
+ set_errno(ENOMEM);
+ return ERROR;
+ }
+
+ /* Loop until the entire file is sent */
+
for (;;)
{
/* Read data from the file */
@@ -102,6 +113,9 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
(void)ftpc_xfrabort(session, linstream);
ret = ERROR;
}
+
+ /* Break out of the loop */
+
break;
}
@@ -111,8 +125,11 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
if (nwritten != nread)
{
(void)ftpc_xfrabort(session, routstream);
+
+ /* Break out of the loop and return failue */
+
ret = ERROR;
- break;
+ break;
}
/* Increment the size of the file sent */
@@ -136,16 +153,16 @@ static int ftpc_sendtext(FAR struct ftpc_session_s *session,
FAR FILE *linstream, FAR FILE *routstream)
{
char *buf = (char *)malloc(CONFIG_FTP_BUFSIZE);
- int c;
+ int ch;
int ret = OK;
/* Write characters one at a time. */
- while ((c = fgetc(linstream)) != EOF)
+ while ((ch = fgetc(linstream)) != EOF)
{
/* If it is a newline, send a carriage return too */
- if (c == '\n')
+ if (ch == '\n')
{
if (fputc('\r', routstream) == EOF)
{
@@ -161,7 +178,7 @@ static int ftpc_sendtext(FAR struct ftpc_session_s *session,
/* Send the character */
- if (fputc(c, routstream) == EOF)
+ if (fputc(ch, routstream) == EOF)
{
(void)ftpc_xfrabort(session, routstream);
ret = ERROR;
@@ -340,11 +357,11 @@ static int ftpc_sendfile(struct ftpc_session_s *session, const char *path,
if (xfrmode == FTPC_XFRMODE_ASCII)
{
- ret = ftpc_sendtext(session, stream, session->data.instream);
+ ret = ftpc_sendtext(session, stream, session->data.outstream);
}
else
{
- ret = ftpc_sendbinary(session, stream, session->data.instream);
+ ret = ftpc_sendbinary(session, stream, session->data.outstream);
}
ftpc_sockflush(&session->data);