Shell: Don't open ~/.history for writing on startup

When we only want to read the file, we should open it for reading.
This commit is contained in:
Andreas Kling 2019-09-04 14:59:29 +02:00
parent cc98ea1956
commit ce837d157f

View file

@ -527,13 +527,13 @@ static int run_command(const String& cmd)
return return_value; return return_value;
} }
CFile get_history_file() CFile get_history_file(CIODevice::OpenMode mode)
{ {
StringBuilder sb; StringBuilder sb;
sb.append(g.home); sb.append(g.home);
sb.append("/.history"); sb.append("/.history");
CFile f(sb.to_string()); CFile f(sb.to_string());
if (!f.open(CIODevice::ReadWrite)) { if (!f.open(mode)) {
fprintf(stderr, "Error opening file '%s': '%s'\n", f.filename().characters(), f.error_string()); fprintf(stderr, "Error opening file '%s': '%s'\n", f.filename().characters(), f.error_string());
exit(1); exit(1);
} }
@ -542,7 +542,7 @@ CFile get_history_file()
void load_history() void load_history()
{ {
CFile history_file = get_history_file(); CFile history_file = get_history_file(CIODevice::ReadOnly);
while (history_file.can_read_line()) { while (history_file.can_read_line()) {
const auto&b = history_file.read_line(1024); const auto&b = history_file.read_line(1024);
// skip the newline and terminating bytes // skip the newline and terminating bytes
@ -552,7 +552,7 @@ void load_history()
void save_history() void save_history()
{ {
CFile history_file = get_history_file(); CFile history_file = get_history_file(CIODevice::WriteOnly);
for (const auto& line : editor.history()) { for (const auto& line : editor.history()) {
history_file.write(line); history_file.write(line);
history_file.write("\n"); history_file.write("\n");