ftpgetinfo.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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. double filesize = 0.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,
  68. &filesize);
  69. if((CURLE_OK == res) && (filesize>0.0))
  70. printf("filesize %s: %0.0f bytes\n", filename, filesize);
  71. }
  72. else {
  73. /* we failed */
  74. fprintf(stderr, "curl told us %d\n", res);
  75. }
  76. /* always cleanup */
  77. curl_easy_cleanup(curl);
  78. }
  79. curl_global_cleanup();
  80. return 0;
  81. }