Advertisement
Guest User

Untitled

a guest
Mar 31st, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <utime.h>
  12.  
  13.  
  14. typedef struct myNode {
  15.     char *name;
  16. } myNode;
  17.  
  18. typedef struct myList {
  19.     myNode *node;
  20.     struct myList *next;
  21. } myList;
  22.  
  23.  
  24. void addToList(myList* root, myNode* node) {
  25.     if (root->node == NULL) {
  26.         root->node = node;
  27.         root->next = NULL;
  28.     }
  29.     else {
  30.         while (root->next != NULL) {
  31.             root = root->next;
  32.         }
  33.         root->next = malloc(sizeof(myList));
  34.         root->next->node = node;
  35.         root->next->next = NULL;
  36.     }
  37. }
  38.  
  39. myList* createList() {
  40.     myList* list = malloc(sizeof(myList));
  41.     list->node = NULL;
  42.     list->next = NULL;
  43.     return list;
  44. }
  45.  
  46. myNode* createNode(char* name) {
  47.     myNode* node = malloc(sizeof(myNode));
  48.     node->name = name;
  49.  
  50.     return node;
  51. }
  52.  
  53.  
  54. void myPrint(myList* root) {
  55.     if (root == NULL) return;
  56.     printf("address: %p, name: %s\n", (void*) root->node, root->node->name);
  57.     myPrint(root->next);
  58. }
  59.  
  60. myList* listFilesInDirectory(char* path) {
  61.     DIR* dir = opendir(path);
  62.     myList* list = createList();
  63.     struct dirent* entry;
  64.    
  65.     while ((entry = readdir(dir)) != NULL) {
  66.         myNode* node = createNode(entry->d_name);
  67.         addToList(list, node);
  68.     }
  69.     closedir(dir);
  70.     return list;
  71. }
  72.  
  73. int main() {
  74.     myList* sourceFiles = listFilesInDirectory("source/");
  75.  
  76.     printf("SourceFiles content 1:\n");
  77.     myPrint(sourceFiles);
  78.     printf("-----------\n");
  79.  
  80.     myList* destFiles = listFilesInDirectory("dest/");
  81.  
  82.     printf("SourceFiles content 2:\n");
  83.     myPrint(sourceFiles);
  84.     printf("-----------\n");
  85.  
  86.     printf("DestFiles content:\n");
  87.     myPrint(destFiles);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement