diff --git a/src/client.c b/src/client.c index 05fdf67..4df920d 100644 --- a/src/client.c +++ b/src/client.c @@ -44,6 +44,10 @@ void cleanup() { strncpy(globalSocket, "", 2); } } +// compare files +int compare(const FTSENT ** first, const FTSENT ** second) { + return (strcmp((*first)->fts_name, (*second)->fts_name)); +} // -h static void usage(const char *argv0) { @@ -63,7 +67,6 @@ static void usage(const char *argv0) { printf("-c file1[,file2]: rimuovi dal server una lista di file (se presenti), separati da virgole.\n"); printf("-p: stampa sullo standard output le informazioni riguardo ogni operazione effettuata.\n"); } - // -f int cmd_f(char *socket); // -w @@ -81,11 +84,6 @@ int cmd_u(char *filelist, int print); // -c int cmd_c(char *filelist, int print); -// compare files -int compare(const FTSENT ** first, const FTSENT ** second) { - return (strcmp((*first)->fts_name, (*second)->fts_name)); -} - // ----------------------------------------------------------------------------- // MAIN @@ -619,10 +617,149 @@ int cmd_W(char *filelist, char *Dir, int print) { } int cmd_r(char *filelist, char *dir, 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("r - Leggo i seguenti file dal server:\n"); + fflush(stdout); + } + + while ((token = strsep_gnu(&string, ",")) != NULL) { + int ok = 1; + int opened = 0; + + if (print != 0) { + printf("%s ", token); + fflush(stdout); + } + + if (openFile(token, 0) == -1) { + ok = 0; + } else { + opened = 1; + } + + void *buf = NULL; + size_t size = -1; + + printInfo(0); + // read the content of the file + if (ok && readFile(token, &buf, &size) == -1) { + ok = 0; + } + if (print) { + printInfo(1); + } + if (ok && print) { + printf("Dimensione: %zu B\t", size); + fflush(stdout); + } + if (buf) { + free(buf); + } + + // close the file + if (opened && closeFile(token) == -1) { + ok = 0; + } + if (print) { + printf("[Esito: "); + + if (ok) { + printf("ok"); + } + + else { + printf("errore"); + perror("-r"); + } + } + if (print) { + printf("]\n"); + fflush(stdout); + } + } + free(tofree); + + if (print && dir) { + printf("I file letti sono stati scritti nella cartella %s.\n.", dir); + fflush(stdout); + } else if (print) { + printf("I file letti non sono stati memorizzati su disco.\n"); + fflush(stdout); + } + return 0; } int cmd_R(char *numStr, char *dir, int print) { + if (print) { + printf("R - Leggo i seguenti file dal server:\n"); + fflush(stdout); + } + + int n = -1; + + if(!numStr) // skips the step of converting n from string to int + goto skipGetNumber; + + // we copy numStr because we are nice + char *tofree = malloc(strnlen(numStr, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, numStr, strnlen(numStr, MAXARGLENGTH)); + + char *secondArg = tofree; + strsep_gnu(&secondArg, ","); + + if (!secondArg) { + n = -1; + } else if (secondArg[0] == 'n' && secondArg[1] == '=') { + char *number = &secondArg[2]; + for (int i = 0; i < strlen(number); ++i) { + if (!isdigit(number[i])) { + free(tofree); + errno = EINVAL; + return -1; + } + } + n = (int) strtol(number, NULL, 10); + n = (n==0)?-1:n; + } else { + free(tofree); + errno = EINVAL; + return -1; + } + + free(tofree); + +skipGetNumber: + if (readNFiles(n, dir) == -1) { + return -1; + } + + if (print && dir) { + printf("I file letti sono stati scritti nella cartella %s.\n", dir); + } else if (print) { + printf("I file letti non sono stati memorizzati su disco.\n"); + } + return 0; }