From f621349ca7e8046a181c46f78a7af8ab8a6563f2 Mon Sep 17 00:00:00 2001 From: EdwardEisenhauer Date: Wed, 3 Mar 2021 16:32:13 +0100 Subject: [PATCH] Copying 8 bytes --- Topic-1/main.c | 24 +++++++++++++++++++++--- Topic-1/test.sh | 11 ++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Topic-1/main.c b/Topic-1/main.c index 0eb0d2e..a2ea912 100644 --- a/Topic-1/main.c +++ b/Topic-1/main.c @@ -1,6 +1,10 @@ #include #include #include +#include +#include + +#define BUFFER_SIZE 8 enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT}; @@ -22,8 +26,22 @@ int print_help() { } int cp(char *argv[]){ - printf("%s\n", argv[0]); - open(); + int old, new; + old = open(argv[1], O_RDONLY); + new = open(argv[2], O_CREAT | O_WRONLY); + char buffer[BUFFER_SIZE]; + + int read_bytes = read(old, buffer, sizeof(buffer)); + if (read_bytes < 0) { + fprintf(stderr, "There was a problem with reading a file!\n"); + return 3; + } + printf("%d bytes were read", read_bytes); + write(new, buffer, read_bytes); + + close(old); + close(new); + return 0; } @@ -42,7 +60,7 @@ int main(int argc, char *argv[]) { } } if (argc == 3) { - cp(argv); + if (cp(argv) > 0) return 1; } return 0; } \ No newline at end of file diff --git a/Topic-1/test.sh b/Topic-1/test.sh index 4f1dd40..0e8f08c 100755 --- a/Topic-1/test.sh +++ b/Topic-1/test.sh @@ -1,6 +1,7 @@ #!/bin/bash -rm raz dwa; -touch raz; -gcc main.c -o cp; -./cp raz dwa; -ls | grep dwa; +rm -f old_file new_file; +echo "QWERTYUIOP" > old_file; +gcc -Wall main.c -o cp; +./cp old_file new_file; +chmod +rw new_file; +diff old_file new_file;