ftpgetinfo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <curl/curl.h>
  27. /* <DESC>
  28. * Checks a single file's size and mtime from an FTP server.
  29. * </DESC>
  30. */
  31. static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
  32. {
  33. (void)ptr;
  34. (void)data;
  35. /* we are not interested in the headers itself,
  36. so we only return the size we would have saved ... */
  37. return (size_t)(size * nmemb);
  38. }
  39. int main(void)
  40. {
  41. char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
  42. CURL *curl;
  43. CURLcode res;
  44. long filetime = -1;
  45. curl_off_t filesize = 0;
  46. const char *filename = strrchr(ftpurl, '/') + 1;
  47. curl_global_init(CURL_GLOBAL_DEFAULT);
  48. curl = curl_easy_init();
  49. if(curl) {
  50. curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
  51. /* No download if the file */
  52. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  53. /* Ask for filetime */
  54. curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
  55. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
  56. curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
  57. /* Switch on full protocol/debug output */
  58. /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
  59. res = curl_easy_perform(curl);
  60. if(CURLE_OK == res) {
  61. /* https://curl.se/libcurl/c/curl_easy_getinfo.html */
  62. res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
  63. if((CURLE_OK == res) && (filetime >= 0)) {
  64. time_t file_time = (time_t)filetime;
  65. printf("filetime %s: %s", filename, ctime(&file_time));
  66. }
  67. res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
  68. &filesize);
  69. if((CURLE_OK == res) && (filesize>0))
  70. printf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  71. filename, filesize);
  72. }
  73. else {
  74. /* we failed */
  75. fprintf(stderr, "curl told us %d\n", res);
  76. }
  77. /* always cleanup */
  78. curl_easy_cleanup(curl);
  79. }
  80. curl_global_cleanup();
  81. return 0;
  82. }