home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / CLIBSRC1.ZIP / STRDUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  1.4 KB  |  53 lines

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