Blame gnulib/lib/xstrndup.c

Packit eba2e2
/* Duplicate a bounded initial segment of a string, with out-of-memory
Packit eba2e2
   checking.
Packit eba2e2
   Copyright (C) 2003, 2006-2007, 2009-2014 Free Software Foundation, Inc.
Packit eba2e2
Packit eba2e2
   This program is free software: you can redistribute it and/or modify
Packit eba2e2
   it under the terms of the GNU General Public License as published by
Packit eba2e2
   the Free Software Foundation; either version 3 of the License, or
Packit eba2e2
   (at your option) any later version.
Packit eba2e2
Packit eba2e2
   This program is distributed in the hope that it will be useful,
Packit eba2e2
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit eba2e2
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit eba2e2
   GNU General Public License for more details.
Packit eba2e2
Packit eba2e2
   You should have received a copy of the GNU General Public License
Packit eba2e2
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit eba2e2
Packit eba2e2
#include <config.h>
Packit eba2e2
Packit eba2e2
/* Specification.  */
Packit eba2e2
#include "xstrndup.h"
Packit eba2e2
Packit eba2e2
#include <string.h>
Packit eba2e2
#include "xalloc.h"
Packit eba2e2
Packit eba2e2
/* Return a newly allocated copy of at most N bytes of STRING.
Packit eba2e2
   In other words, return a copy of the initial segment of length N of
Packit eba2e2
   STRING.  */
Packit eba2e2
char *
Packit eba2e2
xstrndup (const char *string, size_t n)
Packit eba2e2
{
Packit eba2e2
  char *s = strndup (string, n);
Packit eba2e2
  if (! s)
Packit eba2e2
    xalloc_die ();
Packit eba2e2
  return s;
Packit eba2e2
}