Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!lll-crg!caip!clyde!burl!ulysses!bellcore!whuxcc!lcuxlm!whuxl!houxm!mtuxo!mtune!codas!bsdpkh!heff From: heff@bsdpkh.UUCP (Paul K Heffner) Newsgroups: net.unix Subject: Re: strdup Message-ID: <241@bsdpkh.UUCP> Date: Fri, 29-Aug-86 12:50:32 EDT Article-I.D.: bsdpkh.241 Posted: Fri Aug 29 12:50:32 1986 Date-Received: Sat, 30-Aug-86 10:02:08 EDT References: <3325@brl-smoke.ARPA> Organization: AT&T-IS (SDSS), Orlando Fl. Lines: 54 hjb writes: > /* the following code is in the public domain. I have never seen */ > /* the original UNIX code for this function. */ > /* hjb 08/25/86 */ > > char * > strdup(s) > char *s; > { > char *p; > extern char *malloc(); > > if(p=malloc(strlen(s)+1)) > strcpy(p,s); > return p; > } > > I am not sure, but I think that strdup comes from MicroSoft, and is only > present under XENIX and their MSDOS C compiler. It's a useful function, > though, and everybody should have it. I'm using the latest release of the Software Generation System on an AT&T 3b2 and strdup is indeed found in STRING(3C). Following is the description from the man page: char *strdup (s1) char *s1; Strdup returns a pointer to a new string which is a duplicate of the string pointed to by s1. The space for the new string is obtained using malloc(3C). If the new string cannot be created, null is returned. The above code doesn't return a null if malloc fails, so perhaps (for sake of consistancy) the code should be changed to: #define NULL (char *)0 char * strdup(s) char *s; { char *p; extern char *malloc(); if((p = malloc(strlen(s) + 1)) != NULL) { strcpy(p,s); return p; } return NULL; } Paul Heffner {ihnp4,akgua,attmail}!bsdpkh!heff