/*------------------------------------------------------------------------- * Filename: strdup.c * Version: $Id: strdup.c,v 1.2 1999/05/02 20:26:27 erik Exp $ * Copyright: Copyright (C) 1996 - 1999, Erik Mouw * Author: Erik Mouw * Description: strdup() replacement function for brain damaged OSes * Created at: Tue Oct 1 13:26:10 1996 * Modified by: Erik Mouw * Modified at: Sun May 2 16:49:08 1999 *-----------------------------------------------------------------------*/ /* * This will be compiled if your system misses the strdup() function. * * * strdup.c, Copyright (C) 1996 - 1999, Erik Mouw * * Email: * J.A.K.Mouw@its.tudelft.nl * * Snail mail: * J.A.K. Mouw * Information and Communication Theory Group * Department of Electrical Engineering * Faculty of Information Technology and Systems * Delft University of Technology * P.O. Box 5031 * 2600 GA Delft * The Netherlands * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software (the "Software"), to deal in the * Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Except as contained in this notice, the name of Erik Mouw shall not * be used in advertising or otherwise to promote the sale, use or * other dealings in this Software without prior written authorization * from Erik Mouw. * * * strdup - duplicate a string * * The strdup() function returns a pointer to a new string which is a * duplicate of the string s. Memory for the new string is obtained * with malloc(3), and can be freed with free(3). * * The strdup() function returns a pointer to the duplicated string, * or NULL if insufficient memory was available. * */ #ident "$Id: strdup.c,v 1.2 1999/05/02 20:26:27 erik Exp $" #ifdef HAVE_CONFIG_H # include "config.h" #endif #if STDC_HEADERS # include #else # ifdef HAVE_MALLOC_H # include # else # ifndef NULL # define NULL ((void *)0) # endif # endif #endif #ifdef __cplusplus extern "C" char *strdup(const char *s) #else char *strdup(const char *s) #endif { char *newstr = NULL; const char *src; char *dest; int len; int i; if(s == NULL) { return(NULL); } /* determine string length */ src = s; for(len = 0; *src != '\0'; src++, len++); /* don't forget the '\0'! */ len++; /* allocate new string */ newstr = (char*)malloc(len); if(newstr == NULL) { return(NULL); } src = s; dest = newstr; /* copy the stuff */ for(i = 0; i < len ; i++) { *dest = *src; src++; dest++; } /* that's it */ return(newstr); }