From 1aa5f2e1d99e65f1cbf3ab676f929447d6a072c3 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 4 May 2022 23:42:12 +0200 Subject: [PATCH] comments for client.c --- lib/ini/ini.c | 344 +++++++++++++++++++++++++------------------------- src/client.c | 229 +++++++++++++++------------------ 2 files changed, 276 insertions(+), 297 deletions(-) diff --git a/lib/ini/ini.c b/lib/ini/ini.c index ab5f11d..d2f7ecd 100644 --- a/lib/ini/ini.c +++ b/lib/ini/ini.c @@ -28,247 +28,247 @@ #include "ini.h" struct ini_t { - char *data; - char *end; + char *data; + char *end; }; /* Case insensitive string compare */ static int strcmpci(const char *a, const char *b) { - for (;;) { - int d = tolower(*a) - tolower(*b); - if (d != 0 || !*a) { - return d; + for (;;) { + int d = tolower(*a) - tolower(*b); + if (d != 0 || !*a) { + return d; + } + a++, b++; } - a++, b++; - } } /* Returns the next string in the split data */ static char* next(ini_t *ini, char *p) { - p += strlen(p); - while (p < ini->end && *p == '\0') { - p++; - } - return p; + p += strlen(p); + while (p < ini->end && *p == '\0') { + p++; + } + return p; } static void trim_back(ini_t *ini, char *p) { - while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) { - *p-- = '\0'; - } + while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) { + *p-- = '\0'; + } } static char* discard_line(ini_t *ini, char *p) { - while (p < ini->end && *p != '\n') { - *p++ = '\0'; - } - return p; + while (p < ini->end && *p != '\n') { + *p++ = '\0'; + } + return p; } static char *unescape_quoted_value(ini_t *ini, char *p) { - /* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q` - * as escape sequences are always larger than their resultant data */ - char *q = p; - p++; - while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') { - if (*p == '\\') { - /* Handle escaped char */ - p++; - switch (*p) { - default : *q = *p; break; - case 'r' : *q = '\r'; break; - case 'n' : *q = '\n'; break; - case 't' : *q = '\t'; break; - case '\r' : - case '\n' : - case '\0' : goto end; - } + /* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q` + * as escape sequences are always larger than their resultant data */ + char *q = p; + p++; + while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') { + if (*p == '\\') { + /* Handle escaped char */ + p++; + switch (*p) { + default : *q = *p; break; + case 'r' : *q = '\r'; break; + case 'n' : *q = '\n'; break; + case 't' : *q = '\t'; break; + case '\r' : + case '\n' : + case '\0' : goto end; + } - } else { - /* Handle normal char */ - *q = *p; + } else { + /* Handle normal char */ + *q = *p; + } + q++, p++; } - q++, p++; - } end: - return q; + return q; } /* Splits data in place into strings containing section-headers, keys and * values using one or more '\0' as a delimiter. Unescapes quoted values */ static void split_data(ini_t *ini) { - char *value_start, *line_start; - char *p = ini->data; + char *value_start, *line_start; + char *p = ini->data; - while (p < ini->end) { - switch (*p) { - case '\r': - case '\n': - case '\t': - case ' ': - *p = '\0'; - /* Fall through */ + while (p < ini->end) { + switch (*p) { + case '\r': + case '\n': + case '\t': + case ' ': + *p = '\0'; + /* Fall through */ - case '\0': - p++; - break; + case '\0': + p++; + break; - case '[': - p += strcspn(p, "]\n"); - *p = '\0'; - break; + case '[': + p += strcspn(p, "]\n"); + *p = '\0'; + break; - case ';': - p = discard_line(ini, p); - break; + case ';': + p = discard_line(ini, p); + break; - default: - line_start = p; - p += strcspn(p, "=\n"); + default: + line_start = p; + p += strcspn(p, "=\n"); - /* Is line missing a '='? */ - if (*p != '=') { - p = discard_line(ini, line_start); - break; - } - trim_back(ini, p - 1); + /* Is line missing a '='? */ + if (*p != '=') { + p = discard_line(ini, line_start); + break; + } + trim_back(ini, p - 1); - /* Replace '=' and whitespace after it with '\0' */ - do { - *p++ = '\0'; - } while (*p == ' ' || *p == '\r' || *p == '\t'); + /* Replace '=' and whitespace after it with '\0' */ + do { + *p++ = '\0'; + } while (*p == ' ' || *p == '\r' || *p == '\t'); - /* Is a value after '=' missing? */ - if (*p == '\n' || *p == '\0') { - p = discard_line(ini, line_start); - break; - } + /* Is a value after '=' missing? */ + if (*p == '\n' || *p == '\0') { + p = discard_line(ini, line_start); + break; + } - if (*p == '"') { - /* Handle quoted string value */ - value_start = p; - p = unescape_quoted_value(ini, p); + if (*p == '"') { + /* Handle quoted string value */ + value_start = p; + p = unescape_quoted_value(ini, p); - /* Was the string empty? */ - if (p == value_start) { - p = discard_line(ini, line_start); - break; - } + /* Was the string empty? */ + if (p == value_start) { + p = discard_line(ini, line_start); + break; + } - /* Discard the rest of the line after the string value */ - p = discard_line(ini, p); + /* Discard the rest of the line after the string value */ + p = discard_line(ini, p); - } else { - /* Handle normal value */ - p += strcspn(p, "\n"); - trim_back(ini, p - 1); - } - break; + } else { + /* Handle normal value */ + p += strcspn(p, "\n"); + trim_back(ini, p - 1); + } + break; + } } - } } ini_t* ini_load(const char *filename) { - ini_t *ini = NULL; - FILE *fp = NULL; - int n, sz; + ini_t *ini = NULL; + FILE *fp = NULL; + int n, sz; - /* Init ini struct */ - ini = malloc(sizeof(*ini)); - if (!ini) { - goto fail; - } - memset(ini, 0, sizeof(*ini)); + /* Init ini struct */ + ini = malloc(sizeof(*ini)); + if (!ini) { + goto fail; + } + memset(ini, 0, sizeof(*ini)); - /* Open file */ - fp = fopen(filename, "rb"); - if (!fp) { - goto fail; - } + /* Open file */ + fp = fopen(filename, "rb"); + if (!fp) { + goto fail; + } - /* Get file size */ - fseek(fp, 0, SEEK_END); - sz = ftell(fp); - rewind(fp); + /* Get file size */ + fseek(fp, 0, SEEK_END); + sz = ftell(fp); + rewind(fp); - /* Load file content into memory, null terminate, init end var */ - ini->data = malloc(sz + 1); - ini->data[sz] = '\0'; - ini->end = ini->data + sz; - n = fread(ini->data, 1, sz, fp); - if (n != sz) { - goto fail; - } + /* Load file content into memory, null terminate, init end var */ + ini->data = malloc(sz + 1); + ini->data[sz] = '\0'; + ini->end = ini->data + sz; + n = fread(ini->data, 1, sz, fp); + if (n != sz) { + goto fail; + } - /* Prepare data */ - split_data(ini); + /* Prepare data */ + split_data(ini); - /* Clean up and return */ - fclose(fp); - return ini; + /* Clean up and return */ + fclose(fp); + return ini; fail: - if (fp) fclose(fp); - if (ini) ini_free(ini); - return NULL; + if (fp) fclose(fp); + if (ini) ini_free(ini); + return NULL; } void ini_free(ini_t *ini) { - free(ini->data); - free(ini); + free(ini->data); + free(ini); } const char* ini_get(ini_t *ini, const char *section, const char *key) { - char *current_section = ""; - char *val; - char *p = ini->data; + char *current_section = ""; + char *val; + char *p = ini->data; - if (*p == '\0') { - p = next(ini, p); - } - - while (p < ini->end) { - if (*p == '[') { - /* Handle section */ - current_section = p + 1; - - } else { - /* Handle key */ - val = next(ini, p); - if (!section || !strcmpci(section, current_section)) { - if (!strcmpci(p, key)) { - return val; - } - } - p = val; + if (*p == '\0') { + p = next(ini, p); } - p = next(ini, p); - } + while (p < ini->end) { + if (*p == '[') { + /* Handle section */ + current_section = p + 1; - return NULL; + } else { + /* Handle key */ + val = next(ini, p); + if (!section || !strcmpci(section, current_section)) { + if (!strcmpci(p, key)) { + return val; + } + } + p = val; + } + + p = next(ini, p); + } + + return NULL; } int ini_sget( - ini_t *ini, const char *section, const char *key, - const char *scanfmt, void *dst -) { - const char *val = ini_get(ini, section, key); - if (!val) { - return 0; - } - if (scanfmt) { - sscanf(val, scanfmt, dst); - } else { - *((const char**) dst) = val; - } - return 1; + ini_t *ini, const char *section, const char *key, + const char *scanfmt, void *dst + ) { + const char *val = ini_get(ini, section, key); + if (!val) { + return 0; + } + if (scanfmt) { + sscanf(val, scanfmt, dst); + } else { + *((const char**) dst) = val; + } + return 1; } diff --git a/src/client.c b/src/client.c index 3b9b707..0a5dfe1 100644 --- a/src/client.c +++ b/src/client.c @@ -1,5 +1,4 @@ #define _POSIX_C_SOURCE 200809L - #include #include #include @@ -7,6 +6,7 @@ #include #include #include +// fts uses this type but is not defined anywhere typedef unsigned short u_short; #include #include @@ -19,47 +19,42 @@ typedef unsigned short u_short; #define UNIX_PATH_MAX 256 #define MAXARGLENGTH 256 +// path of socket static char globalSocket[UNIX_PATH_MAX] = ""; - - -// struttura della lista dei comandi +// structure of the command list typedef struct cmd_s { - char name; // nome del comando - char *arg; // (eventuale) argomento del comando - struct cmd_s *next; // puntatore al prossimo comando nella lista + char name; // name of the command + char *arg; // argument of the command + struct cmd_s *next; // pointer to next element } cmd_t; // ----------------------------------------------------------------------------- // helper functions -// libera la memoria della lista dei comandi +// frees command list's memory void destroyCommandList(cmd_t *l); -// aggiunge un comando alla lista +// adds a command to the list int addCommand(cmd_t **l, char cmd, char *arg); -// riordina i comandi +// reorder the list int reorderCommandList(cmd_t **l); -// esegue tutti i comandi nella lista +// execute all commands int execute(cmd_t *l, int print); -int printList(cmd_t *l); - - -// per chiudere la connessione prima dell'uscita +// close connection before the exit void cleanup() { if (strncmp(globalSocket, "", 2) != 0) { closeConnection(globalSocket); strncpy(globalSocket, "", 2); } } -// compare files +// to compare files (fts) int compare(const FTSENT ** first, const FTSENT ** second) { return (strcmp((*first)->fts_name, (*second)->fts_name)); } // -h static void usage(const char *argv0) { - // TODO change this printf("Uso: %s\n", argv0); printf("-h: stampa il presente messaggio d'aiuto.\n"); printf("-f filename: connettiti al socket AF_UNIX 'filename'.\n"); @@ -105,7 +100,7 @@ int main(int argc, char* argv[]) { struct sigaction siga; - // ignoro il segnale SIGPIPE + // ignore SIGPIPE memset(&siga, 0, sizeof(siga)); siga.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &siga, NULL) == -1) { @@ -113,7 +108,7 @@ int main(int argc, char* argv[]) { return 1; } - // lista dei comandi + // list of commands cmd_t *cmds = NULL; int opt = 0; @@ -122,13 +117,13 @@ int main(int argc, char* argv[]) { int print = 0; char f = 0; - // add args to list + // add args to command list while ((opt = getopt(argc, argv, ":hpf:t:w:W:D:r:R:d:l:u:c:")) != -1) { switch (opt) { case 'h': // help message usage(argv[0]); goto _cleanup; - case 'f': // socket name + case 'f': // name of socket if(!f) { if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { fprintf(stderr, "Il comando -f necessita di un argomento.\n"); @@ -141,10 +136,11 @@ int main(int argc, char* argv[]) { } break; case 'p': // print to stdout - printInfo(1, stdout); + if(!print) + printInfo(1, stdout); print|=1; break; - case 'w': // send files from folder (n is read after) + case 'w': // send file from folder (n is read later) if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { fprintf(stderr, "Il comando -w necessita di un argomento.\n"); goto _cleanup; @@ -154,13 +150,13 @@ int main(int argc, char* argv[]) { addCommand(&cmds, opt, args); break; case 'W': // files to send separated by ',' - case 'D': // directory to store recived files + case 'D': // dir to write file into case 'r': // files to read from server separated by ',' - case 'd': // directory to store read files + case 'd': // dir to write read files into case 't': // time in ms between requests - case 'l': // file to request lock of separated by ',' - case 'u': // file to relese lock of separated by ',' - case 'c': // files to remove separated by ',' + case 'l': // file to request lock on separated by ',' + case 'u': // file to relese lock on separated by ',' + case 'c': // file to delete separated by ',' if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { fprintf(stderr, "Il comando -%c necessita di un argomento.\n", optopt); goto _cleanup; @@ -228,7 +224,7 @@ void destroyCommandList(cmd_t *l) { } cmd_t *tmp; - // scorro tutta la lista e libero la memoria + // visit every node and free memory while (l) { tmp = l; @@ -239,20 +235,6 @@ void destroyCommandList(cmd_t *l) { } } -int printList(cmd_t *l) { - if(!l) { - errno = EINVAL; - return -1; - } - - cmd_t *tmp = l; - while(tmp) { - fprintf(stdout, "%c with args: [%s]\n", tmp->name, tmp->arg); - tmp = tmp->next; - } - return 0; -} - int addCommand(cmd_t **l, char cmd, char *arg) { if(!l) { errno = EINVAL; @@ -277,7 +259,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) { } new->next = NULL; - // se lista vuota aggiungo in cima, altrimenti scorro la lista + // if empty list -> add to the top if (*l == NULL) { *l = new; return 0; @@ -293,6 +275,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) { } // inverts the order of -D and -d angainst -w, -W or -r, -R +// and adds -D/-d /dev/null after int reorderCommandList(cmd_t **l) { if(!l) { errno = EINVAL; @@ -309,24 +292,25 @@ int reorderCommandList(cmd_t **l) { switch (tmp->name) { case 'd': { if(prev == NULL || (prev->name != 'r' && prev->name != 'R')) { - fprintf(stdout, "\nError: -d has no -r or -R matching before\n"); + fprintf(stderr, "Il comando -d necessita -r o -R prima\n"); return -1; } - // invert tmp and prev - cmd_t t; - t.arg = tmp->arg; - t.name = tmp->name; + { // invert tmp and prev + cmd_t t; + t.arg = tmp->arg; + t.name = tmp->name; - tmp->arg = prev->arg; - tmp->name = prev->name; + tmp->arg = prev->arg; + tmp->name = prev->name; - prev->arg = t.arg; - prev->name = t.name; + prev->arg = t.arg; + prev->name = t.name; + } if(!tmp->next) break; - // add redirect to dev/null for next command if it's r or R + // add redirect to /dev/null for next command if it's r or R if(tmp->next->name == 'r' || tmp->next->name == 'R') { cmd_t *new = calloc(1, sizeof(*new)); if(!new){ @@ -351,19 +335,20 @@ int reorderCommandList(cmd_t **l) { } case 'D': { if(prev == NULL || (prev->name != 'w' && prev->name != 'W')) { - fprintf(stdout, "\nError: -D has no -w or -W matching before\n"); + fprintf(stderr, "Il comando -D necessita -w o -W prima\n"); return -1; } - // invert tmp and prev - cmd_t t; - t.arg = tmp->arg; - t.name = tmp->name; + { // invert tmp and prev + cmd_t t; + t.arg = tmp->arg; + t.name = tmp->name; - tmp->arg = prev->arg; - tmp->name = prev->name; + tmp->arg = prev->arg; + tmp->name = prev->name; - prev->arg = t.arg; - prev->name = t.name; + prev->arg = t.arg; + prev->name = t.name; + } if(!tmp->next) break; @@ -415,14 +400,14 @@ int execute(cmd_t *l, int print) { // loop that serches for -t while(tmp) { switch(tmp->name) { - case 't': {// time in ms between requests + case 't': { // time in ms between requests long num; num = strtol(tmp->arg, NULL, 10); if(num==0 && errno==EINVAL) { perror("Invalid time specified after -t"); return -1; } - // milliseconds converted to nanoseconds + // milliseconds to nanoseconds interval.tv_nsec = (num%1000) * 1000000; // seconds interval.tv_sec = (num/1000); @@ -481,6 +466,8 @@ int execute(cmd_t *l, int print) { // loop that checks for consistencies: // 1) -D with no -w or -W after // 2) -d with no -r or -R after + // reduntant since we also reorder before but this function could be used + // alone so we have to check tmp = l; int unmachedD = 0; int unmachedd = 0; @@ -488,7 +475,7 @@ int execute(cmd_t *l, int print) { switch (tmp->name) { case 'D': if(unmachedD) { - fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); + fprintf(stderr, "Il comando -D necessita -w o -W dopo\n"); return 0; } unmachedD = 1; @@ -499,7 +486,7 @@ int execute(cmd_t *l, int print) { break; case 'd': if(unmachedd) { - fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); + fprintf(stderr, "Il comando -d necessita -r o -R dopo\n"); return 0; } unmachedd = 1; @@ -514,11 +501,11 @@ int execute(cmd_t *l, int print) { tmp = tmp->next; } if(unmachedD) { - fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); + fprintf(stderr, "Il comando -D necessita -w o -W dopo\n"); return 0; } if(unmachedd) { - fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); + fprintf(stderr, "Il comando -d necessita -r o -R dopo\n"); return 0; } fflush(stdout); @@ -547,7 +534,7 @@ int execute(cmd_t *l, int print) { perror("-D"); } if (print) - printf("\nD - Cartella per le scritture: %s\tEsito: ok\n", Dir); + printf("D - Cartella per le scritture: %s [Esito: ok]\n", Dir); break; case 'r': cmd_r(tmp->arg, dir, print); @@ -566,7 +553,7 @@ int execute(cmd_t *l, int print) { perror("-d"); } if(print) - printf("\nd - Cartella per le letture: %s\tEsito: ok\n", dir); + printf("d - Cartella per le letture: %s [Esito: ok]\n", dir); break; case 'l': cmd_l(tmp->arg, print); @@ -602,6 +589,8 @@ int execute(cmd_t *l, int print) { // ----------------------------------------------------------------------------- // funzioni relative ai comandi + +// -f int cmd_f(char *socket) { if(!socket) { errno = EINVAL; @@ -618,6 +607,7 @@ int cmd_f(char *socket) { return 0; } +// -w int cmd_w(char *dirname, char *Dir, int print) { if(!dirname) { errno = EINVAL; @@ -625,7 +615,7 @@ int cmd_w(char *dirname, char *Dir, int print) { } int num; - // we copy dirname because we are nice + // copy dirname because we modify it after char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char)); if(!tofree) { perror("malloc"); @@ -656,12 +646,11 @@ int cmd_w(char *dirname, char *Dir, int print) { return -1; } - if (print) { - printf("\nw - Scrivo i seguenti file sul server: \n"); - fflush(stdout); - } + if (print) + printf("w - Scrivo i seguenti file sul server:\n"); // we use fts to traverse all files in the directory recursively + // and write them to a list so that we can open them later FTS *fhandle = NULL; FTSENT *child = NULL; FTSENT *parent = NULL; @@ -694,7 +683,7 @@ int cmd_w(char *dirname, char *Dir, int print) { if(print) { printf("%s [", listFiles[i]); } - // we send the file with the other function but set print to 0 + // we send the file with cmd_W but set print to 0 int r = cmd_W(listFiles[i], Dir, 0); if(print && !r) { printf("Esito: ok"); @@ -714,13 +703,14 @@ int cmd_w(char *dirname, char *Dir, int print) { return 0; } +// -W int cmd_W(char *filelist, char *Dir, int print) { if(!filelist) { errno = EINVAL; return -1; } - // we copy filelist because we are nice + // we copy filelist because we modify it later char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); if(!tofree) { perror("malloc"); @@ -733,11 +723,10 @@ int cmd_W(char *filelist, char *Dir, int print) { char *string = tofree; if (print == 1) { - printf("W - Scrivo i seguenti file sul server: \n"); - fflush(stdout); + printf("W - Scrivo i seguenti file sul server:\n"); } - int r = 0; + int r = 0; // return value while ((token = strsep_gnu(&string, ",")) != NULL) { int ok = 1; int opened = 0; @@ -746,20 +735,21 @@ int cmd_W(char *filelist, char *Dir, int print) { fflush(stdout); } - // creo il file in modalita' locked + // create the file locked if (openFile(token, O_CREATE | O_LOCK) == -1) { ok = 0; - } else { // creato con successo + } else { // success opened = 1; } + // write to the file if (opened && (writeFile(token, Dir) == -1)) { ok = 0; } - if (opened && !ok) { // errore precedente -> elimino il file vuoto + if (opened && !ok) { // previous error -> delete the empty file removeFile(token); - } else if (opened && (closeFile(token) == -1)) { // chiudo il file + } else if (opened && (closeFile(token) == -1)) { // close the file ok = 0; } @@ -767,13 +757,12 @@ int cmd_W(char *filelist, char *Dir, int print) { printf("Esito: "); if (ok) - printf("ok"); + printf("ok]\n"); else { - printf("errore"); - perror("-w"); + printf("errore]\n"); + perror("-W"); } - printf("]\n"); fflush(stdout); } if(!ok) @@ -784,13 +773,14 @@ int cmd_W(char *filelist, char *Dir, int print) { return r; } +// -r int cmd_r(char *filelist, char *dir, int print) { if (!filelist) { errno = EINVAL; return -1; } - // we copy filelist because we are nice + // we copy filelist because we modify it later char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1); if(!tofree) { @@ -813,9 +803,9 @@ int cmd_r(char *filelist, char *dir, int print) { if (print != 0) { printf("%s ", token); - fflush(stdout); } + // open the file if (openFile(token, 0) == -1) { ok = 0; } else { @@ -828,8 +818,7 @@ int cmd_r(char *filelist, char *dir, int print) { printInfo(0, stdout); // read the content of the file if (ok && readFile(token, &buf, &size) == -1) { - fprintf(stdout, "\nreadFile\n"); - fflush(stdout); + fprintf(stderr, "Errore durante la readFile\n"); ok = 0; } if (print) { @@ -845,22 +834,18 @@ int cmd_r(char *filelist, char *dir, int print) { // close the file if (opened && closeFile(token) == -1) { - fprintf(stdout, "\ncloseFile\n"); - fflush(stdout); + fprintf(stderr, "Errore durante la closeFile\n"); ok = 0; } if (print) { printf("[Esito: "); if (ok) { - printf("ok"); + printf("ok]\n"); } else { - printf("errore"); + printf("errore]\n"); perror("-r"); } - } - if (print) { - printf("]\n"); fflush(stdout); } } @@ -873,10 +858,10 @@ int cmd_r(char *filelist, char *dir, int print) { printf("I file letti non sono stati memorizzati su disco.\n"); fflush(stdout); } - return 0; } +// -R int cmd_R(char *numStr, char *dir, int print) { if (print) { printf("R - Leggo i seguenti file dal server:\n"); @@ -884,7 +869,6 @@ int cmd_R(char *numStr, char *dir, int print) { } int n = -1; - if(!numStr) // skips the step of converting n from string to int goto skipGetNumber; @@ -920,13 +904,14 @@ skipGetNumber: return 0; } +// -l int cmd_l(char *filelist, int print) { if (!filelist) { errno = EINVAL; return -1; } - // we copy filelist because we are nice + // we copy filelist because we modify it later char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); if(!tofree) { perror("malloc"); @@ -950,15 +935,12 @@ int cmd_l(char *filelist, int print) { if (lockFile(token) == -1) { if (print != 0) { - printf("errore"); + printf("errore]\n"); + fflush(stdout); perror("-l"); } } else if(print != 0) { - printf("ok"); - } - - if (print != 0) { - printf("]\n"); + printf("ok]\n"); fflush(stdout); } } @@ -966,13 +948,14 @@ int cmd_l(char *filelist, int print) { return 0; } +// -u int cmd_u(char *filelist, int print) { if (!filelist) { errno = EINVAL; return -1; } - // we copy filelist because we are nice + // we copy filelist because we modify it later char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); if(!tofree) { perror("calloc"); @@ -996,15 +979,12 @@ int cmd_u(char *filelist, int print) { if (unlockFile(token) == -1) { if (print != 0) { - printf("errore"); + printf("errore]\n"); + fflush(stdout); perror("-u"); } } else if(print != 0) { - printf("ok"); - } - - if (print != 0) { - printf("]\n"); + printf("ok]\n"); fflush(stdout); } } @@ -1012,13 +992,14 @@ int cmd_u(char *filelist, int print) { return 0; } +// -c int cmd_c(char *filelist, int print) { if (!filelist) { errno = EINVAL; return -1; } - // we copy filelist because we are nice + // we copy filelist because we modify it later char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); if(!tofree) { perror("calloc"); @@ -1042,24 +1023,22 @@ int cmd_c(char *filelist, int print) { if (lockFile(token) == -1) { if (print) { - printf("errore"); + printf("errore]\n"); + fflush(stdout); perror("-c"); } } else { if (removeFile(token) == -1) { if (print) { - printf("errore"); + printf("errore]\n"); + fflush(stdout); perror("-c"); } } else if (print) { - printf("ok"); + printf("ok]\n"); + fflush(stdout); } } - - if (print != 0) { - printf("]\n"); - fflush(stdout); - } } free(tofree); return 0;