src/scanner.c

changeset 44
9574a181ec26
parent 43
104e75d18ede
child 48
0d2c13c24fd0
equal deleted inserted replaced
43:104e75d18ede 44:9574a181ec26
115 closedir(dirf); 115 closedir(dirf);
116 116
117 return list; 117 return list;
118 } 118 }
119 119
120 int scanDirectory(scanner_t scanner, settings_t* settings) { 120 int scanDirectory(scanner_t scanner, settings_t* settings,
121 string_list_t* output) {
121 122
122 int lines, a; 123 int lines, a;
123 int lineSum = 0; 124 int lineSum = 0;
124 bool bfile; 125 bool bfile;
126 char *outbuf;
125 127
126 filelist_t *filelist = buildFileList(scanner, settings, NULL); 128 filelist_t *filelist = buildFileList(scanner, settings, NULL);
127 129
128 while (filelist != NULL) { 130 while (filelist != NULL) {
129 131
130 /* Scan subdirectories */ 132 /* Scan subdirectories */
131 if (!S_ISREG(filelist->st_mode)) { 133 if (!S_ISREG(filelist->st_mode)) {
132 printf("%*s\n", filelist->displayname_len+scanner.spaces, 134 if (settings->recursive && S_ISDIR(filelist->st_mode)) {
135 string_list_t *recoutput = new_string_list_t();
136 lines = scanDirectory(
137 (scanner_t) {filelist->filename, scanner.spaces+1},
138 settings, recoutput);
139 lineSum += lines;
140 if (!settings->matchesOnly || recoutput->count > 0) {
141 outbuf = (char*) malloc(81);
142 snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
143 filelist->displayname_len+scanner.spaces, filelist->displayname,
144 60-filelist->displayname_len-scanner.spaces-1, "", lines);
145 add_string(output, outbuf);
146 for (int i = 0 ; i < recoutput->count ; i++) {
147 add_string(output, recoutput->items[i]);
148 }
149 }
150 destroy_string_list_t(recoutput);
151 } else {
152 outbuf = (char*) malloc(81);
153 snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
133 filelist->displayname); 154 filelist->displayname);
134 if (settings->recursive && S_ISDIR(filelist->st_mode)) { 155 add_string(output, outbuf);
135 lineSum += scanDirectory(
136 (scanner_t) {filelist->filename, scanner.spaces+1}, settings);
137 } 156 }
138 } else { 157 } else {
139 if ((settings->includeSuffixes->count == 0 158 if ((settings->includeSuffixes->count == 0
140 || testSuffix(filelist->displayname, settings->includeSuffixes)) 159 || testSuffix(filelist->displayname, settings->includeSuffixes))
141 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) { 160 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
147 char line_buffer[REGEX_MAX_LINELENGTH]; 166 char line_buffer[REGEX_MAX_LINELENGTH];
148 int line_buffer_offset = 0; 167 int line_buffer_offset = 0;
149 168
150 FILE *file = fopen(filelist->filename, "r"); 169 FILE *file = fopen(filelist->filename, "r");
151 if (file == NULL) { 170 if (file == NULL) {
152 printf("%*s", filelist->displayname_len+scanner.spaces, 171 outbuf = (char*) malloc(81);
172 snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
153 filelist->displayname); 173 filelist->displayname);
174 add_string(output, outbuf);
154 perror(" File acces failed"); 175 perror(" File acces failed");
155 } else { 176 } else {
156 do { 177 do {
157 a = fgetc(file); 178 a = fgetc(file);
158 179
182 fclose(file); 203 fclose(file);
183 204
184 /* Print and sum line count */ 205 /* Print and sum line count */
185 if (bfile) { 206 if (bfile) {
186 if (!settings->matchesOnly) { 207 if (!settings->matchesOnly) {
187 printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, 208 outbuf = (char*) malloc(81);
209 snprintf(outbuf, 81,
210 "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
188 filelist->displayname, 211 filelist->displayname,
189 60-filelist->displayname_len-scanner.spaces, "", "binary"); 212 60-filelist->displayname_len-scanner.spaces, "", "binary");
213 add_string(output, outbuf);
190 } 214 }
191 } else { 215 } else {
192 lineSum += lines; 216 lineSum += lines;
193 printf("%*s%*s%13d lines\n", 217 outbuf = (char*) malloc(81);
218 snprintf(outbuf, 81, "%*s%*s%13d lines\n",
194 filelist->displayname_len+scanner.spaces, filelist->displayname, 219 filelist->displayname_len+scanner.spaces, filelist->displayname,
195 60-filelist->displayname_len-scanner.spaces, "", lines); 220 60-filelist->displayname_len-scanner.spaces, "", lines);
221 add_string(output, outbuf);
196 } 222 }
197 } 223 }
198 } else { 224 } else {
199 if (!settings->matchesOnly) { 225 if (!settings->matchesOnly) {
200 /* Print hint */ 226 /* Print hint */
201 printf("%*s%*s%19s\n", 227 outbuf = (char*) malloc(81);
228 snprintf(outbuf, 81, "%*s%*s%19s\n",
202 filelist->displayname_len+scanner.spaces, filelist->displayname, 229 filelist->displayname_len+scanner.spaces, filelist->displayname,
203 60-filelist->displayname_len-scanner.spaces, "", "no match"); 230 60-filelist->displayname_len-scanner.spaces, "", "no match");
231 add_string(output, outbuf);
204 } 232 }
205 } 233 }
206 } 234 }
207 235
208 free(filelist->filename); 236 free(filelist->filename);

mercurial