home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / STRDUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  1.7 KB  |  50 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strdup.c
  3.  *
  4.  * function(s)
  5.  *        strdup - copies a string into a newly-created location
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21.  
  22. /*---------------------------------------------------------------------*
  23.  
  24. Name            strdup - copies a string into a newly-created location
  25.  
  26. Usage           char *strdup(const char *str);
  27.  
  28. Prototype in    string.h
  29.  
  30. Description    strdup makes a duplicate of string str, obtaining space with a
  31.          call to malloc. The allocated space is (strlen (str) + 1)
  32.          bytes long.
  33.  
  34. Return value    strdup returns a pointer to the storage location containing
  35.         the duplicated str, or returns NULL if space could not be
  36.         allocated.
  37.  
  38. *---------------------------------------------------------------------*/
  39. char *strdup(const char *s)
  40. {
  41.     char    *p;
  42.     unsigned n;
  43.  
  44.     n = strlen(s) + 1;
  45.     if ((p = (char *)malloc(n)) != NULL)
  46.         memcpy(p, s, n);
  47.     return (p);
  48. }
  49.  
  50.