Blame gnulib/lib/strndup.c

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