ftpgetinfo.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <curl/curl.h>
  25. /*
  26. * This is an example showing how to check a single file's size and mtime
  27. * from an FTP server.
  28. */
  29. static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
  30. {
  31. (void)ptr;
  32. (void)data;
  33. /* we are not interested in the headers itself,
  34. so we only return the size we would have saved ... */
  35. return (size_t)(size * nmemb);
  36. }
  37. int main(void)
  38. {
  39. char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
  40. CURL *curl;
  41. CURLcode res;
  42. long filetime = -1;
  43. double filesize = 0.0;
  44. const char *filename = strrchr(ftpurl, '/') + 1;
  45. curl_global_init(CURL_GLOBAL_DEFAULT);
  46. curl = curl_easy_init();
  47. if(curl) {
  48. curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
  49. /* No download if the file */
  50. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  51. /* Ask for filetime */
  52. curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
  53. /* No header output: TODO 14.1 http-style HEAD output for ftp */
  54. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
  55. curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
  56. /* Switch on full protocol/debug output */
  57. /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
  58. res = curl_easy_perform(curl);
  59. if(CURLE_OK == res) {
  60. /* http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
  61. res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
  62. if((CURLE_OK == res) && (filetime >= 0)) {
  63. time_t file_time = (time_t)filetime;
  64. printf("filetime %s: %s", filename, ctime(&file_time));
  65. }
  66. res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
  67. if((CURLE_OK == res) && (filesize>0.0))
  68. printf("filesize %s: %0.0f bytes\n", filename, filesize);
  69. } else {
  70. /* we failed */
  71. fprintf(stderr, "curl told us %d\n", res);
  72. }
  73. /* always cleanup */
  74. curl_easy_cleanup(curl);
  75. }
  76. curl_global_cleanup();
  77. return 0;
  78. }