X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=client%2Ffhistory.c;h=3b286eac389e1e3073380a717f6fcdc135b18375;hb=8526300b58a12ecbb6e48b320839e28e4460a925;hp=21f9b6f98b9be8db3bce7e277695e2fd763c7375;hpb=ee6ab2ee3a9ee1a8c65d7272ec7fba1d886f5af0;p=yaz-moved-to-github.git diff --git a/client/fhistory.c b/client/fhistory.c index 21f9b6f..3b286ea 100644 --- a/client/fhistory.c +++ b/client/fhistory.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -67,13 +68,15 @@ int file_history_load(file_history_t fh) return ret; } +#define FILE_SAVE_HISTORY_MAX 16384 + int file_history_save(file_history_t fh) { FILE *f; char* homedir = getenv("HOME"); char fname[1024]; int ret = 0; - int sz = wrbuf_len(fh->wr); + size_t sz = wrbuf_len(fh->wr); if (!sz) return 0; @@ -87,7 +90,21 @@ int file_history_save(file_history_t fh) } else { - size_t w = fwrite(wrbuf_buf(fh->wr), 1, sz, f); + size_t w; + const char *start = wrbuf_buf(fh->wr); + if (sz > FILE_SAVE_HISTORY_MAX) + { + const char *nl = strchr( + wrbuf_buf(fh->wr) + sz - FILE_SAVE_HISTORY_MAX, + '\n'); + if (nl) + { + nl++; + sz = sz - (nl - start); + start = nl; + } + } + w = fwrite(start, 1, sz, f); if (w != sz) ret = -1; if (fclose(f)) @@ -99,11 +116,11 @@ int file_history_save(file_history_t fh) int file_history_trav(file_history_t fh, void *client_data, void (*callback)(void *client_data, const char *line)) { - int off = 0; + size_t off = 0; while (off < wrbuf_len(fh->wr)) { - int i; + size_t i; for (i = off; i < wrbuf_len(fh->wr); i++) { if (wrbuf_buf(fh->wr)[i] == '\n')