diff options
| Commit | 3ba593c0c4da762969a48a5c89c4a096e93680a5 (patch) | |
|---|---|---|
| Author | 2023-10-04 10:20:22 +0300 | |
| Committer | 2023-10-04 10:20:22 +0300 | |
| Files | 10f076dbd76e7dec550064f0d20179a23f757afe | |
| uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.tar uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.tar.gz uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.zip | ||
| Parent | 91d5a6e02ebb1535026887d49da3ef2f48406519 (diff) | |
[SP/P1] Solved all exercises
7 files changed, 180 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practicum1/cmp.c | 21 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/cp.c | 20 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/cut.c | 29 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/fgrep.c | 38 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/head.c | 19 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/tail.c | 24 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum1/wc.c | 29 |
diff --git a/Year 3/System programming/Practicum1/cmp.c b/Year 3/System programming/Practicum1/cmp.c new file mode 100644 index 0000000..4d12458 --- /dev/null +++ b/Year 3/System programming/Practicum1/cmp.c @@ -0,0 +1,21 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f1 = open(argv[1], O_RDONLY); + int f2 = open(argv[2], O_RDONLY); + if (f1 == -1 || f2 == -1) return 2; + + char buf1; + char buf2; + int status = 0; + while (status == 0 && (read(f1, &buf1, 1) || read(f2, &buf2, 1))) { + if (buf1 != buf2) status = 1; + } + close(f1); + close(f2); + return status; +} diff --git a/Year 3/System programming/Practicum1/cp.c b/Year 3/System programming/Practicum1/cp.c new file mode 100644 index 0000000..f44e44d --- /dev/null +++ b/Year 3/System programming/Practicum1/cp.c @@ -0,0 +1,20 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f1 = open(argv[1], O_RDONLY); + if (f1 == -1) return 1; + int f2 = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0755); + if (f2 == -1) return 2; + + char buf; + while(read(f1, &buf, 1)) { + write(f2, &buf, 1); + } + + close(f1); + close(f2); +} diff --git a/Year 3/System programming/Practicum1/cut.c b/Year 3/System programming/Practicum1/cut.c new file mode 100644 index 0000000..78259fa --- /dev/null +++ b/Year 3/System programming/Practicum1/cut.c @@ -0,0 +1,29 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f = open(argv[1], O_RDONLY); + if (f == -1) return 1; + + int m, n; + scanf("%d-%d", &m, &n); + + int col = 1; + char buf; + while (read(f, &buf, 1)) { + if (buf == '\n') { + col = 1; + write(0, &buf, 1); + continue; + } + if (m <= col && col <= n) { + write(0, &buf, 1); + } + col++; + } + + close(f); +} diff --git a/Year 3/System programming/Practicum1/fgrep.c b/Year 3/System programming/Practicum1/fgrep.c new file mode 100644 index 0000000..7603e73 --- /dev/null +++ b/Year 3/System programming/Practicum1/fgrep.c @@ -0,0 +1,38 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f1 = open(argv[1], O_RDONLY); + if (f1 == -1) return 1; + int f2 = open(argv[2], O_RDONLY); + if (f2 == -1) return 2; + + char buf; + int fromStart = 0, backtrackTo = 0; + while(read(f2, &buf, 1)) { + if (buf == '\n') { + backtrackTo = 0; + continue; + } + backtrackTo--; + fromStart++; + + int anyMatched = 0; + char pbuf, mbuf = buf; + + while(read(f1, &pbuf, 1)) { + if (pbuf != mbuf) { + while (pbuf != '\n') read(f1, &pbuf, 1); + lseek(f2, + } + else if (pbuf == '\n') { + anyMatched = 1; + } + } + + } +} + diff --git a/Year 3/System programming/Practicum1/head.c b/Year 3/System programming/Practicum1/head.c new file mode 100644 index 0000000..73bb144 --- /dev/null +++ b/Year 3/System programming/Practicum1/head.c @@ -0,0 +1,19 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f = open(argv[1], O_RDONLY); + if (f == -1) return 1; + + int lines = 1; + char buf; + while (lines > 0 && read(f, &buf, 1)) { + if (buf == '\n') lines--; + write(0, &buf, 1); + } + + close(f); +} diff --git a/Year 3/System programming/Practicum1/tail.c b/Year 3/System programming/Practicum1/tail.c new file mode 100644 index 0000000..35b7d7f --- /dev/null +++ b/Year 3/System programming/Practicum1/tail.c @@ -0,0 +1,24 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f = open(argv[1], O_RDONLY); + if (f == -1) return 1; + + int lines = 1; + char buf; + lseek(f, 0, SEEK_END); + do { + read(f, &buf, 1); + if (buf == '\n') lines--; + } while(lines > 0 && lseek(f, -2, SEEK_CUR)); + + while (read(f, &buf, 1)) { + write(0, &buf, 1); + } + + close(f); +} diff --git a/Year 3/System programming/Practicum1/wc.c b/Year 3/System programming/Practicum1/wc.c new file mode 100644 index 0000000..fc0cee5 --- /dev/null +++ b/Year 3/System programming/Practicum1/wc.c @@ -0,0 +1,29 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f = open(argv[1], O_RDONLY); + if (f == -1) return 1; + + char buf; + int prevNotSpace = 1; + int bytes = 0, newlines = 0, words = 0; + while (read(f, &buf, 1)) { + bytes++; + if (buf == '\n') { + newlines++; + words++; + } + if (buf == ' ' || buf == '\t') { + words += prevNotSpace; + prevNotSpace = 0; + } + else prevNotSpace = 1; + } + + close(f); + printf("%d %d %d %s\n", newlines, words, bytes, argv[1]); +} |
