Mon, 27 Jul 2020 17:19:56 +0200
adds option to compute individual sums
universe@10 | 1 | /* |
universe@34 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@57 | 3 | * Copyright 2018 Mike Becker. All rights reserved. |
universe@34 | 4 | * |
universe@34 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@34 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@34 | 7 | * |
universe@34 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@34 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@34 | 10 | * |
universe@34 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@34 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@34 | 13 | * documentation and/or other materials provided with the distribution. |
universe@34 | 14 | * |
universe@34 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@34 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@34 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@34 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@34 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@34 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@34 | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@34 | 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@34 | 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@57 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@10 | 25 | */ |
universe@1 | 26 | |
universe@1 | 27 | |
universe@10 | 28 | #include "scanner.h" |
universe@21 | 29 | #include "bfile_heuristics.h" |
universe@27 | 30 | #include "regex_parser.h" |
universe@23 | 31 | #include <sys/stat.h> |
universe@3 | 32 | |
universe@41 | 33 | typedef struct filelist filelist_t; |
universe@23 | 34 | |
universe@41 | 35 | struct filelist { |
universe@41 | 36 | char *displayname; |
universe@41 | 37 | int displayname_len; |
universe@41 | 38 | char *filename; |
universe@61 | 39 | char *ext; |
universe@41 | 40 | int st_mode; |
universe@41 | 41 | filelist_t *next; |
universe@41 | 42 | }; |
universe@41 | 43 | |
universe@61 | 44 | static bool testSuffix(char* filename, string_list_t* list) { |
universe@61 | 45 | bool ret = false; |
universe@61 | 46 | int tokenlen, fnamelen = strlen(filename); |
universe@61 | 47 | for (int t = 0 ; t < list->count ; t++) { |
universe@61 | 48 | tokenlen = strlen(list->items[t]); |
universe@61 | 49 | if (fnamelen >= tokenlen && tokenlen > 0) { |
universe@61 | 50 | if (strncmp(filename+fnamelen-tokenlen, |
universe@61 | 51 | list->items[t], tokenlen) == 0) { |
universe@61 | 52 | ret = true; |
universe@61 | 53 | break; |
universe@61 | 54 | } |
universe@61 | 55 | } |
universe@61 | 56 | } |
universe@61 | 57 | return ret; |
universe@61 | 58 | } |
universe@61 | 59 | |
universe@61 | 60 | static void addLinesPerExtension(scanresult_ext_t* result, |
universe@61 | 61 | char* ext, int lines) { |
universe@61 | 62 | if (!result) return; |
universe@61 | 63 | |
universe@61 | 64 | if (!ext) ext = "w/o"; |
universe@61 | 65 | |
universe@61 | 66 | for (int i = 0 ; i < result->count ; i++) { |
universe@61 | 67 | if (strcasecmp(result->extensions[i], ext) == 0) { |
universe@61 | 68 | result->lines[i] += lines; |
universe@61 | 69 | return; |
universe@61 | 70 | } |
universe@61 | 71 | } |
universe@61 | 72 | |
universe@61 | 73 | if (result->count == result->capacity) { |
universe@61 | 74 | int newcap = result->capacity+8; |
universe@61 | 75 | char** extarr = realloc(result->extensions, newcap*sizeof(char*)); |
universe@61 | 76 | int* linesarr = realloc(result->lines, newcap*sizeof(int)); |
universe@61 | 77 | if (!extarr || !linesarr) { |
universe@61 | 78 | fprintf(stderr, "Memory allocation error.\n"); |
universe@61 | 79 | abort(); |
universe@61 | 80 | } |
universe@61 | 81 | result->extensions = extarr; |
universe@61 | 82 | result->lines = linesarr; |
universe@61 | 83 | result->capacity = newcap; |
universe@61 | 84 | } |
universe@61 | 85 | |
universe@61 | 86 | result->extensions[result->count] = strdup(ext); |
universe@61 | 87 | result->lines[result->count] = lines; |
universe@61 | 88 | result->count++; |
universe@61 | 89 | } |
universe@61 | 90 | |
universe@61 | 91 | scanresult_t* new_scanresult_t(settings_t* settings) { |
universe@61 | 92 | scanresult_t* result = calloc(1, sizeof(scanresult_t)); |
universe@61 | 93 | if (settings->individual_sums) { |
universe@61 | 94 | result->ext = calloc(1, sizeof(scanresult_ext_t)); |
universe@61 | 95 | } |
universe@61 | 96 | return result; |
universe@61 | 97 | } |
universe@61 | 98 | |
universe@61 | 99 | void destroy_scanresult_t(scanresult_t* result) { |
universe@61 | 100 | if (result->ext) { |
universe@61 | 101 | if (result->ext->count > 0) { |
universe@61 | 102 | for (int i = 0 ; i < result->ext->count ; i++) { |
universe@61 | 103 | free(result->ext->extensions[i]); |
universe@61 | 104 | } |
universe@61 | 105 | free(result->ext->extensions); |
universe@61 | 106 | free(result->ext->lines); |
universe@61 | 107 | } |
universe@61 | 108 | free(result->ext); |
universe@61 | 109 | } |
universe@61 | 110 | free(result); |
universe@61 | 111 | } |
universe@61 | 112 | |
universe@61 | 113 | |
universe@61 | 114 | static filelist_t *buildFileList(scanner_t scanner, settings_t* settings, |
universe@41 | 115 | filelist_t* list) { |
universe@41 | 116 | |
universe@23 | 117 | DIR *dirf; |
universe@3 | 118 | struct dirent *entry; |
universe@23 | 119 | struct stat statbuf; |
universe@41 | 120 | |
universe@23 | 121 | if ((dirf = opendir(scanner.dir)) == NULL) { |
universe@58 | 122 | fprintf(stderr, "%s - ", scanner.dir); |
universe@58 | 123 | perror("Directory access failed"); |
universe@23 | 124 | return 0; |
universe@23 | 125 | } |
universe@23 | 126 | |
universe@23 | 127 | while ((entry = readdir(dirf)) != NULL) { |
universe@3 | 128 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@41 | 129 | |
universe@41 | 130 | /* Create new filelist entry */ |
universe@41 | 131 | filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); |
universe@42 | 132 | newentry->next = NULL; |
universe@41 | 133 | |
universe@42 | 134 | newentry->displayname_len = strlen(entry->d_name); |
universe@42 | 135 | newentry->displayname = (char*) malloc(newentry->displayname_len+1); |
universe@42 | 136 | memcpy(newentry->displayname, entry->d_name, newentry->displayname_len); |
universe@42 | 137 | newentry->displayname[newentry->displayname_len] = 0; |
universe@41 | 138 | |
universe@42 | 139 | newentry->st_mode = 0; |
universe@41 | 140 | |
universe@40 | 141 | /* Construct absolute pathname string */ |
universe@41 | 142 | size_t dirnamelen = strlen(scanner.dir); |
universe@42 | 143 | char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len); |
universe@41 | 144 | memcpy(filename, scanner.dir, dirnamelen); |
universe@41 | 145 | filename[dirnamelen] = settings->fileSeparator; |
universe@42 | 146 | memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); |
universe@42 | 147 | filename[1+dirnamelen+newentry->displayname_len] = 0; |
universe@42 | 148 | newentry->filename = filename; |
universe@61 | 149 | |
universe@61 | 150 | /* Obtain file extension */ |
universe@61 | 151 | newentry->ext = strrchr(newentry->displayname, '.'); |
universe@14 | 152 | |
universe@22 | 153 | /* Check for subdirectory */ |
universe@23 | 154 | if (stat(filename, &statbuf) == 0) { |
universe@42 | 155 | newentry->st_mode = statbuf.st_mode; |
universe@23 | 156 | } else { |
universe@23 | 157 | perror(" Error in stat call"); |
universe@3 | 158 | continue; |
universe@3 | 159 | } |
universe@42 | 160 | |
universe@42 | 161 | if (list) { |
universe@42 | 162 | // create fake root to have a pointer on the true root |
universe@42 | 163 | filelist_t root; |
universe@42 | 164 | root.next = list; |
universe@42 | 165 | filelist_t *parent = &root; |
universe@42 | 166 | while (parent->next && |
universe@42 | 167 | (strcasecmp(parent->next->displayname, newentry->displayname) < 0 || |
universe@42 | 168 | (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode)) |
universe@42 | 169 | ) && |
universe@42 | 170 | (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode)) |
universe@42 | 171 | ) { |
universe@42 | 172 | parent = parent->next; |
universe@42 | 173 | } |
universe@42 | 174 | newentry->next = parent->next; |
universe@42 | 175 | parent->next = newentry; |
universe@42 | 176 | list = root.next; |
universe@42 | 177 | } else { |
universe@42 | 178 | list = newentry; |
universe@42 | 179 | } |
universe@41 | 180 | } |
universe@41 | 181 | } |
universe@41 | 182 | |
universe@41 | 183 | closedir(dirf); |
universe@41 | 184 | |
universe@41 | 185 | return list; |
universe@41 | 186 | } |
universe@3 | 187 | |
universe@60 | 188 | void scanDirectory(scanner_t scanner, settings_t* settings, |
universe@60 | 189 | string_list_t* output, scanresult_t* result) { |
universe@41 | 190 | |
universe@61 | 191 | result->lines = 0; |
universe@60 | 192 | int a; |
universe@41 | 193 | bool bfile; |
universe@44 | 194 | char *outbuf; |
universe@41 | 195 | |
universe@41 | 196 | filelist_t *filelist = buildFileList(scanner, settings, NULL); |
universe@41 | 197 | |
universe@41 | 198 | while (filelist != NULL) { |
universe@41 | 199 | |
universe@41 | 200 | /* Scan subdirectories */ |
universe@42 | 201 | if (!S_ISREG(filelist->st_mode)) { |
universe@44 | 202 | if (settings->recursive && S_ISDIR(filelist->st_mode)) { |
universe@44 | 203 | string_list_t *recoutput = new_string_list_t(); |
universe@60 | 204 | scanresult_t recresult; |
universe@61 | 205 | recresult.ext = result->ext; |
universe@60 | 206 | scanDirectory( |
universe@44 | 207 | (scanner_t) {filelist->filename, scanner.spaces+1}, |
universe@60 | 208 | settings, recoutput, &recresult); |
universe@61 | 209 | result->lines += recresult.lines; |
universe@44 | 210 | if (!settings->matchesOnly || recoutput->count > 0) { |
universe@44 | 211 | outbuf = (char*) malloc(81); |
universe@44 | 212 | snprintf(outbuf, 81, "%*s/%*s%13d lines\n", |
universe@44 | 213 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@60 | 214 | 60-filelist->displayname_len-scanner.spaces-1, "", |
universe@61 | 215 | recresult.lines); |
universe@44 | 216 | add_string(output, outbuf); |
universe@44 | 217 | for (int i = 0 ; i < recoutput->count ; i++) { |
universe@44 | 218 | add_string(output, recoutput->items[i]); |
universe@44 | 219 | } |
universe@44 | 220 | } |
universe@44 | 221 | destroy_string_list_t(recoutput); |
universe@44 | 222 | } else { |
universe@44 | 223 | outbuf = (char*) malloc(81); |
universe@44 | 224 | snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces, |
universe@41 | 225 | filelist->displayname); |
universe@44 | 226 | add_string(output, outbuf); |
universe@41 | 227 | } |
universe@41 | 228 | } else { |
universe@30 | 229 | if ((settings->includeSuffixes->count == 0 |
universe@41 | 230 | || testSuffix(filelist->displayname, settings->includeSuffixes)) |
universe@41 | 231 | && !testSuffix(filelist->displayname, settings->excludeSuffixes)) { |
universe@41 | 232 | |
universe@25 | 233 | /* Count lines */ |
universe@60 | 234 | int lines = 0; |
universe@25 | 235 | bfile = false; |
universe@25 | 236 | bfile_reset(settings->bfileHeuristics); |
universe@54 | 237 | regex_parser_reset(settings->regex); |
universe@27 | 238 | char line_buffer[REGEX_MAX_LINELENGTH]; |
universe@25 | 239 | int line_buffer_offset = 0; |
universe@25 | 240 | |
universe@41 | 241 | FILE *file = fopen(filelist->filename, "r"); |
universe@3 | 242 | if (file == NULL) { |
universe@44 | 243 | outbuf = (char*) malloc(81); |
universe@44 | 244 | snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces, |
universe@41 | 245 | filelist->displayname); |
universe@44 | 246 | add_string(output, outbuf); |
universe@3 | 247 | perror(" File acces failed"); |
universe@41 | 248 | } else { |
universe@41 | 249 | do { |
universe@41 | 250 | a = fgetc(file); |
universe@3 | 251 | |
universe@41 | 252 | bfile = bfile_check(settings->bfileHeuristics, a); |
universe@3 | 253 | |
universe@41 | 254 | if (a == 10 || a == EOF) { |
universe@41 | 255 | line_buffer[line_buffer_offset] = 0; |
universe@41 | 256 | if (regex_parser_do(settings->regex, line_buffer) == 0) { |
universe@41 | 257 | /* Only subtract lines when matching has finished */ |
universe@41 | 258 | if (!regex_parser_matching(settings->regex)) { |
universe@41 | 259 | lines -= settings->regex->matched_lines; |
universe@41 | 260 | } |
universe@41 | 261 | } |
universe@21 | 262 | |
universe@41 | 263 | line_buffer_offset = 0; |
universe@41 | 264 | lines++; |
universe@41 | 265 | } else { |
universe@41 | 266 | if (line_buffer_offset < REGEX_MAX_LINELENGTH) { |
universe@41 | 267 | line_buffer[line_buffer_offset] = a; |
universe@41 | 268 | line_buffer_offset++; |
universe@41 | 269 | } else { |
universe@41 | 270 | line_buffer[line_buffer_offset-1] = 0; |
universe@41 | 271 | settings->confusing_lnlen = true; |
universe@28 | 272 | } |
universe@28 | 273 | } |
universe@41 | 274 | } while (!bfile && a != EOF); |
universe@41 | 275 | fclose(file); |
universe@25 | 276 | |
universe@41 | 277 | /* Print and sum line count */ |
universe@41 | 278 | if (bfile) { |
universe@41 | 279 | if (!settings->matchesOnly) { |
universe@44 | 280 | outbuf = (char*) malloc(81); |
universe@44 | 281 | snprintf(outbuf, 81, |
universe@44 | 282 | "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, |
universe@41 | 283 | filelist->displayname, |
universe@41 | 284 | 60-filelist->displayname_len-scanner.spaces, "", "binary"); |
universe@44 | 285 | add_string(output, outbuf); |
universe@41 | 286 | } |
universe@25 | 287 | } else { |
universe@61 | 288 | addLinesPerExtension(result->ext, filelist->ext, lines); |
universe@61 | 289 | result->lines += lines; |
universe@44 | 290 | outbuf = (char*) malloc(81); |
universe@44 | 291 | snprintf(outbuf, 81, "%*s%*s%13d lines\n", |
universe@41 | 292 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@41 | 293 | 60-filelist->displayname_len-scanner.spaces, "", lines); |
universe@44 | 294 | add_string(output, outbuf); |
universe@3 | 295 | } |
universe@21 | 296 | } |
universe@16 | 297 | } else { |
universe@3 | 298 | if (!settings->matchesOnly) { |
universe@22 | 299 | /* Print hint */ |
universe@44 | 300 | outbuf = (char*) malloc(81); |
universe@44 | 301 | snprintf(outbuf, 81, "%*s%*s%19s\n", |
universe@41 | 302 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
universe@41 | 303 | 60-filelist->displayname_len-scanner.spaces, "", "no match"); |
universe@44 | 304 | add_string(output, outbuf); |
universe@3 | 305 | } |
universe@3 | 306 | } |
universe@3 | 307 | } |
universe@41 | 308 | |
universe@41 | 309 | free(filelist->filename); |
universe@41 | 310 | free(filelist->displayname); |
universe@41 | 311 | filelist_t *freethis = filelist; |
universe@41 | 312 | filelist = filelist->next; |
universe@41 | 313 | free(freethis); |
universe@3 | 314 | } |
universe@3 | 315 | } |