working -w

This commit is contained in:
elvis
2022-04-29 22:31:08 +02:00
parent 59ac9663c9
commit 6bf73e8d18
3 changed files with 31 additions and 24 deletions

View File

@ -533,6 +533,9 @@ int cmd_w(char *dirname, char *Dir, int print) {
fhandle = fts_open(&firstArg, FTS_COMFOLLOW, &compare);
char **listFiles = calloc(0, sizeof(char *));
int numFiles = 0;
if(fhandle != NULL) {
// we check for num == 0 so that -1 yields all files in folder
while (num!=0 && (parent = fts_read(fhandle)) != NULL) {
@ -540,28 +543,10 @@ int cmd_w(char *dirname, char *Dir, int print) {
while (num!=0 && (child != NULL)) { // for all children in folder
if(child->fts_info == FTS_F) { // if child is a file
char *tmp = malloc(child->fts_namelen + child->fts_pathlen + 2);
snprintf(tmp, child->fts_namelen + child->fts_pathlen + 2, "%s/%s", child->fts_path, child->fts_name);
if(print) {
printf("%s [", tmp);
}
// we send the file with the other function but set print to
// 0 since we do the printing before
int r = cmd_W(tmp, Dir, 0);
if(print && !r) {
printf("Esito: ok");
} else if (print && r) {
printf("Esito: errore");
}
if(print) {
printf("]\n");
}
free(tmp);
++numFiles;
listFiles = realloc(listFiles, numFiles * sizeof(char *));
listFiles[numFiles-1] = calloc(child->fts_namelen + child->fts_pathlen + 2, sizeof(char));
snprintf(listFiles[numFiles-1], child->fts_namelen + child->fts_pathlen + 2, "%s/%s", child->fts_path, child->fts_name);
--num;
}
child = child->fts_link;
@ -569,6 +554,27 @@ int cmd_w(char *dirname, char *Dir, int print) {
}
fts_close(fhandle);
}
for(int i=0; i<numFiles; ++i) {
if(print) {
printf("%s [", listFiles[i]);
}
// we send the file with the other function but set print to 0
int r = cmd_W(listFiles[i], Dir, 0);
if(print && !r) {
printf("Esito: ok");
} else if(print && r) {
printf("Esito: errore");
}
if(print) {
printf("]\n");
}
free(listFiles[i]);
}
free(listFiles);
free(tofree);
return 0;
}
@ -786,7 +792,7 @@ int cmd_l(char *filelist, int print) {
}
// we copy filelist because we are nice
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) {
perror("malloc");
return -1;