strndup.c revision 178825
1215236Sdelphij/*
291913Sgreen * Copyright (c) 1995 - 1999 Kungliga Tekniska H�gskolan
386669Sgreen * (Royal Institute of Technology, Stockholm, Sweden).
4215236Sdelphij * All rights reserved.
5215236Sdelphij *
686669Sgreen * Redistribution and use in source and binary forms, with or without
7215236Sdelphij * modification, are permitted provided that the following conditions
8215236Sdelphij * are met:
9215236Sdelphij *
1086669Sgreen * 1. Redistributions of source code must retain the above copyright
11215236Sdelphij *    notice, this list of conditions and the following disclaimer.
12215236Sdelphij *
13215236Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
14215236Sdelphij *    notice, this list of conditions and the following disclaimer in the
15215236Sdelphij *    documentation and/or other materials provided with the distribution.
16215236Sdelphij *
17215236Sdelphij * 3. Neither the name of the Institute nor the names of its contributors
18215236Sdelphij *    may be used to endorse or promote products derived from this software
19215236Sdelphij *    without specific prior written permission.
20215236Sdelphij *
21215236Sdelphij * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2286669Sgreen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2386669Sgreen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2486750Sfjoe * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2586669Sgreen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2686669Sgreen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2786733Sgreen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2886669Sgreen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2986669Sgreen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3086669Sgreen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3186669Sgreen * SUCH DAMAGE.
3286669Sgreen */
3386669Sgreen
3486669Sgreen#ifdef HAVE_CONFIG_H
3586669Sgreen#include <config.h>
3686669SgreenRCSID("$Id: strndup.c 21005 2007-06-08 01:54:35Z lha $");
3786669Sgreen#endif
3886733Sgreen#include <stdlib.h>
3986669Sgreen#include <string.h>
40215236Sdelphij
4191913Sgreen#include "roken.h"
4291913Sgreen
4391913Sgreen#ifndef HAVE_STRNDUP
4486669Sgreenchar * ROKEN_LIB_FUNCTION
4591913Sgreenstrndup(const char *old, size_t sz)
4686669Sgreen{
4791913Sgreen    size_t len = strnlen (old, sz);
48215236Sdelphij    char *t    = malloc(len + 1);
4991913Sgreen
5086669Sgreen    if (t != NULL) {
51215236Sdelphij	memcpy (t, old, len);
52215236Sdelphij	t[len] = '\0';
5386669Sgreen    }
5486669Sgreen    return t;
5586669Sgreen}
5686669Sgreen#endif /* HAVE_STRNDUP */
5786669Sgreen