home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strdup.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  1KB  |  53 lines

  1. /***
  2. *strdup.c - duplicate a string in malloc'd memory
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _strdup() - grab new memory, and duplicate the string into it.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14.  
  15. /***
  16. *char *_strdup(string) - duplicate string into malloc'd memory
  17. *
  18. *Purpose:
  19. *       Allocates enough storage via malloc() for a copy of the
  20. *       string, copies the string into the new memory, and returns
  21. *       a pointer to it.
  22. *
  23. *Entry:
  24. *       char *string - string to copy into new memory
  25. *
  26. *Exit:
  27. *       returns a pointer to the newly allocated storage with the
  28. *       string in it.
  29. *
  30. *       returns NULL if enough memory could not be allocated, or
  31. *       string was NULL.
  32. *
  33. *Uses:
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. char * __cdecl _strdup (
  40.         const char * string
  41.         )
  42. {
  43.         char *memory;
  44.  
  45.         if (!string)
  46.                 return(NULL);
  47.  
  48.         if (memory = malloc(strlen(string) + 1))
  49.                 return(strcpy(memory,string));
  50.  
  51.         return(NULL);
  52. }
  53.