1*e4b17023SJohn Marino /* Implement the xstrndup function.
2*e4b17023SJohn Marino Copyright (C) 2005 Free Software Foundation, Inc.
3*e4b17023SJohn Marino Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of the libiberty library.
6*e4b17023SJohn Marino Libiberty is free software; you can redistribute it and/or
7*e4b17023SJohn Marino modify it under the terms of the GNU Library General Public
8*e4b17023SJohn Marino License as published by the Free Software Foundation; either
9*e4b17023SJohn Marino version 2 of the License, or (at your option) any later version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino Libiberty is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*e4b17023SJohn Marino Library General Public License for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino You should have received a copy of the GNU Library General Public
17*e4b17023SJohn Marino License along with libiberty; see the file COPYING.LIB. If
18*e4b17023SJohn Marino not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*e4b17023SJohn Marino Boston, MA 02110-1301, USA. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino /*
22*e4b17023SJohn Marino
23*e4b17023SJohn Marino @deftypefn Replacement char* xstrndup (const char *@var{s}, size_t @var{n})
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino Returns a pointer to a copy of @var{s} with at most @var{n} characters
26*e4b17023SJohn Marino without fail, using @code{xmalloc} to obtain memory. The result is
27*e4b17023SJohn Marino always NUL terminated.
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino @end deftypefn
30*e4b17023SJohn Marino
31*e4b17023SJohn Marino */
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino #ifdef HAVE_CONFIG_H
34*e4b17023SJohn Marino #include "config.h"
35*e4b17023SJohn Marino #endif
36*e4b17023SJohn Marino #include <sys/types.h>
37*e4b17023SJohn Marino #ifdef HAVE_STRING_H
38*e4b17023SJohn Marino #include <string.h>
39*e4b17023SJohn Marino #else
40*e4b17023SJohn Marino # ifdef HAVE_STRINGS_H
41*e4b17023SJohn Marino # include <strings.h>
42*e4b17023SJohn Marino # endif
43*e4b17023SJohn Marino #endif
44*e4b17023SJohn Marino #include "ansidecl.h"
45*e4b17023SJohn Marino #include "libiberty.h"
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino char *
xstrndup(const char * s,size_t n)48*e4b17023SJohn Marino xstrndup (const char *s, size_t n)
49*e4b17023SJohn Marino {
50*e4b17023SJohn Marino char *result;
51*e4b17023SJohn Marino size_t len = strlen (s);
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino if (n < len)
54*e4b17023SJohn Marino len = n;
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino result = XNEWVEC (char, len + 1);
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino result[len] = '\0';
59*e4b17023SJohn Marino return (char *) memcpy (result, s, len);
60*e4b17023SJohn Marino }
61