Logic to -w

This commit is contained in:
elvis
2022-04-20 22:34:06 +02:00
parent 4229c625bf
commit 72cb3ec3ea
2 changed files with 61 additions and 0 deletions

View File

@ -4,6 +4,9 @@
#include <time.h>
#define O_CREATE 1
#define O_LOCK 2
// struttura dati per la lista dei file aperti
typedef struct openFile_s {
char *filename; // nome del file

View File

@ -553,10 +553,68 @@ int cmd_w(char *dirname, char *Dir, int print) {
}
int cmd_W(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 == 1) {
printf("W - Scrivo i seguenti file sul server:\n");
fflush(stdout);
}
while ((token = strsep(&string, ",")) != NULL) {
int ok = 1;
int opened = 0;
if (print == 1) {
printf("%s\n", token);
fflush(stdout);
}
// creo il file in modalita' locked
if (openFile(token, O_CREATE | O_LOCK) == -1) {
ok = 0;
} else { // creato con successo
opened = 1;
}
if (opened && writeFile(token, Dir) == -1) {
ok = 0;
}
if (opened && !ok) { // errore precedente -> elimino il file vuoto
removeFile(token);
} else if (opened && closeFile(token) == -1) { // chiudo il file
ok = 0;
}
if (print != 0) {
printf("Esito: ");
if (ok)
printf("ok");
else {
printf("errore");
perror("-w");
}
printf("\n");
fflush(stdout);
}
}
free(tofree);
return 0;
}