home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / c / Strndup < prev    next >
Encoding:
Text File  |  1990-07-15  |  382 b   |  26 lines

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