commit 5d398161bb0123a6e191f722a2600cd0c7cf2fca Author: EdwardEisenhauer Date: Wed Mar 3 15:22:13 2021 +0100 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..0958901 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# AAE-CNAS-Labs + +Repository with tasks for Computer Networks and Systems laboratory classes + +## Topic 1 – Little warmup +I'm sure you are all proficient C/C++ programmers, but - just in case you had a long break and forgot some things - lets do a little warmup. + +Write your own version of Unix "cp" (copy) command. + +As a bare minimum - please write a program that: + - accepts source and destination filenames as its arguments (you DO remember how to use argc and argv ?) + - makes a copy of indicated file + - returns 0 on success, any non-zero value on error + - uses ONLY POSIX I/O functions (open, close, read, write - consult appropriate manuals) + +NOTICE: Do not assume, that you can read entire file into memory. Please read manuals carefully - pay attention to each function's return value! + +I'm sure this task will prove to be to easy to some of you - in this case you can extend your program - for example you can add features to allow: + - copy source_file dest_dir (copy source file to the indicated directory, keeping its name) + - copy file1 file2 file3 dest_dir (copy files file1...file3 into destination directory) + - ... ? + +As a solution - provide your source code here. \ No newline at end of file diff --git a/Topic-1/main.c b/Topic-1/main.c new file mode 100644 index 0000000..0eb0d2e --- /dev/null +++ b/Topic-1/main.c @@ -0,0 +1,48 @@ +#include +#include +#include + +enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT}; + +int print_error_message(enum ERROR error_type) { + if (error_type == NO_ARGUMENTS) { + fprintf(stderr,"cp: missing file operand\n"); // TODO: Maybe replace "cp" with argv[0]? + fprintf(stderr,"Try cp --help for more information.\n"); + return 0; + } else if (error_type == WRONG_ARGUMENT) { + fprintf(stderr, "cp: unrecognized option\n"); + fprintf(stderr,"Try cp --help for more information.\n"); + } + return 0; +} + +int print_help() { + printf("HELP\n"); + return 0; +} + +int cp(char *argv[]){ + printf("%s\n", argv[0]); + open(); + return 0; +} + +int main(int argc, char *argv[]) { + if (argc == 1) { + print_error_message(NO_ARGUMENTS); + return 1; + } + if (argc == 2) { + if (strcmp(argv[1], "--help") == 0) { + print_help(); + return 0; + } else { + print_error_message(WRONG_ARGUMENT); + return 2; + } + } + if (argc == 3) { + cp(argv); + } + return 0; +} \ No newline at end of file