|
@@ -0,0 +1,100 @@
|
|
|
+#include <dirent.h>
|
|
|
+#include <stdio.h>
|
|
|
+#include <string.h>
|
|
|
+
|
|
|
+const char** filters = NULL;
|
|
|
+int filterAmount = 0;
|
|
|
+char path[PATH_MAX] = {'\0'};
|
|
|
+int pathLength = 0;
|
|
|
+int allLines = 0;
|
|
|
+
|
|
|
+void appendToPath(const char* s) {
|
|
|
+ for(int i = 0; pathLength < (PATH_MAX - 1) && s[i] != '\0'; i++) {
|
|
|
+ path[pathLength++] = s[i];
|
|
|
+ }
|
|
|
+ path[pathLength] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+int enterPath(const char* name) {
|
|
|
+ int length = pathLength;
|
|
|
+ appendToPath("/");
|
|
|
+ appendToPath(name);
|
|
|
+ return length;
|
|
|
+}
|
|
|
+
|
|
|
+void resetPath(int marker) {
|
|
|
+ path[marker] = '\0';
|
|
|
+ pathLength = marker;
|
|
|
+}
|
|
|
+
|
|
|
+void countLines() {
|
|
|
+ FILE* file = fopen(path, "r");
|
|
|
+ if(file == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int lines = 0;
|
|
|
+ while(1) {
|
|
|
+ int c = fgetc(file);
|
|
|
+ if(c == EOF) {
|
|
|
+ lines++;
|
|
|
+ break;
|
|
|
+ } else if(c == '\n') {
|
|
|
+ lines++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ printf("%s = %d\n", path, lines);
|
|
|
+ allLines += lines;
|
|
|
+ fclose(file);
|
|
|
+}
|
|
|
+
|
|
|
+void handleFile(const char* name) {
|
|
|
+ int end = strlen(name);
|
|
|
+ for(int i = 0; i < filterAmount; i++) {
|
|
|
+ int l = strlen(filters[i]);
|
|
|
+ if(end >= l && !strcmp(filters[i], name + (end - l))) {
|
|
|
+ int marker = enterPath(name);
|
|
|
+ countLines();
|
|
|
+ resetPath(marker);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void scanFolder() {
|
|
|
+ DIR* dir = opendir(path);
|
|
|
+ if(dir == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ while(1) {
|
|
|
+ struct dirent* entry = readdir(dir);
|
|
|
+ if(entry == NULL) {
|
|
|
+ break;
|
|
|
+ } else if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
|
|
|
+ continue;
|
|
|
+ } else if(entry->d_type == DT_DIR) {
|
|
|
+ int marker = enterPath(entry->d_name);
|
|
|
+ scanFolder();
|
|
|
+ resetPath(marker);
|
|
|
+ } else if(entry->d_type == DT_REG) {
|
|
|
+ handleFile(entry->d_name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ closedir(dir);
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argAmount, const char** args) {
|
|
|
+ if(argAmount < 3) {
|
|
|
+ if(argAmount > 0) {
|
|
|
+ printf("%s <path> <ending_1> [ending_2] ...\n", args[0]);
|
|
|
+ } else {
|
|
|
+ puts("... <path> <ending_1> [ending_2] ...");
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ filters = args + 2;
|
|
|
+ filterAmount = argAmount - 2;
|
|
|
+ appendToPath(args[1]);
|
|
|
+ scanFolder();
|
|
|
+ printf("Lines: %d\n", allLines);
|
|
|
+ return 0;
|
|
|
+}
|