113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include <stdio.h>
2513498266Sopenharmony_ci#include <string.h>
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include <curl/curl.h>
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci/* <DESC>
3013498266Sopenharmony_ci * Checks a single file's size and mtime from an FTP server.
3113498266Sopenharmony_ci * </DESC>
3213498266Sopenharmony_ci */
3313498266Sopenharmony_ci
3413498266Sopenharmony_cistatic size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
3513498266Sopenharmony_ci{
3613498266Sopenharmony_ci  (void)ptr;
3713498266Sopenharmony_ci  (void)data;
3813498266Sopenharmony_ci  /* we are not interested in the headers itself,
3913498266Sopenharmony_ci     so we only return the size we would have saved ... */
4013498266Sopenharmony_ci  return (size_t)(size * nmemb);
4113498266Sopenharmony_ci}
4213498266Sopenharmony_ci
4313498266Sopenharmony_ciint main(void)
4413498266Sopenharmony_ci{
4513498266Sopenharmony_ci  char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
4613498266Sopenharmony_ci  CURL *curl;
4713498266Sopenharmony_ci  CURLcode res;
4813498266Sopenharmony_ci  long filetime = -1;
4913498266Sopenharmony_ci  curl_off_t filesize = 0;
5013498266Sopenharmony_ci  const char *filename = strrchr(ftpurl, '/') + 1;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_DEFAULT);
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  curl = curl_easy_init();
5513498266Sopenharmony_ci  if(curl) {
5613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
5713498266Sopenharmony_ci    /* No download if the file */
5813498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
5913498266Sopenharmony_ci    /* Ask for filetime */
6013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
6113498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
6213498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
6313498266Sopenharmony_ci    /* Switch on full protocol/debug output */
6413498266Sopenharmony_ci    /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci    res = curl_easy_perform(curl);
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci    if(CURLE_OK == res) {
6913498266Sopenharmony_ci      /* https://curl.se/libcurl/c/curl_easy_getinfo.html */
7013498266Sopenharmony_ci      res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
7113498266Sopenharmony_ci      if((CURLE_OK == res) && (filetime >= 0)) {
7213498266Sopenharmony_ci        time_t file_time = (time_t)filetime;
7313498266Sopenharmony_ci        printf("filetime %s: %s", filename, ctime(&file_time));
7413498266Sopenharmony_ci      }
7513498266Sopenharmony_ci      res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
7613498266Sopenharmony_ci                              &filesize);
7713498266Sopenharmony_ci      if((CURLE_OK == res) && (filesize>0))
7813498266Sopenharmony_ci        printf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
7913498266Sopenharmony_ci               filename, filesize);
8013498266Sopenharmony_ci    }
8113498266Sopenharmony_ci    else {
8213498266Sopenharmony_ci      /* we failed */
8313498266Sopenharmony_ci      fprintf(stderr, "curl told us %d\n", res);
8413498266Sopenharmony_ci    }
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci    /* always cleanup */
8713498266Sopenharmony_ci    curl_easy_cleanup(curl);
8813498266Sopenharmony_ci  }
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  curl_global_cleanup();
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  return 0;
9313498266Sopenharmony_ci}
94