#include "curl_ftp.h"
int upload(char *host, char *user, char *pass, char *path, char *rname){
char url[512];
FILE *handle;
CURL *client;
struct stat sbuf;
int size;
int ret;
if( host == NULL || user == NULL || pass == NULL ||
path == NULL || rname == NULL){
return 1;
}
memset(url, 0, sizeof(url));
ret = snprintf(url, sizeof(url)-1, "ftp://%s:%s@%s:21/%s", user, pass, host, rname);
if(ret == -1){
return 2;
}
memset(&sbuf, 0, sizeof(struct stat));
ret = stat(path, &sbuf);
if(ret == -1){
return 3;
}
handle = fopen(path, "r");
if(handle == NULL){
return 4;
}
client = curl_easy_init();
if(client == NULL){
fclose(handle);
return 5;
}
curl_easy_setopt(client, CURLOPT_UPLOAD, 1);
curl_easy_setopt(client, CURLOPT_URL, url);
curl_easy_setopt(client, CURLOPT_READDATA, handle);
curl_easy_setopt(client, CURLOPT_INFILESIZE, sbuf.st_size);
ret = curl_easy_perform(client);
curl_easy_cleanup(client);
fclose(handle);
return 0;
}
#ifndef CURL_FTP_H
#define CURL_FTP_H
int upload(char *host, char *user, char *pass, char *path, char *rname);
#endif