Lösung 2 liest den Inhalt des Remote-Directory "." und lädt alle Dateien herunter.
C-Code
/****************************************************
* ftpdown2.c FTP Download
* compile: gcc -Wall -o ftpdown2 ftpdown2.c -lftp
*****************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ftplib.h>
#define REMOTE_HOST "localhost"
#define REMOTE_USER "anonymous"
#define REMOTE_PW "anonymous"
#define FLIST_MAX 64
enum { ERROR_CONNECT,
ERROR_LOGIN,
ERROR_DIR,
ERROR_ACCESS,
ERROR_GET };
void ftp_error(int rtype, int rcode) {
fprintf(stderr, "%s\n", "***FTP Error:");
switch (rtype) {
case ERROR_CONNECT:
fprintf(stderr, "%s", "no connection.");
break;
case ERROR_LOGIN:
fprintf(stderr, "%s", "login not permitted.");
break;
case ERROR_DIR:
fprintf(stderr, "%s", "cannot get remote directory list.");
break;
case ERROR_ACCESS:
fprintf(stderr, "%s", "cannot access remote directory.");
break;
case ERROR_GET:
fprintf(stderr, "%s", "cannot get the remote file.");
break;
default:
fprintf(stderr, "%s", "unknown error.");
}
fprintf(stderr, " (error code = %d)\n", rcode);
}
void ftp_abort(int rtype, int rcode, netbuf *nc) {
ftp_error(rtype, rcode);
FtpQuit(nc);
exit(EXIT_FAILURE);
}
char *comp(char *fname) {
/* remove line feed */
if (fname[strlen(fname)-1] == '\n')
fname[strlen(fname)-1] = '\0';
return fname;
}
char *get_local_path(const char *filename) {
static char local_path[256];
char fname[256];
char tmbuf[16];
char *extn;
time_t t;
strcpy(fname, filename);
if ((extn = strchr(fname, '.')) != NULL) {
*extn = '\0';
++extn;
}
strcpy(local_path, getenv("HOME"));
strcat(local_path, "/Downloads/FTP_");
strcat(local_path, fname);
t = time(NULL);
strftime(tmbuf, sizeof(tmbuf), "_%Y_%m_%d", localtime(&t));
strcat(local_path, tmbuf);
if (extn != NULL) {
strcat(local_path, ".");
strcat(local_path, extn);
}
return local_path;
}
int main(void) {
netbuf *nctrl, *ndata;
char dirlst[256];
char rddbuf[256];
char *flist[FLIST_MAX];
char stbuf[64];
int fcount;
int ftprc;
FtpInit();
/* connect - get handle nctrl */
if ((ftprc = FtpConnect(REMOTE_HOST, &nctrl)) == 0)
ftp_abort(ERROR_CONNECT, ftprc, nctrl);
/* get remote system type */
if ((ftprc = FtpSysType(stbuf, sizeof(stbuf), nctrl)) == 1)
printf("System type of remote host is %s\n", stbuf);
/* login */
if ((ftprc = FtpLogin(REMOTE_USER, REMOTE_PW, nctrl)) == 0)
ftp_abort(ERROR_LOGIN, ftprc, nctrl);
/* download remote directory list */
strcpy(dirlst, get_local_path("dir.lst"));
if ((ftprc = FtpDir(dirlst, "*.txt", nctrl)) == 1)
printf("Download remote directory list: %s.\n", dirlst);
else
ftp_abort(ERROR_DIR, ftprc, nctrl);
/* access remote directory - get handle ndata */
if ((ftprc = FtpAccess(".", FTPLIB_DIR, FTPLIB_ASCII, nctrl, &ndata)) == 0)
ftp_abort(ERROR_ACCESS, ftprc, nctrl);
/* get and store filenames */
fcount=0;
flist[0]=NULL;
memset(rddbuf, 0, sizeof(rddbuf));
while (FtpRead(rddbuf, sizeof(rddbuf), ndata) > 0) {
if ((flist[fcount] = malloc(strlen(comp(rddbuf))+1)) != NULL)
strcpy(flist[fcount], rddbuf);
else
break;
flist[++fcount] = NULL;
if (fcount > FLIST_MAX-2)
break;
}
FtpClose(ndata);
printf("%d remote file(s) found.\n", fcount);
/* get and store files */
fcount=0;
while (flist[fcount] != NULL) {
if ((ftprc = FtpGet(get_local_path(flist[fcount]), flist[fcount], FTPLIB_ASCII, nctrl)) == 0)
ftp_error(ERROR_GET, ftprc);
else
printf("Download: %s -> %s\n", flist[fcount], get_local_path(flist[fcount]));
memset(rddbuf, 0, sizeof(rddbuf));
free(flist[fcount++]);
}
/* clean up */
FtpQuit(nctrl);
return 0;
}