static int ftpdir(char *host, char *user, char *pass, char *root, char *fspec) { if (!ftpOpen(host)) { fprintf(stderr,"Unable to connect to node %s\n%s",host,ftplib_lastresp); return 0; } if (!ftpLogin(user,pass)) { fprintf(stderr,"Login failure\n%s",ftplib_lastresp); return 0; } if (root != NULL) { if (!ftpChdir(root)) { fprintf(stderr,"Change directory failed\n%s",ftplib_lastresp); return 0; } } if (!ftpNlst(NULL,fspec)) { fprintf(stderr,"Directory failure\n%s",ftplib_lastresp); return 0; } ftpQuit(); return 1; }
int ftpget(char *host, char *user, char *pass, char *root, char mode) { char fnm[256]; if (!ftpOpen(host)) { fprintf(stderr,"Unable to connect to node %s\n%s",host,ftplib_lastresp); return 0; } if (!ftpLogin(user,pass)) { fprintf(stderr,"Login failure\n%s",ftplib_lastresp); return 0; } if (root) if (!ftpChdir(root)) { fprintf(stderr,"Change directory to %s failed\n%s", root,ftplib_lastresp); return 0; } while (gets(fnm) != NULL) { if (!ftpGet(fnm,fnm,mode)) { fprintf(stderr,"Get of %s failed\n%s",fnm,ftplib_lastresp); } else { if (ftplib_debug > 1) fprintf(stderr,"File %s received\n",fnm); } } ftpQuit(); return 1; }
error_t ftpClientTest(void) { error_t error; size_t length; IpAddr ipAddr; FtpClientContext ftpContext; static char_t buffer[256]; //Debug message TRACE_INFO("\r\n\r\nResolving server name...\r\n"); //Resolve FTP server name error = getHostByName(NULL, "ftp.gnu.org", &ipAddr, 0); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to resolve server name!\r\n"); //Exit immediately return error; } //Debug message TRACE_INFO("Connecting to FTP server %s\r\n", ipAddrToString(&ipAddr, NULL)); //Connect to the FTP server error = ftpConnect(&ftpContext, NULL, &ipAddr, 21, FTP_NO_SECURITY | FTP_PASSIVE_MODE); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to connect to FTP server!\r\n"); //Exit immediately return error; } //Debug message TRACE_INFO("Successful connection\r\n"); //Start of exception handling block do { //Login to the FTP server using the provided username and password error = ftpLogin(&ftpContext, "anonymous", "password", ""); //Any error to report? if(error) break; //Open the specified file for reading error = ftpOpenFile(&ftpContext, "welcome.msg", FTP_FOR_READING | FTP_BINARY_TYPE); //Any error to report? if(error) break; //Dump the contents of the file while(1) { //Read data error = ftpReadFile(&ftpContext, buffer, sizeof(buffer) - 1, &length, 0); //End of file? if(error) break; //Properly terminate the string with a NULL character buffer[length] = '\0'; //Dump current data TRACE_INFO("%s", buffer); } //End the string with a line feed TRACE_INFO("\r\n"); //Close the file error = ftpCloseFile(&ftpContext); //End of exception handling block } while(0); //Close the connection ftpClose(&ftpContext); //Debug message TRACE_INFO("Connection closed...\r\n"); //Return status code return error; }