From 0b28848f9b6584a44054f4e46871b1878ada44b8 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 5 Jul 2021 17:23:29 +0200 Subject: os: add homework 1 --- Year_2/OS/hw/hw1.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Year_2/OS/hw/hw1.c (limited to 'Year_2') diff --git a/Year_2/OS/hw/hw1.c b/Year_2/OS/hw/hw1.c new file mode 100644 index 0000000..814b840 --- /dev/null +++ b/Year_2/OS/hw/hw1.c @@ -0,0 +1,90 @@ +/* + Homework n.1 + + Scrivere un programma in linguaggio C che permetta di copiare un numero + arbitrario di file regolari su una directory di destinazione preesistente. + + Il programma dovra' accettare una sintassi del tipo: + $ homework-1 file1.txt path/file2.txt "nome con spazi.pdf" directory-destinazione +*/ +#include +#include +#include +#include +#include +#include +#include + +int copyfile(int*, int*); + +int main(int argc, char* argv[0]) +{ + int fd, fd_dst; + int i; + unsigned long size; + char dst[1024]; + char buffer[1024]; + char *p1, *p2; + + if (argc < 3) { + fprintf(stderr, "Use %s [..] ", argv[0]); + exit(1); + } + + i = 1; + strncpy(dst, argv[argc - 1], 1024); + size = strlen(dst); + strncpy(dst + size, "/", 1024 - size); + size++; + while (i < argc - 1) { + /* Open the source file */ + if ((fd = open(argv[i], O_RDONLY)) == -1) { + perror(argv[i]); + exit(1); + } + + /* Open the destination file */ + strncpy(buffer, dst, size); + p1 = p2 = argv[i++]; + while (*p2 != '\0') { + if (*p2 == '/') + p1 = p2 + 1; + p2++; + } + strncpy(buffer + size, p1, 1024 - size); + + if ((fd_dst = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) { + perror(argv[argc - 1]); + exit(1); + } + + /* Ignore the return value, the last number of bytes returned. Result is + * always = 0 */ + copyfile(&fd, &fd_dst); + close(fd); + close(fd_dst); + printf("`%s` written successfully\n", buffer); + } + + return 0; +} + +int copyfile(int* src, int* dst) +{ + int n; + char* buffer[1024]; + + do { + if ((n = read(*src, buffer, 1024)) == -1) { + perror("Error reading the source file"); + exit(1); + } + + if (write(*dst, buffer, n) == -1) { + perror("Error writing the destination file"); + exit(1); + } + } while (n > 0); + + return n; +} -- cgit v1.2.3-18-g5258