Back to index

cell-binutils  2.17cvs20070401
strdup.c
Go to the documentation of this file.
00001 /*
00002 
00003 @deftypefn Supplemental char* strdup (const char *@var{s})
00004 
00005 Returns a pointer to a copy of @var{s} in memory obtained from
00006 @code{malloc}, or @code{NULL} if insufficient memory was available.
00007 
00008 @end deftypefn
00009 
00010 */
00011 
00012 #include <ansidecl.h>
00013 #include <stddef.h>
00014 
00015 extern size_t strlen (const char*);
00016 extern PTR    malloc (size_t);
00017 extern PTR    memcpy (PTR, const PTR, size_t);
00018 
00019 char *
00020 strdup(const char *s)
00021 {
00022   size_t len = strlen (s) + 1;
00023   char *result = (char*) malloc (len);
00024   if (result == (char*) 0)
00025     return (char*) 0;
00026   return (char*) memcpy (result, s, len);
00027 }