coreutils/lib/strndup.c

63 lines
1.6 KiB
C
Raw Normal View History

2000-05-04 14:35:49 +08:00
/* Copyright (C) 1996, 1997, 1998, 2000 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,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1996-06-24 01:58:15 +08:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
1996-06-24 01:58:15 +08:00
#include <stdio.h>
#include <sys/types.h>
2000-05-04 14:35:49 +08:00
#if defined _LIBC || defined STDC_HEADERS
# include <stdlib.h>
2000-05-04 14:35:49 +08:00
# include <string.h>
#else
char *malloc ();
#endif
1996-06-24 01:58:15 +08:00
2000-07-05 01:37:43 +08:00
#ifndef HAVE_DECL_STRNLEN
"this configure-time declaration test was not run"
#endif
#if !HAVE_DECL_STRNLEN
size_t strnlen ();
#endif
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';
return (char *) 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