Use 'goto fail' to remove code duplication

This commit is contained in:
Rene Kita
2022-12-25 15:32:49 +01:00
parent 7c233b6590
commit de813c322c

View File

@@ -65,26 +65,24 @@ saveHistory(Hist *hist, size_t size)
if (hist == NULL || hist->list == NULL) if (hist == NULL || hist->list == NULL)
return; return;
tmpf = tmpfname(TMPF_HIST, NULL)->ptr; tmpf = tmpfname(TMPF_HIST, NULL)->ptr;
if ((f = fopen(tmpf, "w")) == NULL) { if ((f = fopen(tmpf, "w")) == NULL)
/* FIXME: gettextize? */ goto fail;
disp_err_message("Can't open history", FALSE);
return;
}
for (item = hist->list->first; item && hist->list->nitem > size; for (item = hist->list->first; item && hist->list->nitem > size;
item = item->next) item = item->next)
size++; size++;
for (; item; item = item->next) for (; item; item = item->next)
fprintf(f, "%s\n", (char *)item->ptr); fprintf(f, "%s\n", (char *)item->ptr);
if (fclose(f) == EOF) { if (fclose(f) == EOF)
/* FIXME: gettextize? */ goto fail;
disp_err_message("Can't save history", FALSE);
return;
}
rename_ret = rename(tmpf, rcFile(HISTORY_FILE)); rename_ret = rename(tmpf, rcFile(HISTORY_FILE));
if (rename_ret != 0) { if (rename_ret != 0)
disp_err_message("Can't save history", FALSE); goto fail;
return;
fail:
disp_err_message("Can't open history", FALSE);
return; return;
}
} }
#endif /* USE_HISTORY */ #endif /* USE_HISTORY */