diff options
[OS] Added some work I did a while ago
2 files changed, 61 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 |
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; +} |
