fix reading and writing to files

This commit is contained in:
Gregory 2019-03-23 20:59:47 +02:00
parent 9b1450d2f4
commit c7c3c67d2c
4 changed files with 85 additions and 10 deletions

View file

@ -1,15 +1,36 @@
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include "libft.h" #include "libft.h"
#include "ft_base64.h" #include "ft_base64.h"
static int open_stream(char *filename, int flags, int mode) static int open_input(char *filename)
{ {
int fd; 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); exit(1);
} }
return fd; return fd;
@ -42,10 +63,9 @@ static void read_args
else if (ft_strcmp(current_arg, "-e") == 0 && ++i) else if (ft_strcmp(current_arg, "-e") == 0 && ++i)
continue; continue;
else if (ft_strcmp(current_arg, "-i") == 0) 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) else if (ft_strcmp(current_arg, "-o") == 0)
ctx->output_fd = open_stream(next_arg, O_CREAT | O_WRONLY, ctx->output_fd = open_output(next_arg);
S_IRUSR | S_IWUSR);
else if (++i) else if (++i)
continue; continue;
i++; i++;

View file

@ -1,3 +1,4 @@
#include <fcntl.h>
#include "ft_base64.h" #include "ft_base64.h"
#include "libft.h" #include "libft.h"
@ -13,8 +14,8 @@ void ft_base64_decode_finish
return ; return ;
if (buffer_index != 0) if (buffer_index != 0)
{ {
ft_putstr("\nbase64: invalid input: readed \ ft_putstr_fd("\nbase64: invalid input: readed \
bytes count isn't multiple of 4\n"); bytes count isn't multiple of 4\n", STDERR_FILENO);
exit(1); exit(1);
} }
} }

View file

@ -1,5 +1,9 @@
#include "ft_des.h"
#include <stddef.h> #include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "ft_des.h"
int ft_des_base64_arg_parser int ft_des_base64_arg_parser
( (
@ -58,6 +62,48 @@ int ft_des_encode_arg_parser
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
ctx->encode = 1; ctx->decode = 0;
return (++position); 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);
}

View file

@ -18,6 +18,14 @@ t_des_argument_parser g_arg_parsers[] = {
"-a", "-a",
ft_des_base64_arg_parser, ft_des_base64_arg_parser,
}, },
{
"-i",
ft_des_input_file_arg_parser,
},
{
"-o",
ft_des_output_file_arg_parser,
},
{ NULL, NULL}, { NULL, NULL},
}; };