From 023482caa20c0d3b5ab1641f26d8384829b1e43f Mon Sep 17 00:00:00 2001
From: Tavian Barnes <tavianator@tavianator.com>
Date: Sat, 16 Dec 2023 11:25:33 -0500
Subject: main: Warn if setlocale() fails

This should help users understand why issues like #128 happen.
---
 src/main.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/main.c b/src/main.c
index a05f9c9..1ddbc54 100644
--- a/src/main.c
+++ b/src/main.c
@@ -116,15 +116,26 @@ int main(int argc, char *argv[]) {
 	}
 
 	// Use the system locale instead of "C"
-	setlocale(LC_ALL, "");
+	int locale_err = 0;
+	if (!setlocale(LC_ALL, "")) {
+		locale_err = errno;
+	}
 
+	// Parse the command line
 	struct bfs_ctx *ctx = bfs_parse_cmdline(argc, argv);
 	if (!ctx) {
 		return EXIT_FAILURE;
 	}
 
+	// Warn if setlocale() failed, unless there's no expression to evaluate
+	if (locale_err && ctx->warn && ctx->expr) {
+		bfs_warning(ctx, "Failed to set locale: %s\n\n", xstrerror(locale_err));
+	}
+
+	// Walk the file system tree, evaluating the expression on each file
 	int ret = bfs_eval(ctx);
 
+	// Free the parsed command line, and detect any last-minute errors
 	if (bfs_ctx_free(ctx) != 0 && ret == EXIT_SUCCESS) {
 		ret = EXIT_FAILURE;
 	}
-- 
cgit v1.2.3