diff options
| Commit | 54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1 (patch) | |
|---|---|---|
| Author | 2023-12-02 18:49:11 +0200 | |
| Committer | 2023-12-02 18:49:11 +0200 | |
| Files | 62b872040b800398285bf747b7b8faf7fe7c5be1 | |
| uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.tar uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.tar.gz uni-projects-54bc74f4bcc80a9f3c874db16c43b6b1cd07d3e1.zip | ||
| Parent | a594346d7e2ca97717cae7e705c00d6fe0b9a460 (diff) | |
[SP/P02] Solved exercises from second practice
4 files changed, 111 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice02/ex1-1.c | 25 | ||||
| -rw-r--r-- | Year 3/System programming/Practice02/ex1-2.c | 32 | ||||
| -rw-r--r-- | Year 3/System programming/Practice02/ex2.c | 24 | ||||
| -rw-r--r-- | Year 3/System programming/Practice02/ex3-stat.c | 30 |
diff --git a/Year 3/System programming/Practice02/ex1-1.c b/Year 3/System programming/Practice02/ex1-1.c new file mode 100644 index 0000000..55f4ccf --- /dev/null +++ b/Year 3/System programming/Practice02/ex1-1.c @@ -0,0 +1,25 @@ +#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 = open(argv[1], O_RDONLY); + if (f2 < 0) { + perror("open"); + return 2; + } + + char buf1, buf2; + while (read(f1, &buf1, 1) && read(f2, &buf2, 1)) { + printf("%c%c", buf1, buf2); + } + + close(f1); + close(f2); +} 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); +} diff --git a/Year 3/System programming/Practice02/ex2.c b/Year 3/System programming/Practice02/ex2.c new file mode 100644 index 0000000..6a8c420 --- /dev/null +++ b/Year 3/System programming/Practice02/ex2.c @@ -0,0 +1,24 @@ +#include <fcntl.h> // open, close, O_WRONLY, ... +#include <unistd.h> // dup +#include <stdio.h> // printf + +int main(int argc, char* argv[]) { + int f1 = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0755); + if (f1 < 0) { + perror("open"); + return 1; + } + + close(1); + + int f2 = dup(f1); + if (f2 < 0) { + perror("dup"); + return 2; + } + + printf("Hello World!\n"); + + close(f1); + close(f2); +} diff --git a/Year 3/System programming/Practice02/ex3-stat.c b/Year 3/System programming/Practice02/ex3-stat.c new file mode 100644 index 0000000..24a7777 --- /dev/null +++ b/Year 3/System programming/Practice02/ex3-stat.c @@ -0,0 +1,30 @@ +#include <sys/stat.h> // stat, struct stat, S_ISREG, ... +#include <stdio.h> // printf, 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; + } + + printf(" File: %s\n", argv[i]); + printf(" Size: %ld\tBlocks: %ld\tIO Block: %ld\t", buf.st_size, buf.st_blocks, buf.st_blksize); + + if (S_ISREG(buf.st_mode)) printf("regular file"); + else if (S_ISDIR(buf.st_mode)) printf("directory"); + else if (S_ISCHR(buf.st_mode)) printf("character special file"); + else if (S_ISBLK(buf.st_mode)) printf("block special file"); + else if (S_ISFIFO(buf.st_mode)) printf("fifo"); + else if (S_ISLNK(buf.st_mode)) printf("symbolic link"); + else if (S_ISSOCK(buf.st_mode)) printf("socket"); + printf("\n"); + + printf("Device: %ld\tInode: %ld\tLinks: %ld\n", buf.st_dev, buf.st_ino, buf.st_nlink); + printf("Access: %u\tUid: %u\tGid: %u\n", buf.st_mode, buf.st_uid, buf.st_gid); + printf("Access: %ld\n", buf.st_atime); + printf("Modify: %ld\n", buf.st_mtime); + printf("Change: %ld\n", buf.st_ctime); + } +} |
