Лр 7 задания 1 2 3

parent 3b1cfde2
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
int
main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(!curl)
{
fprintf(stderr, "Curl init failed!");
return 1;
}
curl_easy_setopt(curl,CURLOPT_URL , "https://example.com");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl failed with %s\n", curl_easy_strerror(res));
else
fprintf(stdout, "curl succeded with code %d\n", res);
curl_easy_cleanup(curl);
return 0;
}
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
size_t write_function(char *buffer,size_t size, size_t nitems, void *userdata);
int
main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(!curl)
{
fprintf(stderr, "Curl init failed!");
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL , "https://moodle.herzen.spb.ru/pluginfile.php/1671864/mod_resource/content/1/%D0%9B%D0%B0%D0%B1%D0%BE%D1%80%D0%B0%D1%82%D0%BE%D1%80%D0%BD%D0%B0%D1%8F%20%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%207%20-%20libcurl.pdf");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl failed with %s\n", curl_easy_strerror(res));
else
fprintf(stdout, "curl succeded with code %d\n", res);
curl_easy_cleanup(curl);
return 0;
}
size_t
write_function(char *buffer, size_t size, size_t nitems, void * stream)
{
if(stream != NULL)
fwrite(buffer, size, nitems, stream);
return size*nitems;
}
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void usage();
char *get_default_filepath(char *);
int
main(int argc, char** argv)
{
char *filepath = NULL;
char *url = NULL;
if(argc == 2){
url = argv[1];
filepath = get_default_filepath(url); //filepath is a pointer inside url
}else if(argc == 3){
url = argv[1];
filepath = argv[2];
}else
usage();
printf("url:%s\nfilepath:%s\n", url, filepath);
CURL *curl = curl_easy_init();
CURLcode res;
if(!curl)
{
fprintf(stderr, "Curl init failled, baling!");
exit(1);
}
FILE *file;
file = fopen(filepath,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
if(!file){
fprintf(stderr, "Could not open file!");
exit(1);
}
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
else
fprintf(stdout, "curl returned %s; Downloaded %s \n", curl_easy_strerror(res), filepath);
fclose(file);
curl_easy_cleanup(curl);
return 0;
}
char* get_default_filepath(char *url) {
char *filepath = strdup(url);
char *tmp = strtok(filepath, "/");
while(tmp != NULL) {
filepath = tmp;
tmp = strtok(NULL, "/");
}
return filepath;
}
void
usage(){
puts("usage: <url> <file path>(optional, default is ./<filename on url>)");
exit(0);
}
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment