summaryrefslogtreecommitdiff
path: root/noncore/net/ftplib/ftplib.c
Side-by-side diff
Diffstat (limited to 'noncore/net/ftplib/ftplib.c') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/ftplib/ftplib.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/noncore/net/ftplib/ftplib.c b/noncore/net/ftplib/ftplib.c
index efcd6f0..addf9d2 100644
--- a/noncore/net/ftplib/ftplib.c
+++ b/noncore/net/ftplib/ftplib.c
@@ -1127,97 +1127,101 @@ GLOBALDEF int FtpPwd(char *path, int max, netbuf *nControl)
{
int l = max;
char *b = path;
char *s;
if (!FtpSendCmd("PWD",'2',nControl))
return 0;
s = strchr(nControl->response, '"');
if (s == NULL)
return 0;
s++;
while ((--l) && (*s) && (*s != '"'))
*b++ = *s++;
*b++ = '\0';
return 1;
}
/*
* FtpXfer - issue a command and transfer data
*
* return 1 if successful, 0 otherwise
*/
static int FtpXfer(const char *localfile, const char *path,
netbuf *nControl, int typ, int mode)
{
int l,c;
char *dbuf;
FILE *local = NULL;
netbuf *nData;
int rv=1;
if (localfile != NULL)
{
char ac[4] = "w";
if (typ == FTPLIB_FILE_WRITE)
ac[0] = 'r';
if (mode == FTPLIB_IMAGE)
ac[1] = 'b';
local = fopen(localfile, ac);
if (local == NULL)
{
strncpy(nControl->response, strerror(errno),
sizeof(nControl->response));
return 0;
}
}
if (local == NULL)
local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
if (!FtpAccess(path, typ, mode, nControl, &nData))
+ {
+ if (localfile != NULL)
+ fclose(local);
return 0;
+ }
dbuf = malloc(FTPLIB_BUFSIZ);
if (typ == FTPLIB_FILE_WRITE)
{
while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
if ((c = FtpWrite(dbuf, l, nData)) < l)
{
printf("short write: passed %d, wrote %d\n", l, c);
rv = 0;
break;
}
}
else
{
while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
if (fwrite(dbuf, 1, l, local) <= 0)
{
perror("localfile write");
rv = 0;
break;
}
}
free(dbuf);
fflush(local);
if (localfile != NULL)
fclose(local);
FtpClose(nData);
return rv;
}
/*
* FtpNlst - issue an NLST command and write response to output
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpNlst(const char *outputfile, const char *path,
netbuf *nControl)
{
return FtpXfer(outputfile, path, nControl, FTPLIB_DIR, FTPLIB_ASCII);
}
/*
* FtpDir - issue a LIST command and write response to output
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpDir(const char *outputfile, const char *path, netbuf *nControl)
{
return FtpXfer(outputfile, path, nControl, FTPLIB_DIR_VERBOSE, FTPLIB_ASCII);