output as a file

This commit is contained in:
Gregory 2019-02-21 00:33:54 +02:00
parent 48c1c4f34c
commit a6a88824df
2 changed files with 10 additions and 8 deletions

View file

@ -2,11 +2,12 @@
#include "libft.h" #include "libft.h"
#include "ft_base64.h" #include "ft_base64.h"
#include "fcntl.h" #include "fcntl.h"
#include "sys/stat.h"
static int open_stream(char *filename) static int open_stream(char *filename, int flags, int mode)
{ {
int fd; int fd;
if ((fd = open((const char *)filename, O_RDONLY)) == -1) if ((fd = open((const char *)filename, flags, mode)) == -1)
{ {
perror("base64"); perror("base64");
exit(1); exit(1);
@ -38,13 +39,14 @@ static void read_args
next_arg = i + 1 < argc ? argv[i + 1] : NULL; next_arg = i + 1 < argc ? argv[i + 1] : NULL;
if (ft_strcmp(current_arg, "-d") == 0) if (ft_strcmp(current_arg, "-d") == 0)
flags->decode = 1; flags->decode = 1;
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); ctx->input_fd = open_stream(next_arg, O_RDONLY, 0);
else if (ft_strcmp(current_arg, "-o") == 0) else if (ft_strcmp(current_arg, "-o") == 0)
ctx->output_fd = open_stream(next_arg); ctx->output_fd = open_stream(next_arg, O_CREAT | O_WRONLY,
else if (i++) S_IRUSR | S_IWUSR);
else if (++i)
continue; continue;
i++; i++;
} }

View file

@ -17,12 +17,12 @@ void ft_base64_encode_finish
return ; return ;
if (!buffer_index) if (!buffer_index)
{ {
ft_putstr("\n"); write(ctx->output_fd, "\n", 1);
return ; return ;
} }
ft_bzero(&buff->block[buffer_index], padding_size); ft_bzero(&buff->block[buffer_index], padding_size);
ft_base64_encode_transform(ctx, buff->block, chars); ft_base64_encode_transform(ctx, buff->block, chars);
ft_memset(chars + FT_BASE64_CHARS_SIZE - padding_size, '=', padding_size); ft_memset(chars + FT_BASE64_CHARS_SIZE - padding_size, '=', padding_size);
write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE); write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE);
ft_putstr("\n"); write(ctx->output_fd, "\n", 1);
} }