Date: Thu, 4 Sep 1997 02:38:32 -0600 (MDT) From: Wes Peters <softweyr@xmission.com> To: "Paul Missman" <missmanp@milo.cfw.com> Cc: questions@freebsd.org Subject: Re: FTP under program control Message-ID: <199709040838.CAA14489@obie.softweyr.ml.org> In-Reply-To: <199709040033.TAA07859@nospam.hiwaay.net> References: <missmanp@milo.cfw.com> <199709032320.QAA14728@hub.freebsd.org> <199709040033.TAA07859@nospam.hiwaay.net>
next in thread | previous in thread | raw e-mail | index | archive | help
Paul asked:
% I was interested in using ftp as a mechanism, under program control,
% to transfer files from a series of data collection programs to a
% central database program. I know that the FreeBSD make files for the
% ports programs make use of ftp, to transfer the ports sources
% automatically. How is this being done? Can anyone point me to some
% sample code?
man ftpio. This library contains simple routines to talk to FTP servers
programmatically. Here is a sample, a small command-line ftp client
that allows you to 'stuff' a file onto another system:
/*
* Stuff - ftp put a file on a remote system using a URL.
*/
#include <stdio.h>
#include <ftpio.h>
static char *programName;
void
usage(void)
{
printf("\
usage: %s [-v] host user pass local remote\n\
\n\
\t -a - ascii mode.\n\
\t -b - binary mode.\n\
\t -v - be verbose.\n\
\n\
\t host - hostname to stuff the file on.\n\
\t user - username on host.\n\
\t pass - password for user.\n\
\t local - local file to stuff.\n\
\tremote - directory on remote host.\n",
programName);
}
main(int argc, char *argv[])
{
int status = 0, errors = 0, nbytes;
int verbose = 0, binary = 0, ascii = 0;
char buf[BUFSIZ];
FILE *file;
FILE *serv;
FILE *sess;
char *host, *user, *pass, *local, *remote;
programName = argv[0];
/*
* Parse command-line options.
*/
while (argc > 6 && *argv[1] == '-')
{
char opt = *(argv[1] + 1);
switch (opt)
{
case 'a':
ascii = 1;
if (verbose)
printf("%s: ascii mode.\n", programName);
break;
case 'b':
binary = 1;
if (verbose)
printf("%s: binary mode.\n", programName);
break;
case 'v':
verbose = 1;
printf("%s: being verbose.\n", programName);
break;
default:
printf("%s: unknown option %c.\n", programName, opt);
}
/*
* Fixup the command line so we don't see the options anymore.
*/
++argv;
--argc;
}
if (argc != 6)
{
usage();
return -1;
}
host = argv[1];
user = argv[2];
pass = argv[3];
local = argv[4];
remote = argv[5];
file = fopen(local, "r");
if (file == NULL)
{
printf("%s: cannot open %s for reading.\n", programName, local);
++errors;
}
else
{
printf("Opened %s for reading.\n", local);
}
serv = ftpLogin(host, user, pass, 0, verbose, &status);
if (serv == NULL)
{
printf("%s: cannot open %s@%s for writing.\n", programName, user, host);
++errors;
}
else
{
printf("Opened %s@%s for writing.\n", user, host);
}
if ((status = ftpChdir(serv, remote)) != 0)
{
printf("%s: cannot chdir to %s:%s.\n", programName, host, remote);
++errors;
}
else
{
printf("%s: current working directory %s:%s.\n", programName, host, remote);
}
if (ascii)
{
if ((status = ftpAscii(serv)) != 0)
{
printf("%s: cannot put %s in ASCII mode.\n", programName, host);
++errors;
}
else
{
printf("%s: %s ASCII mode.\n", programName, host, remote);
}
}
if (binary)
{
if ((status = ftpBinary(serv)) != 0)
{
printf("%s: cannot put %s in BINARY mode.\n", programName, host);
++errors;
}
else
{
printf("%s: %s BINARY mode.\n", programName, host, remote);
}
}
sess = ftpPut(serv, local);
if (sess == NULL)
{
printf("%s: cannot create remote file %s.\n", programName, file);
++errors;
}
else
{
printf("%s: created %s.\n", programName, local);
}
if (status)
{
printf("%s: %s\n", programName, ftpErrString(status));
return status;
}
if (errors)
{
return -errors;
}
errors = 0;
while ((nbytes = fread(buf, 1, BUFSIZ, file)) > 0)
{
if (verbose)
printf("Writing %d bytes to block #%d\n", nbytes, errors++);
if (fwrite(buf, 1, nbytes, sess) != nbytes)
{
printf("%s: error writing block #%d.\n", programName, errors - 1);
}
}
fclose(sess);
fclose(serv);
fclose(file);
return status;
}
--
"Where am I, and what am I doing in this handbasket?"
Wes Peters Softweyr LLC
http://www.xmission.com/~softweyr softweyr@xmission.com
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199709040838.CAA14489>
