ftpgetinfo.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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.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 
25 #include <curl/curl.h>
26 
27 /* <DESC>
28  * Checks a single file's size and mtime from an FTP server.
29  * </DESC>
30  */
31 
32 static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
33 {
34  (void)ptr;
35  (void)data;
36  /* we are not interested in the headers itself,
37  so we only return the size we would have saved ... */
38  return (size_t)(size * nmemb);
39 }
40 
41 int main(void)
42 {
43  char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
44  CURL *curl;
45  CURLcode res;
46  long filetime = -1;
47  double filesize = 0.0;
48  const char *filename = strrchr(ftpurl, '/') + 1;
49 
51 
52  curl = curl_easy_init();
53  if(curl) {
54  curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
55  /* No download if the file */
56  curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
57  /* Ask for filetime */
58  curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
59  /* No header output: TODO 14.1 http-style HEAD output for ftp */
60  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
61  curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
62  /* Switch on full protocol/debug output */
63  /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
64 
65  res = curl_easy_perform(curl);
66 
67  if(CURLE_OK == res) {
68  /* https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
69  res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
70  if((CURLE_OK == res) && (filetime >= 0)) {
71  time_t file_time = (time_t)filetime;
72  printf("filetime %s: %s", filename, ctime(&file_time));
73  }
75  &filesize);
76  if((CURLE_OK == res) && (filesize>0.0))
77  printf("filesize %s: %0.0f bytes\n", filename, filesize);
78  }
79  else {
80  /* we failed */
81  fprintf(stderr, "curl told us %d\n", res);
82  }
83 
84  /* always cleanup */
85  curl_easy_cleanup(curl);
86  }
87 
89 
90  return 0;
91 }
#define filesize(name, stat_data)
Definition: mime.c:127
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
int main(void)
Definition: ftpgetinfo.c:41
static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
Definition: ftpgetinfo.c:32
#define curl_easy_getinfo(handle, info, arg)
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:2521
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
const char * filename
Definition: ftpget.c:32
static CURL * curl
Definition: sessioninfo.c:35
Definition: debug.c:29
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09