Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commita594346d7e2ca97717cae7e705c00d6fe0b9a460 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-12-02 18:10:23 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-12-02 18:10:23 +0200
Files995c765606f9f4277e8ba5051a315e76dfb90391
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.tar
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.tar.gz
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.zip
Parentcf86e964b166a32227aa4bb0cc1de27a37244c69 (diff)
[SP/P01] Solved exercises from first practice (again)
diff --git a/Year 3/System programming/Practice01/ex1-cp.c b/Year 3/System programming/Practice01/ex1-cp.c
new file mode 100644
index 0000000..ff39983
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex1-cp.c
@@ -0,0 +1,25 @@
+#include <fcntl.h> // open, close, O_RDONLY, ...
+#include <stdio.h> // perror
+#include <unistd.h> // read, write
+
+int main(int argc, char* argv[]) {
+ int f1 = open(argv[1], O_RDONLY);
+ if (f1 < 0) {
+ perror("open");
+ return 1;
+ }
+
+ int f2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0755);
+ if (f2 < 0) {
+ perror("open");
+ return 2;
+ }
+
+ char buf;
+ while(read(f1, &buf, 1)) {
+ write(f2, &buf, 1);
+ }
+
+ close(f1);
+ close(f2);
+}
diff --git a/Year 3/System programming/Practice01/ex2-head.c b/Year 3/System programming/Practice01/ex2-head.c
new file mode 100644
index 0000000..197e8d2
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex2-head.c
@@ -0,0 +1,21 @@
+#include <unistd.h> // ssize_t, read, write
+#include <fcntl.h> // open, close, O_RDONLY, ...
+#include <stdio.h> // printf, perror
+
+int main(int argc, char* argv[]) {
+ int f = open(argv[1], O_RDONLY);
+ if (f < 0) {
+ perror("open");
+ return 1;
+ }
+
+ ssize_t lines = 10;
+
+ char buf;
+ while (read(f, &buf, 1) && lines > 0) {
+ if (buf == '\n') lines--;
+ printf("%c", buf);
+ }
+
+ close(f);
+}
diff --git a/Year 3/System programming/Practice01/ex3-cmp.c b/Year 3/System programming/Practice01/ex3-cmp.c
new file mode 100644
index 0000000..19bc336
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex3-cmp.c
@@ -0,0 +1,31 @@
+#include <unistd.h> // ssize_t, read, write
+#include <fcntl.h> // open, close, O_RDONLY, ...
+#include <stdio.h> // perror
+
+// exit 0 - equal
+// exit 1 - different
+int main(int argc, char* argv[]) {
+ int f1 = open(argv[1], O_RDONLY);
+ if (f1 < 0) {
+ perror("open");
+ return 2;
+ }
+
+ int f2 = open(argv[2], O_RDONLY);
+ if (f2 < 0) {
+ perror("open");
+ return 3;
+ }
+
+ char buf1, buf2;
+ ssize_t read1, read2;
+ do {
+ read1 = read(f1, &buf1, 1);
+ read2 = read(f2, &buf2, 1);
+ if (buf1 != buf2) return 1;
+ } while (read1 > 0 && read2 > 0);
+ if (read1 != read2) return 1;
+
+ close(f1);
+ close(f2);
+}
diff --git a/Year 3/System programming/Practice01/ex4-wc.c b/Year 3/System programming/Practice01/ex4-wc.c
new file mode 100644
index 0000000..29fb572
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex4-wc.c
@@ -0,0 +1,33 @@
+#include <unistd.h> // read, write
+#include <fcntl.h> // open, close, O_RDONLY
+#include <stdio.h> // printf, perror
+
+int isBlank(char c) {
+ return c == ' ' || c == '\n' || c == '\t';
+}
+
+// print line, word and byte count
+int main(int argc, char* argv[]) {
+ int f = open(argv[1], O_RDONLY);
+ if (f < 0) {
+ perror("open");
+ return 1;
+ }
+
+ int lines = 0, words = 0, bytes = 0;
+ char buf;
+ int lastWasBlank = 0;
+ while (read(f, &buf, 1)) {
+ bytes++;
+ if (buf == '\n') lines++;
+ if (isBlank(buf)) {
+ words += !lastWasBlank;
+ lastWasBlank = 1;
+ }
+ else lastWasBlank = 0;
+ }
+
+ close(f);
+
+ printf("%d %d %d %s\n", lines, words, bytes, argv[1]);
+}
diff --git a/Year 3/System programming/Practice01/ex5-cut.c b/Year 3/System programming/Practice01/ex5-cut.c
new file mode 100644
index 0000000..e1d6bf2
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex5-cut.c
@@ -0,0 +1,32 @@
+#include <fcntl.h> // open, close, O_RDONLY
+#include <stdio.h> // printf, scanf, perror
+#include <unistd.h> // read
+
+// print characters from column m to n (1 indexed) for each row
+int main(int argc, char* argv[]) {
+ int f = open(argv[1], O_RDONLY);
+ if (f < 0) {
+ perror("open");
+ 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;
+ printf("\n");
+ continue;
+ }
+
+ if (m <= col && col <= n) {
+ printf("%c", buf);
+ }
+ col++;
+ }
+
+ close(f);
+}