strdup() "need source"

Matt Landau mlandau at bbn.com
Fri Jun 16 07:14:43 AEST 1989


Not that it's any big deal, and not that we won't have 250 different
people each posting his or her own pet version of strdup to comp.lang.c,
but here's a minimalist version:

    /* strdup():  Return a copy of the argument string. */

    char *strdup(s)
    char *s;
    {
	extern char *malloc(), *strcpy();
	char *p;

	if (s && (p = malloc(strlen(s) + 1)))
	    return (strcpy(p, s));
	return (NULL);
    }

Although one can get even more minimalist with

    return (s && (p = malloc(strlen(s) + 1))) ? strcpy(p, s) : NULL;

:-)

--
 Matt Landau		    	mlandau at bbn.com
 
 Diplomacy is the art of saying "nice doggy" until you can find a rock.



More information about the Comp.lang.c mailing list