perror for client.c

This commit is contained in:
elvis
2022-05-05 21:12:44 +02:00
parent aae9e4869c
commit 15ab8f44d0

View File

@ -104,7 +104,7 @@ int main(int argc, char* argv[]) {
memset(&siga, 0, sizeof(siga)); memset(&siga, 0, sizeof(siga));
siga.sa_handler = SIG_IGN; siga.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &siga, NULL) == -1) { if (sigaction(SIGPIPE, &siga, NULL) == -1) {
perror("sigaction.\n"); perror("main: sigaction");
return 1; return 1;
} }
@ -194,12 +194,12 @@ int main(int argc, char* argv[]) {
} }
if(reorderCommandList(&cmds) < 0) { if(reorderCommandList(&cmds) < 0) {
perror("reorder"); perror("main: reorderCommandList");
goto _cleanup; goto _cleanup;
} }
if(execute(cmds, print) < 0) { if(execute(cmds, print) < 0) {
perror("execute"); perror("main: execute");
goto _cleanup; goto _cleanup;
} }
@ -243,7 +243,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
cmd_t *new = calloc(1, sizeof(cmd_t)); cmd_t *new = calloc(1, sizeof(cmd_t));
if(!new) { if(!new) {
perror("calloc"); perror("addCommand: calloc");
return -1; return -1;
} }
new->name = cmd; new->name = cmd;
@ -251,7 +251,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
if (arg) { if (arg) {
new->arg = malloc(MAXARGLENGTH); new->arg = malloc(MAXARGLENGTH);
if (new->arg == NULL) { if (new->arg == NULL) {
perror("malloc arg"); perror("addCommand: malloc");
free(new); free(new);
return -1; return -1;
} }
@ -314,7 +314,7 @@ int reorderCommandList(cmd_t **l) {
if(tmp->next->name == 'r' || tmp->next->name == 'R') { if(tmp->next->name == 'r' || tmp->next->name == 'R') {
cmd_t *new = calloc(1, sizeof(*new)); cmd_t *new = calloc(1, sizeof(*new));
if(!new){ if(!new){
perror("calloc new"); perror("reorderCommandList: case d, calloc");
return -1; return -1;
} }
@ -323,7 +323,7 @@ int reorderCommandList(cmd_t **l) {
new->arg = malloc(MAXARGLENGTH); new->arg = malloc(MAXARGLENGTH);
if (new->arg == NULL) { if (new->arg == NULL) {
perror("malloc arg"); perror("reorderCommandList: case d, malloc");
free(new); free(new);
return -1; return -1;
} }
@ -357,7 +357,7 @@ int reorderCommandList(cmd_t **l) {
if(tmp->next->name == 'w' || tmp->next->name == 'W') { if(tmp->next->name == 'w' || tmp->next->name == 'W') {
cmd_t *new = calloc(1, sizeof(*new)); cmd_t *new = calloc(1, sizeof(*new));
if(!new){ if(!new){
perror("calloc new"); perror("reorderCommandList: case D, calloc");
return -1; return -1;
} }
@ -366,7 +366,7 @@ int reorderCommandList(cmd_t **l) {
new->arg = malloc(MAXARGLENGTH); new->arg = malloc(MAXARGLENGTH);
if (new->arg == NULL) { if (new->arg == NULL) {
perror("malloc arg"); perror("reorderCommandList: case D, malloc");
free(new); free(new);
return -1; return -1;
} }
@ -404,7 +404,7 @@ int execute(cmd_t *l, int print) {
long num; long num;
num = strtol(tmp->arg, NULL, 10); num = strtol(tmp->arg, NULL, 10);
if(num==0 && errno==EINVAL) { if(num==0 && errno==EINVAL) {
perror("Invalid time specified after -t"); fprintf(stderr, "t - Tempo non riconosciuto dopo l'opzione -t\n");
return -1; return -1;
} }
// milliseconds to nanoseconds // milliseconds to nanoseconds
@ -436,15 +436,15 @@ int execute(cmd_t *l, int print) {
strncpy(globalSocket, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH)+1); strncpy(globalSocket, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH)+1);
if(cmd_f(tmp->arg) != 0) { if(cmd_f(tmp->arg) != 0) {
if (print) if (print)
perror("-f"); perror("execute: -f");
ok = 0; ok = 0;
} }
if (print) { if (print) {
fprintf(stdout, "f - Connessione al socket: %s\tEsito: ", globalSocket); fprintf(stdout, "f - Connessione al socket: %s [Esito: ", globalSocket);
if(ok) if(ok)
printf("ok\n"); printf("ok]\n");
if(!ok) if(!ok)
printf("errore\n"); printf("errore]\n");
} }
if(!ok) { if(!ok) {
closeConnection(tmp->arg); closeConnection(tmp->arg);
@ -531,7 +531,7 @@ int execute(cmd_t *l, int print) {
if (setDirectory(Dir, 1) == -1) { if (setDirectory(Dir, 1) == -1) {
if(print) if(print)
perror("-D"); perror("execute: -D");
} }
if (print) if (print)
printf("D - Cartella per le scritture: %s [Esito: ok]\n", Dir); printf("D - Cartella per le scritture: %s [Esito: ok]\n", Dir);
@ -550,7 +550,7 @@ int execute(cmd_t *l, int print) {
if(setDirectory(dir, 0) == -1) { if(setDirectory(dir, 0) == -1) {
if(print) if(print)
perror("-d"); perror("execute: -d");
} }
if(print) if(print)
printf("d - Cartella per le letture: %s [Esito: ok]\n", dir); printf("d - Cartella per le letture: %s [Esito: ok]\n", dir);
@ -588,7 +588,7 @@ int execute(cmd_t *l, int print) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// funzioni relative ai comandi // commands
// -f // -f
int cmd_f(char *socket) { int cmd_f(char *socket) {
@ -618,7 +618,7 @@ int cmd_w(char *dirname, char *Dir, int print) {
// copy dirname because we modify it after // copy dirname because we modify it after
char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("malloc"); perror("cmd_w: calloc");
return -1; return -1;
} }
strncpy(tofree, dirname, strnlen(dirname, MAXARGLENGTH)); strncpy(tofree, dirname, strnlen(dirname, MAXARGLENGTH));
@ -713,7 +713,7 @@ int cmd_W(char *filelist, char *Dir, int print) {
// we copy filelist because we modify it later // we copy filelist because we modify it later
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
if(!tofree) { if(!tofree) {
perror("malloc"); perror("cmd_W: malloc");
return -1; return -1;
} }
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1); memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
@ -782,11 +782,11 @@ int cmd_r(char *filelist, char *dir, int print) {
// we copy filelist because we modify it later // we copy filelist because we modify it later
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
if(!tofree) { if(!tofree) {
perror("malloc"); perror("cmd_r: malloc");
return -1; return -1;
} }
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
char *token = NULL; char *token = NULL;
@ -914,7 +914,7 @@ int cmd_l(char *filelist, int print) {
// we copy filelist because we modify it later // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("malloc"); perror("cmd_l: malloc");
return -1; return -1;
} }
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
@ -937,7 +937,7 @@ int cmd_l(char *filelist, int print) {
if (print != 0) { if (print != 0) {
printf("errore]\n"); printf("errore]\n");
fflush(stdout); fflush(stdout);
perror("-l"); perror("cmd_l: lockFile");
} }
} else if(print != 0) { } else if(print != 0) {
printf("ok]\n"); printf("ok]\n");
@ -958,7 +958,7 @@ int cmd_u(char *filelist, int print) {
// we copy filelist because we modify it later // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("calloc"); perror("cmd_u: calloc");
return -1; return -1;
} }
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
@ -981,7 +981,7 @@ int cmd_u(char *filelist, int print) {
if (print != 0) { if (print != 0) {
printf("errore]\n"); printf("errore]\n");
fflush(stdout); fflush(stdout);
perror("-u"); perror("cmd_u: unlockFile");
} }
} else if(print != 0) { } else if(print != 0) {
printf("ok]\n"); printf("ok]\n");
@ -1002,7 +1002,7 @@ int cmd_c(char *filelist, int print) {
// we copy filelist because we modify it later // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("calloc"); perror("cmd_c: calloc");
return -1; return -1;
} }
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
@ -1025,14 +1025,14 @@ int cmd_c(char *filelist, int print) {
if (print) { if (print) {
printf("errore]\n"); printf("errore]\n");
fflush(stdout); fflush(stdout);
perror("-c"); perror("cmd_c: lockFile");
} }
} else { } else {
if (removeFile(token) == -1) { if (removeFile(token) == -1) {
if (print) { if (print) {
printf("errore]\n"); printf("errore]\n");
fflush(stdout); fflush(stdout);
perror("-c"); perror("cmd_c: removeFile");
} }
} else if (print) { } else if (print) {
printf("ok]\n"); printf("ok]\n");