diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-05 18:53:46 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-05 18:53:46 +0000 |
commit | 260b4435a85b9f4ae5d70830c1259af7afee256f (patch) | |
tree | 37aac92ff9a19ef32dc04b0d9d70c24a8233a278 /apps | |
parent | 672c0505c6c10c227c9315ed4f94746462392ea5 (diff) | |
download | nuttx-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')
-rwxr-xr-x | apps/examples/ftpc/ftpc_cmds.c | 18 | ||||
-rw-r--r-- | apps/netutils/ftpc/ftpc_getreply.c | 51 | ||||
-rw-r--r-- | apps/netutils/ftpc/ftpc_help.c | 4 | ||||
-rw-r--r-- | apps/netutils/ftpc/ftpc_listdir.c | 8 | ||||
-rw-r--r-- | apps/netutils/ftpc/ftpc_putfile.c | 31 |
5 files changed, 88 insertions, 24 deletions
diff --git a/apps/examples/ftpc/ftpc_cmds.c b/apps/examples/ftpc/ftpc_cmds.c index 42091760b..f95f643cb 100755 --- a/apps/examples/ftpc/ftpc_cmds.c +++ b/apps/examples/ftpc/ftpc_cmds.c @@ -252,21 +252,35 @@ int cmd_rhelp(SESSION handle, int argc, char **argv) int cmd_rls(SESSION handle, int argc, char **argv) { FAR struct ftpc_dirlist_s *dirlist; + FAR char *dirname = NULL; int i; - dirlist = ftpc_listdir(handle, argv[1]); + /* Get the directory listing */ + + if (argc > 1) + { + dirname = argv[1]; + } + + dirlist = ftpc_listdir(handle, dirname); if (!dirlist) { return ERROR; } - printf("%s/\n", argv[1]); + /* Print the directory listing */ + + printf("%s/\n", dirname ? dirname : "."); for (i = 0; i < dirlist->nnames; i++) { printf(" %s\n", dirlist->name[i]); } FFLUSH(); + /* We are responsible for freeing the directory structure allocated by + * ftpc_listdir(). + */ + ftpc_dirfree(dirlist); return OK; } 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); |