Blame gl/strndup.c

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