src/scanner.c

changeset 42
0402b9b41b0a
parent 41
c2e73e175341
child 43
104e75d18ede
equal deleted inserted replaced
41:c2e73e175341 42:0402b9b41b0a
50 filelist_t* list) { 50 filelist_t* list) {
51 51
52 DIR *dirf; 52 DIR *dirf;
53 struct dirent *entry; 53 struct dirent *entry;
54 struct stat statbuf; 54 struct stat statbuf;
55 filelist_t *listentry = list;
56 55
57 if ((dirf = opendir(scanner.dir)) == NULL) { 56 if ((dirf = opendir(scanner.dir)) == NULL) {
58 printf("%s", scanner.dir); 57 printf("%s", scanner.dir);
59 perror(" Directory access failed"); 58 perror(" Directory access failed");
60 return 0; 59 return 0;
63 while ((entry = readdir(dirf)) != NULL) { 62 while ((entry = readdir(dirf)) != NULL) {
64 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 63 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
65 64
66 /* Create new filelist entry */ 65 /* Create new filelist entry */
67 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); 66 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
68 // TODO: don't just append - create a sorted list! 67 newentry->next = NULL;
69 if (listentry) { 68
70 listentry->next = newentry; 69 newentry->displayname_len = strlen(entry->d_name);
71 } 70 newentry->displayname = (char*) malloc(newentry->displayname_len+1);
72 listentry = newentry; 71 memcpy(newentry->displayname, entry->d_name, newentry->displayname_len);
73 if (!list) { 72 newentry->displayname[newentry->displayname_len] = 0;
74 list = listentry; 73
75 } 74 newentry->st_mode = 0;
76
77 listentry->next = NULL;
78
79 listentry->displayname_len = strlen(entry->d_name);
80 listentry->displayname = (char*) malloc(listentry->displayname_len+1);
81 memcpy(listentry->displayname, entry->d_name, listentry->displayname_len);
82 listentry->displayname[listentry->displayname_len] = 0;
83
84 listentry->st_mode = 0;
85 75
86 /* Construct absolute pathname string */ 76 /* Construct absolute pathname string */
87 size_t dirnamelen = strlen(scanner.dir); 77 size_t dirnamelen = strlen(scanner.dir);
88 char *filename = (char*) malloc(2+dirnamelen+listentry->displayname_len); 78 char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len);
89 memcpy(filename, scanner.dir, dirnamelen); 79 memcpy(filename, scanner.dir, dirnamelen);
90 filename[dirnamelen] = settings->fileSeparator; 80 filename[dirnamelen] = settings->fileSeparator;
91 memcpy(filename+dirnamelen+1, entry->d_name, listentry->displayname_len); 81 memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
92 filename[1+dirnamelen+listentry->displayname_len] = 0; 82 filename[1+dirnamelen+newentry->displayname_len] = 0;
93 listentry->filename = filename; 83 newentry->filename = filename;
94 84
95 /* Check for subdirectory */ 85 /* Check for subdirectory */
96 if (stat(filename, &statbuf) == 0) { 86 if (stat(filename, &statbuf) == 0) {
97 listentry->st_mode = statbuf.st_mode; 87 newentry->st_mode = statbuf.st_mode;
98 } else { 88 } else {
99 perror(" Error in stat call"); 89 perror(" Error in stat call");
100 continue; 90 continue;
101 } 91 }
92
93 if (list) {
94 // create fake root to have a pointer on the true root
95 filelist_t root;
96 root.next = list;
97 filelist_t *parent = &root;
98 while (parent->next &&
99 (strcasecmp(parent->next->displayname, newentry->displayname) < 0 ||
100 (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode))
101 ) &&
102 (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode))
103 ) {
104 parent = parent->next;
105 }
106 newentry->next = parent->next;
107 parent->next = newentry;
108 list = root.next;
109 } else {
110 list = newentry;
111 }
102 } 112 }
103 } 113 }
104 114
105 closedir(dirf); 115 closedir(dirf);
106 116
116 filelist_t *filelist = buildFileList(scanner, settings, NULL); 126 filelist_t *filelist = buildFileList(scanner, settings, NULL);
117 127
118 while (filelist != NULL) { 128 while (filelist != NULL) {
119 129
120 /* Scan subdirectories */ 130 /* Scan subdirectories */
121 if (!(filelist->st_mode & S_IFREG)) { 131 if (!S_ISREG(filelist->st_mode)) {
122 printf("%*s\n", filelist->displayname_len+scanner.spaces, 132 printf("%*s\n", filelist->displayname_len+scanner.spaces,
123 filelist->displayname); 133 filelist->displayname);
124 if (settings->recursive && (filelist->st_mode & S_IFDIR)) { 134 if (settings->recursive && S_ISDIR(filelist->st_mode)) {
125 scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1}, 135 scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1},
126 settings); 136 settings);
127 } 137 }
128 } else { 138 } else {
129 if ((settings->includeSuffixes->count == 0 139 if ((settings->includeSuffixes->count == 0

mercurial