From 2aacf1b28daa876123919f7fab67f2a538864ee6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 24 Aug 2023 13:02:16 +0300 Subject: [OS] Added some work I did a while ago --- Year 2/Operating Systems/Pract11/cp.c | 24 +++++++++++++++++++ Year 2/Operating Systems/Pract11/wc.c | 37 ++++++++++++++++++++++++++++++ Year 2/Operating Systems/Pract12/cat-my | Bin 0 -> 15744 bytes Year 2/Operating Systems/Pract12/cat.c | 21 +++++++++++++++++ Year 2/Operating Systems/Pract12/tee-my | Bin 0 -> 15736 bytes Year 2/Operating Systems/Pract12/tee.c | 27 ++++++++++++++++++++++ Year 2/Operating Systems/Pract12/test.txt | 1 + 7 files changed, 110 insertions(+) create mode 100644 Year 2/Operating Systems/Pract11/cp.c create mode 100644 Year 2/Operating Systems/Pract11/wc.c create mode 100755 Year 2/Operating Systems/Pract12/cat-my create mode 100644 Year 2/Operating Systems/Pract12/cat.c create mode 100755 Year 2/Operating Systems/Pract12/tee-my create mode 100644 Year 2/Operating Systems/Pract12/tee.c create mode 100644 Year 2/Operating Systems/Pract12/test.txt diff --git a/Year 2/Operating Systems/Pract11/cp.c b/Year 2/Operating Systems/Pract11/cp.c new file mode 100644 index 0000000..044b278 --- /dev/null +++ b/Year 2/Operating Systems/Pract11/cp.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +const size_t BUF_SIZE = 1024; + +int main(int argc, char *argv[]) { + if (argc < 3) return 1; + + int fd_from = open(argv[1], O_RDONLY); + if (fd_from < 0) return 2; + + int fd_to = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0744); + if (fd_to < 0) return 3; + + char buf[BUF_SIZE]; + size_t actual = read(fd_from, buf, BUF_SIZE); + while (actual > 0) { + write(fd_to, buf, actual); + actual = read(fd_from, buf, BUF_SIZE); + } + return 0; +} diff --git a/Year 2/Operating Systems/Pract11/wc.c b/Year 2/Operating Systems/Pract11/wc.c new file mode 100644 index 0000000..94627a0 --- /dev/null +++ b/Year 2/Operating Systems/Pract11/wc.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +const size_t BUF_SIZE = 1024; + +int main(int argc, char* argv[]) { + if (argc < 2) return 1; + + int fd_in = open(argv[1], O_RDONLY); + if (fd_in < 0) return 2; + + char buf[BUF_SIZE]; + int actual = read(fd_in, buf, BUF_SIZE); + + int lines = 0, words = 0, bytes = 0; + + while (actual > 0) { + bytes += actual; + for (int i = 0; i < actual; i++) { + if (buf[i] == '\n') { + lines++; + if (i == 0 || buf[i-1] == '\n') words--; + } + else if (buf[i] == ' ') { + words++; + while (buf[i] == ' ') i++; + } + } + actual = read(fd_in, buf, BUF_SIZE); + } + words += lines; + printf("%d %d %d\n", lines, words, bytes); + return 0; +} diff --git a/Year 2/Operating Systems/Pract12/cat-my b/Year 2/Operating Systems/Pract12/cat-my new file mode 100755 index 0000000..36409be Binary files /dev/null and b/Year 2/Operating Systems/Pract12/cat-my differ diff --git a/Year 2/Operating Systems/Pract12/cat.c b/Year 2/Operating Systems/Pract12/cat.c new file mode 100644 index 0000000..353e2ca --- /dev/null +++ b/Year 2/Operating Systems/Pract12/cat.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +const size_t BUFF_SIZE = 1024; + +int main(int argc, char* argv[]) { + int fd_in = 0; + if (argc > 1) + fd_in = open(argv[1], O_RDONLY); + + char buf[BUFF_SIZE]; + int actual = read(fd_in, buf, BUFF_SIZE); + while (actual > 0) { + printf("%s", buf); + actual = read(fd_in, buf, BUFF_SIZE); + } + return 0; +} diff --git a/Year 2/Operating Systems/Pract12/tee-my b/Year 2/Operating Systems/Pract12/tee-my new file mode 100755 index 0000000..8773ef6 Binary files /dev/null and b/Year 2/Operating Systems/Pract12/tee-my differ diff --git a/Year 2/Operating Systems/Pract12/tee.c b/Year 2/Operating Systems/Pract12/tee.c new file mode 100644 index 0000000..34e9884 --- /dev/null +++ b/Year 2/Operating Systems/Pract12/tee.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include + +const size_t BUFF_SIZE = 1024; + +int main(int argc, char* argv[]) { + int fd_in = 0; + + int fd_outs[BUFF_SIZE]; + fd_outs[0] = 1; // stdout + for (int i = 1; i < argc; i++) { + fd_outs[i] = open(argv[i], O_WRONLY | O_TRUNC | O_CREAT, 0766); + } + + char buf[BUFF_SIZE]; + int actual = read(fd_in, buf, BUFF_SIZE); + while (actual > 0) { + for (int i = 0; i < argc; i++) { + write(fd_outs[i], buf, actual); + } + actual = read(fd_in, buf, BUFF_SIZE); + } + return 0; +} diff --git a/Year 2/Operating Systems/Pract12/test.txt b/Year 2/Operating Systems/Pract12/test.txt new file mode 100644 index 0000000..58c2a86 --- /dev/null +++ b/Year 2/Operating Systems/Pract12/test.txt @@ -0,0 +1 @@ +123f -- cgit v1.3.1-26-gb76a