lineedit: fix atomic replace of history file; hush: fix $HISTFILE handling
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 4 Sep 2011 13:28:03 +0000 (15:28 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 4 Sep 2011 13:28:03 +0000 (15:28 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/lineedit.c
shell/ash.c
shell/hush.c

index 10265192e609208ffb842f6e0bb1be98d9ebbf53..5d139043a659dca8513933f0d524a8a0e544fb0e 100644 (file)
@@ -1425,7 +1425,7 @@ static void save_history(char *str)
 
                /* write out temp file and replace hist_file atomically */
                new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
-               fd = open(state->hist_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+               fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
                if (fd >= 0) {
                        FILE *fp;
                        int i;
@@ -1475,7 +1475,7 @@ static void remember_in_history(char *str)
        /* i <= state->max_history */
        state->cur_history = i;
        state->cnt_history = i;
-# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
+# if ENABLE_FEATURE_EDITING_SAVEHISTORY
        if ((state->flags & SAVE_HISTORY) && state->hist_file)
                save_history(str);
 # endif
index d48cd016f567d87ffa7b08a706b3986ad534f3a9..bf376bd0dcf738eae89530869632127adea07d3a 100644 (file)
@@ -13194,7 +13194,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
        }
 
        if (sflag || minusc == NULL) {
-#if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
+#if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
                if (iflag) {
                        const char *hp = lookupvar("HISTFILE");
                        if (hp)
index e4138adf79748dc2a779272425537da64e979c77..42143fd9e7dcfa93a1ba6d05c0388323d185cadb 100644 (file)
@@ -7816,23 +7816,7 @@ int hush_main(int argc, char **argv)
         */
 
 #if ENABLE_FEATURE_EDITING
-       G.line_input_state = new_line_input_t(FOR_SHELL);
-# if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_HUSH_SAVEHISTORY
-       {
-               const char *hp = get_local_var_value("HISTFILE");
-               if (!hp) {
-                       hp = get_local_var_value("HOME");
-                       if (hp) {
-                               G.line_input_state->hist_file = concat_path_file(hp, ".hush_history");
-                               //set_local_var(xasprintf("HISTFILE=%s", ...));
-                       }
-               }
-# if ENABLE_FEATURE_SH_HISTFILESIZE
-               hp = get_local_var_value("HISTFILESIZE");
-               G.line_input_state->max_history = size_from_HISTFILESIZE(hp);
-# endif
-       }
-# endif
+       G.line_input_state = new_line_input_t(FOR_SHELL & ~SAVE_HISTORY);
 #endif
 
        /* Initialize some more globals to non-zero values */
@@ -8104,6 +8088,28 @@ int hush_main(int argc, char **argv)
                /* -1 is special - makes xfuncs longjmp, not exit
                 * (we reset die_sleep = 0 whereever we [v]fork) */
                enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */
+
+# if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0
+               {
+                       const char *hp = get_local_var_value("HISTFILE");
+                       if (!hp) {
+                               hp = get_local_var_value("HOME");
+                               if (hp)
+                                       hp = concat_path_file(hp, ".hush_history");
+                       } else {
+                               hp = xstrdup(hp);
+                       }
+                       if (hp) {
+                               G.line_input_state->hist_file = hp;
+                               G.line_input_state->flags |= SAVE_HISTORY;
+                               //set_local_var(xasprintf("HISTFILE=%s", ...));
+                       }
+#  if ENABLE_FEATURE_SH_HISTFILESIZE
+                       hp = get_local_var_value("HISTFILESIZE");
+                       G.line_input_state->max_history = size_from_HISTFILESIZE(hp);
+#  endif
+               }
+# endif
        } else {
                install_special_sighandlers();
        }