Blame gnulib/lib/strndup.c

Packit eba2e2
/* A replacement function, for systems that lack strndup.
Packit eba2e2
Packit eba2e2
   Copyright (C) 1996-1998, 2001-2003, 2005-2007, 2009-2014 Free Software
Packit eba2e2
   Foundation, Inc.
Packit eba2e2
Packit eba2e2
   This program is free software; you can redistribute it and/or modify it
Packit eba2e2
   under the terms of the GNU General Public License as published by the
Packit eba2e2
   Free Software Foundation; either version 3, or (at your option) any
Packit eba2e2
   later version.
Packit eba2e2
Packit eba2e2
   This program is distributed in the hope that it will be useful,
Packit eba2e2
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit eba2e2
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit eba2e2
   GNU General Public License for more details.
Packit eba2e2
Packit eba2e2
   You should have received a copy of the GNU General Public License
Packit eba2e2
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit eba2e2
Packit eba2e2
#include <config.h>
Packit eba2e2
Packit eba2e2
#include <string.h>
Packit eba2e2
Packit eba2e2
#include <stdlib.h>
Packit eba2e2
Packit eba2e2
char *
Packit eba2e2
strndup (char const *s, size_t n)
Packit eba2e2
{
Packit eba2e2
  size_t len = strnlen (s, n);
Packit eba2e2
  char *new = malloc (len + 1);
Packit eba2e2
Packit eba2e2
  if (new == NULL)
Packit eba2e2
    return NULL;
Packit eba2e2
Packit eba2e2
  new[len] = '\0';
Packit eba2e2
  return memcpy (new, s, len);
Packit eba2e2
}