diff libiberty/strdup.c @ 0:a06113de4d67

first commit
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2009 14:47:48 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libiberty/strdup.c	Fri Jul 17 14:47:48 2009 +0900
@@ -0,0 +1,27 @@
+/*
+
+@deftypefn Supplemental char* strdup (const char *@var{s})
+
+Returns a pointer to a copy of @var{s} in memory obtained from
+@code{malloc}, or @code{NULL} if insufficient memory was available.
+
+@end deftypefn
+
+*/
+
+#include <ansidecl.h>
+#include <stddef.h>
+
+extern size_t	strlen (const char*);
+extern PTR	malloc (size_t);
+extern PTR	memcpy (PTR, const PTR, size_t);
+
+char *
+strdup(const char *s)
+{
+  size_t len = strlen (s) + 1;
+  char *result = (char*) malloc (len);
+  if (result == (char*) 0)
+    return (char*) 0;
+  return (char*) memcpy (result, s, len);
+}