Use a consistent style for including <config.h>.
[gnulib.git] / lib / strndup.c
1 /* Copyright (C) 1996, 1997, 1998, 2000, 2003, 2005 Free Software Foundation, Inc.
2
3    NOTE: The canonical source of this file is maintained with the GNU C Library.
4    Bugs can be reported to bug-glibc@prep.ai.mit.edu.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* Get strnlen. */
28 #include "strnlen.h"
29
30 #undef __strndup
31 #undef strndup
32
33 #ifndef weak_alias
34 # define __strndup strndup
35 #endif
36
37 char *
38 __strndup (const char *s, size_t n)
39 {
40   size_t len = strnlen (s, n);
41   char *new = malloc (len + 1);
42
43   if (new == NULL)
44     return NULL;
45
46   new[len] = '\0';
47   return memcpy (new, s, len);
48 }
49 #ifdef weak_alias
50 weak_alias (__strndup, strndup)
51 #endif