From 7462265f08c068c2a1020b52b29b5b3603c45d9a Mon Sep 17 00:00:00 2001 From: s449273 Date: Fri, 3 Jan 2020 10:24:51 +0100 Subject: [PATCH] GET_ALL_FILES update --- server/serverv2.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/server/serverv2.c b/server/serverv2.c index 4341153..fb0611c 100644 --- a/server/serverv2.c +++ b/server/serverv2.c @@ -7,6 +7,7 @@ #include #include #include +#include #include int main(int argc, char **argv) @@ -48,7 +49,7 @@ int main(int argc, char **argv) &addrlen)) >= 0) { start: - printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr)); + printf("CONNECT FROM : %s\n", inet_ntoa(endpoint.sin_addr)); bzero(buff, 512); recv(sdconnection, buff, 512, 0); @@ -60,25 +61,30 @@ int main(int argc, char **argv) bzero(names_files, 512); bzero(name_file, 512); - n = scandir(".", &namelist, NULL, alphasort); - if (n == -1) + DIR *d = opendir("."); + struct dirent *enterDirectory; + + if (d == NULL) { - perror("scandir"); - exit(EXIT_FAILURE); + printf("OPEN ERROR\n"); + return EXIT_FAILURE; } - while (n--) + while ((enterDirectory = readdir(d)) != NULL) { - sprintf(name_file, "%s\n", namelist[n]->d_name); - strcat(names_files, name_file); + if (enterDirectory->d_type == DT_REG) + { - free(namelist[n]); + sprintf(name_file, "%s\n", enterDirectory->d_name); + strcat(names_files, name_file); + } } - free(namelist); + + closedir(d); if (s = send(sdconnection, names_files, 512, 0) == 512) { - printf("SEND SUCCESS"); + printf("SEND SUCCESS\n"); goto start; } } @@ -186,4 +192,4 @@ int main(int argc, char **argv) } close(sdsocket); return 0; -} \ No newline at end of file +}