strndup.c revision 178825
197403Sobrien/*
297403Sobrien * Copyright (c) 1995 - 1999 Kungliga Tekniska H�gskolan
3169691Skan * (Royal Institute of Technology, Stockholm, Sweden).
4169691Skan * All rights reserved.
597403Sobrien *
697403Sobrien * Redistribution and use in source and binary forms, with or without
797403Sobrien * modification, are permitted provided that the following conditions
897403Sobrien * are met:
997403Sobrien *
1097403Sobrien * 1. Redistributions of source code must retain the above copyright
1197403Sobrien *    notice, this list of conditions and the following disclaimer.
1297403Sobrien *
1397403Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1497403Sobrien *    notice, this list of conditions and the following disclaimer in the
1597403Sobrien *    documentation and/or other materials provided with the distribution.
1697403Sobrien *
1797403Sobrien * 3. Neither the name of the Institute nor the names of its contributors
1897403Sobrien *    may be used to endorse or promote products derived from this software
1997403Sobrien *    without specific prior written permission.
20169691Skan *
2197403Sobrien * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2297403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2397403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2497403Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2597403Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2697403Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2797403Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2897403Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2997403Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3097403Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3197403Sobrien * SUCH DAMAGE.
3297403Sobrien */
3397403Sobrien
3497403Sobrien#ifdef HAVE_CONFIG_H
3597403Sobrien#include <config.h>
3697403SobrienRCSID("$Id: strndup.c 21005 2007-06-08 01:54:35Z lha $");
37169691Skan#endif
38169691Skan#include <stdlib.h>
39169691Skan#include <string.h>
40169691Skan
41132720Skan#include "roken.h"
42132720Skan
4397403Sobrien#ifndef HAVE_STRNDUP
4497403Sobrienchar * ROKEN_LIB_FUNCTION
4597403Sobrienstrndup(const char *old, size_t sz)
46169691Skan{
47132720Skan    size_t len = strnlen (old, sz);
4897403Sobrien    char *t    = malloc(len + 1);
49169691Skan
50169691Skan    if (t != NULL) {
51117397Skan	memcpy (t, old, len);
52117397Skan	t[len] = '\0';
53117397Skan    }
54117397Skan    return t;
55117397Skan}
56117397Skan#endif /* HAVE_STRNDUP */
57117397Skan