Xref: utzoo comp.sources.wanted:15339 alt.sources.wanted:945 Path: utzoo!utgpu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!samsung!spool.mu.edu!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.sources.wanted,alt.sources.wanted Subject: Re: What is strdup() supposed to do? Does anyone have a copy? Message-ID: <6150@auspex.auspex.com> Date: 19 Feb 91 18:50:21 GMT References: <1991Feb17.045913.17126@sbcs.sunysb.edu> <1991Feb17.164731.7564@onion.rain.com> <1991Feb18.154159.430@persoft.com> Followup-To: comp.sources.wanted Organization: Auspex Systems, Santa Clara Lines: 72 >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 Most compilers these days should be smart enough to generate equally good code for if (foo = bar()) do something; and foo = bar; if (foo) do something; >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. There is only such a risk if you don't check for null pointers before doing a "strdup()". The version of "strdup()" in the S5R3 source - from which the version is many UNIX implementations is derived - doesn't do the check for you. The version in 4.3-reno doesn't do so either. Given that, correct code doesn't pass NULL pointers to "strdup()".... Speaking of the 4.3-reno version, here it is - whether you consider the use of "bcopy()" rather than "strcpy()" the Right Thing or not is up to you ("memcpy()" could be used instead, if you have it and don't have "bcopy()"): /* * Copyright (c) 1988 The Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that: (1) source distributions retain this entire copyright * notice and comment, and (2) distributions including binaries display * the following acknowledgement: ``This product includes software * developed by the University of California, Berkeley and its contributors'' * in the documentation or other materials provided with the distribution * and in all advertising materials mentioning features or use of this * software. Neither the name of the University nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)strdup.c 5.3 (Berkeley) 6/1/90"; #endif /* LIBC_SCCS and not lint */ #include #include #include char * strdup(str) char *str; { int len; char *copy, *malloc(); len = strlen(str) + 1; if (!(copy = malloc((u_int)len))) return((char *)NULL); bcopy(str, copy, len); return(copy); }