From a6a88824df1faf72e95d3373aae91b81ac60851b Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 21 Feb 2019 00:33:54 +0200 Subject: [PATCH] output as a file --- src/base64/ft_base64.c | 14 ++++++++------ src/base64/ft_base64_encode_finish.c | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/base64/ft_base64.c b/src/base64/ft_base64.c index 9952a551..ed822ea2 100644 --- a/src/base64/ft_base64.c +++ b/src/base64/ft_base64.c @@ -2,11 +2,12 @@ #include "libft.h" #include "ft_base64.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; - if ((fd = open((const char *)filename, O_RDONLY)) == -1) + if ((fd = open((const char *)filename, flags, mode)) == -1) { perror("base64"); exit(1); @@ -38,13 +39,14 @@ static void read_args next_arg = i + 1 < argc ? argv[i + 1] : NULL; if (ft_strcmp(current_arg, "-d") == 0) flags->decode = 1; - else if (ft_strcmp(current_arg, "-e") == 0 && i++) + 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); + ctx->input_fd = open_stream(next_arg, O_RDONLY, 0); else if (ft_strcmp(current_arg, "-o") == 0) - ctx->output_fd = open_stream(next_arg); - else if (i++) + ctx->output_fd = open_stream(next_arg, O_CREAT | O_WRONLY, + S_IRUSR | S_IWUSR); + else if (++i) continue; i++; } diff --git a/src/base64/ft_base64_encode_finish.c b/src/base64/ft_base64_encode_finish.c index 53b59a65..0a93591b 100644 --- a/src/base64/ft_base64_encode_finish.c +++ b/src/base64/ft_base64_encode_finish.c @@ -17,12 +17,12 @@ void ft_base64_encode_finish return ; if (!buffer_index) { - ft_putstr("\n"); + write(ctx->output_fd, "\n", 1); return ; } ft_bzero(&buff->block[buffer_index], padding_size); ft_base64_encode_transform(ctx, buff->block, chars); ft_memset(chars + FT_BASE64_CHARS_SIZE - padding_size, '=', padding_size); write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE); - ft_putstr("\n"); + write(ctx->output_fd, "\n", 1); } \ No newline at end of file