Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. void getLSTF(char * path, int socket, char * finalString){
  2. DIR *dir;
  3. struct dirent *ent;
  4.  
  5. if ((dir = opendir (path)) != NULL) {
  6. /* print all the files and directories within directory */
  7. while ((ent = readdir (dir)) != NULL) {
  8. char filename[PATH_MAX];
  9. snprintf(filename, sizeof(filename), "%s/%s", path, ent->d_name);
  10. struct stat st;
  11. if(stat(filename, &st) == 0 && S_ISREG(st.st_mode))
  12. pos += sprintf(&finalString[pos], "%s\n", ent->d_name);
  13. }
  14.  
  15. closedir (dir);
  16. } else {
  17. /* could not open directory */
  18. perror (path);
  19. }
  20. }
  21.  
  22. void getLists(char * path, int socket, bool recursive){
  23. pos = 0;
  24. char finalString[PATH_MAX];
  25.  
  26. if (recursive == true) {
  27. getLSTR(path, socket, &finalString);
  28. write(socket, finalString, pos);
  29. }
  30. else {
  31. getLSTF(path, socket, &finalString);
  32. write(socket , finalString, pos);
  33. }
  34. sprintf(&finalString[pos], '\0');
  35.  
  36. }
  37.  
  38. /* stampa su stind la lista dei file regolari della cartella in input e delle sue sottocartelle */
  39.  
  40. void getLSTR(const char * name, int socket, char * finalString){
  41. DIR *dir;
  42. struct dirent *entry;
  43.  
  44. if (!(dir = opendir(name)))
  45. return;
  46.  
  47. while ((entry = readdir(dir)) != NULL) {
  48. if (entry->d_type == DT_DIR) {
  49. char path[1024];
  50. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  51. continue;
  52. snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
  53. getLSTR(path, socket, finalString);
  54. } else {
  55. //printf("pos: %d file: - %s\n", pos, entry->d_name);
  56. pos += sprintf(&finalString[pos], "- %s\n", entry->d_name);
  57. }
  58. }
  59.  
  60. closedir(dir);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement