Blame lib/strndup.c

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