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
0b730f7f
Commit
0b730f7f
authored
May 02, 2024
by
Ильин Владимир Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Убран лишний case URL_FILE_AND_FILEPATH_FILE
parent
2ab5d5b5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
46 deletions
+38
-46
main.c
avtomat/main.c
+38
-46
No files found.
avtomat/main.c
View file @
0b730f7f
#include <curl/curl.h>
#include <curl/easy.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
...
...
@@ -13,9 +14,8 @@ 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
URL_ARG
,
URL_FILE
,
};
int
...
...
@@ -26,6 +26,7 @@ main(int argc, char** argv)
char
*
url_file_path
=
NULL
;
char
*
filepaths_file_path
=
NULL
;
char
*
place_holder
=
NULL
;
// for get_default_filepath func
//get args
...
...
@@ -58,11 +59,10 @@ main(int argc, char** argv)
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
;
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
();
...
...
@@ -70,59 +70,51 @@ main(int argc, char** argv)
FILE
*
url_file
=
NULL
;
FILE
*
filepaths_file
=
NULL
;
char
*
curr_url
;
char
*
curr_filepath
;
size_t
url_len
=
0
;
size_t
filepath_len
=
0
;
switch
(
download_type
){
case
URL_
ONLY
:
case
URL_
ARG
:
download_file
(
url
,
filepath
,
curl
);
break
;
//TODO:refactor (merge if possible) 2 cases below
case
URL_FILE_ONLY
:
case
URL_FILE
:
url_file
=
fopen
(
url_file_path
,
"r"
);
if
(
!
url_file
)
die
(
"Could not open given url file %s!"
,
url_file_path
);
while
(
getline
(
&
curr_url
,
&
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
,
place_holder
),
curl
);
}
free
(
place_holder
);
free
(
curr_url
);
fclose
(
url_file
);
break
;
case
URL_FILE_AND_FILEPATHS_FILE
:
url_file
=
fopen
(
url_file_path
,
"r"
);
filepaths_file
=
fopen
(
filepaths_file_path
,
"r"
);
if
(
!
url_file
)
die
(
"Could not open given url file %s!"
,
url_file_path
);
if
(
!
filepaths_file
)
die
(
"Could not open given filepath file %s!"
,
filepaths_file_path
);
while
(
getline
(
&
curr_url
,
&
filepath_len
,
url_file
)
!=
-
1
){
if
(
getline
(
&
curr_filepath
,
&
url_len
,
filepaths_file
)
==
-
1
)
curr_filepath
=
get_default_filepath
(
curr_url
,
place_holder
);
// Determine the filepath
char
*
download_filepath
=
NULL
;
if
(
filepaths_file_path
!=
NULL
)
{
// Try to read a filepath from the filepaths file
if
(
getline
(
&
curr_filepath
,
&
filepath_len
,
filepaths_file
)
!=
-
1
)
{
if
(
curr_filepath
[
strlen
(
curr_filepath
)
-
1
]
==
'\n'
)
curr_filepath
[
strlen
(
curr_filepath
)
-
1
]
=
'\0'
;
download_filepath
=
curr_filepath
;
}
}
// If no filepath was read from the filepaths file, use the default logic
if
(
!
download_filepath
)
{
download_filepath
=
get_default_filepath
(
curr_url
,
place_holder
);
}
if
(
curr_url
[
strlen
(
curr_url
)
-
1
]
==
'\n'
)
curr_url
[
strlen
(
curr_url
)
-
1
]
=
'\0'
;
download_file
(
curr_url
,
curr_filepath
,
curl
);
download_file
(
curr_url
,
download_filepath
,
curl
);
}
free
(
place_holder
);
free
(
curr_url
);
if
(
filepaths_file_path
!=
NULL
)
{
free
(
curr_filepath
);
fclose
(
url_file
);
fclose
(
filepaths_file
);
}
fclose
(
url_file
);
break
;
}
curl_easy_cleanup
(
curl
);
...
...
@@ -154,11 +146,12 @@ download_file(char *url, char *filepath, CURL *curl)
CURLcode
res
=
curl_easy_perform
(
curl
);
fprintf
(
stdout
,
"Starting %s
\n
"
,
filepath
);
fclose
(
file
);
if
(
res
!=
CURLE_OK
)
fprintf
(
stderr
,
"
curl_easy_perform() failed: %s
\n
"
,
curl_easy_strerror
(
res
)
);
fprintf
(
stderr
,
"
Failed: %s
\n
to download %s"
,
curl_easy_strerror
(
res
),
filepath
);
else
fprintf
(
stdout
,
"
curl returned
%s; Downloaded %s
\n
"
,
curl_easy_strerror
(
res
),
filepath
);
fprintf
(
stdout
,
"%s; Downloaded %s
\n
"
,
curl_easy_strerror
(
res
),
filepath
);
}
char
*
...
...
@@ -199,7 +192,6 @@ usage()
exit
(
0
);
}
int
file_exists
(
const
char
*
filename
)
{
return
access
(
filename
,
F_OK
)
!=
-
1
;
}
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