/*----------------------------------------------------------------*/ /* strdup.c 02/21/2011 Keven Miller kevenm@3kranger.com */ /*----------------------------------------------------------------*/ #ifdef __mpexl #pragma list off #pragma LOCALITY "3kRanger" #endif /*------------------------------------------------------------------ strdup: duplicate a string into heap space, and return its ptr. setvar ccopts "-Aa -C +w1" ccxllk strdupc, strdup,,-DTEST ------------------------------------------------------------------*/ #include /* strcpy */ #include /* malloc */ #define proc proc char* strdup (char *str) { int len; char *newstr; newstr = 0; if (str) { len = strlen (str); newstr = malloc (len +1); if (newstr) strcpy (newstr, str); } return newstr; } /*----------------------------------------------------------------*/ #ifdef TEST #undef TEST #include proc int main () { char *cp, str [80]; strcpy (str, "This is a string to test strdup!"); cp = strdup (str); printf ("Orig: %p [%s]\n", str, str); printf ("Dup : %p [%s]\n", cp, cp); } #endif /*----------------------------------------------------------------*/