From 2a5a57e16f216a9bf8933345014c91f198bb1215 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 15 May 2023 15:55:53 +0530 Subject: 8.1: initial commit --- 8.1/Makefile | 13 +++++++++++++ 8.1/error.c | 17 +++++++++++++++++ 8.1/error.h | 1 + 8.1/filecopy.c | 16 ++++++++++++++++ 8.1/filecopy.h | 1 + 8.1/main.c | 25 +++++++++++++++++++++++++ 6 files changed, 73 insertions(+) create mode 100644 8.1/Makefile create mode 100644 8.1/error.c create mode 100644 8.1/error.h create mode 100644 8.1/filecopy.c create mode 100644 8.1/filecopy.h create mode 100644 8.1/main.c (limited to '8.1') diff --git a/8.1/Makefile b/8.1/Makefile new file mode 100644 index 0000000..84ef405 --- /dev/null +++ b/8.1/Makefile @@ -0,0 +1,13 @@ +objects = main.o filecopy.o error.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +cat : $(objects) + $(CC) $(CFLAGS) -o cat $(objects) + +main.o : error.h +filecopy.o : error.h + +.PHONY : clean +clean: + rm -f cat $(objects) diff --git a/8.1/error.c b/8.1/error.c new file mode 100644 index 0000000..85cd7f0 --- /dev/null +++ b/8.1/error.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include "error.h" + +void error(char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + + va_end(args); + exit(1); +} diff --git a/8.1/error.h b/8.1/error.h new file mode 100644 index 0000000..e862ac8 --- /dev/null +++ b/8.1/error.h @@ -0,0 +1 @@ +void error(char *fmt, ...); diff --git a/8.1/filecopy.c b/8.1/filecopy.c new file mode 100644 index 0000000..b35ccbd --- /dev/null +++ b/8.1/filecopy.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include "filecopy.h" +#include "error.h" + +void filecopy(int ifd, int ofd) +{ + ssize_t n; + char buff[BUFSIZ]; + + while ((n = read(ifd, buff, BUFSIZ)) > 0) + if (write(ofd, buff, n) != n) + error("cat: write error"); +} diff --git a/8.1/filecopy.h b/8.1/filecopy.h new file mode 100644 index 0000000..22b0546 --- /dev/null +++ b/8.1/filecopy.h @@ -0,0 +1 @@ +void filecopy(int ifd, int ofd); diff --git a/8.1/main.c b/8.1/main.c new file mode 100644 index 0000000..e3e5dd1 --- /dev/null +++ b/8.1/main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include "filecopy.h" +#include "error.h" + +int main(int argc, char *argv[]) +{ + ssize_t fd; + + if (argc == 1) { + filecopy(STDIN_FILENO, STDOUT_FILENO); + } else { + while (--argc > 0) { + if ((fd = open(*++argv, O_RDONLY)) == -1) { + error("cp: can't open %s", *argv); + } else { + filecopy(fd, STDOUT_FILENO); + close(fd); + } + } + } + + return 0; +} -- cgit v1.2.3