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
23119ece
Commit
23119ece
authored
May 04, 2024
by
Ильин Владимир Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get_default_filepath refactoring
parent
7e35c98b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
29 deletions
+29
-29
main.c
avtomat/main.c
+29
-29
No files found.
avtomat/main.c
View file @
23119ece
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
void
usage
();
void
usage
();
void
die
(
const
char
*
format
,
va_list
args
);
void
die
(
const
char
*
format
,
va_list
args
);
char
*
get_default_filepath
(
c
har
*
,
char
*
);
char
*
get_default_filepath
(
c
onst
char
*
);
CURLcode
download_file
(
char
*
url
,
char
*
filepath
,
CURL
*
curl
);
CURLcode
download_file
(
char
*
url
,
char
*
filepath
,
CURL
*
curl
);
int
file_exists
(
const
char
*
filename
);
int
file_exists
(
const
char
*
filename
);
void
download_result_message
(
CURLcode
res
,
char
*
url
,
char
*
filepath
);
void
download_result_message
(
CURLcode
res
,
char
*
url
,
char
*
filepath
);
...
@@ -32,7 +32,7 @@ main(int argc, char** argv)
...
@@ -32,7 +32,7 @@ main(int argc, char** argv)
usage
();
usage
();
else
if
(
argc
==
2
){
else
if
(
argc
==
2
){
url
=
argv
[
1
];
url
=
argv
[
1
];
filepath
=
strchr
(
url
,
'/'
);
filepath
=
get_default_filepath
(
url
);
}
else
{
}
else
{
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
if
(
!
strcmp
(
argv
[
i
],
"-f"
))
if
(
!
strcmp
(
argv
[
i
],
"-f"
))
...
@@ -60,23 +60,24 @@ main(int argc, char** argv)
...
@@ -60,23 +60,24 @@ main(int argc, char** argv)
if
(
show_progressbar
==
1
)
if
(
show_progressbar
==
1
)
curl_easy_setopt
(
curl
,
CURLOPT_NOPROGRESS
,
0L
);
curl_easy_setopt
(
curl
,
CURLOPT_NOPROGRESS
,
0L
);
FILE
*
url_file
=
NULL
;
FILE
*
filepaths_file
=
NULL
;
char
*
curr_url
;
char
*
curr_filepath
;
size_t
url_len
=
0
;
size_t
filepath_len
=
0
;
CURLcode
result
;
CURLcode
result
;
if
(
url
!=
NULL
&&
filepath
!=
NULL
&&
url_file_path
==
NULL
&&
filepaths_file_path
==
NULL
){
// only 1 file
if
(
url
!=
NULL
&&
filepath
!=
NULL
&&
url_file_path
==
NULL
&&
filepaths_file_path
==
NULL
){
// only 1 file
//fprintf(stdout, "Starting %s\n", filepath);
//fprintf(stdout, "Starting %s\n", filepath);
result
=
download_file
(
url
,
filepath
,
curl
);
result
=
download_file
(
url
,
filepath
,
curl
);
download_result_message
(
result
,
url
,
filepath
);
download_result_message
(
result
,
url
,
filepath
);
free
(
filepath
);
}
else
if
((
url
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
==
NULL
)
||
// -uf
}
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
==
NULL
&&
filepath
==
NULL
&&
url_file_path
!=
NULL
&&
filepaths_file_path
!=
NULL
)){
// -uf and -ff
FILE
*
url_file
=
NULL
;
FILE
*
filepaths_file
=
NULL
;
char
*
curr_url
;
char
*
curr_filepath
;
size_t
url_len
=
0
;
size_t
filepath_len
=
0
;
url_file
=
fopen
(
url_file_path
,
"r"
);
url_file
=
fopen
(
url_file_path
,
"r"
);
if
(
filepaths_file_path
)
if
(
filepaths_file_path
)
...
@@ -86,7 +87,6 @@ main(int argc, char** argv)
...
@@ -86,7 +87,6 @@ main(int argc, char** argv)
if
(
filepaths_file_path
)
if
(
filepaths_file_path
)
die_if_null
(
filepaths_file
,
"Could not open given file file %s!"
,
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
){
while
(
getline
(
&
curr_url
,
&
url_len
,
url_file
)
!=
-
1
){
drop_newline
(
curr_url
);
drop_newline
(
curr_url
);
// Determine the filepath
// Determine the filepath
...
@@ -95,7 +95,7 @@ main(int argc, char** argv)
...
@@ -95,7 +95,7 @@ main(int argc, char** argv)
drop_newline
(
curr_filepath
);
drop_newline
(
curr_filepath
);
//fallback to default logic if EOF in filepath file or none filepath file given
//fallback to default logic if EOF in filepath file or none filepath file given
}
else
}
else
curr_filepath
=
strchr
(
curr_url
,
'/'
);
curr_filepath
=
get_default_filepath
(
curr_url
);
//fprintf(stdout, "Starting %s\n", filepath);
//fprintf(stdout, "Starting %s\n", filepath);
result
=
download_file
(
curr_url
,
curr_filepath
,
curl
);
result
=
download_file
(
curr_url
,
curr_filepath
,
curl
);
...
@@ -147,14 +147,9 @@ download_file(char *url, char *filepath, CURL *curl)
...
@@ -147,14 +147,9 @@ download_file(char *url, char *filepath, CURL *curl)
{
{
FILE
*
file
;
FILE
*
file
;
file
=
fopen
(
filepath
,
"wb"
);
file
=
fopen
(
filepath
,
"wb"
);
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_URL
,
url
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEDATA
,
file
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEDATA
,
file
);
CURLcode
res
=
curl_easy_perform
(
curl
);
CURLcode
res
=
curl_easy_perform
(
curl
);
...
@@ -164,21 +159,26 @@ download_file(char *url, char *filepath, CURL *curl)
...
@@ -164,21 +159,26 @@ download_file(char *url, char *filepath, CURL *curl)
}
}
char
*
char
*
get_default_filepath
(
c
har
*
url
,
char
*
place_holder
)
get_default_filepath
(
c
onst
char
*
url
)
{
{
char
*
url_c
opy
=
realloc
(
place_holder
,
strlen
(
url
)
+
1
);
char
*
url_c
py
=
malloc
(
strlen
(
url
)
+
1
);
strcpy
(
url_cpy
,
url
);
die_if_null
(
url_c
o
py
,
"Malloc failed!"
);
die_if_null
(
url_cpy
,
"Malloc failed!"
);
strcpy
(
url_copy
,
url
);
char
*
tok_pointer
=
strtok
(
url_cpy
,
"/"
);
char
*
tok_buf
=
tok_pointer
;
char
*
filepath
=
url_copy
;
//case of adress without '/' this will return adress as filename
char
*
tmp
=
strtok
(
filepath
,
"/"
);
while
(
tok_buf
!=
NULL
){
tok_pointer
=
tok_buf
;
while
(
tmp
!=
NULL
){
tok_buf
=
strtok
(
NULL
,
"/"
);
filepath
=
tmp
;
tmp
=
strtok
(
NULL
,
"/"
);
}
}
char
*
filepath
=
malloc
(
strlen
(
tok_pointer
)
+
1
);
die_if_null
(
filepath
,
"Malloc failed!"
);
strcpy
(
filepath
,
tok_pointer
);
free
(
url_cpy
);
return
filepath
;
return
filepath
;
}
}
...
...
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