bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] xalloc: add xstrndup


From: Simon Josefsson
Subject: [Bug-gnulib] xalloc: add xstrndup
Date: Fri, 15 Aug 2003 01:21:26 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

I find xalloc useful, but missed a xstrndup.

I did not include MODULES.html.sh documentation since xmalloc,
xstrdup, etc was not mentioned.  If this is not right, I could send
another patch.

Thanks.

ChangeLog:

2003-08-15  Simon Josefsson  <address@hidden>

        * modules/xalloc: Add xstrndup.c.

m4/ChangeLog:

2003-08-15  Simon Josefsson  <address@hidden>

        * xalloc.m4: Comment fix.

lib/ChangeLog:

2003-08-15  Simon Josefsson  <address@hidden>

        * xstrndup.c: New file.

Index: m4/xalloc.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/xalloc.m4,v
retrieving revision 1.1
diff -u -p -r1.1 xalloc.m4
--- m4/xalloc.m4        31 Dec 2002 13:42:07 -0000      1.1
+++ m4/xalloc.m4        14 Aug 2003 23:17:00 -0000
@@ -1,5 +1,5 @@
 # xalloc.m4 serial 1
-dnl Copyright (C) 2002 Free Software Foundation, Inc.
+dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
 dnl Public License, this file may be distributed as part of a program
@@ -19,7 +19,7 @@ AC_DEFUN([gl_PREREQ_XMALLOC], [
   AC_REQUIRE([jm_FUNC_REALLOC])
 ])
 
-# Prerequisites of lib/xstrdup.c.
+# Prerequisites of lib/xstrdup.c, lib/xstrndup.c.
 AC_DEFUN([gl_PREREQ_XSTRDUP], [
   AC_REQUIRE([AC_HEADER_STDC])
   AC_CHECK_HEADERS_ONCE(string.h)
Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.4
diff -u -p -r1.4 xalloc
--- modules/xalloc      22 Jul 2003 22:10:56 -0000      1.4
+++ modules/xalloc      14 Aug 2003 23:17:00 -0000
@@ -5,6 +5,7 @@ Files:
 lib/xalloc.h
 lib/xmalloc.c
 lib/xstrdup.c
+lib/xstrndup.c
 m4/xalloc.m4
 
 Depends-on:
@@ -18,7 +19,7 @@ configure.ac:
 gl_XALLOC
 
 Makefile.am:
-lib_SOURCES += xalloc.h xmalloc.c xstrdup.c
+lib_SOURCES += xalloc.h xmalloc.c xstrdup.c xstrndup.c
 
 Include:
 "xalloc.h"
Index: lib/xstrndup.c
===================================================================
RCS file: lib/xstrndup.c
diff -N lib/xstrndup.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/xstrndup.c      14 Aug 2003 23:17:00 -0000
@@ -0,0 +1,42 @@
+/* xstrndup.c -- copy at most n bytes of a string with out of memory checking
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if STDC_HEADERS || HAVE_STRING_H
+# include <string.h>
+#else
+# include <strings.h>
+#endif
+
+#include <sys/types.h>
+
+#include "xalloc.h"
+
+/* Return a newly allocated copy of at most n bytes of STRING.  */
+
+char *
+xstrndup (const char *string, size_t n)
+{
+  /* FIXME we may allocate more than needed amount. however strlen()
+     may read out of bounds in case string is not zero terminated.
+     looping through string (limited by n) waste cpu, this waste
+     memory. */
+  return strncpy (xmalloc (n), string, n);
+}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]