Added xstrndup (similar to strndup)
[yaz-moved-to-github.git] / src / xmalloc.c
index 61d2d4f..4aa922c 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
- * All rights reserved.
- *
- * $Id: xmalloc.c,v 1.8 2006-12-13 15:28:28 adam Exp $
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
+ * See the file LICENSE for details.
  */
 /**
  * \file xmalloc.c
@@ -343,9 +341,24 @@ void xfree_f(void *p, const char *file, int line)
         yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
     xfree_d(p, file, line);
 }
+
+char *xstrndup_f(const char *s, size_t n, const char *file, int line)
+{
+    size_t l = strlen(s);
+    if (l < n)
+        return xstrdup_f(s, file, line);
+    {
+        char *a = xmalloc_f(n+1, file, line);
+        memcpy(a, s, n);
+        a[n] = '\0';
+        return a;
+    } 
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab