From 8bafe70a68d7a2ad92723a63a8dec3d08393ffe0 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 20 Apr 2022 23:33:40 +0200 Subject: [PATCH] Logic for -l, -u and -c --- src/client.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 136 insertions(+), 3 deletions(-) diff --git a/src/client.c b/src/client.c index 85ed4af..05fdf67 100644 --- a/src/client.c +++ b/src/client.c @@ -574,11 +574,11 @@ int cmd_W(char *filelist, char *Dir, int print) { fflush(stdout); } - while ((token = strsep(&string, ",")) != NULL) { + while ((token = strsep_gnu(&string, ",")) != NULL) { int ok = 1; int opened = 0; if (print == 1) { - printf("%s\n", token); + printf("%s [", token); fflush(stdout); } @@ -609,7 +609,7 @@ int cmd_W(char *filelist, char *Dir, int print) { perror("-w"); } - printf("\n"); + printf("]\n"); fflush(stdout); } } @@ -627,13 +627,146 @@ int cmd_R(char *numStr, char *dir, int print) { } int cmd_l(char *filelist, int print) { + if (!filelist) { + errno = EINVAL; + return -1; + } + + // we copy filelist because we are nice + char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); + + char *token; + char *string = tofree; + + if (print) { + printf("l - Acquisisco la mutua esclusione sui seguenti file:\n"); + fflush(stdout); + } + + while ((token = strsep_gnu(&string, ",")) != NULL) { + if (print != 0) { + printf("%s [Esito: ", token); + fflush(stdout); + } + + if (lockFile(token) == -1) { + if (print != 0) { + printf("errore"); + perror("-l"); + } + } else if(print != 0) { + printf("ok"); + } + + if (print != 0) { + printf("]\n"); + fflush(stdout); + } + } + free(tofree); return 0; } int cmd_u(char *filelist, int print) { + if (!filelist) { + errno = EINVAL; + return -1; + } + + // we copy filelist because we are nice + char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); + + char *token; + char *string = tofree; + + if (print) { + printf("u - Rilascio la mutua esclusione sui seguenti file:\n"); + fflush(stdout); + } + + while ((token = strsep_gnu(&string, ",")) != NULL) { + if (print != 0) { + printf("%s [Esito: ", token); + fflush(stdout); + } + + if (unlockFile(token) == -1) { + if (print != 0) { + printf("errore"); + perror("-u"); + } + } else if(print != 0) { + printf("ok"); + } + + if (print != 0) { + printf("]\n"); + fflush(stdout); + } + } + free(tofree); return 0; } int cmd_c(char *filelist, int print) { + if (!filelist) { + errno = EINVAL; + return -1; + } + + // we copy filelist because we are nice + char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); + + char *token; + char *string = tofree; + + if (print) { + printf("c - Cancello i seguenti file dal server:\n"); + fflush(stdout); + } + + while ((token = strsep_gnu(&string, ",")) != NULL) { + if (print != 0) { + printf("%s [Esito: ", token); + fflush(stdout); + } + + if (lockFile(token) == -1) { + if (print) { + printf("errore"); + perror("-c"); + } + } else { + if (removeFile(token) == -1) { + if (print) { + printf("errore"); + perror("-c"); + } + } else if (print) { + printf("ok"); + } + } + + if (print != 0) { + printf("]\n"); + fflush(stdout); + } + } + free(tofree); return 0; }