diff --git a/src/base64/ft_base64.c b/src/base64/ft_base64.c index f9fdf404..9caa50a5 100644 --- a/src/base64/ft_base64.c +++ b/src/base64/ft_base64.c @@ -1,15 +1,36 @@ #include #include +#include #include #include "libft.h" #include "ft_base64.h" -static int open_stream(char *filename, int flags, int mode) +static int open_input(char *filename) { int fd; - if ((fd = open((const char *)filename, flags, mode)) == -1) + struct stat stat_buff; + + if ((fd = open((const char *)filename, O_RDONLY, 0)) == -1) { - perror("base64"); + perror("base64: input stream error"); + exit(1); + } + stat(filename, &stat_buff); + if (S_ISDIR(stat_buff.st_mode)) + { + ft_putstr_fd("base64: input path is not a file", STDERR_FILENO); + exit(1); + } + return fd; +} + +static int open_output(char *filename) +{ + int fd; + if ((fd = open((const char *)filename, + O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1) + { + perror("base64: output stream error"); exit(1); } return fd; @@ -42,10 +63,9 @@ static void read_args else if (ft_strcmp(current_arg, "-e") == 0 && ++i) continue; else if (ft_strcmp(current_arg, "-i") == 0) - ctx->input_fd = open_stream(next_arg, O_RDONLY, 0); + ctx->input_fd = open_input(next_arg); else if (ft_strcmp(current_arg, "-o") == 0) - ctx->output_fd = open_stream(next_arg, O_CREAT | O_WRONLY, - S_IRUSR | S_IWUSR); + ctx->output_fd = open_output(next_arg); else if (++i) continue; i++; diff --git a/src/base64/ft_base64_decode_finish.c b/src/base64/ft_base64_decode_finish.c index 7b352c2b..241fd6cc 100644 --- a/src/base64/ft_base64_decode_finish.c +++ b/src/base64/ft_base64_decode_finish.c @@ -1,3 +1,4 @@ +#include #include "ft_base64.h" #include "libft.h" @@ -13,8 +14,8 @@ void ft_base64_decode_finish return ; if (buffer_index != 0) { - ft_putstr("\nbase64: invalid input: readed \ -bytes count isn't multiple of 4\n"); + ft_putstr_fd("\nbase64: invalid input: readed \ +bytes count isn't multiple of 4\n", STDERR_FILENO); exit(1); } } \ No newline at end of file diff --git a/src/des/ft_des_arg_parsers.c b/src/des/ft_des_arg_parsers.c index 1d322b6c..b549c627 100644 --- a/src/des/ft_des_arg_parsers.c +++ b/src/des/ft_des_arg_parsers.c @@ -1,5 +1,9 @@ -#include "ft_des.h" #include +#include +#include +#include +#include +#include "ft_des.h" int ft_des_base64_arg_parser ( @@ -58,6 +62,48 @@ int ft_des_encode_arg_parser { (void)argc; (void)argv; - ctx->encode = 1; + ctx->decode = 0; return (++position); } + +int ft_des_input_file_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *ctx +) +{ + struct stat stat_buff; + + if (position + 1 >= argc) + ft_des_print_error("there is no filaname after -i flag."); + if ((ctx->input_fd = open(argv[position + 1], O_RDONLY, 0)) == -1) + { + perror("des"); + exit(1); + } + stat(argv[position + 1], &stat_buff); + if (S_ISDIR(stat_buff.st_mode)) + ft_des_print_error("input path is not a file."); + return (position + 2); +} + +int ft_des_output_file_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *ctx +) +{ + if (position + 1 >= argc) + ft_des_print_error("there is no filaname after -o flag."); + if ((ctx->output_fd = open( + argv[position + 1], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1) + { + perror("des"); + exit(1); + } + return (position + 2); +} diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index a576e3fe..2eb2a858 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -18,6 +18,14 @@ t_des_argument_parser g_arg_parsers[] = { "-a", ft_des_base64_arg_parser, }, + { + "-i", + ft_des_input_file_arg_parser, + }, + { + "-o", + ft_des_output_file_arg_parser, + }, { NULL, NULL}, };