1*9663SMark.Logan@Sun.COM /* Duplicate a bounded initial segment of a string, with out-of-memory
2*9663SMark.Logan@Sun.COM checking.
3*9663SMark.Logan@Sun.COM Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
4*9663SMark.Logan@Sun.COM
5*9663SMark.Logan@Sun.COM This program is free software; you can redistribute it and/or modify
6*9663SMark.Logan@Sun.COM it under the terms of the GNU General Public License as published by
7*9663SMark.Logan@Sun.COM the Free Software Foundation; either version 2, or (at your option)
8*9663SMark.Logan@Sun.COM any later version.
9*9663SMark.Logan@Sun.COM
10*9663SMark.Logan@Sun.COM This program is distributed in the hope that it will be useful,
11*9663SMark.Logan@Sun.COM but WITHOUT ANY WARRANTY; without even the implied warranty of
12*9663SMark.Logan@Sun.COM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*9663SMark.Logan@Sun.COM GNU General Public License for more details.
14*9663SMark.Logan@Sun.COM
15*9663SMark.Logan@Sun.COM You should have received a copy of the GNU General Public License
16*9663SMark.Logan@Sun.COM along with this program; if not, write to the Free Software Foundation,
17*9663SMark.Logan@Sun.COM Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*9663SMark.Logan@Sun.COM
19*9663SMark.Logan@Sun.COM #include <config.h>
20*9663SMark.Logan@Sun.COM
21*9663SMark.Logan@Sun.COM /* Specification. */
22*9663SMark.Logan@Sun.COM #include "xstrndup.h"
23*9663SMark.Logan@Sun.COM
24*9663SMark.Logan@Sun.COM #include <string.h>
25*9663SMark.Logan@Sun.COM #include "xalloc.h"
26*9663SMark.Logan@Sun.COM
27*9663SMark.Logan@Sun.COM /* Return a newly allocated copy of at most N bytes of STRING.
28*9663SMark.Logan@Sun.COM In other words, return a copy of the initial segment of length N of
29*9663SMark.Logan@Sun.COM STRING. */
30*9663SMark.Logan@Sun.COM char *
xstrndup(const char * string,size_t n)31*9663SMark.Logan@Sun.COM xstrndup (const char *string, size_t n)
32*9663SMark.Logan@Sun.COM {
33*9663SMark.Logan@Sun.COM char *s = strndup (string, n);
34*9663SMark.Logan@Sun.COM if (! s)
35*9663SMark.Logan@Sun.COM xalloc_die ();
36*9663SMark.Logan@Sun.COM return s;
37*9663SMark.Logan@Sun.COM }
38