remove the CVSish $Id$ lines
[platform/upstream/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   /* Check for binutils 2.19.1 from ftp.gnu.org's FTP site. */
32   char ftpurl[] = "ftp://ftp.gnu.org/gnu/binutils/binutils-2.19.1.tar.bz2";
33   CURL *curl;
34   CURLcode res;
35   const time_t filetime;
36   const double filesize;
37   const char *filename = strrchr(ftpurl, '/') + 1;
38
39   curl_global_init(CURL_GLOBAL_DEFAULT);
40
41   curl = curl_easy_init();
42   if(curl) {
43     curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
44     /* No download if the file */
45     curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
46     /* Ask for filetime */
47     curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
48     /* No header output: TODO 14.1 http-style HEAD output for ftp */
49     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
50     curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
51     /* Switch on full protocol/debug output */
52     /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
53
54     res = curl_easy_perform(curl);
55
56     if(CURLE_OK == res) {
57       /* http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
58       res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
59       if((CURLE_OK == res) && filetime)
60         printf("filetime %s: %s", filename, ctime(&filetime));
61       res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
62       if((CURLE_OK == res) && filesize)
63         printf("filesize %s: %0.0f bytes\n", filename, filesize);
64     } else {
65       /* we failed */
66       fprintf(stderr, "curl told us %d\n", res);
67     }
68
69     /* always cleanup */
70     curl_easy_cleanup(curl);
71   }
72
73   curl_global_cleanup();
74
75   return 0;
76 }