1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-06-27 04:26:41 +02:00
Files
coreutils/lib/strndup.c
T

65 lines
1.5 KiB
C
Raw Normal View History

2006-01-24 07:52:03 +00:00
/* Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006 Free
Software Foundation, Inc.
1996-06-23 17:58:15 +00:00
2000-05-04 06:35:49 +00:00
NOTE: The canonical source of this file is maintained with the GNU C Library.
Bugs can be reported to bug-glibc@prep.ai.mit.edu.
1996-06-23 17:58:15 +00:00
2000-05-04 06:35:49 +00:00
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
1996-06-25 05:06:53 +00:00
2000-05-04 06:35:49 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-06-23 17:58:15 +00:00
2000-05-04 06:35:49 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
2005-05-14 07:58:06 +00:00
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1996-06-23 17:58:15 +00:00
1996-06-25 05:06:53 +00:00
#ifdef HAVE_CONFIG_H
2005-09-22 06:05:39 +00:00
# include <config.h>
1996-06-25 05:06:53 +00:00
#endif
2006-01-24 07:52:03 +00:00
#if !_LIBC
# include "strndup.h"
#endif
1996-06-23 17:58:15 +00:00
2003-09-13 22:01:20 +00:00
#include <stdlib.h>
#include <string.h>
1996-06-23 17:58:15 +00:00
2006-01-24 07:52:03 +00:00
#if !_LIBC
# include "strnlen.h"
# ifndef __strnlen
# define __strnlen strnlen
# endif
#endif
2000-07-04 17:37:43 +00:00
2000-05-04 06:35:49 +00:00
#undef __strndup
#undef strndup
#ifndef weak_alias
# define __strndup strndup
#endif
1996-06-23 17:58:15 +00:00
char *
2006-01-24 07:52:03 +00:00
__strndup (s, n)
const char *s;
size_t n;
1996-06-23 17:58:15 +00:00
{
2006-01-24 07:52:03 +00:00
size_t len = __strnlen (s, n);
2000-05-04 06:35:49 +00:00
char *new = malloc (len + 1);
1996-06-23 17:58:15 +00:00
if (new == NULL)
return NULL;
2000-05-04 06:35:49 +00:00
new[len] = '\0';
2003-09-13 22:01:20 +00:00
return memcpy (new, s, len);
1996-06-23 17:58:15 +00:00
}
2006-01-24 07:52:03 +00:00
#ifdef libc_hidden_def
libc_hidden_def (__strndup)
#endif
2000-05-04 06:35:49 +00:00
#ifdef weak_alias
weak_alias (__strndup, strndup)
#endif