Mercurial > hg > octave-kai > gnulib-hg
annotate 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 |
rev | line source |
---|---|
2469 | 1 /* Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc. |
627 | 2 |
2469 | 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. | |
627 | 5 |
2469 | 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. | |
630 | 10 |
2469 | 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. | |
627 | 15 |
2469 | 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
627 | 19 |
630 | 20 #ifdef HAVE_CONFIG_H |
21 # include "config.h" | |
22 #endif | |
627 | 23 |
633
7a867865a82f
(strndup): Include stdio.h and sys/types.h to
Jim Meyering <jim@meyering.net>
parents:
630
diff
changeset
|
24 #include <stdio.h> |
7a867865a82f
(strndup): Include stdio.h and sys/types.h to
Jim Meyering <jim@meyering.net>
parents:
630
diff
changeset
|
25 #include <sys/types.h> |
7a867865a82f
(strndup): Include stdio.h and sys/types.h to
Jim Meyering <jim@meyering.net>
parents:
630
diff
changeset
|
26 |
2469 | 27 #if defined _LIBC || defined STDC_HEADERS |
28 # include <stdlib.h> | |
630 | 29 # include <string.h> |
30 #else | |
31 char *malloc (); | |
32 #endif | |
627 | 33 |
2469 | 34 #undef __strndup |
35 #undef strndup | |
36 | |
37 #ifndef weak_alias | |
38 # define __strndup strndup | |
39 #endif | |
40 | |
627 | 41 char * |
2469 | 42 __strndup (const char *s, size_t n) |
627 | 43 { |
2469 | 44 size_t len = strnlen (s, n); |
45 char *new = malloc (len + 1); | |
627 | 46 |
47 if (new == NULL) | |
48 return NULL; | |
49 | |
2469 | 50 new[len] = '\0'; |
51 return (char *) memcpy (new, s, len); | |
627 | 52 } |
2469 | 53 #ifdef weak_alias |
54 weak_alias (__strndup, strndup) | |
55 #endif |