ftpgetinfo.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include <stdio.h>
00023 #include <string.h>
00024 
00025 #include <curl/curl.h>
00026 
00027 /* <DESC>
00028  * Checks a single file's size and mtime from an FTP server.
00029  * </DESC>
00030  */
00031 
00032 static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
00033 {
00034   (void)ptr;
00035   (void)data;
00036   /* we are not interested in the headers itself,
00037      so we only return the size we would have saved ... */
00038   return (size_t)(size * nmemb);
00039 }
00040 
00041 int main(void)
00042 {
00043   char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
00044   CURL *curl;
00045   CURLcode res;
00046   long filetime = -1;
00047   double filesize = 0.0;
00048   const char *filename = strrchr(ftpurl, '/') + 1;
00049 
00050   curl_global_init(CURL_GLOBAL_DEFAULT);
00051 
00052   curl = curl_easy_init();
00053   if(curl) {
00054     curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
00055     /* No download if the file */
00056     curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
00057     /* Ask for filetime */
00058     curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
00059     /* No header output: TODO 14.1 http-style HEAD output for ftp */
00060     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
00061     curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
00062     /* Switch on full protocol/debug output */
00063     /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
00064 
00065     res = curl_easy_perform(curl);
00066 
00067     if(CURLE_OK == res) {
00068       /* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
00069       res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
00070       if((CURLE_OK == res) && (filetime >= 0)) {
00071         time_t file_time = (time_t)filetime;
00072         printf("filetime %s: %s", filename, ctime(&file_time));
00073       }
00074       res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
00075                               &filesize);
00076       if((CURLE_OK == res) && (filesize>0.0))
00077         printf("filesize %s: %0.0f bytes\n", filename, filesize);
00078     }
00079     else {
00080       /* we failed */
00081       fprintf(stderr, "curl told us %d\n", res);
00082     }
00083 
00084     /* always cleanup */
00085     curl_easy_cleanup(curl);
00086   }
00087 
00088   curl_global_cleanup();
00089 
00090   return 0;
00091 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02