]> git.saurik.com Git - bison.git/blob - lib/xstrndup.c
Sync with latest FSF version.
[bison.git] / lib / xstrndup.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program 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
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17 #if HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #if STDC_HEADERS || HAVE_STRING_H
22 # include <string.h>
23 #else
24 # include <strings.h>
25 #endif
26
27 #include <sys/types.h>
28
29 #include "xalloc.h"
30
31 #ifndef HAVE_DECL_STRNLEN
32 "this configure-time declaration test was not run"
33 #endif
34 #if !HAVE_DECL_STRNLEN
35 size_t strnlen ();
36 #endif
37
38 char *xstrndup (const char *s, size_t n);
39
40 char *
41 xstrndup (const char *s, size_t n)
42 {
43 size_t len = strnlen (s, n);
44 char *new = xmalloc (len + 1);
45
46 if (new == NULL)
47 return NULL;
48
49 new[len] = '\0';
50 return (char *) memcpy (new, s, len);
51 }