Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum5 / ex6.c
diff options
context:
space:
mode:
Commitd4366c2eeb49a37cd50db3962d5489e453a1f18c (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-11-08 13:26:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-11-08 13:26:46 +0200
Files672203edadf385adf5f2b2d0f47ab43abe8f479d /Year 3/System programming/Practicum5/ex6.c
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar.gz
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.zip
Parentb9d200d296d362b2f3191ac8f2164a0bbc79b2a6 (diff)
[SP/P5] Added solutions to some exercises from today
1 files changed, 30 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum5/ex6.c30
diff --git a/Year 3/System programming/Practicum5/ex6.c b/Year 3/System programming/Practicum5/ex6.c
new file mode 100644
index 0000000..86e206c
--- /dev/null
+++ b/Year 3/System programming/Practicum5/ex6.c
@@ -0,0 +1,30 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ pid_t f = fork();
+ if (f < 0) {
+ perror("fork");
+ return 1;
+ }
+
+ if (f == 0) {
+ char *commargs[argc];
+ commargs[0] = argv[1];
+ for (int i = 0; i < argc - 2; i++)
+ commargs[i+1] = argv[i+1];
+ commargs[argc - 1] = NULL;
+
+ int w = open(argv[argc-1], O_WRONLY | O_CREAT, 0755);
+ dup2(w, 1);
+ close(w);
+ execvp(*commargs, commargs + 1);
+ }
+ else {
+ int status = -2;
+ wait(&status);
+ printf("Exit: %d\n", status);
+ }
+}