xref: /curl/docs/examples/ftpgetinfo.c (revision fbf5d507)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <curl/curl.h>
28 
29 /* <DESC>
30  * Checks a single file's size and mtime from an FTP server.
31  * </DESC>
32  */
33 
throw_away(void * ptr,size_t size,size_t nmemb,void * data)34 static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
35 {
36   (void)ptr;
37   (void)data;
38   /* we are not interested in the headers itself,
39      so we only return the size we would have saved ... */
40   return (size_t)(size * nmemb);
41 }
42 
main(void)43 int main(void)
44 {
45   char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
46   CURL *curl;
47   CURLcode res;
48   long filetime = -1;
49   curl_off_t filesize = 0;
50   const char *filename = strrchr(ftpurl, '/') + 1;
51 
52   curl_global_init(CURL_GLOBAL_DEFAULT);
53 
54   curl = curl_easy_init();
55   if(curl) {
56     curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
57     /* No download if the file */
58     curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
59     /* Ask for filetime */
60     curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
61     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
62     curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
63     /* Switch on full protocol/debug output */
64     /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
65 
66     res = curl_easy_perform(curl);
67 
68     if(CURLE_OK == res) {
69       /* https://curl.se/libcurl/c/curl_easy_getinfo.html */
70       res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
71       if((CURLE_OK == res) && (filetime >= 0)) {
72         time_t file_time = (time_t)filetime;
73         printf("filetime %s: %s", filename, ctime(&file_time));
74       }
75       res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
76                               &filesize);
77       if((CURLE_OK == res) && (filesize > 0))
78         printf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
79                filename, filesize);
80     }
81     else {
82       /* we failed */
83       fprintf(stderr, "curl told us %d\n", res);
84     }
85 
86     /* always cleanup */
87     curl_easy_cleanup(curl);
88   }
89 
90   curl_global_cleanup();
91 
92   return 0;
93 }
94