working -R

This commit is contained in:
elvis
2022-04-29 21:04:45 +02:00
parent 6a7b018f25
commit 59ac9663c9
5 changed files with 69 additions and 38 deletions

View File

@ -747,24 +747,13 @@ int cmd_R(char *numStr, char *dir, int print) {
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));
if(strlen(numStr) < 2)
goto skipGetNumber;
char *secondArg = tofree;
strsep_gnu(&secondArg, ",");
if (!secondArg) {
n = -1;
} else if (secondArg[0] == 'n' && secondArg[1] == '=') {
char *number = &secondArg[2];
if (numStr[0] == 'n' && numStr[1] == '=') {
char *number = &numStr[2];
for (int i = 0; i < strlen(number); ++i) {
if (!isdigit(number[i])) {
free(tofree);
errno = EINVAL;
return -1;
}
@ -772,13 +761,10 @@ int cmd_R(char *numStr, char *dir, int print) {
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;

View File

@ -67,13 +67,16 @@ int main(int argc, char *argv[]) {
int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
ini_free(config);
queueT *queue = NULL;
taglia_t *taglia = NULL;
sigset_t mask;
sigfillset(&mask);
sigdelset(&mask, SIGPIPE); // tolgo soltanto la sigpipe
if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
fprintf(stderr, "ERROR setting mask\n");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// ignoro SIGPIPE per evitare di essere terminato da una scrittura su un socket
@ -82,18 +85,18 @@ int main(int argc, char *argv[]) {
s.sa_handler = SIG_IGN;
if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) {
perror("sigaction");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// remove("mysock"); maybe necessary???
// creo la struttura per il log
taglia_t *taglia = taglia_init(logFile, 0);
taglia = taglia_init(logFile, 0);
free(logFile); // free del nome del file
if(taglia==NULL) {
perror("taglia_init");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// risorse per il logfile usate nel main
char buf[2048];
@ -109,7 +112,7 @@ int main(int argc, char *argv[]) {
}
if (pipe(request_pipe) == -1) {
perror("pipe");
goto _cleanup;
goto _cleanup_beforesigthread;
}
@ -171,7 +174,7 @@ int main(int argc, char *argv[]) {
// creo la queue
queueT *queue = createQueue(maxFiles, maxSize);
queue = createQueue(maxFiles, maxSize);
// creo la lista dei client in attesa a una lock
waiting_t *waiting = NULL;
@ -437,6 +440,9 @@ int main(int argc, char *argv[]) {
return 0;
_cleanup:
pthread_cancel(sighandler_thread);
pthread_join(sighandler_thread, NULL);
_cleanup_beforesigthread:
if(queue) {
destroyQueue(queue);
}