Link SSL with libyaz.la and yaz-client only.
[yaz-moved-to-github.git] / client / fhistory.c
index 7fb5f3f..2025939 100644 (file)
@@ -1,14 +1,13 @@
-/* 
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
  * See the file LICENSE for details.
- *
- * $Id: fhistory.c,v 1.3 2007-03-19 22:17:41 adam Exp $
  */
 /** \file fhistory.c
  *  \brief file history implementation
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <time.h>
@@ -26,7 +25,7 @@ struct file_history {
 
 file_history_t file_history_new()
 {
-    file_history_t fh = xmalloc(sizeof(*fh));
+    file_history_t fh = (file_history_t) xmalloc(sizeof(*fh));
     fh->wr = wrbuf_alloc();
     return fh;
 }
@@ -69,6 +68,8 @@ 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;
@@ -89,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))