[PATCH] history: check for HISTFILE env variable
Export this patch
if exists and not empty, use its as history file's path.
---
frontend/readline.c | 19 +++++++++++++++++--
main.c | 3 ++-
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/frontend/readline.c b/frontend/readline.c
index 19880fd..6cb53ec 100644
--- a/frontend/readline.c
+++ b/frontend/readline.c
@@ -38,8 +38,23 @@ static void sigint_handler(int n) {
static char *get_history_path(void) {
const char *home = getenv("HOME");
- int len = snprintf(NULL, 0, "%s/.mrsh_history", home);
- char *path = malloc(len + 1);
+ extern struct mrsh_state *state;
+ const char *histfile = mrsh_env_get(state, "HISTFILE", NULL);
+ int len;
+ char *path;
+
+ if (histfile != NULL && strcmp(histfile, "") != 0) {
+ len = strlen(histfile);
+ path = malloc(len + 1);
+ if (path == NULL) {
+ return NULL;
+ }
+ snprintf(path, len + 1, "%s", histfile);
+ return path;
+ }
+
+ len = snprintf(NULL, 0, "%s/.mrsh_history", home);
+ path = malloc(len + 1);
if (path == NULL) {
return NULL;
}
diff --git a/main.c b/main.c
index 0f433d2..25e278e 100644
--- a/main.c
+++ b/main.c
@@ -15,9 +15,10 @@
#include "frontend.h"
extern char **environ;
+struct mrsh_state *state;
int main(int argc, char *argv[]) {
- struct mrsh_state *state = mrsh_state_create();
+ state = mrsh_state_create();
struct mrsh_init_args init_args = {0};
if (mrsh_process_args(state, &init_args, argc, argv) != 0) {
--
2.30.0