Git init
[external/curl.git] / docs / examples / ftpgetinfo.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <curl/curl.h>
14 #include <curl/types.h>
15 #include <curl/easy.h>
16
17 /*
18  * This is an example showing how to check a single file's size and mtime
19  * from an FTP server.
20  */
21
22 static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
23 {
24   /* we are not interested in the headers itself,
25      so we only return the size we would have saved ... */
26   return (size_t)(size * nmemb);
27 }
28
29 int main(void)
30 {
31   char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
32   CURL *curl;
33   CURLcode res;
34   const time_t filetime;
35   const double filesize;
36   const char *filename = strrchr(ftpurl, '/') + 1;
37
38   curl_global_init(CURL_GLOBAL_DEFAULT);
39
40   curl = curl_easy_init();
41   if(curl) {
42     curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
43     /* No download if the file */
44     curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
45     /* Ask for filetime */
46     curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
47     /* No header output: TODO 14.1 http-style HEAD output for ftp */
48     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
49     curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
50     /* Switch on full protocol/debug output */
51     /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
52
53     res = curl_easy_perform(curl);
54
55     if(CURLE_OK == res) {
56       /* http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
57       res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
58       if((CURLE_OK == res) && filetime)
59         printf("filetime %s: %s", filename, ctime(&filetime));
60       res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
61       if((CURLE_OK == res) && filesize)
62         printf("filesize %s: %0.0f bytes\n", filename, filesize);
63     } else {
64       /* we failed */
65       fprintf(stderr, "curl told us %d\n", res);
66     }
67
68     /* always cleanup */
69     curl_easy_cleanup(curl);
70   }
71
72   curl_global_cleanup();
73
74   return 0;
75 }