Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
C programming 2nd term
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ильин Владимир Александрович
C programming 2nd term
Commits
7e35c98b
Commit
7e35c98b
authored
May 04, 2024
by
Ильин Владимир Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1k lines commit
parent
69b53949
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
63 deletions
+62
-63
main.c
avtomat/main.c
+62
-63
No files found.
avtomat/main.c
View file @
7e35c98b
...
...
@@ -8,17 +8,13 @@
#include <unistd.h>
void
usage
();
void
die
(
const
char
*
format
,
...
);
void
die
(
const
char
*
format
,
va_list
args
);
char
*
get_default_filepath
(
char
*
,
char
*
);
CURLcode
download_file
(
char
*
url
,
char
*
filepath
,
CURL
*
curl
);
int
file_exists
(
const
char
*
filename
);
void
download_result_message
(
CURLcode
res
,
char
*
filepath
);
void
download_result_message
(
CURLcode
res
,
char
*
url
,
char
*
filepath
);
void
drop_newline
(
char
*
);
enum
Download_types
{
URL_ARG
,
URL_FILE
,
};
void
die_if_null
(
void
*
obj_ptr
,
const
char
*
format
,
...);
int
main
(
int
argc
,
char
**
argv
)
...
...
@@ -29,14 +25,14 @@ main(int argc, char** argv)
char
*
url_file_path
=
NULL
;
char
*
filepaths_file_path
=
NULL
;
char
*
place_holder
=
NULL
;
// for get_default_filepath func
int
show_progressbar
=
0
;
//get args
if
(
argc
==
1
)
if
(
argc
==
1
||
!
(
strcmp
(
argv
[
1
],
"-h"
)
!=
0
)
)
usage
();
else
if
(
argc
==
2
){
url
=
argv
[
1
];
filepath
=
get_default_filepath
(
url
,
place_holder
);
filepath
=
strchr
(
url
,
'/'
);
}
else
{
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
if
(
!
strcmp
(
argv
[
i
],
"-f"
))
...
...
@@ -47,6 +43,10 @@ main(int argc, char** argv)
url_file_path
=
argv
[
++
i
];
else
if
(
!
strcmp
(
argv
[
i
],
"-ff"
))
filepaths_file_path
=
argv
[
++
i
];
else
if
(
!
strcmp
(
argv
[
i
],
"-h"
))
usage
();
else
if
(
!
strcmp
(
argv
[
i
],
"-p"
))
show_progressbar
=
1
;
else
{
usage
();
}
...
...
@@ -54,21 +54,11 @@ main(int argc, char** argv)
printf
(
"URL: %s
\n
Filepath: %s
\n
URL File: %s
\n
Filepaths File: %s
\n
"
,
url
,
filepath
,
url_file_path
,
filepaths_file_path
);
CURL
*
curl
=
curl_easy_init
();
if
(
!
curl
)
die
(
"Curl init failed!"
);
enum
Download_types
download_type
;
if
(
url
!=
NULL
&&
filepath
!=
NULL
&&
url_file_path
==
NULL
&&
filepaths_file_path
==
NULL
){
// only 1 file
download_type
=
URL_ARG
;
}
else
if
((
url
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
==
NULL
)
||
// -uf
(
url
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
!=
NULL
)){
// -uf and -ff
download_type
=
URL_FILE
;
}
else
{
puts
(
"Unsupported arguments combination given!"
);
usage
();
}
die_if_null
(
curl
,
"Curl init failed!"
);
if
(
show_progressbar
==
1
)
curl_easy_setopt
(
curl
,
CURLOPT_NOPROGRESS
,
0L
);
FILE
*
url_file
=
NULL
;
FILE
*
filepaths_file
=
NULL
;
...
...
@@ -81,66 +71,73 @@ main(int argc, char** argv)
CURLcode
result
;
switch
(
download_type
){
case
URL_ARG
:
if
(
url
!=
NULL
&&
filepath
!=
NULL
&&
url_file_path
==
NULL
&&
filepaths_file_path
==
NULL
){
// only 1 file
//fprintf(stdout, "Starting %s\n", filepath);
result
=
download_file
(
url
,
filepath
,
curl
);
download_result_message
(
result
,
filepath
);
break
;
case
URL_FILE
:
download_result_message
(
result
,
url
,
filepath
);
}
else
if
((
url
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
==
NULL
)
||
// -uf
(
url
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
!=
NULL
)){
// -uf and -ff
url_file
=
fopen
(
url_file_path
,
"r"
);
if
(
filepaths_file_path
)
filepaths_file
=
fopen
(
filepaths_file_path
,
"r"
);
die_if_null
(
url_file
,
"Could not open given url file %s!"
,
url_file_path
);
if
(
!
url_file
)
die
(
"Could not open given url file %s!"
,
url_file_path
);
if
(
filepaths_file_path
&&
!
filepaths_file
)
die
(
"Could not open given file file %s!"
,
filepaths_file_path
);
if
(
filepaths_file_path
)
die_if_null
(
filepaths_file
,
"Could not open given file file %s!"
,
filepaths_file_path
);
while
(
getline
(
&
curr_url
,
&
url_len
,
url_file
)
!=
-
1
){
drop_newline
(
curr_url
);
// Determine the filepath
if
(
filepaths_file_path
!=
NULL
)
{
if
(
filepaths_file_path
!=
NULL
&&
getline
(
&
curr_filepath
,
&
filepath_len
,
filepaths_file
)
!=
-
1
)
{
// Try to read a filepath from the filepaths file
if
(
getline
(
&
curr_filepath
,
&
filepath_len
,
filepaths_file
)
!=
-
1
)
{
drop_newline
(
curr_filepath
);
}
//fallback to default logic if none filepath file given
}
else
{
curr_filepath
=
get_default_filepath
(
curr_url
,
place_holder
);
}
//fallback to default logic if EOF in filepath file or none filepath file given
}
else
curr_filepath
=
strchr
(
curr_url
,
'/'
);
//fprintf(stdout, "Starting %s\n", filepath);
result
=
download_file
(
curr_url
,
curr_filepath
,
curl
);
download_result_message
(
result
,
curr_filepath
);
download_result_message
(
result
,
curr_
url
,
curr_
filepath
);
}
free
(
curr_url
);
if
(
filepaths_file_path
!=
NULL
)
{
free
(
curr_filepath
);
free
(
curr_filepath
);
if
(
filepaths_file_path
!=
NULL
)
fclose
(
filepaths_file
);
}
else
free
(
place_holder
);
fclose
(
url_file
);
break
;
}
}
else
{
puts
(
"Unsupported arguments combination given!"
);
usage
();
}
curl_easy_cleanup
(
curl
);
return
0
;
}
void
die
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
die
(
const
char
*
format
,
va_list
args
)
{
vfprintf
(
stderr
,
format
,
args
);
va_end
(
args
);
fputc
(
'\n'
,
stderr
);
exit
(
EXIT_FAILURE
);
}
void
die_if_null
(
void
*
obj_ptr
,
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
if
(
obj_ptr
==
NULL
)
die
(
format
,
args
);
va_end
(
args
);
}
void
download_result_message
(
CURLcode
res
,
char
*
filepath
)
download_result_message
(
CURLcode
res
,
char
*
url
,
char
*
filepath
)
{
if
(
res
!=
CURLE_OK
)
fprintf
(
stderr
,
"Failed: %s
\n
to download %s
"
,
curl_easy_strerror
(
res
),
filepath
);
fprintf
(
stderr
,
"Failed: %s
\n
to download %s
from %s!
\n
"
,
curl_easy_strerror
(
res
),
filepath
,
url
);
else
fprintf
(
stdout
,
"%s; Downloaded %s
\n
"
,
curl_easy_strerror
(
res
),
filepath
);
}
...
...
@@ -150,17 +147,17 @@ download_file(char *url, char *filepath, CURL *curl)
{
FILE
*
file
;
file
=
fopen
(
filepath
,
"wb"
);
if
(
!
file
){
die
(
"Error: Could not open file for writing: %s
\n
"
,
filepath
)
;
if
(
file
==
NULL
){
fprintf
(
stderr
,
"Error: Could not open file for writing: %s
\n
"
,
filepath
);
return
CURLE_WRITE_ERROR
;
}
curl_easy_setopt
(
curl
,
CURLOPT_URL
,
url
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEDATA
,
file
);
CURLcode
res
=
curl_easy_perform
(
curl
);
fprintf
(
stdout
,
"Starting %s
\n
"
,
filepath
);
fclose
(
file
);
return
res
;
...
...
@@ -170,14 +167,14 @@ char*
get_default_filepath
(
char
*
url
,
char
*
place_holder
)
{
char
*
url_copy
=
realloc
(
place_holder
,
strlen
(
url
)
+
1
);
if
(
!
url_copy
)
{
die
(
"Malloc failed!"
);
}
die_if_null
(
url_copy
,
"Malloc failed!"
);
strcpy
(
url_copy
,
url
);
char
*
filepath
=
url_copy
;
char
*
tmp
=
strtok
(
filepath
,
"/"
);
while
(
tmp
!=
NULL
){
filepath
=
tmp
;
tmp
=
strtok
(
NULL
,
"/"
);
...
...
@@ -193,8 +190,10 @@ usage()
"
\t
-f <filepath> to save file to. Can be omitted; filename from URL will be used.
\n
"
"
\t
-uf <filepath> file with URLs to fetch files from.
\n
"
"
\t
-ff <filepath> file with paths to save files from URLs file. Can be omitted; filename from URL will be used.
\n
"
"
\t
-h print this.
\n
"
"
\t
-p enable default curl progressbar.
\n
"
"At least one of the -u arguments should be given.
\n
"
"Supported combinations:
\n
"
"Supported combinations
for url/filepath options
:
\n
"
"
\t
<url>
\n
"
"
\t
-u <url>
\n
"
"
\t
-u <url> -f <path to save file to>
\n
"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment