From owner-freebsd-questions Thu Sep 4 01:33:54 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.7/8.8.7) id BAA15686 for questions-outgoing; Thu, 4 Sep 1997 01:33:54 -0700 (PDT) Received: from obie.softweyr.ml.org ([199.104.124.49]) by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id BAA15680 for ; Thu, 4 Sep 1997 01:33:47 -0700 (PDT) Received: (from wes@localhost) by obie.softweyr.ml.org (8.7.5/8.6.12) id CAA14489; Thu, 4 Sep 1997 02:38:32 -0600 (MDT) Date: Thu, 4 Sep 1997 02:38:32 -0600 (MDT) Message-Id: <199709040838.CAA14489@obie.softweyr.ml.org> From: Wes Peters To: "Paul Missman" CC: questions@freebsd.org Subject: Re: FTP under program control In-Reply-To: <199709040033.TAA07859@nospam.hiwaay.net> References: <199709032320.QAA14728@hub.freebsd.org> <199709040033.TAA07859@nospam.hiwaay.net> Sender: owner-freebsd-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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 #include 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