Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitb50c86bd91351bfde316a4aa6e06e95588e4553f (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-12-02 19:57:42 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-12-02 19:57:42 +0200
Filesb783c0ff51ab2c0d0166d136ee91ffb358863b23
uni-projects-b50c86bd91351bfde316a4aa6e06e95588e4553f.tar
uni-projects-b50c86bd91351bfde316a4aa6e06e95588e4553f.tar.gz
uni-projects-b50c86bd91351bfde316a4aa6e06e95588e4553f.zip
Parent54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1 (diff)
[SP/P03] Solved exercises
diff --git a/Year 3/System programming/Practice03/ex1.c b/Year 3/System programming/Practice03/ex1.c
new file mode 100644
index 0000000..944ee0a
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex1.c
@@ -0,0 +1,25 @@
+#include <fcntl.h> // open
+#include <unistd.h> // close
+#include <stdio.h> // perror
+#include <sys/stat.h> // mode_t
+
+int main(int argc, char* argv[]) {
+ int f1 = open(argv[1], O_CREAT, 0777);
+ if (f1 < 0) {
+ perror("open");
+ return 1;
+ }
+
+ mode_t oldmask = umask(0);
+
+ int f2 = open(argv[2], O_CREAT, 0777);
+ if (f2 < 0) {
+ perror("open");
+ return 2;
+ }
+
+ umask(oldmask);
+
+ close(f1);
+ close(f2);
+}
diff --git a/Year 3/System programming/Practice03/ex2.c b/Year 3/System programming/Practice03/ex2.c
new file mode 100644
index 0000000..4b67c11
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex2.c
@@ -0,0 +1,16 @@
+#include <sys/stat.h>
+#include <stdio.h> // perror
+
+int main(int argc, char* argv[]) {
+ for (int i = 1; i < argc; i++) {
+ struct stat buf;
+ if(stat(argv[i], &buf) < 0) {
+ perror("stat");
+ return 1;
+ }
+ if (chmod(argv[i], (buf.st_mode | S_IWGRP) & ~S_IXOTH) < 0) {
+ perror("chmod");
+ return 2;
+ }
+ }
+}
diff --git a/Year 3/System programming/Practice03/ex3.c b/Year 3/System programming/Practice03/ex3.c
new file mode 100644
index 0000000..45019bd
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex3.c
@@ -0,0 +1,17 @@
+#include <sys/stat.h>
+#include <utime.h> // struct utimbuf, utime
+#include <stdio.h> // perror
+
+int main(int argc, char* argv[]) {
+ struct stat buf;
+ if(stat(argv[1], &buf) < 0) {
+ perror("stat");
+ return 1;
+ }
+
+ struct utimbuf newtime = { buf.st_atime - 60 * 60 * 1, buf.st_mtime + 60 * 1 };
+ if (utime(argv[1], &newtime) < 0) {
+ perror("utime");
+ return 2;
+ }
+}
diff --git a/Year 3/System programming/Practice03/ex4.c b/Year 3/System programming/Practice03/ex4.c
new file mode 100644
index 0000000..0358e6c
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex4.c
@@ -0,0 +1,13 @@
+#include <unistd.h> // link, unlink
+#include <stdio.h> // perror
+
+int main(int argc, char* argv[]) {
+ if(link(argv[1], argv[2]) < 0) {
+ perror("link");
+ return 1;
+ }
+ if(unlink(argv[1]) < 0) {
+ perror("unlink");
+ return 2;
+ }
+}
diff --git a/Year 3/System programming/Practice03/ex5.c b/Year 3/System programming/Practice03/ex5.c
new file mode 100644
index 0000000..22e6991
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex5.c
@@ -0,0 +1,16 @@
+#include <unistd.h> // symlink, readlink
+#include <stdio.h> // perror, printf
+
+int main(int argc, char* argv[]) {
+ if(symlink(argv[1], argv[2]) < 0) {
+ perror("symlink");
+ return 1;
+ }
+
+ char buf[1024];
+ if(readlink(argv[2], buf, 1024) < 0) {
+ perror("readlink");
+ return 2;
+ }
+ printf("%s\n", buf);
+}
diff --git a/Year 3/System programming/Practice03/ex6.c b/Year 3/System programming/Practice03/ex6.c
new file mode 100644
index 0000000..d327085
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex6.c
@@ -0,0 +1,18 @@
+#include <stdio.h> // perror
+#include <sys/stat.h> // mkdir
+#include <unistd.h> // symlink, rmdir
+
+int main(int argc, char* argv[]) {
+ if(mkdir("./test", 0777) < 0) {
+ perror("mkdir");
+ return 1;
+ }
+ if(symlink("./test/myfile.txt", "./test/link.txt") < 0) {
+ perror("symlink");
+ return 2;
+ }
+ if(rmdir("./test") < 0) {
+ perror("rmdir");
+ return 3;
+ }
+}
diff --git a/Year 3/System programming/Practice03/ex7-cd.c b/Year 3/System programming/Practice03/ex7-cd.c
new file mode 100644
index 0000000..ad3b7ec
--- /dev/null
+++ b/Year 3/System programming/Practice03/ex7-cd.c
@@ -0,0 +1,9 @@
+#include <unistd.h> // chdir
+#include <stdio.h> // perror
+
+int main(int argc, char* argv[]) {
+ if(chdir(argv[1]) < 0) {
+ perror("chdir");
+ return 1;
+ }
+}