Back to index

glibc  2.9
strndup.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include "config.h"
00021 #endif
00022 
00023 #include <stdio.h>
00024 #include <sys/types.h>
00025 
00026 #if defined _LIBC || defined STDC_HEADERS
00027 # include <stdlib.h>
00028 # include <string.h>
00029 #else
00030 char *malloc ();
00031 #endif
00032 
00033 #undef __strndup
00034 #undef strndup
00035 
00036 #ifndef weak_alias
00037 # define __strndup strndup
00038 #endif
00039 
00040 char *
00041 __strndup (s, n)
00042      const char *s;
00043      size_t n;
00044 {
00045   size_t len = __strnlen (s, n);
00046   char *new = (char *) malloc (len + 1);
00047 
00048   if (new == NULL)
00049     return NULL;
00050 
00051   new[len] = '\0';
00052   return (char *) memcpy (new, s, len);
00053 }
00054 #ifdef libc_hidden_def
00055 libc_hidden_def (__strndup)
00056 #endif
00057 #ifdef weak_alias
00058 weak_alias (__strndup, strndup)
00059 #endif