Blame lib/strndup.c

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