copied from id-utils-3.0k
[gnulib.git] / lib / strndup.c
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
7
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB.  If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA.  */
17
18 #include <config.h>
19 #include <ansidecl.h>
20 #include "xstring.h"
21 #include "xmalloc.h"
22
23
24 /* Duplicate S, returning an identical malloc'd string.  */
25 char *
26 DEFUN(strndup, (s, n), const char *s AND size_t n)
27 {
28   char *new = malloc(n + 1);
29
30   if (new == NULL)
31     return NULL;
32
33   memcpy (new, s, n);
34   new[n] = '\0';
35
36   return new;
37 }