Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice02 / ex1-2.c
diff options
context:
space:
mode:
Commit54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-12-02 18:49:11 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-12-02 18:49:11 +0200
Files62b872040b800398285bf747b7b8faf7fe7c5be1 /Year 3/System programming/Practice02/ex1-2.c
uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.tar
uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.tar.gz
uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.zip
Parenta594346d7e2ca97717cae7e705c00d6fe0b9a460 (diff)
[SP/P02] Solved exercises from second practice
1 files changed, 32 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice02/ex1-2.c32
diff --git a/Year 3/System programming/Practice02/ex1-2.c b/Year 3/System programming/Practice02/ex1-2.c
new file mode 100644
index 0000000..ef98a27
--- /dev/null
+++ b/Year 3/System programming/Practice02/ex1-2.c
@@ -0,0 +1,32 @@
+#include <fcntl.h> // open, close, O_RDONLY
+#include <unistd.h> // read
+#include <stdio.h> // printf, perror
+
+int main(int argc, char* argv[]) {
+ int f1 = open(argv[1], O_RDONLY);
+ if (f1 < 0) {
+ perror("open");
+ return 1;
+ }
+
+ int f2 = dup(f1);
+ if (f2 < 0) {
+ perror("dup");
+ return 2;
+ }
+
+ int readTimes = 10; // няколко пъти
+ char buf1, buf2;
+ while (readTimes > 0 && read(f1, &buf1, 1) && read(f2, &buf2, 1)) {
+ printf("%c%c", buf1, buf2);
+ readTimes--;
+ }
+
+ close(f1);
+
+ while (read(f2, &buf2, 1)) {
+ printf("%c", buf2);
+ }
+
+ close(f2);
+}