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