home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / c / strndup < prev    next >
Encoding:
Text File  |  1992-07-21  |  395 b   |  22 lines

  1. /*      > C.Strndup     - save a string on the heap; return pointer to it */
  2.  
  3. #include <string.h>
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6. #include "utils.h"
  7.  
  8. char *strndup (const char *str, int n)
  9. {
  10.         char *p = malloc (n+1);
  11.  
  12.         if ( p == NULL )
  13.                 fatal (1, "Not enough memory to save string\n");
  14.  
  15.     if ( n > 0 )
  16.         memcpy (p, str, n);
  17.  
  18.     p[n] = '\0';
  19.  
  20.         return p;
  21. }
  22.