Xref: utzoo comp.sources.wanted:15318 alt.sources.wanted:939 Path: utzoo!utgpu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!uunet!shelby!rutgers!uwvax!persoft!dag From: dag@persoft.com (Daniel A. Glasser) Newsgroups: comp.sources.wanted,alt.sources.wanted Subject: Re: What is strdup() supposed to do? Does anyone have a copy? Message-ID: <1991Feb18.154159.430@persoft.com> Date: 18 Feb 91 15:41:59 GMT References: <1991Feb14.050716.9501@shibaya.lonestar.org> <1991Feb17.045913.17126@sbcs.sunysb.edu> <1991Feb17.164731.7564@onion.rain.com> Organization: Persoft, Inc. Lines: 71 In article <1991Feb17.164731.7564@onion.rain.com> jeff@onion.rain.com (Jeff Beadles) writes: >Here's an updated and working version of strdup() > >char * >strdup (s) >char *s; >{ > static char *rets; > extern char *malloc(); /* Might be extern void *malloc(); */ > extern int strlen(); > > rets=malloc (sizeof (char) * strlen (s) + sizeof(char) ); > > /* Malloc failed. Oops! */ > if (!rets) { > return((char *)0); > } > > (void) strcpy (rets, s); > > return (rets); >} Two minor nits... Nit #1: Why is rets static? Is there any reason to not leave it as automatic? The value is not used between calls, and why waste data space when the stack is available for these purposes? I'd suggest leaving the "static" off. Nit #2: You should simplify your expression in the "malloc()", that is, sizeof(char) * strlen(foo) + sizeof(char) could be written sizeof(char) * (strlen(foo) + 1) I realize that this doesn't make it any less work for the CPU (both are 1 add and 1 multiply), but to the latter form makes it clearer what is being done (allocating enough space for strlen(foo)+1 chars). Also note that some compilers will complain about the extern function declarations within function scope. Declarations of these functions should be left to header files (if they are declared in header files) so as not to get in trouble with ANSI-C compliant implementations. A shorter version (assuming declarations of malloc(), strlen(), and strcpy(), and a definition for NULL in some header file or preceeding this function in the source file is: char *strdup(orig) /* Allocate a new copy of a string */ char *orig; /* a pointer to the original string */ { char *copy; /* where we keep the copy. */ if (orig == (char *)NULL) /* If the original is NULL */ return (char *)NULL; /* so is the result. */ if (copy = malloc((strlen(orig) + 1) * sizeof(char))) strcpy(copy, orig); /* if malloc() worked, copy the */ /* string data. */ return copy; /* return the result of malloc() */ } /* end of strdup() */ I did add one more error check, and I used some shorthand (the assignment within the if()) that some people may take exception to, however this is just about as efficient (code wise) as you can get and still insure robust behavior of the function. If you drop the NULL check on the original, you can have it even smaller but at the risk that the strcpy or strlen call will die a horrible death. -- Daniel A. Glasser | Persoft, Inc. | dag@persoft.com "Their brains were small, and they died."