summaryrefslogtreecommitdiffstats
path: root/lib/libc/string/strndup.c
diff options
context:
space:
mode:
authorkib <kib@FreeBSD.org>2008-12-06 09:37:54 +0000
committerkib <kib@FreeBSD.org>2008-12-06 09:37:54 +0000
commit723ad6dc01479a43dfb77d3c94e81e41c181a99f (patch)
treec1149b45a13e1314f8a9f8b52878247f9d99a859 /lib/libc/string/strndup.c
parent55095b18c6876d38831d4517ad0485562d76f7bb (diff)
downloadFreeBSD-src-723ad6dc01479a43dfb77d3c94e81e41c181a99f.zip
FreeBSD-src-723ad6dc01479a43dfb77d3c94e81e41c181a99f.tar.gz
Import the strndup(3) function.
Copyright attribution is kept the same as in original NetBSD source. Submitted by: Florian Smeets <flo kasimir com> Obtained from: NetBSD MFC after: 2 weeks
Diffstat (limited to 'lib/libc/string/strndup.c')
-rw-r--r--lib/libc/string/strndup.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/libc/string/strndup.c b/lib/libc/string/strndup.c
new file mode 100644
index 0000000..56aa6a8
--- /dev/null
+++ b/lib/libc/string/strndup.c
@@ -0,0 +1,53 @@
+/* $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere Exp $ */
+
+/*
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strndup(const char *str, size_t n)
+{
+ size_t len;
+ char *copy;
+
+ for (len = 0; len < n && str[len]; len++)
+ continue;
+
+ if ((copy = malloc(len + 1)) == NULL)
+ return (NULL);
+ memcpy(copy, str, len);
+ copy[len] = '\0';
+ return (copy);
+}
OpenPOWER on IntegriCloud