coreutils/lib/strndup.c

52 lines
1.4 KiB
C
Raw Normal View History

2005-09-22 14:05:39 +08:00
/* Copyright (C) 1996, 1997, 1998, 2000, 2003, 2005 Free Software Foundation, Inc.
1996-06-24 01:58:15 +08:00
2000-05-04 14:35:49 +08: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-24 01:58:15 +08:00
2000-05-04 14:35:49 +08: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.
2000-05-04 14:35:49 +08: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-24 01:58:15 +08:00
2000-05-04 14:35:49 +08: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 15:58:06 +08:00
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1996-06-24 01:58:15 +08:00
#ifdef HAVE_CONFIG_H
2005-09-22 14:05:39 +08:00
# include <config.h>
#endif
1996-06-24 01:58:15 +08:00
2003-09-14 06:01:20 +08:00
#include <stdlib.h>
#include <string.h>
1996-06-24 01:58:15 +08:00
2005-09-22 14:05:39 +08:00
/* Get strnlen. */
#include "strnlen.h"
2000-07-05 01:37:43 +08:00
2000-05-04 14:35:49 +08:00
#undef __strndup
#undef strndup
#ifndef weak_alias
# define __strndup strndup
#endif
1996-06-24 01:58:15 +08:00
char *
2000-05-04 14:35:49 +08:00
__strndup (const char *s, size_t n)
1996-06-24 01:58:15 +08:00
{
2000-05-04 14:35:49 +08:00
size_t len = strnlen (s, n);
char *new = malloc (len + 1);
1996-06-24 01:58:15 +08:00
if (new == NULL)
return NULL;
2000-05-04 14:35:49 +08:00
new[len] = '\0';
2003-09-14 06:01:20 +08:00
return memcpy (new, s, len);
1996-06-24 01:58:15 +08:00
}
2000-05-04 14:35:49 +08:00
#ifdef weak_alias
weak_alias (__strndup, strndup)
#endif