about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ignore.c55
-rw-r--r--rf.c51
2 files changed, 67 insertions, 39 deletions
diff --git a/ignore.c b/ignore.c
index d7b6428..8abca6e 100644
--- a/ignore.c
+++ b/ignore.c
@@ -12,25 +12,42 @@
 static int total_size;
 
 struct ignores *init_ignores(char *path) {
+	int i = 0;
+	FILE *ignore = fopen(path, "r");
+
+	if (ignore == NULL) {
+		return NULL;
+	}
+
+	char *line = NULL;
+	size_t llen = 0;
 	total_size = IGNORE_SIZE;
 	struct ignores *ignores = calloc(sizeof(struct ignores), 1);
 	ignores->list = calloc(sizeof(char *), IGNORE_SIZE);
 	ignores->size = 0;
 
-	int i = 0;
-	FILE *ignore = fopen(path, "r");
-	char *line = NULL;
-	size_t llen = 0;
-	ssize_t r;
+	while (getline(&line, &llen, ignore) != -1) {
+		/** isspace doesn't necessarily hold true for `\n` for anything
+		 * other than the 'C' locale on some platforms, so we have to do
+		 * an additional check for this
+		 */
+
+		if (strlen(line) == 1 && (isspace(line[0]) || line[0] == '\n')) {
+			continue;
+		}
 
-	if (ignore != NULL) {
-		while ((r = getline(&line, &llen, ignore)) != -1) {
-			char *l = calloc(sizeof(char *), strlen(line) - 1);
+		if (strlen(line) > 0) {
+			char *l = calloc(sizeof(char), strlen(line));
+
+			if (l == NULL) {
+				fprintf(stderr, "calloc\n");
+				exit(EXIT_FAILURE);
+			}
 
 			for (size_t j = 0, k = 0; j < strlen(line); j++) {
 				char c = line[j];
 
-				if (isspace(c)) {
+				if (isspace(c) || c == '\n') {
 					break;
 				}
 
@@ -45,23 +62,25 @@ struct ignores *init_ignores(char *path) {
 
 			ignores->list[i++] = l;
 		}
-
-		free(line);
-		fclose(ignore);
 	}
 
+	free(line);
+	fclose(ignore);
+
 	ignores->size = i;
 
 	return ignores;
 }
 
 void free_ignores(struct ignores *ignores) {
-	if (ignores->size > 0) {
-		for (int i = 0; i < ignores->size; i++) {
-			free(ignores->list[i]);
+	if (ignores != NULL) {
+		if (ignores->size > 0) {
+			for (int i = 0; i < ignores->size; i++) {
+				free(ignores->list[i]);
+			}
 		}
-	}
 
-	free(ignores->list);
-	free(ignores);
+		free(ignores->list);
+		free(ignores);
+	}
 }
diff --git a/rf.c b/rf.c
index b183ecd..edaeef0 100644
--- a/rf.c
+++ b/rf.c
@@ -51,27 +51,33 @@ static int is_child(char *dirname) {
 }
 
 static int excluded(char *name) {
-	for (int i = 0; i < global_ignores->size; i++) {
-		int res = fnmatch(global_ignores->list[i], name, 0);
+	if (global_ignores != NULL) {
+		for (int i = 0; i < global_ignores->size; i++) {
+			int res = fnmatch(global_ignores->list[i], name, 0);
 
-		if (res == 0) {
-			return 1;
+			if (res == 0) {
+				return 1;
+			}
 		}
 	}
 
-	for (int i = 0; i < config_ignores->size; i++) {
-		int res = fnmatch(config_ignores->list[i], name, 0);
+	if (config_ignores != NULL) {
+		for (int i = 0; i < config_ignores->size; i++) {
+			int res = fnmatch(config_ignores->list[i], name, 0);
 
-		if (res == 0) {
-			return 1;
+			if (res == 0) {
+				return 1;
+			}
 		}
 	}
 
-	for (int i = 0; i < local_ignores->size; i++) {
-		int res = fnmatch(local_ignores->list[i], name, 0);
+	if (local_ignores != NULL) {
+		for (int i = 0; i < local_ignores->size; i++) {
+			int res = fnmatch(local_ignores->list[i], name, 0);
 
-		if (res == 0) {
-			return 1;
+			if (res == 0) {
+				return 1;
+			}
 		}
 	}
 
@@ -122,15 +128,18 @@ static int recurse_find(char **patterns, int *pattern_count, char *dirname,
 				for (; p < *pattern_count; p++) {
 					char *pattern = patterns[p];
 
-					if (switches->substring &&
-						(strstr(switches->wholename ? full_path : entry->d_name,
-							 pattern) != NULL)) {
-						matched = 1;
-					} else if (fnmatch(pattern,
-								   switches->wholename ? full_path
-													   : entry->d_name,
-								   0) == 0) {
-						matched = 1;
+					if (switches->substring) {
+						if (strstr(
+								switches->wholename ? full_path : entry->d_name,
+								pattern) != NULL) {
+							matched = 1;
+						}
+					} else {
+						if (fnmatch(pattern,
+								switches->wholename ? full_path : entry->d_name,
+								0) == 0) {
+							matched = 1;
+						}
 					}
 				}