universe@3: #include "cline.h" universe@3: #include "functions.h" universe@1: universe@1: int checkArgument(const char* arg, const char* expected) { universe@1: int len = strlen(expected); universe@1: int ret = 0; universe@1: universe@1: if (arg[0] == '-') { universe@1: if (arg[1] != '-') { universe@1: for (int t = 0 ; t < len ; t++) { universe@1: ret |= (strchr(arg, expected[t]) > 0) << t; universe@1: } universe@1: } universe@1: } universe@1: universe@1: return ret; universe@1: } universe@3: universe@3: bool testSuffix(char* filename, settings_t* settings) { universe@3: bool ret = false; universe@3: int tokenlen, fnamelen = strlen(filename); universe@6: for (int t = 0 ; t < settings->suffixList->count ; t++) { universe@6: tokenlen = strlen(settings->suffixList->items[t]); universe@3: if (fnamelen >= tokenlen && tokenlen > 0) { universe@3: if (strncmp(filename+fnamelen-tokenlen, universe@6: settings->suffixList->items[t], tokenlen) == 0) { universe@3: ret = true; universe@3: break; universe@3: } universe@3: } universe@3: } universe@3: return ret ^ !settings->includeSuffixes; universe@3: } universe@3: universe@3: int scanDirectory(DIR *dir, const int spaces, universe@3: char* currdir, settings_t* settings) { universe@3: DIR *subdir; universe@3: char* subdirname; universe@3: struct dirent *entry; universe@3: int lines, digits, a; universe@3: int lineSum = 0; universe@3: universe@3: while ((entry = readdir(dir)) != NULL) { universe@3: if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { universe@3: // Print occurence universe@3: char entryname[strlen(entry->d_name)+spaces]; universe@3: for (int t = 0 ; t < spaces ; t++) { universe@3: entryname[t]=' '; universe@3: } universe@3: entryname[spaces] = 0; universe@3: strcat(entryname, entry->d_name); universe@3: universe@3: // Check for subdirectory universe@3: char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; universe@3: strcpy(subdirname, currdir); universe@3: strncat(subdirname, &settings->fileSeparator, 1); universe@3: strcat(subdirname, entry->d_name); universe@3: if ((subdir = opendir(subdirname)) != NULL) { universe@3: printf("%-60s\n", entryname); universe@3: if (settings->recursive) { universe@3: lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); universe@3: } universe@3: closedir(subdir); universe@3: continue; universe@3: } universe@3: universe@3: // Count lines universe@3: lines = 0; universe@3: char filename[(1+strlen(currdir)+strlen(entry->d_name))]; universe@3: strcpy(filename, currdir); universe@3: strncat(filename, &settings->fileSeparator, 1); universe@3: strcat(filename, entry->d_name); universe@3: if (testSuffix(filename, settings)) { universe@3: FILE *file = fopen(filename, "r"); universe@3: if (file == NULL) { universe@3: perror(" File acces failed"); universe@3: continue; universe@3: } universe@3: universe@3: do { universe@3: a = fgetc(file); universe@3: universe@3: if (a == 10) { universe@3: lines++; universe@3: } universe@3: } while (a != EOF); universe@3: fclose(file); universe@3: universe@3: // Print line count universe@3: #ifdef _WIN32 universe@3: printf("%-60s%13d lines\n", entryname, lines); universe@3: #else universe@3: printf("%-60s%14d lines\n", entryname, lines); universe@3: #endif /* _WIN32 */ universe@3: universe@3: lineSum += lines; universe@3: } universe@3: else { universe@3: if (!settings->matchesOnly) { universe@3: // Print hint universe@3: #ifdef _WIN32 universe@3: printf("%-60s%19s\n", entryname, "no match"); universe@3: #else universe@3: printf("%-60s%20s\n", entryname, "no match"); universe@3: #endif /* _WIN32 */ universe@3: } universe@3: } universe@3: } universe@3: } universe@3: return lineSum; universe@3: }