diff options
| Commit | 2aacf1b28daa876123919f7fab67f2a538864ee6 (patch) | |
|---|---|---|
| Author | 2023-08-24 13:02:16 +0300 | |
| Committer | 2023-08-24 13:02:16 +0300 | |
| Files | cf6ba1b6f6028f67cfc549fa5e490b0e508e33fe | |
| uni-projects-2aacf1b28daa876123919f7fab67f2a538864ee6.tar uni-projects-2aacf1b28daa876123919f7fab67f2a538864ee6.tar.gz uni-projects-2aacf1b28daa876123919f7fab67f2a538864ee6.zip | ||
| Parent | 1d1f9e1e713a8b6a786f2b191d8a9db5f1dca79f (diff) | |
[OS] Added some work I did a while ago
7 files changed, 110 insertions, 0 deletions
| -rw-r--r-- | Year 2/Operating Systems/Pract11/cp.c | 24 | ||||
| -rw-r--r-- | Year 2/Operating Systems/Pract11/wc.c | 37 | ||||
| -rwxr-xr-x | Year 2/Operating Systems/Pract12/cat-my | bin | 0 -> 15744 bytes | |||
| -rw-r--r-- | Year 2/Operating Systems/Pract12/cat.c | 21 | ||||
| -rwxr-xr-x | Year 2/Operating Systems/Pract12/tee-my | bin | 0 -> 15736 bytes | |||
| -rw-r--r-- | Year 2/Operating Systems/Pract12/tee.c | 27 | ||||
| -rw-r--r-- | Year 2/Operating Systems/Pract12/test.txt | 1 |
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 <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +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 <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +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 Binary files differnew file mode 100755 index 0000000..36409be --- /dev/null +++ b/Year 2/Operating Systems/Pract12/cat-my 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 <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +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 Binary files differnew file mode 100755 index 0000000..8773ef6 --- /dev/null +++ b/Year 2/Operating Systems/Pract12/tee-my 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 <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdio.h> + +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 |
