comparison lib/strndup.c @ 2469:3e7cc1ecce32

update from glibc
author Jim Meyering <jim@meyering.net>
date Thu, 04 May 2000 06:35:49 +0000
parents 38fd8f5d359d
children e17209e9562d
comparison
equal deleted inserted replaced
2468:e64b459023ba 2469:3e7cc1ecce32
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc. 1 /* Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
2 2
3 NOTE: The canonical source of this file is maintained with the GNU C Library. 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. 4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
5 5
6 This program is free software; you can redistribute it and/or modify it 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 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 8 Free Software Foundation; either version 2, or (at your option) any
9 later version. 9 later version.
10 10
11 This program is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU General Public License 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, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include "config.h" 21 # include "config.h"
22 #endif 22 #endif
23 23
24 #include <stdio.h> 24 #include <stdio.h>
25 #include <sys/types.h> 25 #include <sys/types.h>
26 26
27 #ifdef STDC_HEADERS 27 #if defined _LIBC || defined STDC_HEADERS
28 # include <stdlib.h>
28 # include <string.h> 29 # include <string.h>
29 # include <stdlib.h>
30 #else 30 #else
31 char *malloc (); 31 char *malloc ();
32 #endif 32 #endif
33 33
34 /* Duplicate S, returning an identical malloc'd string. */ 34 #undef __strndup
35 #undef strndup
36
37 #ifndef weak_alias
38 # define __strndup strndup
39 #endif
40
35 char * 41 char *
36 strndup (const char *s, size_t n) 42 __strndup (const char *s, size_t n)
37 { 43 {
38 char *new = malloc (n + 1); 44 size_t len = strnlen (s, n);
45 char *new = malloc (len + 1);
39 46
40 if (new == NULL) 47 if (new == NULL)
41 return NULL; 48 return NULL;
42 49
43 new[n] = '\0'; 50 new[len] = '\0';
44 return (char *) memcpy (new, s, n); 51 return (char *) memcpy (new, s, len);
45 } 52 }
53 #ifdef weak_alias
54 weak_alias (__strndup, strndup)
55 #endif