Libcurl - Progress Data

Hi,

I codding a download manager,use libcurl library.i can download any file easily.But i want to show file detail in console.Some of them,

  • File Size
  • Percent
  • Left Time

Libcurl has some functions to use,but there isnt good documentation to understand them. i cant find any example to understand.Please help me,Thank you.

Caner Bulut

i solve the problem and want to share with you.

#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

struct FtpFile {
char *filename;
FILE *stream;
};

double *Bar;

int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile )stream;
if(out && !out->stream) {
/
open file for writing /
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /
failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}

size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fread(ptr, size, nmemb, stream);
}

int progress_callback( char Bar,double t,double d,double ultotal,double ulnow)
{
printf("deneme : %f \n",d/t
100);
return 0;
}

void thred(char *url)
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={
"curl.tar.gz",
NULL
};

curl\_global\_init\(CURL\_GLOBAL_DEFAULT\);

curl = curl\_easy_init\(\);
if\(curl\) \{

// printf("%s Indiriliyor...\n",url);
curl_easy_setopt(curl, CURLOPT_URL,url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &Bar);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

  if\(CURLE_OK != res\) \{
    /* we failed */
    fprintf\(stderr, "curl told us %d\\n", res\);
  \}
\}

if\(ftpfile.stream\)
  fclose\(ftpfile.stream\); /* close the local file */

curl\_global_cleanup\(\);

return 0;

}

int main( int argc,
char *argv[] )
{

pthread_t tid[25];
int error,t_count=0,temp_count=0;
int a=0,sabit=0,i=0;
char *dosadi[5];
dosadi[1]="ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz";
dosadi[2]="ftp://ftp.tugraz.at/mirror/devil-linux/devel/sources/1.0/cipe-1.5.4.tar.gz";
dosadi[3]="ftp://ftp.gnu.org/gnu/bash/bash-1.14.7.tar.gz";
dosadi[4]="ftp://ftp.keystealth.org/pub/gnu/gnu/gnu.ps.gz";
dosadi[5]="ftp://ftp.tugraz.at/mirror/devil-linux/devel/sources/1.0/attr-2.4.8.src.tar.gz";

for(i=1; i<=5; i++)
{
error = pthread_create(&tid[i],
NULL,
thred,
dosadi[i]);
}
//
for(i=1; i<=5; i++) {
error = pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d Yokedildi.\n", i);
}

}

Regards
Caner

hello
hi facing problem since 6days to geeting upload a file to ftp server.
pls anyone have source code