Задача на автомат2

parent e8a538e9
INCS=-lcurl INCS=-lcurl
CFLAGS=-fdiagnostics-color=always -Wall -Wextra -Wpedantic -Wformat -Wshadow -Wunused -Wconversion -Wsign-conversion ${INCS} -O3 -g CFLAGS=-fdiagnostics-color=always -Wall -Wextra -Wpedantic -Wformat -Wshadow -Wunused -Wconversion -Wsign-conversion ${INCS} -g
CC=gcc CC=gcc
all: all:
......
...@@ -4,10 +4,19 @@ ...@@ -4,10 +4,19 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
void usage(); void usage();
void die(const char *);
char *get_default_filepath(char *); char *get_default_filepath(char *);
void download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename);
enum Download_types{
URL_ONLY,
URL_FILE_ONLY,
URL_FILE_AND_FILEPATHS_FILE
};
int int
main(int argc, char** argv) main(int argc, char** argv)
...@@ -15,65 +24,142 @@ main(int argc, char** argv) ...@@ -15,65 +24,142 @@ main(int argc, char** argv)
char *filepath = NULL; char *filepath = NULL;
char *url = NULL; char *url = NULL;
if(argc == 2){ char *url_file_path = NULL;
url = argv[1]; char *filepaths_file_path = NULL;
filepath = get_default_filepath(url); //filepath is a pointer inside url
}else if(argc == 3){ //get args
if(argc == 1)
usage();
else if(argc == 2){
url = argv[1]; url = argv[1];
filepath = argv[2]; filepath = get_default_filepath(url);
}else }else{
for(int i = 1; i < argc; i++)
if(!strcmp(argv[i], "-f"))
filepath = argv[++i];
else if(!strcmp(argv[i], "-u"))
url = argv[++i];
else if(!strcmp(argv[i], "-uf"))
url_file_path = argv[++i];
else if(!strcmp(argv[i], "-ff"))
filepaths_file_path = argv[++i];
else{
usage(); usage();
}
}
printf("url:%s\nfilepath:%s\n", url, filepath); printf("url:%s\nfilepath:%s\nurl file=%s\nfilepath file=%s\n", url, filepath, url_file_path, filepaths_file_path);
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
CURLcode res;
if(!curl) if(!curl)
{ die("Curl init failed!");
fprintf(stderr, "Curl init failled, baling!");
exit(1); enum Download_types download_type;
if (url != NULL && filepath != NULL && url_file_path == NULL && filepaths_file_path == NULL){ // only 1 file
download_type = URL_ONLY;
}else if(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path == NULL){ // -uf
download_type = URL_FILE_ONLY;
}else if(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path != NULL){ // -uf and -ff
download_type = URL_FILE_AND_FILEPATHS_FILE;
}else{
puts("Unsupported arguments combination given!");
usage();
} }
FILE *file; FILE *url_file = NULL;
file = fopen(filepath,"wb"); FILE *filepaths_file = NULL;
switch(download_type){
case URL_ONLY:
download_file(url, filepath, curl);
break;
case URL_FILE_ONLY:
url_file = fopen(url_file_path, "r");
curl_easy_setopt(curl, CURLOPT_URL, url); if(!url_file)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); die("Could not open given url file!");
char *curr_url;
size_t len = 0;
while(getline(&curr_url, &len, url_file) != -1){
if (curr_url[strlen(curr_url) - 1] == '\n')
curr_url[strlen(curr_url) - 1] = '\0';
download_file(curr_url, get_default_filepath(curr_url), curl);
}
free(curr_url);
fclose(url_file);
break;
case URL_FILE_AND_FILEPATHS_FILE:
puts("-uf and -ff case");
break;
}
curl_easy_cleanup(curl);
return 0;
}
void
die(const char * error)
{
fprintf(stderr,"%s\n",error);
exit(EXIT_FAILURE);
}
void
download_file(char *url, char *filepath, CURL *curl)
{
FILE *file;
file = fopen(filepath,"wb");
if(!file){ if(!file){
fprintf(stderr, "Could not open file!"); fprintf(stderr, "Could not open file!");
exit(1); exit(1);
} }
res = curl_easy_perform(curl); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
CURLcode res = curl_easy_perform(curl);
fclose(file);
if(res != CURLE_OK) if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
else else
fprintf(stdout, "curl returned %s; Downloaded %s \n", curl_easy_strerror(res), filepath); 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 *url_copy = malloc(strlen(url) + 1);
if (!url_copy) {
die("Malloc failed!");
}
strcpy(url_copy, url);
char* get_default_filepath(char *url) { char *filepath = url_copy;
char *filepath = strdup(url);
char *tmp = strtok(filepath, "/"); char *tmp = strtok(filepath, "/");
while(tmp != NULL) { while(tmp != NULL){
filepath = tmp; filepath = tmp;
tmp = strtok(NULL, "/"); tmp = strtok(NULL, "/");
} }
//TODO: move malloc and free from here
return filepath; return filepath;
} }
void void
usage(){ usage()
puts("usage: <url> <file path>(optional, default is ./<filename on url>)"); {
puts("Usage:\n\t-u url to get file from, other arguments can be omited.\n\t-f filepath to save file to. Can be omited, filename from url will be used.\n\t-uf file with urls to fetch files from. \n\t-ff filepath file, file with paths to save files from urls file. Can be omited, filename from url will be used.\n\tTo perform at least one of -u arguments should be given.\nSupported combinations:\n\t<url>\n\t-u <url>\n\t-u <url> -f <path to save fileto>\n\t-uf <path to file with urls>\n\t-uf <path to file with url> -ff<path to file with filenames>\n!Program rewrites files!");
exit(0); exit(0);
} }
int file_exists(const char *filename) {
return access(filename, F_OK) != -1;
}
https://dl.suckless.org/tools/9base-6.tar.gz
https://dl.suckless.org/dwm/dwm-6.5.tar.gz
https://dl.suckless.org/st/st-0.9.2.tar.gz
https://dl.suckless.org/tools/dmenu-5.3.tar.gz
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