diff options
author | ache <ache@FreeBSD.org> | 1996-11-14 06:59:41 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 1996-11-14 06:59:41 +0000 |
commit | 22406d98dcada3e438c6d4b3b707192f7fb71969 (patch) | |
tree | fddc91814c20d646f9cb4eb065e29ea8c51cf709 /lib/libftpio/ftpio.c | |
parent | 922ccfd39574911c8000211ea9fcb454fcc70265 (diff) | |
download | FreeBSD-src-22406d98dcada3e438c6d4b3b707192f7fb71969.zip FreeBSD-src-22406d98dcada3e438c6d4b3b707192f7fb71969.tar.gz |
For functions ftpGetURL, ftpPutURL, ftpLogin it was impossible to know
FTP error return code because
1) They return NULL, it means that ftpErrno can't be used because
it takes file pointer
2) They don't have FILE-type argument as f.e. ftpGet/ftpPut to use
it for ftpErrno instead.
For that functions I add yet one int* type argument to store
FTP error return code. It is impossible to add some global variable
for that reason, because user can have multiply FTP connections
opened at the same time.
So, interface changed, major number bumped.
Userland changes will follows.
Minor bugfixes, the code:
Forget to close file in few places, when failure occurse
Forget to NULL cached host name, multiply free is possible
Diffstat (limited to 'lib/libftpio/ftpio.c')
-rw-r--r-- | lib/libftpio/ftpio.c | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/lib/libftpio/ftpio.c b/lib/libftpio/ftpio.c index f9ef0cd..5df25b5 100644 --- a/lib/libftpio/ftpio.c +++ b/lib/libftpio/ftpio.c @@ -14,7 +14,7 @@ * Turned inside out. Now returns xfers as new file ids, not as a special * `state' of FTP_t * - * $Id: ftpio.c,v 1.16 1996/11/14 05:05:26 ache Exp $ + * $Id: ftpio.c,v 1.17 1996/11/14 05:22:12 ache Exp $ * */ @@ -253,11 +253,13 @@ ftpGet(FILE *fp, char *file, off_t *seekto) /* Returns a standard FILE pointer type representing an open control connection */ FILE * -ftpLogin(char *host, char *user, char *passwd, int port, int verbose) +ftpLogin(char *host, char *user, char *passwd, int port, int verbose, int *retcode) { FTP_t n; FILE *fp; + if (retcode) + *retcode = 0; if (networkInit() != SUCCESS) return NULL; @@ -267,6 +269,8 @@ ftpLogin(char *host, char *user, char *passwd, int port, int verbose) fp = funopen(n, ftp_read_method, ftp_write_method, NULL, ftp_close_method); /* BSD 4.4 function! */ fp->_file = n->fd_ctrl; } + if (n && retcode) + *retcode = n->errno; return fp; } @@ -299,7 +303,7 @@ ftpPassive(FILE *fp, int st) } FILE * -ftpGetURL(char *url, char *user, char *passwd) +ftpGetURL(char *url, char *user, char *passwd, int *retcode) { char host[255], name[255]; int port; @@ -307,8 +311,10 @@ ftpGetURL(char *url, char *user, char *passwd) static FILE *fp = NULL; static char *prev_host; + if (retcode) + *retcode = 0; if (get_url_info(url, host, &port, name) == SUCCESS) { - if (prev_host) { + if (fp && prev_host) { if (!strcmp(prev_host, host)) { /* Try to use cached connection */ fp2 = ftpGet(fp, name, NULL); @@ -316,20 +322,30 @@ ftpGetURL(char *url, char *user, char *passwd) /* Connection timed out or was no longer valid */ fclose(fp); free(prev_host); + prev_host = NULL; } else return fp2; } else { /* It's a different host now, flush old */ - free(prev_host); fclose(fp); + free(prev_host); + prev_host = NULL; } } - fp = ftpLogin(host, user, passwd, port, 0); + fp = ftpLogin(host, user, passwd, port, 0, retcode); if (fp) { fp2 = ftpGet(fp, name, NULL); - prev_host = strdup(host); + if (!fp2) { + /* Connection timed out or was no longer valid */ + if (retcode) + *retcode = ftpErrno(fp); + fclose(fp); + fp = NULL; + } + else + prev_host = strdup(host); return fp2; } } @@ -337,21 +353,29 @@ ftpGetURL(char *url, char *user, char *passwd) } FILE * -ftpPutURL(char *url, char *user, char *passwd) +ftpPutURL(char *url, char *user, char *passwd, int *retcode) { char host[255], name[255]; int port; static FILE *fp = NULL; FILE *fp2; + if (retcode) + *retcode = 0; if (fp) { /* Close previous managed connection */ fclose(fp); fp = NULL; } if (get_url_info(url, host, &port, name) == SUCCESS) { - fp = ftpLogin(host, user, passwd, port, 0); + fp = ftpLogin(host, user, passwd, port, 0, retcode); if (fp) { fp2 = ftpPut(fp, name); + if (!fp2) { + if (retcode) + *retcode = ftpErrno(fp); + fclose(fp); + fp = NULL; + } return fp2; } } |