diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
| commit | 880102ae9358db130ef67cc9a7177a1e1de76875 (patch) | |
| tree | b5929722cf123e7c1701f4bc164a905f79510bf5 /c10/ex01 | |
| parent | dcf89e9366538c70fc08ceaabd5b770f5d5b890d (diff) | |
| download | piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.gz piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.bz2 piscine-880102ae9358db130ef67cc9a7177a1e1de76875.zip | |
c10 tail stuff, c11 start, c12/c13 files
Diffstat (limited to 'c10/ex01')
| -rw-r--r-- | c10/ex01/Makefile | 34 | ||||
| -rw-r--r-- | c10/ex01/helper.c | 5 | ||||
| -rw-r--r-- | c10/ex01/include.h | 5 | ||||
| -rw-r--r-- | c10/ex01/io.c | 29 | ||||
| -rw-r--r-- | c10/ex01/main.c | 8 |
5 files changed, 67 insertions, 14 deletions
diff --git a/c10/ex01/Makefile b/c10/ex01/Makefile index 362d434..862705c 100644 --- a/c10/ex01/Makefile +++ b/c10/ex01/Makefile @@ -1 +1,35 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/07/16 14:50:14 by cacharle #+# #+# # +# Updated: 2019/07/16 14:50:19 by cacharle ### ########.fr # +# # +# **************************************************************************** # + OUT = ft_cat +CC = gcc +CCFLAGS = -Wall -Wextra -Werror +SRC = main.c io.c helper.c +OBJ = $(SRC:.c=.o) + +.PHONY: all +all: $(OUT) + +$(OUT): $(OBJ) + $(CC) $(CCFLAGS) -o $@ $^ + +%.o: %.c include.h + $(CC) $(CCFLAGS) -c $< -o $@ + +.PHONY: clean +clean: + rm -f $(OBJ) + +.PHONY: clean +fclean: clean + rm -f $(OUT) + diff --git a/c10/ex01/helper.c b/c10/ex01/helper.c index 8a1c13e..a6a9acd 100644 --- a/c10/ex01/helper.c +++ b/c10/ex01/helper.c @@ -6,13 +6,14 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 10:14:16 by cacharle #+# #+# */ -/* Updated: 2019/07/16 10:26:44 by cacharle ### ########.fr */ +/* Updated: 2019/07/16 13:42:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <unistd.h> #include <errno.h> #include <string.h> +#include <libgen.h> #include "include.h" void ft_putstr_err(char *str) @@ -25,7 +26,7 @@ void handle_error(char *program_name, char *arg) { char *error_msg; - ft_putstr_err(program_name); + ft_putstr_err(basename(program_name)); ft_putstr_err(": "); ft_putstr_err(arg); ft_putstr_err(": "); diff --git a/c10/ex01/include.h b/c10/ex01/include.h index fed49e8..3a48093 100644 --- a/c10/ex01/include.h +++ b/c10/ex01/include.h @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/15 16:49:28 by cacharle #+# #+# */ -/* Updated: 2019/07/16 10:23:45 by cacharle ### ########.fr */ +/* Created: 2019/07/16 14:07:19 by cacharle #+# #+# */ +/* Updated: 2019/07/17 08:47:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int read_in_buf(int fildes, char buf[BUF_SIZE]); int write_buf(char buf[BUF_SIZE], int size); int read_write_fildes(int fildes); +void read_stdin(void); int print_file(char *filename); /* diff --git a/c10/ex01/io.c b/c10/ex01/io.c index a6c0b01..b753902 100644 --- a/c10/ex01/io.c +++ b/c10/ex01/io.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/15 19:05:32 by cacharle #+# #+# */ -/* Updated: 2019/07/16 10:52:03 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 11:19:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,12 @@ #include <fcntl.h> #include "include.h" -int read_in_buf(int fildes, char buf[BUF_SIZE]) +int read_in_buf(int fildes, char buf[BUF_SIZE]) { - return (read(fildes, buf, BUF_SIZE - 1)); + return (read(fildes, buf, BUF_SIZE)); } -int write_buf(char buf[BUF_SIZE], int size) +int write_buf(char buf[BUF_SIZE], int size) { return (write(1, buf, size)); } @@ -29,10 +29,15 @@ int read_write_fildes(int fildes) int size; char buf[BUF_SIZE]; - size = BUF_SIZE - 1; - while (size == BUF_SIZE - 1 || fildes == STDOUT_FILENO ) + size = BUF_SIZE; + if (fildes == STDIN_FILENO) { - if ((size = read_in_buf(fildes, buf)) == -1) + read_stdin(); + return (0); + } + while (size != 0) + { + if ((size = read_in_buf(fildes, buf)) < 0) return (-1); if (write_buf(buf, size) == -1) return (-1); @@ -40,7 +45,15 @@ int read_write_fildes(int fildes) return (0); } -int print_file(char *filename) +void read_stdin(void) +{ + char buf[32]; + + while (read(STDIN_FILENO, buf, 1)) + write(STDOUT_FILENO, buf, 1); +} + +int print_file(char *filename) { int fildes; diff --git a/c10/ex01/main.c b/c10/ex01/main.c index b5fb416..3ff790b 100644 --- a/c10/ex01/main.c +++ b/c10/ex01/main.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/15 16:45:20 by cacharle #+# #+# */ -/* Updated: 2019/07/16 10:20:39 by cacharle ### ########.fr */ +/* Updated: 2019/07/16 14:10:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,14 @@ int main(int argc, char **argv) { int i; - int fildes; int status; status = 0; + if (argc == 1) + { + read_stdin(); + return (0); + } i = 1; while (i < argc) { |
