Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice05 / ex7.c
diff options
context:
space:
mode:
Commit3c791c6c357115d4abf71c318c9e7cd7571ea658 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-12-02 21:25:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-12-02 21:25:46 +0200
Files10705fa10c017e7c1c66d1c8feaca92758acefd0 /Year 3/System programming/Practice05/ex7.c
uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.tar
uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.tar.gz
uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.zip
Parent952bee4ac4e4a97d30fc1d681ff0a9e5168fb9c8 (diff)
[SP/P05] Solved exercises
1 files changed, 31 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice05/ex7.c31
diff --git a/Year 3/System programming/Practice05/ex7.c b/Year 3/System programming/Practice05/ex7.c
new file mode 100644
index 0000000..08df367
--- /dev/null
+++ b/Year 3/System programming/Practice05/ex7.c
@@ -0,0 +1,31 @@
+#include <unistd.h> // execvp, fork
+#include <stdio.h> // printf
+#include <sys/wait.h> // wait
+#include <fcntl.h> // open, O_WRONLY
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) return 1;
+
+ pid_t fpid = fork();
+ if (fpid == 0) {
+ int f = open(argv[argc-1], O_WRONLY | O_TRUNC | O_CREAT, 0755);
+ if (f < 0) {
+ perror("open");
+ return 2;
+ }
+ dup2(f, 1);
+ close(f);
+
+ char* args[argc-1];
+ for (int i = 1; i < argc - 1; i++)
+ args[i-1] = argv[i];
+ args[argc-2] = NULL;
+
+ execvp(argv[1], args);
+ }
+ else {
+ int status;
+ wait(&status);
+ printf("%d %d\n", fpid, status);
+ }
+}