Blame gl/strndup.c

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