Blame gl/strndup.c

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