ftpgetinfo.c 2.3 KB

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