xref: /netbsd-src/lib/libc/string/strndup.c (revision 0c74dffd74353332c85ec94bec3c3373bd849532)
1*0c74dffdSnakayama /*	$NetBSD: strndup.c,v 1.4 2007/07/03 12:11:09 nakayama Exp $	*/
2cbfb283cSchristos 
3cbfb283cSchristos /*
4cbfb283cSchristos  * Copyright (c) 1988, 1993
5cbfb283cSchristos  *	The Regents of the University of California.  All rights reserved.
6cbfb283cSchristos  *
7cbfb283cSchristos  * Redistribution and use in source and binary forms, with or without
8cbfb283cSchristos  * modification, are permitted provided that the following conditions
9cbfb283cSchristos  * are met:
10cbfb283cSchristos  * 1. Redistributions of source code must retain the above copyright
11cbfb283cSchristos  *    notice, this list of conditions and the following disclaimer.
12cbfb283cSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13cbfb283cSchristos  *    notice, this list of conditions and the following disclaimer in the
14cbfb283cSchristos  *    documentation and/or other materials provided with the distribution.
15cbfb283cSchristos  * 3. Neither the name of the University nor the names of its contributors
16cbfb283cSchristos  *    may be used to endorse or promote products derived from this software
17cbfb283cSchristos  *    without specific prior written permission.
18cbfb283cSchristos  *
19cbfb283cSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20cbfb283cSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21cbfb283cSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22cbfb283cSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23cbfb283cSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24cbfb283cSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25cbfb283cSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26cbfb283cSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27cbfb283cSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28cbfb283cSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29cbfb283cSchristos  * SUCH DAMAGE.
30cbfb283cSchristos  */
31cbfb283cSchristos 
32*0c74dffdSnakayama #if HAVE_NBTOOL_CONFIG_H
33*0c74dffdSnakayama #include "nbtool_config.h"
34*0c74dffdSnakayama #endif
35*0c74dffdSnakayama 
36cbfb283cSchristos #include <sys/cdefs.h>
37cbfb283cSchristos #if defined(LIBC_SCCS) && !defined(lint)
38cbfb283cSchristos #if 0
39cbfb283cSchristos static char sccsid[] = "@(#)strdup.c	8.1 (Berkeley) 6/4/93";
40cbfb283cSchristos #else
41*0c74dffdSnakayama __RCSID("$NetBSD: strndup.c,v 1.4 2007/07/03 12:11:09 nakayama Exp $");
42cbfb283cSchristos #endif
43cbfb283cSchristos #endif /* LIBC_SCCS and not lint */
44cbfb283cSchristos 
45cbfb283cSchristos #include "namespace.h"
46cbfb283cSchristos 
47cbfb283cSchristos #include <assert.h>
48cbfb283cSchristos #include <errno.h>
49cbfb283cSchristos #include <stdlib.h>
50cbfb283cSchristos #include <string.h>
51cbfb283cSchristos 
52cbfb283cSchristos #ifdef __weak_alias
__weak_alias(strndup,_strndup)53cbfb283cSchristos __weak_alias(strndup,_strndup)
54cbfb283cSchristos #endif
55cbfb283cSchristos 
56*0c74dffdSnakayama #if !HAVE_STRNDUP
57cbfb283cSchristos char *
58cbfb283cSchristos strndup(const char *str, size_t n)
59cbfb283cSchristos {
60cbfb283cSchristos 	size_t len;
61cbfb283cSchristos 	char *copy;
62cbfb283cSchristos 
63cbfb283cSchristos 	_DIAGASSERT(str != NULL);
64cbfb283cSchristos 
653984c614Scbiere 	for (len = 0; len < n && str[len]; len++)
663984c614Scbiere 		continue;
673984c614Scbiere 
689946ea71Scbiere 	if (!(copy = malloc(len + 1)))
699946ea71Scbiere 		return (NULL);
703984c614Scbiere 	memcpy(copy, str, len);
71cbfb283cSchristos 	copy[len] = '\0';
72cbfb283cSchristos 	return (copy);
73cbfb283cSchristos }
74*0c74dffdSnakayama #endif /* !HAVE_STRNDUP */
75